From a4bd995e95daf789b59b74e22facf6fb62aa090a Mon Sep 17 00:00:00 2001 From: Hiddify <114227601+hiddify1@users.noreply.github.com> Date: Wed, 23 Aug 2023 18:44:41 +0000 Subject: [PATCH] new: add KDE set system proxy support --- common/settings/proxy_linux.go | 81 ++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/common/settings/proxy_linux.go b/common/settings/proxy_linux.go index 193b94e04d..0ae82a81dd 100644 --- a/common/settings/proxy_linux.go +++ b/common/settings/proxy_linux.go @@ -16,10 +16,12 @@ import ( var ( hasGSettings bool + isKDE bool sudoUser string ) func init() { + isKDE = common.Error(exec.LookPath("kwriteconfig5")) == nil hasGSettings = common.Error(exec.LookPath("gsettings")) == nil if os.Getuid() == 0 { sudoUser = os.Getenv("SUDO_USER") @@ -37,32 +39,53 @@ func runAsUser(name string, args ...string) error { } func SetSystemProxy(router adapter.Router, port uint16, isMixed bool) (func() error, error) { - if !hasGSettings { - return nil, E.New("unsupported desktop environment") - } - err := runAsUser("gsettings", "set", "org.gnome.system.proxy.http", "enabled", "true") - if err != nil { - return nil, err - } - if isMixed { - err = setGnomeProxy(port, "ftp", "http", "https", "socks") - } else { - err = setGnomeProxy(port, "http", "https") - } - if err != nil { - return nil, err - } - err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "use-same-proxy", F.ToString(isMixed)) - if err != nil { - return nil, err + if hasGSettings { + err := runAsUser("gsettings", "set", "org.gnome.system.proxy.http", "enabled", "true") + if err != nil { + return nil, err + } + if isMixed { + err = setGnomeProxy(port, "ftp", "http", "https", "socks") + } else { + err = setGnomeProxy(port, "http", "https") + } + if err != nil { + return nil, err + } + err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "use-same-proxy", F.ToString(isMixed)) + if err != nil { + return nil, err + } + err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "manual") + if err != nil { + return nil, err + } + return func() error { + return runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "none") + }, nil } - err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "manual") - if err != nil { - return nil, err + if isKDE { + err := runAsUser("kwriteconfig5", "--file","kioslaverc", "--group", "'Proxy Settings'", "--key", "ProxyType", "1") + if err != nil { + return nil, err + } + if isMixed { + err = setKDEProxy(port, "ftp", "http", "https", "socks") + } else { + err = setKDEProxy(port, "http", "https") + } + if err != nil { + return nil, err + } + err := runAsUser("kwriteconfig5", "--file","kioslaverc", "--group", "'Proxy Settings'", "--key", "Authmode", "0") + if err != nil { + return nil, err + } + return func() error { + return err := runAsUser("kwriteconfig5", "--file","kioslaverc", "--group", "'Proxy Settings'", "--key", "ProxyType", "0") + }, nil } - return func() error { - return runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "none") - }, nil + return nil, E.New("unsupported desktop environment") } func setGnomeProxy(port uint16, proxyTypes ...string) error { @@ -78,3 +101,13 @@ func setGnomeProxy(port uint16, proxyTypes ...string) error { } return nil } + +func setKDEProxy(port uint16, proxyTypes ...string) error { + for _, proxyType := range proxyTypes { + err := runAsUser("kwriteconfig5", "--file","kioslaverc", "--group", "'Proxy Settings'", "--key", proxyType+"Proxy", proxyType+"://127.0.0.1:"+F.ToString(port)) + if err != nil { + return err + } + } + return nil +}