Documentation
¶
Overview ¶
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, the system's proxy (macOS network settings, Windows Internet Options, a Linux desktop's GNOME gsettings or KDE kioslaverc), then the login shell's *_PROXY (shell.go). Loopback is never proxied, nor, unless a provider has a proxy of its own, a host on the user's own network (lan.go).
Index ¶
- Constants
- Variables
- func AgainOverHTTP1(ctx context.Context, get func(context.Context) error) error
- func Bridge(socks *url.URL) (string, error)
- func BrokeOff(err error) bool
- func CAPool(ca string) (*x509.CertPool, error)
- func CalmHTTP2(t *http.Transport) error
- func CertSHA256(c *x509.Certificate) string
- func CheckClientCert(cert, key, ca string) error
- func CheckResolve(rules map[string][]string) (map[string][]string, error)
- func Choice(ctx context.Context) string
- func ColonHex(pin string) string
- func Describe() (proxy, source string)
- func DialSOCKS(ctx context.Context, socks *url.URL, addr string) (net.Conn, error)
- func Dispatch(base *http.Transport) http.RoundTripper
- func Downloads(ctx context.Context) context.Context
- func Env(env []string) []string
- func EnvDownloads(env []string) []string
- func EnvForBun(env []string) []string
- func EnvForBunWith(choice string, env []string, loopback bool) ([]string, error)
- func EnvWith(choice string, env []string) []string
- func For(u *url.URL) (*url.URL, error)
- func ForBun(proxy string) string
- func Func(req *http.Request) (*url.URL, error)
- func HTTP2Health() *http.HTTP2Config
- func Held(ctx context.Context) error
- func Hold(ctx context.Context, err error) context.Context
- func Install()
- func IsSOCKS(u *url.URL) bool
- func LocalIP(ip net.IP) bool
- func LocalName(host string) bool
- func NormalPin(s string) string
- func OverHTTP1(ctx context.Context) context.Context
- func Parse(s string) (*url.URL, error)
- func Redact(raw string) string
- func Resolve(choice string) string
- func SetLookup(f func(ctx context.Context, host string) ([]net.IPAddr, error)) (restore func())
- func TrustEnvRoots(t *http.Transport)
- func Watch() func() bool
- func With(ctx context.Context, choice string) context.Context
- func WithClientCert(ctx context.Context, cert, key, ca, pin string, bases ...string) context.Context
- func WithHTTP1(ctx context.Context, bases ...string) context.Context
- func WithResolve(ctx context.Context, rules map[string][]string) context.Context
- func WithUncheckedTLS(ctx context.Context, bases ...string) context.Context
- type PinMismatchError
- type Proxy
Constants ¶
const DeadProxy = "http://127.0.0.1:9"
DeadProxy is where a CLI is sent for a saved proxy that is gone: a port nothing listens on, so its requests fail where a missing proxy would have sent them straight out.
Variables ¶
var ErrClientCertOverHTTPSProxy = errors.New("a provider with a client certificate or a CA of its own can't be reached through an https:// proxy, which would be shown them too: give it an http:// or socks5:// proxy, or none")
ErrClientCertOverHTTPSProxy is why a request made with a provider's own TLS identity isn't sent through an https:// proxy: the proxy's own TLS is dialled with the same config, so it could be shown the certificate, and its own be taken for being signed by the provider's CA.
var ErrUncheckedOverHTTPSProxy = errors.New("a provider whose certificate isn't checked can't be reached through an https:// proxy, whose certificate would go unchecked with it: give it an http:// or socks5:// proxy, or none")
ErrUncheckedOverHTTPSProxy is why a request to a host whose certificate is not checked isn't sent through an https:// proxy: the proxy's own TLS is dialled with the same config, so its certificate would go unchecked too.
var HTTP1 func(u *url.URL) bool
HTTP1 says a request to u goes over HTTP/1.1 only, whatever its transport offers. internal/provider sets it to the ChatGPT backend's host, which Codex itself speaks HTTP/1.1 to; nil leaves every request to its transport.
var PingAfter, PingWait = 10 * time.Second, 10 * time.Second
PingAfter and PingWait: an HTTP/2 connection that has had no frame for PingAfter is pinged, and dropped when no answer comes within PingWait. The health check is the only probe of a connection whose path has died silently, so a turn in flight on it waits for the next one: PingAfter is how long a vendor's silence before a turn's first byte can cost (#4936, a first try read EOF 20.5s in on a connection whose tunnel a NAT or a relay had dropped). The budget PingAfter+PingWait is also the margin a body under uploadBytes needs on a slow uplink, a ping queued behind it answered only after the last byte (#3909): 20s covers 64KB at about 3KB/s.
var PingAgain = 5 * time.Minute
PingAgain is how long a connection still silent after its first health ping waits for the next (CalmHTTP2): gRPC's servers, Google's among them, count a ping that comes sooner after the last, with nothing sent between, as a strike, and end the connection at the third.
Functions ¶
func AgainOverHTTP1 ¶ added in v0.1.3185
AgainOverHTTP1 runs get, and once more with every request under its context over HTTP/1.1 (OverHTTP1) when it failed with the connection broken off (BrokeOff) while ctx is live. get starts over, or goes on from what it kept, as its caller does.
func Bridge ¶ added in v0.1.584
Bridge is the http:// address of a proxy on loopback carrying what it is given through the SOCKS5 proxy socks, one for each, kept while magpie runs.
func BrokeOff ¶ added in v0.1.3185
BrokeOff says err is a transfer the connection broke off: the connection failed or dropped (a net.Error, a reset, unexpected EOF), or an HTTP/2 stream or connection was ended under it (a stream error such as INTERNAL_ERROR, GOAWAY, a lost connection). A context that ended is no such thing: the caller stopped it.
func CAPool ¶
CAPool is caPool for a caller outside: the system's roots with the PEM file ca's beside them.
func CalmHTTP2 ¶ added in v0.1.3256
CalmHTTP2 has t, a transport to the vendors, speak HTTP/2 with a health check of magpie's own instead of Go's (HTTP2Health). Go's pings a connection each time it has read no frame for PingAfter, and the ping's own answer is a frame, so a vendor silent for minutes before a turn's first byte is pinged every 10s for all of them. Google's front end counts the pings it gets while it has sent nothing and ends the connection: GOAWAY ENHANCE_YOUR_CALM "too_many_pings" after about four minutes of gemini thinking through Antigravity, and the turn was lost and sent again (zdevoz on Discord). gRPC's servers, Google's among them, take a ping as abuse when it comes within 5 minutes of the last one with no HEADERS or DATA sent between (keepalive.md: GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS).
Here the first ping of a silence still goes out PingAfter into it, so a connection whose path died before or as a request went out is found as soon as before (#2843, #4936), and dropped when no answer comes within PingWait. A silence starts at the vendor's last HEADERS or DATA, or at a request handed to the connection. A ping answered shows the path alive, and the next waits PingAgain: a 4-minute wait for a first byte has one ping, not twenty-four. A path that dies later in a long wait is found at the next ping, or by the request's own wait for its answer.
t's settings are read now: CalmHTTP2 is called once t is set up, and a clone of t is calmed again with its own connections (Dispatch).
func CertSHA256 ¶
func CertSHA256(c *x509.Certificate) string
CertSHA256 is c's fingerprint as a pin is kept: the SHA-256 of its DER, in lower-case hex.
func CheckClientCert ¶
CheckClientCert says why a provider's TLS identity can't be used: a certificate without its key or a key without its certificate, a pair that doesn't load or has run out, a CA file with no certificate. nil with none set.
func CheckResolve ¶
CheckResolve says why rules can't be kept, and returns them as they are kept: host names in lower case, places trimmed, repeated places left out. nil for none. A host is a name (not an address or a URL), and has at least one place: an IP address, with a port if the place is not on the URL's, or "dns:" and the name to follow.
func Choice ¶ added in v0.1.427
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 Describe ¶
func Describe() (proxy, source string)
Describe says, for the settings page, what a request to u would use.
func DialSOCKS ¶ added in v0.1.584
DialSOCKS connects to addr (host:port) through the SOCKS5 proxy socks, the user and password in its address used when it has them. The host is sent by name, as Go's own transport sends it, so the proxy resolves it.
func Dispatch ¶ added in v0.1.418
func Dispatch(base *http.Transport) http.RoundTripper
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 move of the global proxy (the settings) drops the idle connections the old one dialled, which a transport, pooling by target host alone, would have carried over the switch (#4523). A request HTTP1 names, one to a host its provider asked HTTP/1.1 of (WithHTTP1), every request under OverHTTP1 (a download trying again after its HTTP/2 transfer broke off), and one to a host whose HTTP/2 connection carried a TLS record that didn't decrypt (recordError, for h2Broken after) go through a clone kept for HTTP/1.1 (http1Only), and one to a host whose certificate the provider's user said not to check (WithUncheckedTLS) through a clone kept for that host alone (uncheckTLS), and one made with a provider's own TLS identity (WithClientCert) through a clone kept for it (clientTLS). A host whose IPv6 is taken but goes nowhere is dialed over IPv4 (v4Fallback), and one on the user's own network is given lanDial to connect (lanDialCap).
func Downloads ¶
Downloads is ctx for magpie's downloads of plugins — the plugins list, npm's registry, GitHub, the plugins repo's dist branch, Bun itself — which go through the proxy Settings sets for them (PluginProxy) when it names one (JonahFu on X: GitHub reached only through a proxy, the vendors without). Its "" leaves ctx as it is, so they follow the global proxy as every other request does.
func Env ¶ added in v0.1.53
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 EnvDownloads ¶
EnvDownloads is Env for a program magpie starts to download plugins (bun add and update, git): plugin downloads' proxy (Downloads), or, when that names none, Env's.
func EnvForBun ¶ added in v0.1.1139
EnvForBun is env for a bun that magpie starts (bun add, update, remove, run, or an agent CLI's bun add -g): its *_PROXY with a SOCKS5 one bridged (ForBun). Bun takes only http:// and https:// proxies, and with a socks5:// one every registry fetch failed as UnsupportedProxyProtocol (#1409), which a Mac with only a SOCKS system proxy hands it through Env.
func EnvForBunWith ¶ added in v0.1.1280
EnvForBunWith is EnvWith's env (the proxy choice names, see With) for a Bun-built CLI magpie runs on a provider's behalf — Claude Code, for its subscription's requests, warm-ups, usage and sign-ins — with a SOCKS5 proxy bridged as EnvForBun bridges it. Given socks5:// as it was, Claude Code failed every request as UnsupportedProxyProtocol, after minutes of its own retries (#2651). loopback says the CLI reaches this computer's loopback, where a bridge listens; one that doesn't (Claude Code in a WSL distro under NAT) can't be bridged. A SOCKS proxy left in env, there or where the bridge can't start, is an error, at once: the CLI would fail with it every time.
func EnvWith ¶ added in v0.1.418
EnvWith is Env for a CLI run on one provider's behalf, whose own proxy is choice (see With): "" is Env's.
func ForBun ¶ added in v0.1.584
ForBun is a proxy's address as the plugin host's Bun can use it: a SOCKS5 one is bridged, any other is as it is.
func Func ¶
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 HTTP2Health ¶ added in v0.1.1291
func HTTP2Health() *http.HTTP2Config
HTTP2Health is the HTTP/2 setting of a transport to the vendors. Every request to one host shares one HTTP/2 connection, and one gone silent without closing (a proxy's way out died, a NAT forgot the flow) was given every new request, each waiting for its transport's ResponseHeaderTimeout while the kernel took the writes, until a restart (#2843). A ping finds it, and the requests on it fail, so a group moves on and the next request dials anew. A live vendor answers a ping whatever its streams are doing, so a long wait for a first byte is never cut by it. But a ping's answer restarts the clock, so a wait of minutes is pinged every PingAfter, which Google's front end ends the connection for: the gateway's transports ping by CalmHTTP2 instead, and this is for http.DefaultTransport's short requests.
func Held ¶ added in v0.1.1169
Held is the error ctx holds its requests with (Hold), nil when it holds none.
func Hold ¶ added in v0.1.1169
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 Install ¶
func Install()
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). Its HTTP/2 connections are checked by Go's own pings (HTTP2Health), and the requests magpie sends on its own are written down (probelog).
func LocalIP ¶ added in v0.1.2244
LocalIP says ip is on this computer or the local network: a loopback, private (10/8, 172.16/12, 192.168/16, fc00::/7) or link-local address. Carrier-grade NAT (100.64/10) is not: it is a Tailscale address, but also an ISP's.
func LocalName ¶ added in v0.1.2244
LocalName reports whether host, a name, resolves (in lanWait) to at least one address and every address it resolves to is on this computer or the local network (LocalIP). A name that doesn't resolve, or doesn't in time, is not: what can't be read is unknown, and unknown is masked.
func NormalPin ¶
NormalPin is a typed SHA-256 fingerprint as a pin is kept: lower-case hex, its colons, spaces and a "sha256:" before it taken out; "" for one that isn't 64 hex digits.
func OverHTTP1 ¶ added in v0.1.3185
OverHTTP1 is ctx for requests that go over HTTP/1.1 to every host, through a transport Dispatch made: a download whose HTTP/2 transfer broke off part way tries again with it (AgainOverHTTP1). Some proxies cut an HTTP/2 stream mid-file (unexpected EOF, a stream reset, GOAWAY) while they carry HTTP/1.1 to the same host whole, and a download that tried again over HTTP/2 broke the same way on every try (biubiu8802 on X: the update said "download broke off" each time, while the proxy served other downloads). It is kept through With.
func Parse ¶
Parse reads a proxy address; a bare host:port is an HTTP proxy. A saved proxy ("@id") is read as the address it stands for, its login in it; one that is gone is an error.
func Redact ¶ added in v0.1.1221
Redact is a proxy address as magpie may show it: in a log line, an error, a status or the GUI (#852, #1886). Its user and password are left out, and the rest kept as it was written (no scheme added to one written without). A malformed address is "", because its userinfo cannot be safely separated. Requests still go through the address itself; only what is shown of it goes through here.
func Resolve ¶
Resolve is a proxy choice as the proxy it stands for: a saved proxy (settings.NamedProxy, "@id") as its address with its login, any other choice as it is. A reference to a proxy that isn't there is kept as it is, and Parse refuses it: a request meant for a proxy never goes direct for want of one.
func SetLookup ¶ added in v0.1.2244
SetLookup has names resolved by f, for a test that must not reach the network, and forgets what earlier lookups found; restore puts the system's resolver back.
func TrustEnvRoots ¶
TrustEnvRoots has t take, beside the system's roots, the roots in the files and folders those variables name. Nothing is taken off: a server signed by a root the system trusts is trusted as before, and one signed by none of them is refused as before. With none set, or none holding a certificate, t is left as it is.
func Watch ¶ added in v0.1.2664
func Watch() func() bool
Watch is a check a transport of its own makes before each request: it says the global proxy in magpie's settings moved since the check's last call, and the caller closes its transport's idle connections, which would carry the request after a switch through the old proxy. The setting is read at dial time (Func), but a transport pools by target host, and its HTTP/2 connections by the authority alone, so a connection dialled through the old proxy carries the requests after a switch too, until it is dropped. Dropping the idle connections sends the request after it through the proxy in force; one a request is still reading is left to finish. Each check keeps its own last value, so each transport sees, and answers, a move on the first request it sends after it (#4523, #4824). The first call sees no move.
func With ¶ added in v0.1.418
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, "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. A choice ctx already names is replaced, "" too: a request magpie makes for one provider while serving another's (a web search asked of Codex in a turn served by a provider set to "direct", #1522) goes through its own provider's proxy, never the one it was made under. A provider's leave not to check its certificate (WithUncheckedTLS) goes the same way: it is the outer provider's, and dropped here, and so is its ask for HTTP/1.1 (WithHTTP1), its own TLS identity (WithClientCert) and its host rules (WithResolve).
func WithClientCert ¶
func WithClientCert(ctx context.Context, cert, key, ca, pin string, bases ...string) context.Context
WithClientCert is ctx for requests made on behalf of a provider that shows the server a certificate of its own (cert and key, PEM files), or trusts a CA of its own for the server's (ca, a PEM file), or both: a request through a transport Dispatch made to the host of one of bases, over https, goes through a transport kept for that identity alone, which shows the certificate when the server asks for one and takes a server certificate signed by ca beside the system's roots. Any other host — a redirect elsewhere, an image or a balance URL on another host — is sent without them, and an https:// proxy is refused for it, as the proxy's TLS would be dialled with them (clientTLS). With (a request made for another provider) drops it.
pin, the SHA-256 of a server certificate the user trusted as it is (Zhangzhangi on X: a magpie at home on a domain of its own with a self-signed certificate), has the certificate's chain and name left unchecked at those hosts, and the handshake refused unless the server's own certificate is that one (pinTLS).
func WithHTTP1 ¶ added in v0.1.3133
WithHTTP1 is ctx for requests made on behalf of a provider the user added and asked to be spoken HTTP/1.1 to (kepner_53726 on Discord: a company gateway whose HTTP/2 connections broke mid-reply with "tls: bad record MAC", while HTTP/1.1 to it held): a request through a transport Dispatch made to the host of one of bases goes over HTTP/1.1 only, through a transport kept for HTTP/1.1. Any other host is spoken to as before. With (a request made for another provider) drops it.
func WithResolve ¶
WithResolve is ctx for requests made on behalf of a provider the user added whose host names are connected to addresses of their choosing, as curl's --resolve does (@quantum_lsfc on X: a managed network sends some domains to an internal L4 SNI gateway, an IP in the base URL fails, and /etc/hosts can't be edited). rules maps a host name to the places its TCP connection is made, in order: an IP address (with a port of its own if it names one), or "dns:<name>", the addresses that name resolves to when the connection is made, so a rotation of its records is followed. The URL, so the TLS server name and the Host header, stays as it was; only where the connection goes changes. A place that doesn't connect is followed by the next, and when none does the host is dialled as if there were no rule. A request that goes through a proxy is sent to the proxy as before: the proxy connects to the name itself, so no rule applies to it. With (a request made for another provider) drops it. CheckResolve is what a saved rule is held to.
func WithUncheckedTLS ¶ added in v0.1.1258
WithUncheckedTLS is ctx for requests made on behalf of a provider the user added whose HTTPS server they asked magpie not to check the certificate of (#2383, fangzhengjin: vLLM on the LAN, Ollama behind a self-signed cert, a company CA): a request through a transport Dispatch made to the host of one of bases, over https, has its certificate taken as it is, through a transport kept for that host alone. Any other host — a redirect elsewhere, an image or a balance URL on another host — is checked as before, and an https:// proxy is refused for it (uncheckTLS). With (a request made for another provider) drops it.
Types ¶
type PinMismatchError ¶
type PinMismatchError struct{ Host, Got, Want string }
PinMismatchError is why a server whose certificate was pinned isn't spoken to: it showed another one.
func (*PinMismatchError) Error ¶
func (e *PinMismatchError) Error() string