Fix set KDE6 system proxy

This commit is contained in:
lgjint 2024-06-01 14:48:26 +08:00 committed by 世界
parent 4193df375f
commit 69dc87aa6d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -16,30 +16,40 @@ import (
) )
type LinuxSystemProxy struct { type LinuxSystemProxy struct {
hasGSettings bool hasGSettings bool
hasKWriteConfig5 bool kWriteConfigCmd string
sudoUser string sudoUser string
serverAddr M.Socksaddr serverAddr M.Socksaddr
supportSOCKS bool supportSOCKS bool
isEnabled bool isEnabled bool
} }
func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*LinuxSystemProxy, error) { func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*LinuxSystemProxy, error) {
hasGSettings := common.Error(exec.LookPath("gsettings")) == nil 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 var sudoUser string
if os.Getuid() == 0 { if os.Getuid() == 0 {
sudoUser = os.Getenv("SUDO_USER") sudoUser = os.Getenv("SUDO_USER")
} }
if !hasGSettings && !hasKWriteConfig5 { if !hasGSettings && kWriteConfigCmd == "" {
return nil, E.New("unsupported desktop environment") return nil, E.New("unsupported desktop environment")
} }
return &LinuxSystemProxy{ return &LinuxSystemProxy{
hasGSettings: hasGSettings, hasGSettings: hasGSettings,
hasKWriteConfig5: hasKWriteConfig5, kWriteConfigCmd: kWriteConfigCmd,
sudoUser: sudoUser, sudoUser: sudoUser,
serverAddr: serverAddr, serverAddr: serverAddr,
supportSOCKS: supportSOCKS, supportSOCKS: supportSOCKS,
}, nil }, nil
} }
@ -70,8 +80,8 @@ func (p *LinuxSystemProxy) Enable() error {
return err return err
} }
} }
if p.hasKWriteConfig5 { if p.kWriteConfigCmd != "" {
err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1") err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1")
if err != nil { if err != nil {
return err return err
} }
@ -83,7 +93,7 @@ func (p *LinuxSystemProxy) Enable() error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }
@ -103,8 +113,8 @@ func (p *LinuxSystemProxy) Disable() error {
return err return err
} }
} }
if p.hasKWriteConfig5 { if p.kWriteConfigCmd != "" {
err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0") err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0")
if err != nil { if err != nil {
return err return err
} }
@ -150,7 +160,7 @@ func (p *LinuxSystemProxy) setKDEProxy(proxyTypes ...string) error {
proxyUrl = "http://" + p.serverAddr.String() proxyUrl = "http://" + p.serverAddr.String()
} }
err := p.runAsUser( err := p.runAsUser(
"kwriteconfig5", p.kWriteConfigCmd,
"--file", "--file",
"kioslaverc", "kioslaverc",
"--group", "--group",