@bitssly (X, magpie 0.1.4519, Windows 11 amd64): with the proxy on Auto,
every model failed with 502 "proxy not reachable" once their VPN app was
off, a restart of magpie too, until they set the proxy Off. A VPN or
proxy app that quits without clearing Internet Options leaves
ProxyEnable=1 and ProxyServer=127.0.0.1:port in HKCU, and Auto took the
system proxy as it was: every request went to a port nothing listens on.
Auto now takes a system proxy on loopback only while its port accepts a
connection (liveSystem, through the same listening check the login
shell's proxy already had: 300ms dial, kept 15s while up, 2s while down,
so the VPN coming back is followed within 2 seconds). Every route that
asks Auto goes through it: For (netproxy.Func for the gateway and every
transport, and http.DefaultClient through Install), EnvWith (the CLIs
magpie starts, Bun, and the plugin host, whose proxy check every 15s/2s
restarts it on a change) and Describe. Settings › Network says it:
source "down" reads "Nothing answers at the proxy found, {proxy};
requests go direct until it runs again", in every language.
Not changed, on purpose:
- A proxy set in magpie is never gone around: a dead one still fails.
- The environment's *_PROXY, set for this process by whoever started
it, is taken as it is, dead or not.
- A system proxy elsewhere than loopback isn't probed: a proxy that is
down can't be told from a network that drops a probe.
- internal/agent/codex_proxy.go reads the raw System(): it predicts what
Codex itself does.
- Going direct while the VPN is off means vendors see the computer's own
address. A user who needs to fail closed sets the proxy in magpie.
Run:
- win-run (xiaomi-gpu, Windows 11) go test ./internal/netproxy: ok.
TestWindowsDeadSystemProxyGoesDirect writes ProxyEnable/ProxyServer to
a scratch key that RegOverridePredefKey puts in place of HKCU, so the
box's own Internet Options are untouched. It reads them through the
real system() and the real 300ms dial: direct while nothing listens,
the proxy again once a listener runs.
- win-run go test ./internal/plugin: TestPiPackage,
TestCachedReadsSavedProvidersOnce (#7197), TestDistReadFailedIsntMissing
(#6075) and TestPluginLeavesTheUserNetworkDirect (#4115) are red on
Windows at origin/main 239da9991 too; the last two pass 3 of 3 here.
- wsl-run (magpie-desktop) go test -timeout 45m ./... at ac4da60cb
(these two commits on origin/main 9bf5848c1): ok except
TestRequestStateKeptAcrossACatalogFetchedAgain (#6572, red at
origin/main too), TestSearchQueueKeepsCodexAlive/cancel (#7090) and
the root, cli and gui packages, which need gtk4 there.
- At the pushed head (rebased on e4e9eb092), mote-run go test
-timeout 45m of netproxy, gui, plugin, gateway, usage, provider and
agent: ok except TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes
(#7117, red 3 of 3 at origin/main e4e9eb092 too) and two TempDir
cleanup reds, TestSetPortServesWhenTheOldOneWasTaken (#7112) and
TestRequestChunksFromDisk (#4390; 3 of 3 alone).
- go vet ./... and GOOS=windows go vet ./... on mote: ok; vet natively
on Windows: ok; Linux vet of netproxy, plugin, gateway, agent and
provider in WSL: ok. gofmt -l clean.
- GUI, wsl-run: proxy-down.test.cjs (5 languages, 1100 and 420px,
chromium and webkit), i18n-keys and every *proxy* and settings-* file
at ac4da60cb. Reds: settings-proxy-auth / settings-proxy-remember 4s
clicks beside a Go suite (#7192, #5562; 30 of 30 alone),
provider-proxy (#7100) and settings-update-source webkit, red the same
at origin/main 9bf5848c1. At the pushed head proxy-down, i18n-keys,
settings-search, settings-proxy-remember, proxy-gaps and
proxy-own-wording: 94 of 94.
Broken by hand: liveSystem returning the system proxy as it is turns
TestDeadLoopbackSystemProxyGoesDirect red (WSL) and the Windows
registry test red (xiaomi-gpu).
Not tried: magpie.exe against the box's real Internet Options (shared
machine), which source the reporter's proxy was in (registry or env), and
whether a connection dialled direct is kept after the VPN comes back
(#4523 is that class for a changed proxy).
156 lines
4.1 KiB
Go
156 lines
4.1 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// A proxy exported in the shell profile (`export https_proxy=…` in
|
|
// .zshrc, as Clash's and most proxy apps' guides say) reaches every
|
|
// program run in a terminal, magpie run there too, but not magpie opened
|
|
// from the Dock or a desktop menu: with the proxy app's "system proxy"
|
|
// off, Auto found none and went direct, and the user set the proxy in
|
|
// magpie by hand, then took it out again when they left the proxy off
|
|
// (jimlee0824v2 on X). Auto now takes the login shell's after the
|
|
// system's, as a terminal would. A loopback one is taken only while
|
|
// something listens on its port, so a profile that exports the proxy
|
|
// app's port for good doesn't cut magpie off while the app is closed.
|
|
|
|
// shellVar is what the login shell last said of a proxy variable
|
|
// (proc.ProxyVars), "" for none; it never waits on the shell.
|
|
var shellVar = proc.LoginEnvSaid
|
|
|
|
func shellOf(keys ...string) string {
|
|
for _, k := range keys {
|
|
if v := strings.TrimSpace(shellVar(k)); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// fromShell is fromEnv over the login shell's variables: HTTPS_PROXY for
|
|
// an https:// or wss:// request and HTTP_PROXY for any other, else
|
|
// ALL_PROXY, none for a host its NO_PROXY names; nil when there is none,
|
|
// or it is a loopback one nothing listens on.
|
|
func fromShell(u *url.URL) *url.URL {
|
|
if noProxy(u.Hostname(), shellOf("NO_PROXY", "no_proxy")) {
|
|
return nil
|
|
}
|
|
var raw string
|
|
switch u.Scheme {
|
|
case "https", "wss":
|
|
raw = shellOf("HTTPS_PROXY", "https_proxy")
|
|
default:
|
|
raw = shellOf("HTTP_PROXY", "http_proxy")
|
|
}
|
|
if raw == "" {
|
|
raw = shellOf("ALL_PROXY", "all_proxy")
|
|
}
|
|
return liveShell(raw)
|
|
}
|
|
|
|
// shellProxy is the login shell's proxy for a CLI magpie starts, which
|
|
// reads one for every request (HTTPS_PROXY, else HTTP_PROXY, else
|
|
// ALL_PROXY), and its NO_PROXY's hosts; "" when there is none, or it is a
|
|
// loopback one nothing listens on.
|
|
func shellProxy() (string, []string) {
|
|
p := liveShell(shellOf("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy"))
|
|
if p == nil {
|
|
return "", nil
|
|
}
|
|
var bypass []string
|
|
for _, h := range strings.Split(shellOf("NO_PROXY", "no_proxy"), ",") {
|
|
if h = strings.TrimSpace(h); h != "" {
|
|
bypass = append(bypass, h)
|
|
}
|
|
}
|
|
return p.String(), bypass
|
|
}
|
|
|
|
func liveShell(raw string) *url.URL {
|
|
if raw == "" || strings.HasPrefix(raw, "@") { // never a saved proxy's name
|
|
return nil
|
|
}
|
|
if !strings.Contains(raw, "://") {
|
|
raw = "http://" + raw
|
|
}
|
|
p, err := url.Parse(raw)
|
|
if err != nil || p.Hostname() == "" || !listening(p) {
|
|
return nil
|
|
}
|
|
return p
|
|
}
|
|
|
|
// How long a loopback proxy's port is taken as listening (shellUp) or
|
|
// not (shellDown) once looked at; a proxy app opened after magpie is
|
|
// taken within shellDown.
|
|
var (
|
|
shellUp = 15 * time.Second
|
|
shellDown = 2 * time.Second
|
|
dialShell = dialPort
|
|
)
|
|
|
|
// dialPort is whether something takes a connection at addr.
|
|
func dialPort(addr string) bool {
|
|
c, err := net.DialTimeout("tcp", addr, 300*time.Millisecond)
|
|
if err == nil {
|
|
c.Close()
|
|
}
|
|
return err == nil
|
|
}
|
|
|
|
var shellPorts struct {
|
|
sync.Mutex
|
|
seen map[string]shellPort
|
|
}
|
|
|
|
type shellPort struct {
|
|
at time.Time
|
|
up bool
|
|
}
|
|
|
|
// listening is whether p's port has a listener, for a loopback p; any
|
|
// other is taken as it is, as a terminal's programs take it. Auto asks it
|
|
// of the login shell's proxy and the system's (liveSystem).
|
|
func listening(p *url.URL) bool {
|
|
if !loopback(strings.ToLower(p.Hostname())) {
|
|
return true
|
|
}
|
|
port := p.Port()
|
|
if port == "" {
|
|
switch p.Scheme {
|
|
case "https":
|
|
port = "443"
|
|
case "socks5", "socks5h", "socks4", "socks4a", "socks":
|
|
port = "1080"
|
|
default:
|
|
port = "80"
|
|
}
|
|
}
|
|
addr := net.JoinHostPort(p.Hostname(), port)
|
|
shellPorts.Lock()
|
|
s, ok := shellPorts.seen[addr]
|
|
shellPorts.Unlock()
|
|
keep := shellDown
|
|
if s.up {
|
|
keep = shellUp
|
|
}
|
|
if ok && time.Since(s.at) < keep {
|
|
return s.up
|
|
}
|
|
up := dialShell(addr)
|
|
shellPorts.Lock()
|
|
if shellPorts.seen == nil {
|
|
shellPorts.seen = map[string]shellPort{}
|
|
}
|
|
shellPorts.seen[addr] = shellPort{time.Now(), up}
|
|
shellPorts.Unlock()
|
|
return up
|
|
}
|