github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/sysproxy/sysproxy_linux.go (about) 1 //go:build !android 2 // +build !android 3 4 package sysproxy 5 6 import ( 7 "fmt" 8 "os" 9 "os/exec" 10 "strings" 11 12 "github.com/Asutorufa/yuhaiin/pkg/log" 13 ) 14 15 func SetSysProxy(httpHostname, httpPort, socks5Hostname, socks5Port string) { 16 if httpHostname != "" { 17 log.Debug("set http system proxy", "hostname", httpHostname, "port", httpPort) 18 } 19 if socks5Hostname != "" { 20 log.Debug("set socks5 system proxy", "hostname", socks5Hostname, "port", socks5Port) 21 } 22 23 if err := gnomeSetSysProxy(httpHostname, httpPort, socks5Hostname, socks5Port); err != nil { 24 log.Error("set gnome proxy failed", "err", err) 25 } 26 if err := kdeSetSysProxy(httpHostname, httpPort, socks5Hostname, socks5Port); err != nil { 27 log.Error("set kde proxy failed", "err", err) 28 } 29 } 30 31 func gnomeSetSysProxy(httpH, httpP, socks5H, socks5P string) error { 32 // GNOME 33 gsettings, err := exec.LookPath("gsettings") 34 if err != nil { 35 return nil 36 } 37 //https://wiki.archlinux.org/index.php/Proxy_server 38 //gsettings set org.gnome.system.proxy mode 'manual' 39 //gsettings set org.gnome.system.proxy.http host 'proxy.localdomain.com' 40 //gsettings set org.gnome.system.proxy.http port 8080 41 //gsettings set org.gnome.system.proxy.ftp host 'proxy.localdomain.com' 42 //gsettings set org.gnome.system.proxy.ftp port 8080 43 //gsettings set org.gnome.system.proxy.https host 'proxy.localdomain.com' 44 //gsettings set org.gnome.system.proxy.https port 8080 45 //gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12' , '*.localdomain.com' ]" 46 // gsettings set org.gnome.system.proxy mode 'manual' 47 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy", "mode", "manual").Run() 48 if httpH != "" || httpP != "" { 49 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.http", "host", httpH).Run() 50 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.http", "port", httpP).Run() 51 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.https", "host", httpH).Run() 52 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.https", "port", httpP).Run() 53 } 54 if socks5H != "" || socks5P != "" { 55 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.socks", "host", socks5H).Run() 56 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.socks", "port", socks5P).Run() 57 } 58 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy", "ignore-hosts", 59 fmt.Sprintf("['%s']", strings.Join(priAddr, "','"))).Run() 60 61 return nil 62 } 63 64 func kdeSetSysProxy(httpH, httpP, socks5H, socks5P string) error { 65 // KDE 66 if os.Getenv("XDG_SESSION_DESKTOP") != "KDE" { 67 return nil 68 } 69 70 kwriteconfig5, err := exec.LookPath("kwriteconfig5") 71 if err != nil { 72 return nil 73 } 74 75 // kwriteconfig5 --file kioslaverc --group 'Proxy Settings' --key httpProxy "http://127.0.0.1 8188" 76 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "NoProxyFor", strings.Join(priAddr, ",")).Run() 77 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1").Run() 78 if httpH != "" || httpP != "" { 79 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "httpProxy", fmt.Sprintf("http://%s %s", httpH, httpP)).Run() 80 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "httpsProxy", fmt.Sprintf("http://%s %s", httpH, httpP)).Run() 81 } 82 //_ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ftpProxy", fmt.Sprintf("http://%s %s", httpUrl.Hostname(), httpUrl.Port())).Run() 83 if socks5H != "" || socks5P != "" { 84 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "socksProxy", fmt.Sprintf("socks://%s %s", socks5H, socks5P)).Run() 85 } 86 // Notify kioslaves to reload system proxy configuration. 87 dbusSend, err := exec.LookPath("dbus-send") 88 if err != nil { 89 return fmt.Errorf("lookup dbus-send failed: %w", err) 90 } 91 _ = exec.Command(dbusSend, "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''") 92 93 return nil 94 } 95 96 func UnsetSysProxy() { 97 if err := gnomeUnsetSysProxy(); err != nil { 98 log.Error("unset gnome proxy failed", "err", err) 99 } 100 if err := kdeUnsetSysProxy(); err != nil { 101 log.Error("unset kde proxy failed", "err", err) 102 } 103 } 104 105 func gnomeUnsetSysProxy() error { 106 gsettings, err := exec.LookPath("gsettings") 107 if err != nil { 108 return nil 109 } 110 111 // GNOME 112 // gsettings set org.gnome.system.proxy mode 'manual' 113 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy", "mode", "none").Run() 114 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.http", "host", "0").Run() 115 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.http", "port", "0").Run() 116 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.https", "host", "0").Run() 117 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.https", "port", "0").Run() 118 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.socks", "host", "0").Run() 119 _ = exec.Command(gsettings, "set", "org.gnome.system.proxy.socks", "port", "0").Run() 120 121 return nil 122 } 123 124 func kdeUnsetSysProxy() error { 125 if os.Getenv("XDG_SESSION_DESKTOP") != "KDE" { 126 return nil 127 } 128 kwriteconfig5, err := exec.LookPath("kwriteconfig5") 129 if err != nil { 130 return nil 131 } 132 133 // KDE 134 // kwriteconfig5 --file kioslaverc --group 'Proxy Settings' --key httpProxy "http://127.0.0.1 8188" 135 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0").Run() 136 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "httpProxy", "").Run() 137 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "httpsProxy", "").Run() 138 //_ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ftpProxy", "").Run() 139 _ = exec.Command(kwriteconfig5, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "socksProxy", "").Run() 140 // Notify kioslaves to reload system proxy configuration. 141 dbusSend, err := exec.LookPath("dbus-send") 142 if err != nil { 143 return fmt.Errorf("lookup dbus-send failed: %w", err) 144 } 145 _ = exec.Command(dbusSend, "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''") 146 147 return nil 148 }