What: netproxy.Redact is the one place a proxy address is made fit to show. It is the rule708ae4144(#852) gave the Settings status (proxyAddress, now Redact): the userinfo is left out, the rest kept as written (no scheme added to one written without), a malformed address "". Every place a proxy value reached text now goes through it: - provider/logins.go: the switch's "Claude Code sessions ... go through <user>'s proxy" log line (#1886's first). - provider/claude_proxy_env.go: "<user>'s proxy is SOCKS" (#1886's second). - agent/codex_proxy.go + reach.go: the proxied drift's Detail and Addr, shown in the GUI's pill and dialog; Codex's proxy comes from HTTP_PROXY/ALL_PROXY/the system, which can carry a user:password. - update/mirror.go: CheckProxy's error echoed the raw --proxy value ("not %q"); it now names it redacted, or not at all when malformed. - netproxy.Describe already went through it. Why: an account proxy like http://alice:s3cret@10.0.0.2:8080 landed in magpie's log, which users paste into issues and Discord. Swept and left alone: settings.CheckProxy's error names no value; netproxy.DialSOCKS's error names the host only; Go's transport errors (proxyconnect, socks connect) name host:port, not userinfo; the GUI's provider editor and Settings field return the proxy whole as the user's own editable value; env injection (EnvWith, ForBun, wslrun) must carry it whole. Tests place the credential where the user writes it: http, https, socks5, socks5h, with and without a scheme, percent-encoded user and password, user alone, malformed. TestRedactLeavesOutUserAndPassword, TestClaudeAccountProxyLogsNoCredentials (captures the log over real switches; Claude Code's HTTPS_PROXY still gets the proxy whole), TestCodexProxiedDriftShowsNoCredentials (the probe still goes through the whole proxy), TestCheckProxyErrorShowsNoCredentials. Run: each site reverted alone turns its test red (logins.go, claude_proxy_env.go, reach.go, codex_proxy.go, mirror.go; Redact keeping the userinfo turns Redact and Describe's tests red). gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows. go test ./... at0dd821c13+ this, sandbox HOME: 47 packages ok; agent, gateway and library timed out at 10m on a machine at load ~20, then on rerun hit ENOSPC (the disk filled from other sessions). Rerun with space: library ok in full; agent's TestClaudeOutputTokens and gateway's TestGroupFirstTokenFailsOver (the ENOSPC ones) ok, with every Proxy/Proxied test in both. Not tried in the GUI by hand.
156 lines
5.0 KiB
Go
156 lines
5.0 KiB
Go
package update
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// A network that can't reach GitHub can have a release's file come
|
|
// through a GitHub download mirror: its prefix goes before the file's
|
|
// github.com URL (https://mirror.example/https://github.com/…). magpie
|
|
// names no mirror of its own; one is used only when given (magpie update
|
|
// --mirror, or Settings' UpdateMirror, which magpie update mirror sets).
|
|
//
|
|
// A mirror is a host magpie doesn't trust: the update feed, with each
|
|
// file's SHA-256, is still asked at usemagpie.ai and never through the
|
|
// mirror, the file is checked against that hash, and the macOS app must
|
|
// still be signed by the team that signed the one installed. A mirror
|
|
// that changes a byte gets its download refused.
|
|
|
|
type mirrorKey struct{}
|
|
|
|
// WithMirror has the downloads made under ctx go through the mirror
|
|
// prefix; "off" goes through none, whatever Settings say, and "" leaves
|
|
// ctx as it is.
|
|
func WithMirror(ctx context.Context, prefix string) context.Context {
|
|
if prefix = strings.TrimSpace(prefix); prefix == "" {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, mirrorKey{}, prefix)
|
|
}
|
|
|
|
// WithProxy has the update feed and the downloads asked under ctx go
|
|
// through proxy (http://, https://, socks5://, socks5h://; host:port is
|
|
// http; "direct" for none), over Settings' Proxy and the environment's.
|
|
func WithProxy(ctx context.Context, proxy string) context.Context {
|
|
return netproxy.With(ctx, proxy)
|
|
}
|
|
|
|
// CheckMirror says what is wrong with a mirror prefix, nil when it is one.
|
|
func CheckMirror(prefix string) error {
|
|
u, err := url.Parse(strings.TrimSpace(prefix))
|
|
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" {
|
|
return fmt.Errorf("a mirror is an http(s) address put before the github.com URL, like https://mirror.example/, not %q", prefix)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckProxy says what is wrong with a proxy address, nil when it is one.
|
|
func CheckProxy(proxy string) error {
|
|
if proxy == "direct" {
|
|
return nil
|
|
}
|
|
u, err := netproxy.Parse(proxy)
|
|
if err != nil || u.Host == "" {
|
|
return fmt.Errorf("a proxy is an address like http://127.0.0.1:7890 or socks5://127.0.0.1:1080%s", notProxy(proxy))
|
|
}
|
|
switch u.Scheme {
|
|
case "http", "https", "socks5", "socks5h":
|
|
return nil
|
|
}
|
|
return fmt.Errorf("a proxy is http://, https://, socks5:// or socks5h://%s", notProxy(proxy))
|
|
}
|
|
|
|
// notProxy is ", not <proxy>" for CheckProxy's error, the proxy's user
|
|
// and password left out (#1886); "" when nothing of it can be shown.
|
|
func notProxy(proxy string) string {
|
|
if r := netproxy.Redact(proxy); r != "" {
|
|
return fmt.Sprintf(", not %q", r)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// mirrorOf is the mirror downloads under ctx go through: WithMirror's,
|
|
// else Settings', "" for none.
|
|
func mirrorOf(ctx context.Context) string {
|
|
m, _ := ctx.Value(mirrorKey{}).(string)
|
|
if m == "" {
|
|
m = strings.TrimSpace(settings.Load().UpdateMirror)
|
|
}
|
|
if m == "off" || m == "direct" {
|
|
return ""
|
|
}
|
|
return m
|
|
}
|
|
|
|
// mirrored is raw fetched through the mirror prefix: only a file on
|
|
// GitHub is; anything else (a test's server, a feed pointed elsewhere)
|
|
// goes as it is.
|
|
func mirrored(prefix, raw string) string {
|
|
if prefix == "" {
|
|
return raw
|
|
}
|
|
u, err := url.Parse(raw)
|
|
if err != nil || !githubHost(u.Hostname()) {
|
|
return raw
|
|
}
|
|
if !strings.HasSuffix(prefix, "/") {
|
|
prefix += "/"
|
|
}
|
|
return prefix + raw
|
|
}
|
|
|
|
func githubHost(h string) bool {
|
|
h = strings.ToLower(h)
|
|
return h == "github.com" || strings.HasSuffix(h, ".github.com") || strings.HasSuffix(h, ".githubusercontent.com")
|
|
}
|
|
|
|
// MirrorError is a download that failed through a mirror: the mirror
|
|
// didn't answer, answered with something other than the file, or sent a
|
|
// file whose hash isn't the feed's (never installed). Downloading from
|
|
// GitHub itself may still work, which the GUI offers.
|
|
type MirrorError struct {
|
|
Mirror string // the prefix the download went through
|
|
Err error
|
|
}
|
|
|
|
func (e *MirrorError) Error() string {
|
|
if strings.Contains(e.Err.Error(), e.Mirror) {
|
|
return e.Err.Error()
|
|
}
|
|
return "through the mirror " + e.Mirror + ": " + e.Err.Error()
|
|
}
|
|
|
|
func (e *MirrorError) Unwrap() error { return e.Err }
|
|
|
|
// MirrorOf is the mirror a download error came through, "" when it is no
|
|
// mirror's.
|
|
func MirrorOf(err error) string {
|
|
var m *MirrorError
|
|
if errors.As(err, &m) {
|
|
return m.Mirror
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// CheckMirrorURL is CheckMirror for one typed into Settings: a full
|
|
// https:// address (a download over http would be read by anyone between,
|
|
// though the checksum still guards what is installed).
|
|
func CheckMirrorURL(prefix string) error {
|
|
prefix = strings.TrimSpace(prefix)
|
|
u, err := url.Parse(prefix)
|
|
if err != nil || u.Scheme != "https" || u.Host == "" || strings.ContainsAny(prefix, " \t") {
|
|
return errors.New("a mirror is a full https:// address, like https://mirror.example/")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Mirror is the mirror downloads under ctx go through, "" for none.
|
|
func Mirror(ctx context.Context) string { return mirrorOf(ctx) }
|