Files
magpie/internal/netproxy/shell.go
T
yetone 4e93ff9fce netproxy: Auto takes the login shell's *_PROXY when the system has none
jimlee0824v2 on X asked for the proxy to default by region, because
they set it in magpie by hand and take it out again. Clash's and most
proxy apps' guides have `export https_proxy=…` put in the shell
profile, with the app's "system proxy" off: every program run in a
terminal goes through it, magpie run there too, but magpie opened from
the Dock or a desktop menu has no *_PROXY, found no system proxy, and
went direct.

Auto now goes, in order: magpie's setting ("direct" too), magpie's own
environment, the system's proxy, then the login shell's HTTPS_PROXY
(https://, wss://) or HTTP_PROXY, else ALL_PROXY, with its NO_PROXY
(shell.go). Settings says "Following HTTPS_PROXY" for it, and a CLI
magpie starts is given it as it is the system's (EnvWith). A value the
user set anywhere still wins; the shell's is the user's too.
- A loopback proxy is taken only while its port has a listener, looked
  at again every 2s while down and 15s while up, so a profile that
  exports the proxy app's port for good doesn't cut magpie off while
  the app is closed, and the app opened later is taken within 2s.
- The shell's values come from what proc last read of it (UserPath at
  start, or a LoginPath since), through proc.LoginEnvSaid, a lock-free
  snapshot: LoginPath holds its lock while the shell answers, and this
  is read on every request. A profile edited after magpie started is
  read at the next LoginPath. Windows: none (a desktop app there has
  the user's environment already).

Not a region or language guess: those are wrong for anyone travelling
or on a VPN/TUN, and say nothing of whether a proxy is running. PAC
(Clash's "PAC mode" system proxy) is still not read.

Run:
- red on the old wiring (shell.go present, netproxy.go from the parent):
  TestAutoTakesTheShellsProxy, TestShellProxyByScheme,
  TestClosedShellProxyGoesDirect fail; TestShellProxyComesLast (settings
  "direct", magpie's environment, the system's proxy win) passes there
  and here.
- mica-run: go test -race ./internal/netproxy ./internal/proc ok; go vet
  of both for darwin, GOOS=linux and GOOS=windows ok.
- wsl-run and win-run: go test ./internal/netproxy ./internal/proc ok.
- gofmt -l clean.
- full suite, both commits on 677034e1a (mica-run go test -timeout 45m
  ./...): every package ok but the root package's
  TestNoEagerPackageRegexps, red on main since 6bdeedc6f over
  internal/gateway/chatpage.go (#6689, filed already). Rebased on
  1edebf050 (no netproxy, proc or agent change): netproxy and proc ok
  again at the pushed head.
Not tried: magpie opened from the Dock with a real Clash and a real
.zshrc export.
2026-11-15 01:19:17 +08:00

152 lines
4.0 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 = func(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.
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
}