Files
yetone 04ca2afab2 netproxy: Auto reads a Linux desktop's proxy, GNOME's and KDE's (#4147)
On Linux, Auto read no system proxy at all (system_other.go answered
none), so with Clash Verge's system proxy on, magpie opened from the
desktop's menu, with no *_PROXY, went direct and Settings said "No
system proxy found". The user had to type the proxy into magpie and
take it out again when they turned the proxy off.

system_linux.go reads the desktop's own setting, where its apps and
Clash Verge (sysproxy-rs) put it:
- GNOME (and Cinnamon, MATE, Budgie): `gsettings list-recursively
  org.gnome.system.proxy`, mode 'manual' only; HTTPS over HTTP over
  SOCKS, ignore-hosts as the bypass list.
- KDE: ~/.config/kioslaverc (XDG_CONFIG_HOME), ProxyType=1 only, its
  "http://127.0.0.1 7897" address form, NoProxyFor unless reversed.
  Read first on a KDE session, else after GNOME's.
No gsettings, or no GNOME proxy schema, is none; a gsettings that fails
otherwise is a failed read, which keeps the last good one as on macOS.
PAC ('auto', ProxyType=2/3) is not read, as on macOS and Windows.

Codex's view is unchanged on Linux: its reqwest reads no desktop
setting there, so codexProxyFor takes no system proxy unless on macOS
or Windows (codexSystem), with test cases that fail without it.

Not a timezone or language guess (the X ask from jimlee0824v2): it is
wrong for anyone travelling, on a VPN or a TUN, and the system's proxy
is what says whether one is on.

Run:
- red on the old code: TestSystemReadsGnome, TestSystemReadsKDE and
  TestSystemWithoutDesktopProxy (3 of 3); the two Linux cases of
  TestCodexProxyFor with the old codex_proxy.go.
- real gsettings on Ubuntu 24.04 (WSL magpie-desktop) under
  dbus-run-session with a temp XDG_CONFIG_HOME set to manual
  127.0.0.1:7897: system() gave http://127.0.0.1:7897, bypass
  [localhost 127.0.0.0/8 ::1]. The distro's own setting not touched.
- mica-run: go test ./internal/netproxy ./internal/agent ok; go vet of
  both for darwin, GOOS=linux and GOOS=windows ok.
- wsl-run: go test -run 'Proxy|Gsettings|Kioslaverc|TestSystem|Codex'
  ./internal/netproxy ./internal/agent ok.
- win-run: go test ./internal/netproxy ok.
- gofmt -l clean.
Not tried: a real KDE session (fixture from sysproxy-rs' format), the
GUI on a Linux desktop.
2026-11-15 01:18:58 +08:00

63 lines
1.5 KiB
Go

package netproxy
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/proc"
)
// system reads the desktop's proxy (desktop.go): KDE's kioslaverc on a KDE
// session, GNOME's gsettings otherwise, then the other. A desktop with
// neither (no gsettings, or no GNOME proxy schema, and no kioslaverc) has
// none; a gsettings that is there and fails is a failed read.
func system() (Proxy, bool) {
kde := func() Proxy {
b, err := os.ReadFile(filepath.Join(configDir(), "kioslaverc"))
if err != nil {
return Proxy{}
}
return parseKioslaverc(string(b))
}
if strings.Contains(strings.ToUpper(os.Getenv("XDG_CURRENT_DESKTOP")), "KDE") {
if p := kde(); p.URL != "" {
return p, true
}
}
p, ok := gnome()
if p.URL == "" {
if k := kde(); k.URL != "" {
return k, true
}
}
return p, ok
}
func gnome() (Proxy, bool) {
ctx, cancel := context.WithTimeout(context.Background(), toolWait)
defer cancel()
out, err := proc.CommandContext(ctx, "gsettings", "list-recursively", "org.gnome.system.proxy").Output()
var exit *exec.ExitError
switch {
case err == nil:
return parseGsettings(string(out)), true
case errors.Is(err, exec.ErrNotFound):
return Proxy{}, true
case errors.As(err, &exit) && strings.Contains(string(exit.Stderr), "No such schema"):
return Proxy{}, true
}
return Proxy{}, false
}
func configDir() string {
if d := os.Getenv("XDG_CONFIG_HOME"); filepath.IsAbs(d) {
return d
}
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config")
}