Files
magpie/internal/proc/path_windows.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

147 lines
4.6 KiB
Go

package proc
import (
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows/registry"
"github.com/yetone/magpie/internal/appdir"
)
// UserPath gives magpie, and what it starts, the Node a terminal has from
// fnm (#2685). An app started from the Start menu gets the user's PATH from
// the registry, as a terminal does, but fnm puts its Node on PATH from the
// shell's profile (`fnm env | Invoke-Expression`) and never in the
// registry: an MCP server run with npx, even by npx.cmd's full path, died
// with "'node' is not recognized" once the package it fetched ran node.
// The folder fnm's default alias names is added after the ones PATH has.
func UserPath() {
home, _ := os.UserHomeDir()
addPath(fnmWinPath(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")))
}
// UserBinDirs are the folders a user's command-line tools are installed in
// that exist here — npm's global one, bun's, volta's, pnpm's, mise's
// shims, the standalone installers', each Node version of fnm's, its
// default first — for finding one PATH doesn't reach.
func UserBinDirs() []string {
home, _ := os.UserHomeDir()
// mise's data folder: MISE_DATA_DIR, else XDG_DATA_HOME's mise (which
// mise follows on Windows too), else LOCALAPPDATA's
mise := filepath.Join(appdir.Getenv("LOCALAPPDATA"), "mise")
if d := os.Getenv("XDG_DATA_HOME"); filepath.IsAbs(d) {
mise = filepath.Join(d, "mise")
}
if d := os.Getenv("MISE_DATA_DIR"); filepath.IsAbs(d) {
mise = d
}
known := []string{
filepath.Join(appdir.Getenv("APPDATA"), "npm"),
filepath.Join(home, ".local", "bin"),
bunBin(),
filepath.Join(home, ".bun", "bin"),
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "Volta", "bin"),
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "pnpm"),
filepath.Join(mise, "shims"),
}
for _, r := range fnmWinRoots(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")) {
known = append(known, fnmWinBins(r)...)
}
var have []string
for _, d := range known {
if st, err := os.Stat(d); filepath.IsAbs(d) && err == nil && st.IsDir() {
have = append(have, d)
}
}
return have
}
// LoginPath is the PATH the registry has now, the machine's then the
// user's: what a terminal opened now gets, and what a magpie started
// before a CLI was installed doesn't.
func LoginPath() []string {
var dirs []string
for _, k := range []struct {
root registry.Key
path string
}{
{registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`},
{registry.CURRENT_USER, `Environment`},
} {
key, err := registry.OpenKey(k.root, k.path, registry.QUERY_VALUE)
if err != nil {
continue
}
if v, _, err := key.GetStringValue("Path"); err == nil {
// %SystemRoot%\system32 as Windows expands it: os.ExpandEnv of
// $SystemRoot$\system32 made C:\Windows$\system32
if x, err := registry.ExpandString(v); err == nil {
v = x
}
for _, d := range strings.Split(v, ";") {
if d = strings.TrimSpace(d); d != "" {
dirs = append(dirs, d)
}
}
}
key.Close()
}
return dirs
}
// LoginEnv is "" on Windows: an app started from the Start menu gets the
// user's variables from the registry, as a terminal does, so magpie's own
// environment has them.
func LoginEnv(string) string { return "" }
// LoginEnvKnown is "" on Windows too (LoginEnv).
func LoginEnvKnown(string) string { return "" }
// LoginEnvSaid is "" on Windows too (LoginEnv).
func LoginEnvSaid(string) string { return "" }
// ShellPath is LoginPath on Windows: PowerShell and cmd both take the
// registry's PATH.
func ShellPath(string) []string { return LoginPath() }
// LoginVars are the values the registry has now for names, the user's
// over the machine's: what a terminal opened now gets, and what a magpie
// started before the variable was set (setx, the Environment Variables
// dialog) doesn't. A ${NAME} in an MCP server's headers or environment is
// read so where magpie's own environment hasn't it. The values are only
// returned, never set in magpie's environment or logged.
func LoginVars(names []string) map[string]string {
out := map[string]string{}
for _, k := range []struct {
root registry.Key
path string
}{
{registry.CURRENT_USER, `Environment`},
{registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`},
} {
key, err := registry.OpenKey(k.root, k.path, registry.QUERY_VALUE)
if err != nil {
continue
}
for _, n := range names {
if _, have := out[n]; have || n == "" {
continue
}
v, typ, err := key.GetStringValue(n)
if err != nil {
continue
}
if typ == registry.EXPAND_SZ {
if x, err := registry.ExpandString(v); err == nil {
v = x
}
}
out[n] = v
}
key.Close()
}
return out
}