Settings > Network keeps named proxies (name, address, optional login)
with a Test that says reachable and the latency. Everywhere a proxy can be
chosen (global, plugin downloads, a provider, a key, a Claude account) now
has a Saved mode beside the others; the value is "@<id>". Addresses typed
before are read exactly as before, nothing is migrated or overridden.
netproxy.Parse/Resolve/Redact/EnvWith/Dispatch resolve a reference at the
lowest layer, so a proxy edited once reaches the gateway, usage reads,
sign-ins and the plugin host at the next request; Claude Code's
settings.json env (a snapshot) is rewritten by provider.ProxyEdited.
A reference this computer lacks fails closed (DeadProxy), says the proxy's
name in the request error and as "Proxy missing: {name}" on the provider
row, and never goes direct. The password lives in settings.json (0600), is
never read back by the page, logged, or put in Settings sync. The list
rides with the providers part of a sync (Bundle.Proxies, MergeProxies by
id, passwords only with keys). A proxy in use can't be deleted (409).
The saved list's page code is in proxies.js, loaded when Settings draws it;
app.js grew by Saved-mode helpers only, paid for by 16 long comments cut
(core budget unchanged, no raise).
Run (mote refused ssh all session, so Go and GUI went through win-run on
xiaomi-gpu, Windows 11; gofmt and darwin/linux-nogui/windows go vet were
clean on mote before it went down, at the pre-sync tree):
- go test settings, netproxy, backup, settingsapi, davsync: ok on Windows
(earlier on mote: also gui, provider, plugin with real Bun, gateway).
- saved-proxies.test.cjs 20 of 20 (chromium+webkit, en zh zh-TW ja de,
420px) twice more on Windows; provider-proxy-missing.test.cjs 20 of 20.
One webkit de 420px run of saved-proxies failed once under 4-way
concurrency and passed 3 of 3 alone; not reproduced.
- TestCoreScriptBudget ok after the move (it was 3.4KB over before).
Red on this tree and on origin/main alike (Windows run, not mine):
settings TestSharedCheckedReadsOnceAndKnowsUnknown; provider
Codex keychain/Git tests; plugin TestHostRunsAsMagpieBun, TestPiPackage;
gui TestGatewayHeldByAContainer and others; old-webkit "compat.js is the
first" (headerpresets.js) and ":has() (Safari 15.4)" on origin/main 245f07b19.
Broken by hand: restore skipping Proxies (TestSavedProxiesTravelWithProviders
red), Parse returning nil for a dangling ref (netproxy tests red), the
Proxy-missing badge disabled (provider-proxy-missing 0 of 20), and the Claude
env snapshot failing open (found by reading, test added).
Not tried: real vendors, the real Claude Code, a real two-computer sync, the
macOS suite and the whole GUI suite on this final tree (mote down; the whole
suite ran to test ~3700 on the earlier tree with reds owned by others).
A proxy missing whose sender never had it is named by its id.
253 lines
7.1 KiB
Go
253 lines
7.1 KiB
Go
package settings
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// NamedProxy is a proxy the user keeps once, by name, and picks wherever a
|
|
// proxy can be chosen: Settings' own, plugin downloads', a provider's, a
|
|
// key's, an account's. Each of those holds the reference "@"+ID instead of
|
|
// an address, so one edit here applies to all of them. The address has no
|
|
// login in it; User and Pass are kept apart, in settings.json (0600) as
|
|
// GitHubToken is, and the page is never sent the password. This computer's
|
|
// own (KeepOwn), left out of a backup, as Proxy is.
|
|
type NamedProxy struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
// URL is http://, https:// or socks5:// (host:port means http), with
|
|
// no user and password in it.
|
|
URL string `json:"url"`
|
|
User string `json:"user,omitempty"`
|
|
Pass string `json:"pass,omitempty"`
|
|
}
|
|
|
|
// ProxyRefPrefix starts a proxy choice that names a NamedProxy by its id.
|
|
// No address starts with it, so it never collides with an inline value.
|
|
const ProxyRefPrefix = "@"
|
|
|
|
// IsProxyRef is whether a proxy choice names a saved proxy.
|
|
func IsProxyRef(choice string) bool {
|
|
return strings.HasPrefix(strings.TrimSpace(choice), ProxyRefPrefix)
|
|
}
|
|
|
|
// ProxyRef is the choice that names the saved proxy id.
|
|
func ProxyRef(id string) string { return ProxyRefPrefix + id }
|
|
|
|
// ProxyID is the id a reference names, "" for a choice that isn't one.
|
|
func ProxyID(choice string) string {
|
|
choice = strings.TrimSpace(choice)
|
|
if !strings.HasPrefix(choice, ProxyRefPrefix) {
|
|
return ""
|
|
}
|
|
return strings.TrimPrefix(choice, ProxyRefPrefix)
|
|
}
|
|
|
|
// Address is the proxy as a request takes it, with its login in it. Never
|
|
// shown or logged: netproxy.Redact is what a line of text may carry.
|
|
func (p NamedProxy) Address() (string, error) {
|
|
u, err := url.Parse(withScheme(strings.TrimSpace(p.URL)))
|
|
if err != nil || u.Host == "" {
|
|
return "", fmt.Errorf("proxy %q has no usable address", p.Name)
|
|
}
|
|
if p.User != "" || p.Pass != "" {
|
|
if p.Pass != "" {
|
|
u.User = url.UserPassword(p.User, p.Pass)
|
|
} else {
|
|
u.User = url.User(p.User)
|
|
}
|
|
}
|
|
return u.String(), nil
|
|
}
|
|
|
|
// ResolveProxy is the address a proxy choice stands for: a reference's
|
|
// saved proxy (its login in it), any other choice as it is. ok is false
|
|
// for a reference to a proxy that isn't there (deleted, or brought by a
|
|
// sync from a computer whose list this one doesn't have): the caller fails
|
|
// the request, as a choice never turns into a direct connection.
|
|
func ResolveProxy(choice string) (addr string, ok bool) {
|
|
choice = strings.TrimSpace(choice)
|
|
id := ProxyID(choice)
|
|
if id == "" {
|
|
return choice, true
|
|
}
|
|
for _, p := range Shared().Proxies {
|
|
if p.ID == id {
|
|
a, err := p.Address()
|
|
return a, err == nil
|
|
}
|
|
}
|
|
return choice, false
|
|
}
|
|
|
|
// NewProxyID is an id for a saved proxy.
|
|
func NewProxyID() string {
|
|
b := make([]byte, 5)
|
|
_, _ = rand.Read(b)
|
|
return "px" + hex.EncodeToString(b)
|
|
}
|
|
|
|
func validProxyID(id string) bool {
|
|
if id == "" || len(id) > 40 {
|
|
return false
|
|
}
|
|
for _, c := range id {
|
|
if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '_') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// checkProxies refuses a list of saved proxies that couldn't be used.
|
|
func checkProxies(ps []NamedProxy) error {
|
|
seen := map[string]bool{}
|
|
names := map[string]bool{}
|
|
for i := range ps {
|
|
p := &ps[i]
|
|
p.Name, p.URL = strings.TrimSpace(p.Name), strings.TrimSpace(p.URL)
|
|
if !validProxyID(p.ID) {
|
|
return fmt.Errorf("a saved proxy's id is lower-case letters, digits, - and _, not %q", p.ID)
|
|
}
|
|
if seen[p.ID] {
|
|
return fmt.Errorf("two saved proxies have the id %q", p.ID)
|
|
}
|
|
seen[p.ID] = true
|
|
if p.Name == "" {
|
|
return fmt.Errorf("a saved proxy needs a name")
|
|
}
|
|
k := strings.ToLower(p.Name)
|
|
if names[k] {
|
|
return fmt.Errorf("two saved proxies are named %q", p.Name)
|
|
}
|
|
names[k] = true
|
|
if p.URL == "direct" || strings.HasPrefix(p.URL, ProxyRefPrefix) {
|
|
return fmt.Errorf("proxy %q: type its address, like http://127.0.0.1:7890", p.Name)
|
|
}
|
|
if err := checkAddress(p.URL); err != nil {
|
|
return fmt.Errorf("proxy %q: %w", p.Name, err)
|
|
}
|
|
if u, err := url.Parse(withScheme(p.URL)); err != nil || u.User != nil {
|
|
return fmt.Errorf("proxy %q: the username and password go in their own fields, not in the address", p.Name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func withScheme(raw string) string {
|
|
if !strings.Contains(raw, "://") {
|
|
return "http://" + raw
|
|
}
|
|
return raw
|
|
}
|
|
|
|
func checkAddress(p string) error {
|
|
u, err := url.Parse(withScheme(p))
|
|
if err != nil || u.Host == "" || !slices.Contains([]string{"http", "https", "socks5", "socks5h"}, u.Scheme) {
|
|
return fmt.Errorf("proxy must look like http://127.0.0.1:7890 or socks5://127.0.0.1:1080")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckProxyIn is CheckProxy for a choice made beside the list ps (a
|
|
// Settings being saved holds its own).
|
|
func CheckProxyIn(p string, ps []NamedProxy) error {
|
|
p = strings.TrimSpace(p)
|
|
if IsProxyRef(p) {
|
|
id := ProxyID(p)
|
|
if !slices.ContainsFunc(ps, func(n NamedProxy) bool { return n.ID == id }) {
|
|
return fmt.Errorf("the saved proxy %q isn't there", id)
|
|
}
|
|
return nil
|
|
}
|
|
return CheckProxy(p)
|
|
}
|
|
|
|
// ProxyLabel is how a saved proxy is named in a message: its name, else the
|
|
// name it had (ProxyNames), else its id.
|
|
func ProxyLabel(id string) string {
|
|
s := Shared()
|
|
for _, p := range s.Proxies {
|
|
if p.ID == id {
|
|
return p.Name
|
|
}
|
|
}
|
|
if n := s.ProxyNames[id]; n != "" {
|
|
return n
|
|
}
|
|
return id
|
|
}
|
|
|
|
func rememberProxyNames(m map[string]string, ps []NamedProxy) map[string]string {
|
|
for _, p := range ps {
|
|
if m == nil {
|
|
m = map[string]string{}
|
|
}
|
|
m[p.ID] = p.Name
|
|
}
|
|
return m
|
|
}
|
|
|
|
// WithoutProxyPasswords is ps with each password left out: what goes
|
|
// where credentials don't.
|
|
func WithoutProxyPasswords(ps []NamedProxy) []NamedProxy {
|
|
out := slices.Clone(ps)
|
|
for i := range out {
|
|
out[i].Pass = ""
|
|
}
|
|
return out
|
|
}
|
|
|
|
// MergeProxies is cur with the proxies another computer sent put in: one
|
|
// with the same id takes the sent name, address and login (its password
|
|
// too when keys, else it keeps cur's); one cur lacks is added, its name
|
|
// made unique here. A proxy only cur has stays: a sync never takes away
|
|
// what a choice here may name.
|
|
func MergeProxies(cur, in []NamedProxy, keys bool) []NamedProxy {
|
|
out := slices.Clone(cur)
|
|
taken := map[string]bool{}
|
|
for _, p := range out {
|
|
taken[strings.ToLower(p.Name)] = true
|
|
}
|
|
for _, n := range in {
|
|
if i := slices.IndexFunc(out, func(p NamedProxy) bool { return p.ID == n.ID }); i >= 0 {
|
|
old := out[i]
|
|
if !keys {
|
|
n.Pass = old.Pass
|
|
}
|
|
if !strings.EqualFold(old.Name, n.Name) && taken[strings.ToLower(n.Name)] {
|
|
n.Name = old.Name
|
|
}
|
|
out[i] = n
|
|
continue
|
|
}
|
|
if !keys {
|
|
n.Pass = ""
|
|
}
|
|
base, k := n.Name, 2
|
|
for taken[strings.ToLower(n.Name)] {
|
|
n.Name = fmt.Sprintf("%s %d", base, k)
|
|
k++
|
|
}
|
|
taken[strings.ToLower(n.Name)] = true
|
|
out = append(out, n)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// RestoreProxies puts the proxies another computer sent into this one's
|
|
// (MergeProxies), where a restore or a sync brings them.
|
|
func RestoreProxies(in []NamedProxy, keys bool) error {
|
|
s := Load()
|
|
merged := MergeProxies(s.Proxies, in, keys)
|
|
if slices.Equal(merged, s.Proxies) {
|
|
return nil
|
|
}
|
|
s.Proxies = merged
|
|
return Save(s)
|
|
}
|