2022-07-14 20:30:57 +08:00
|
|
|
//go:build linux && !android
|
|
|
|
|
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
2023-09-03 14:29:37 +08:00
|
|
|
"context"
|
2022-07-15 18:22:09 +08:00
|
|
|
"os"
|
2022-07-14 20:30:57 +08:00
|
|
|
"os/exec"
|
2022-07-15 18:22:09 +08:00
|
|
|
"strings"
|
2022-07-14 20:30:57 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-15 18:22:09 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-14 20:30:57 +08:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
2023-09-03 14:29:37 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2023-03-13 11:31:36 +08:00
|
|
|
"github.com/sagernet/sing/common/shell"
|
2022-07-14 20:30:57 +08:00
|
|
|
)
|
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
type LinuxSystemProxy struct {
|
2024-06-01 14:48:26 +08:00
|
|
|
hasGSettings bool
|
|
|
|
kWriteConfigCmd string
|
|
|
|
sudoUser string
|
|
|
|
serverAddr M.Socksaddr
|
|
|
|
supportSOCKS bool
|
|
|
|
isEnabled bool
|
2023-09-03 14:29:37 +08:00
|
|
|
}
|
2022-07-14 20:30:57 +08:00
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*LinuxSystemProxy, error) {
|
|
|
|
hasGSettings := common.Error(exec.LookPath("gsettings")) == nil
|
2024-06-01 14:48:26 +08:00
|
|
|
kWriteConfigCmds := []string{
|
|
|
|
"kwriteconfig5",
|
|
|
|
"kwriteconfig6",
|
|
|
|
}
|
|
|
|
var kWriteConfigCmd string
|
|
|
|
for _, cmd := range kWriteConfigCmds {
|
|
|
|
if common.Error(exec.LookPath(cmd)) == nil {
|
|
|
|
kWriteConfigCmd = cmd
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
var sudoUser string
|
2022-07-15 18:22:09 +08:00
|
|
|
if os.Getuid() == 0 {
|
|
|
|
sudoUser = os.Getenv("SUDO_USER")
|
|
|
|
}
|
2024-06-01 14:48:26 +08:00
|
|
|
if !hasGSettings && kWriteConfigCmd == "" {
|
2023-09-03 14:29:37 +08:00
|
|
|
return nil, E.New("unsupported desktop environment")
|
|
|
|
}
|
|
|
|
return &LinuxSystemProxy{
|
2024-06-01 14:48:26 +08:00
|
|
|
hasGSettings: hasGSettings,
|
|
|
|
kWriteConfigCmd: kWriteConfigCmd,
|
|
|
|
sudoUser: sudoUser,
|
|
|
|
serverAddr: serverAddr,
|
|
|
|
supportSOCKS: supportSOCKS,
|
2023-09-03 14:29:37 +08:00
|
|
|
}, nil
|
2022-07-15 18:22:09 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
func (p *LinuxSystemProxy) IsEnabled() bool {
|
|
|
|
return p.isEnabled
|
2022-07-14 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
func (p *LinuxSystemProxy) Enable() error {
|
|
|
|
if p.hasGSettings {
|
|
|
|
err := p.runAsUser("gsettings", "set", "org.gnome.system.proxy.http", "enabled", "true")
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
if p.supportSOCKS {
|
|
|
|
err = p.setGnomeProxy("ftp", "http", "https", "socks")
|
2023-08-31 22:44:23 +08:00
|
|
|
} else {
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.setGnomeProxy("http", "https")
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy", "use-same-proxy", F.ToString(p.supportSOCKS))
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "manual")
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2022-08-04 22:01:20 +08:00
|
|
|
}
|
2024-06-01 14:48:26 +08:00
|
|
|
if p.kWriteConfigCmd != "" {
|
|
|
|
err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1")
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
if p.supportSOCKS {
|
|
|
|
err = p.setKDEProxy("ftp", "http", "https", "socks")
|
2023-08-31 22:44:23 +08:00
|
|
|
} else {
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.setKDEProxy("http", "https")
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2024-06-01 14:48:26 +08:00
|
|
|
err = p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "Authmode", "0")
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.runAsUser("dbus-send", "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''")
|
2023-08-31 22:44:23 +08:00
|
|
|
if err != nil {
|
2023-09-03 14:29:37 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.isEnabled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LinuxSystemProxy) Disable() error {
|
|
|
|
if p.hasGSettings {
|
|
|
|
err := p.runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "none")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-06-01 14:48:26 +08:00
|
|
|
if p.kWriteConfigCmd != "" {
|
|
|
|
err := p.runAsUser(p.kWriteConfigCmd, "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0")
|
2023-09-03 14:29:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = p.runAsUser("dbus-send", "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.isEnabled = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LinuxSystemProxy) runAsUser(name string, args ...string) error {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
return shell.Exec(name, args...).Attach().Run()
|
|
|
|
} else if p.sudoUser != "" {
|
|
|
|
return shell.Exec("su", "-", p.sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " "))).Attach().Run()
|
|
|
|
} else {
|
|
|
|
return E.New("set system proxy: unable to set as root")
|
2022-08-04 22:01:20 +08:00
|
|
|
}
|
2022-07-14 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
func (p *LinuxSystemProxy) setGnomeProxy(proxyTypes ...string) error {
|
2022-07-14 20:30:57 +08:00
|
|
|
for _, proxyType := range proxyTypes {
|
2023-09-03 14:29:37 +08:00
|
|
|
err := p.runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "host", p.serverAddr.AddrString())
|
2022-07-14 20:30:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "port", F.ToString(p.serverAddr.Port))
|
2022-07-14 20:30:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-31 22:44:23 +08:00
|
|
|
|
2023-09-03 14:29:37 +08:00
|
|
|
func (p *LinuxSystemProxy) setKDEProxy(proxyTypes ...string) error {
|
2023-08-31 22:44:23 +08:00
|
|
|
for _, proxyType := range proxyTypes {
|
|
|
|
var proxyUrl string
|
|
|
|
if proxyType == "socks" {
|
2023-09-03 14:29:37 +08:00
|
|
|
proxyUrl = "socks://" + p.serverAddr.String()
|
2023-08-31 22:44:23 +08:00
|
|
|
} else {
|
2023-09-03 14:29:37 +08:00
|
|
|
proxyUrl = "http://" + p.serverAddr.String()
|
2023-08-31 22:44:23 +08:00
|
|
|
}
|
2023-09-03 14:29:37 +08:00
|
|
|
err := p.runAsUser(
|
2024-06-01 14:48:26 +08:00
|
|
|
p.kWriteConfigCmd,
|
2023-08-31 22:44:23 +08:00
|
|
|
"--file",
|
|
|
|
"kioslaverc",
|
|
|
|
"--group",
|
2023-09-03 14:29:37 +08:00
|
|
|
"Proxy Settings",
|
2023-08-31 22:44:23 +08:00
|
|
|
"--key", proxyType+"Proxy",
|
|
|
|
proxyUrl,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|