From 69dc87aa6d27a24ace82d4d786f882544e093b27 Mon Sep 17 00:00:00 2001 From: lgjint <78463565+lgjint@users.noreply.github.com> Date: Sat, 1 Jun 2024 14:48:26 +0800 Subject: [PATCH] Fix set KDE6 system proxy --- common/settings/proxy_linux.go | 48 ++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/common/settings/proxy_linux.go b/common/settings/proxy_linux.go index 4211b2f8..84e27b12 100644 --- a/common/settings/proxy_linux.go +++ b/common/settings/proxy_linux.go @@ -16,30 +16,40 @@ import ( ) type LinuxSystemProxy struct { - hasGSettings bool - hasKWriteConfig5 bool - sudoUser string - serverAddr M.Socksaddr - supportSOCKS bool - isEnabled bool + hasGSettings bool + kWriteConfigCmd string + sudoUser string + serverAddr M.Socksaddr + supportSOCKS bool + isEnabled bool } func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*LinuxSystemProxy, error) { hasGSettings := common.Error(exec.LookPath("gsettings")) == nil - hasKWriteConfig5 := common.Error(exec.LookPath("kwriteconfig5")) == nil + kWriteConfigCmds := []string{ + "kwriteconfig5", + "kwriteconfig6", + } + var kWriteConfigCmd string + for _, cmd := range kWriteConfigCmds { + if common.Error(exec.LookPath(cmd)) == nil { + kWriteConfigCmd = cmd + break + } + } var sudoUser string if os.Getuid() == 0 { sudoUser = os.Getenv("SUDO_USER") } - if !hasGSettings && !hasKWriteConfig5 { + if !hasGSettings && kWriteConfigCmd == "" { return nil, E.New("unsupported desktop environment") } return &LinuxSystemProxy{ - hasGSettings: hasGSettings, - hasKWriteConfig5: hasKWriteConfig5, - sudoUser: sudoUser, - serverAddr: serverAddr, - supportSOCKS: supportSOCKS, + hasGSettings: hasGSettings, + kWriteConfigCmd: kWriteConfigCmd, + sudoUser: sudoUser, + serverAddr: serverAddr, + supportSOCKS: supportSOCKS, }, nil } @@ -70,8 +80,8 @@ func (p *LinuxSystemProxy) Enable() error { return err } } - if p.hasKWriteConfig5 { - err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1") + if p.kWriteConfigCmd != "" { + err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1") if err != nil { return err } @@ -83,7 +93,7 @@ func (p *LinuxSystemProxy) Enable() error { if err != nil { return err } - err = p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "Authmode", "0") + err = p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "Authmode", "0") if err != nil { return err } @@ -103,8 +113,8 @@ func (p *LinuxSystemProxy) Disable() error { return err } } - if p.hasKWriteConfig5 { - err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0") + if p.kWriteConfigCmd != "" { + err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0") if err != nil { return err } @@ -150,7 +160,7 @@ func (p *LinuxSystemProxy) setKDEProxy(proxyTypes ...string) error { proxyUrl = "http://" + p.serverAddr.String() } err := p.runAsUser( - "kwriteconfig5", + p.kWriteConfigCmd, "--file", "kioslaverc", "--group",