The reporter starts magpie at login with the proxy on "follow system
proxy". magpie comes up before the proxy app. With no system proxy set
yet, netproxy.For gives none, so magpie's own allowance reads went out
direct, from the user's real IP. That reading was then kept for up to
15s. A system proxy that is set but not listening fails the request
("proxyconnect ... connection refused"); it never falls back to direct.
Proxy freshness:
- netproxy.System re-reads a "none" reading after 2s (noneKeep) instead
of 15s (systemKeep, unchanged for a set proxy).
- The plugin host looks again every 2s while it has no proxy
(proxyLookNone), so a host started before the proxy app takes it soon.
- Catalog sync, the update check, plugin updates and token refresh all go
through netproxy.Func / Dispatch, so they use the current proxy too.
Settings -> Allowances -> "Allowance reads": Automatic (default, as
before) or When I ask (settings.QuotaReads = "asked",
POST /api/settings/quota-reads):
- These count as asking (provider.Asked): the Usage page opened or
refreshed (?asked=1), a card's refresh (RefreshUsage), magpie quota,
magpie accounts, magpie quota wait and the TUI's quotas.
- Every other read (quotaReading -> holdUnasked) carries netproxy.Hold.
netproxy.Func, Dispatch's choice proxies and the plugin host's calls
refuse a held request before it leaves.
- The readers return the last reading, dated (AsOf), or "not read yet".
That covers routing's Allowances / AwaitAllowance, the used-up switch,
the menu bar, alerts, Quotas/KeyBalances/PlanQuotas, Codex/Claude/
Cursor/Grok/Kiro usage and KeyAllowance.
- An account refused for its quota (StaleAllowance) or a key refused
(StaleKeyAllowance) is still read, once.
- Warm-up and check-in are unchanged.
- Strings are added in zh, zh-TW, ja and de. The docs are updated:
providers-accounts (Allowance row), provider-plugins (host proxy).
Run:
- go test -tags nogui ./... (sandbox HOME), gofmt -l, and go vet -tags
nogui on GOOS=linux/windows/darwin for ., netproxy, plugin, settings,
provider, gui and tui.
- New tests, each seen red by breaking the change by hand:
- TestSystemProxySetAfterStartIsTakenSoon and TestHeldRequestNeverLeaves
(netproxy).
- TestHostWithNoProxyLooksAgainSoon (plugin; red with 15s).
- TestAllowancesReadOnlyWhenAsked (red without the hold and without
markStaleRead), TestAllowancesReadByThemselvesByDefault and
TestKeyReadsOnlyWhenAsked (red without holdUnasked and without
unheld for a refused key).
- GUI quota-reads.test.cjs in Chromium and WebKit, at 900 and 360px, in
en/zh/zh-TW/ja/de.
- i18n-keys, gui-ja, gui-de, settings-sections, settings-groups,
currency, number-units and tray-bird.
418 lines
12 KiB
Go
418 lines
12 KiB
Go
// Package netproxy decides which proxy magpie's own requests to vendors go
|
|
// through. An app started from the Dock or the Start menu has no
|
|
// HTTPS_PROXY in its environment, so without this every call to a vendor
|
|
// that is only reachable through a proxy hangs until the agent gives up.
|
|
//
|
|
// In order: the proxy set in magpie's settings (or "direct" for none), the
|
|
// usual *_PROXY variables, then the system's proxy (macOS network
|
|
// settings, Windows Internet Options). Loopback is never proxied, nor,
|
|
// unless a provider has a proxy of its own, a host on the user's own
|
|
// network (lan.go).
|
|
package netproxy
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Install routes http.DefaultTransport (and so http.DefaultClient) through
|
|
// Func, and http.DefaultClient's requests made on a provider's behalf
|
|
// (With) through a transport of that provider's proxy (Dispatch).
|
|
func Install() {
|
|
if t, ok := http.DefaultTransport.(*http.Transport); ok {
|
|
t.Proxy = Func
|
|
http.DefaultClient.Transport = Dispatch(t)
|
|
}
|
|
}
|
|
|
|
// Func is an http.Transport Proxy function: the proxy the request's
|
|
// context names (With), or else the global one, which a host on the
|
|
// user's own network goes around (onLAN).
|
|
func Func(req *http.Request) (*url.URL, error) {
|
|
if err := Held(req.Context()); err != nil {
|
|
return nil, err
|
|
}
|
|
if loopback(req.URL.Hostname()) {
|
|
return nil, nil
|
|
}
|
|
if c := choiceOf(req.Context()); c != "" {
|
|
return forChoice(c)
|
|
}
|
|
p, err := For(req.URL)
|
|
if p != nil && onLAN(req.URL.Hostname()) {
|
|
return nil, nil
|
|
}
|
|
return p, err
|
|
}
|
|
|
|
type choiceKey struct{}
|
|
|
|
type holdKey struct{}
|
|
|
|
// Hold is ctx for work magpie was not asked to do and must not send: no
|
|
// request made with it through Func, or a transport Dispatch made, leaves
|
|
// the machine; each fails with err before it dials (#1518, allowance
|
|
// reads while the user has them read only when asked).
|
|
func Hold(ctx context.Context, err error) context.Context {
|
|
return context.WithValue(ctx, holdKey{}, err)
|
|
}
|
|
|
|
// Held is the error ctx holds its requests with (Hold), nil when it holds
|
|
// none.
|
|
func Held(ctx context.Context) error {
|
|
if ctx == nil {
|
|
return nil
|
|
}
|
|
err, _ := ctx.Value(holdKey{}).(error)
|
|
return err
|
|
}
|
|
|
|
// With is ctx for requests made on behalf of one provider, whose own proxy
|
|
// is choice (issue #237: Codex through a proxy, a vendor at home without):
|
|
// "" follows the global one (ctx is returned as it is), "direct" goes
|
|
// through none, anything else is the proxy's address (http://, https://,
|
|
// socks5://; host:port means http). Requests made with it through Func,
|
|
// or through a transport Dispatch made, take that proxy.
|
|
func With(ctx context.Context, choice string) context.Context {
|
|
if choice = strings.TrimSpace(choice); choice == "" {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, choiceKey{}, choice)
|
|
}
|
|
|
|
// Choice is the proxy ctx names (With), "" when it names none — for a CLI
|
|
// run on a provider's behalf, whose *_PROXY is set from it (EnvWith).
|
|
func Choice(ctx context.Context) string { return choiceOf(ctx) }
|
|
|
|
func choiceOf(ctx context.Context) string {
|
|
if ctx == nil {
|
|
return ""
|
|
}
|
|
c, _ := ctx.Value(choiceKey{}).(string)
|
|
return c
|
|
}
|
|
|
|
// forChoice is the proxy a provider's own choice names; nil for "direct".
|
|
func forChoice(c string) (*url.URL, error) {
|
|
if c == "direct" {
|
|
return nil, nil
|
|
}
|
|
return Parse(c)
|
|
}
|
|
|
|
// Dispatch is a RoundTripper sending a request through base, unless its
|
|
// context names a provider's own proxy (With): then through a clone of
|
|
// base kept for that proxy alone. Connections are so reused among the
|
|
// requests going through one proxy and never carried to another — HTTP/2
|
|
// ones, which a transport shares by host whatever proxy they were dialled
|
|
// through, among them. A host whose IPv6 is taken but goes nowhere is
|
|
// dialed over IPv4 (v4Fallback).
|
|
func Dispatch(base *http.Transport) http.RoundTripper {
|
|
v4Fallback(base)
|
|
return &dispatch{base: base, own: map[string]*http.Transport{}}
|
|
}
|
|
|
|
type dispatch struct {
|
|
base *http.Transport
|
|
mu sync.Mutex
|
|
own map[string]*http.Transport
|
|
}
|
|
|
|
func (d *dispatch) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
c := choiceOf(req.Context())
|
|
if c == "" {
|
|
return v4Retry(d.base, req)
|
|
}
|
|
return v4Retry(d.transport(c), req)
|
|
}
|
|
|
|
func (d *dispatch) transport(c string) *http.Transport {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if t, ok := d.own[c]; ok {
|
|
return t
|
|
}
|
|
if len(d.own) >= 32 { // choices come and go as they are edited
|
|
for k, t := range d.own {
|
|
t.CloseIdleConnections()
|
|
delete(d.own, k)
|
|
}
|
|
}
|
|
t := d.base.Clone()
|
|
t.Proxy = func(req *http.Request) (*url.URL, error) {
|
|
if err := Held(req.Context()); err != nil {
|
|
return nil, err
|
|
}
|
|
if loopback(req.URL.Hostname()) {
|
|
return nil, nil
|
|
}
|
|
return forChoice(c)
|
|
}
|
|
d.own[c] = t
|
|
return t
|
|
}
|
|
|
|
// CloseIdleConnections closes base's and every proxy's own.
|
|
func (d *dispatch) CloseIdleConnections() {
|
|
d.base.CloseIdleConnections()
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
for _, t := range d.own {
|
|
t.CloseIdleConnections()
|
|
}
|
|
}
|
|
|
|
// For is the proxy a request to u goes through; nil means direct.
|
|
func For(u *url.URL) (*url.URL, error) {
|
|
switch s := strings.TrimSpace(settings.Load().Proxy); s {
|
|
case "direct":
|
|
return nil, nil
|
|
case "":
|
|
default:
|
|
return Parse(s)
|
|
}
|
|
if p, err := fromEnv(u); p != nil || err != nil {
|
|
return p, err
|
|
}
|
|
sys := System()
|
|
if sys.URL == "" || bypassed(u.Hostname(), sys.Bypass) {
|
|
return nil, nil
|
|
}
|
|
return Parse(sys.URL)
|
|
}
|
|
|
|
// Env is env (the process's own when nil) for an agent's CLI that magpie
|
|
// starts — a sign-in, or a subscription's run — told to use the proxy
|
|
// magpie's own requests would: the CLI reads only *_PROXY, which an app
|
|
// started from the Dock or the Start menu doesn't have, so a proxy set in
|
|
// magpie or the system's alone left it trying the vendor directly. A
|
|
// proxy set in magpie replaces any in env and "direct" drops them; the
|
|
// system's is only added where env names none. Loopback never goes
|
|
// through it, since a CLI calls back to magpie there.
|
|
func Env(env []string) []string { return EnvWith("", env) }
|
|
|
|
// EnvWith is Env for a CLI run on one provider's behalf, whose own proxy
|
|
// is choice (see With): "" is Env's.
|
|
func EnvWith(choice string, env []string) []string {
|
|
if env == nil {
|
|
env = os.Environ()
|
|
}
|
|
var p string
|
|
var bypass []string
|
|
s := strings.TrimSpace(choice)
|
|
if s == "" {
|
|
s = strings.TrimSpace(settings.Load().Proxy)
|
|
}
|
|
switch s {
|
|
case "direct":
|
|
return withProxy(env, "", nil)
|
|
case "":
|
|
for _, e := range env {
|
|
k, v, _ := strings.Cut(e, "=")
|
|
if proxyVar(k) && !strings.EqualFold(k, "NO_PROXY") && strings.TrimSpace(v) != "" {
|
|
return env
|
|
}
|
|
}
|
|
sys := System()
|
|
if sys.URL == "" {
|
|
return env
|
|
}
|
|
p, bypass = sys.URL, sys.Bypass
|
|
default:
|
|
u, err := Parse(s)
|
|
if err != nil {
|
|
return env
|
|
}
|
|
p = u.String()
|
|
}
|
|
return withProxy(env, p, bypass)
|
|
}
|
|
|
|
func proxyVar(k string) bool {
|
|
switch strings.ToUpper(k) {
|
|
case "HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "NO_PROXY":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// withProxy is env with its *_PROXY replaced by p ("" for none), and
|
|
// NO_PROXY naming loopback and bypass.
|
|
func withProxy(env []string, p string, bypass []string) []string {
|
|
out := make([]string, 0, len(env)+8)
|
|
for _, e := range env {
|
|
if k, _, _ := strings.Cut(e, "="); !proxyVar(k) {
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
if p == "" {
|
|
return out
|
|
}
|
|
no := []string{"localhost", "127.0.0.1", "::1"}
|
|
for _, b := range bypass {
|
|
switch b = strings.TrimPrefix(strings.TrimSpace(b), "*"); b {
|
|
case "", "<local>":
|
|
default:
|
|
no = append(no, b)
|
|
}
|
|
}
|
|
nos := strings.Join(no, ",")
|
|
return append(out, "HTTPS_PROXY="+p, "https_proxy="+p, "HTTP_PROXY="+p, "http_proxy="+p,
|
|
"ALL_PROXY="+p, "all_proxy="+p, "NO_PROXY="+nos, "no_proxy="+nos)
|
|
}
|
|
|
|
// Parse reads a proxy address; a bare host:port is an HTTP proxy.
|
|
func Parse(s string) (*url.URL, error) {
|
|
s = strings.TrimSpace(s)
|
|
if !strings.Contains(s, "://") {
|
|
s = "http://" + s
|
|
}
|
|
return url.Parse(s)
|
|
}
|
|
|
|
// Describe says, for the settings page, what a request to u would use.
|
|
func Describe() (proxy, source string) {
|
|
switch s := strings.TrimSpace(settings.Load().Proxy); s {
|
|
case "direct":
|
|
return "", "off"
|
|
case "":
|
|
default:
|
|
return s, "settings"
|
|
}
|
|
u, _ := url.Parse("https://chatgpt.com")
|
|
if p, _ := fromEnv(u); p != nil {
|
|
return p.String(), "environment"
|
|
}
|
|
if sys := System(); sys.URL != "" {
|
|
return sys.URL, "system"
|
|
}
|
|
return "", "none"
|
|
}
|
|
|
|
// fromEnv is the proxy the environment names for u: HTTPS_PROXY or
|
|
// HTTP_PROXY as Go reads them, else ALL_PROXY (curl's, which Go doesn't
|
|
// read: a shell with only ALL_PROXY=socks5://… set has curl go through it,
|
|
// and magpie, its updates among its requests, too). NO_PROXY is kept for
|
|
// it as well.
|
|
func fromEnv(u *url.URL) (*url.URL, error) {
|
|
if p, err := http.ProxyFromEnvironment(&http.Request{URL: u}); p != nil || err != nil {
|
|
return p, err
|
|
}
|
|
all := envOf("ALL_PROXY", "all_proxy")
|
|
if all == "" || noProxy(u.Hostname(), envOf("NO_PROXY", "no_proxy")) {
|
|
return nil, nil
|
|
}
|
|
return Parse(all)
|
|
}
|
|
|
|
func envOf(keys ...string) string {
|
|
for _, k := range keys {
|
|
if v := strings.TrimSpace(os.Getenv(k)); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// noProxy matches host against NO_PROXY as curl does: "*" for every host,
|
|
// else entries apart by commas, each the host or a domain it is under
|
|
// (example.com and .example.com both take api.example.com); a port on an
|
|
// entry is ignored. Loopback is never proxied anyway.
|
|
func noProxy(host, list string) bool {
|
|
host = strings.ToLower(strings.TrimSuffix(host, "."))
|
|
if host == "" || loopback(host) {
|
|
return true
|
|
}
|
|
for _, e := range strings.Split(list, ",") {
|
|
e = strings.ToLower(strings.TrimSpace(e))
|
|
if e == "*" {
|
|
return true
|
|
}
|
|
if h, _, err := net.SplitHostPort(e); err == nil {
|
|
e = h
|
|
}
|
|
e = strings.TrimPrefix(strings.TrimPrefix(e, "*"), ".")
|
|
if e != "" && (host == e || strings.HasSuffix(host, "."+e)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Proxy is the system's proxy: one address, and the hosts that skip it.
|
|
type Proxy struct {
|
|
URL string
|
|
Bypass []string
|
|
}
|
|
|
|
var sysCache struct {
|
|
sync.Mutex
|
|
at time.Time
|
|
p Proxy
|
|
}
|
|
|
|
// How long a system proxy read stands: a proxy that is set, 15 seconds;
|
|
// none, 2. magpie started at login comes up before the proxy app does
|
|
// (#1518), and its requests went direct for up to 15 seconds after the
|
|
// proxy app had set the system's proxy.
|
|
var (
|
|
systemKeep = 15 * time.Second
|
|
noneKeep = 2 * time.Second
|
|
readSystem = system
|
|
)
|
|
|
|
// System reads the system proxy, at most every 15 seconds while one is
|
|
// set and every 2 while none is.
|
|
func System() Proxy {
|
|
sysCache.Lock()
|
|
defer sysCache.Unlock()
|
|
keep := systemKeep
|
|
if sysCache.p.URL == "" {
|
|
keep = noneKeep
|
|
}
|
|
if sysCache.at.IsZero() || time.Since(sysCache.at) > keep {
|
|
sysCache.p, sysCache.at = readSystem(), time.Now()
|
|
}
|
|
return sysCache.p
|
|
}
|
|
|
|
func loopback(host string) bool {
|
|
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|
|
|
|
// bypassed matches the system's exception list: exact hosts, "*.example.com"
|
|
// or ".example.com" suffixes, and "<local>" for dotless names.
|
|
func bypassed(host string, list []string) bool {
|
|
host = strings.ToLower(host)
|
|
for _, b := range list {
|
|
b = strings.ToLower(strings.TrimSpace(b))
|
|
switch {
|
|
case b == "":
|
|
case b == "<local>":
|
|
if !strings.Contains(host, ".") {
|
|
return true
|
|
}
|
|
case strings.HasPrefix(b, "*."), strings.HasPrefix(b, "."):
|
|
suf := strings.TrimPrefix(b, "*")
|
|
if strings.HasSuffix(host, suf) || host == suf[1:] {
|
|
return true
|
|
}
|
|
case host == b:
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|