2022-07-14 20:30:57 +08:00
|
|
|
//go:build linux && !android
|
|
|
|
|
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2022-08-04 22:01:20 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
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-03-13 11:31:36 +08:00
|
|
|
"github.com/sagernet/sing/common/shell"
|
2022-07-14 20:30:57 +08:00
|
|
|
)
|
|
|
|
|
2022-07-15 18:22:09 +08:00
|
|
|
var (
|
|
|
|
hasGSettings bool
|
2023-08-31 22:44:23 +08:00
|
|
|
isKDE5 bool
|
2022-07-15 18:22:09 +08:00
|
|
|
sudoUser string
|
|
|
|
)
|
2022-07-14 20:30:57 +08:00
|
|
|
|
|
|
|
func init() {
|
2023-08-31 22:44:23 +08:00
|
|
|
isKDE5 = common.Error(exec.LookPath("kwriteconfig5")) == nil
|
2022-07-14 20:30:57 +08:00
|
|
|
hasGSettings = common.Error(exec.LookPath("gsettings")) == nil
|
2022-07-15 18:22:09 +08:00
|
|
|
if os.Getuid() == 0 {
|
|
|
|
sudoUser = os.Getenv("SUDO_USER")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAsUser(name string, args ...string) error {
|
|
|
|
if os.Getuid() != 0 {
|
2023-03-13 11:31:36 +08:00
|
|
|
return shell.Exec(name, args...).Attach().Run()
|
2022-07-15 18:22:09 +08:00
|
|
|
} else if sudoUser != "" {
|
2023-03-13 11:31:36 +08:00
|
|
|
return shell.Exec("su", "-", sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " "))).Attach().Run()
|
2022-07-15 18:22:09 +08:00
|
|
|
} else {
|
|
|
|
return E.New("set system proxy: unable to set as root")
|
|
|
|
}
|
2022-07-14 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
2022-08-04 22:01:20 +08:00
|
|
|
func SetSystemProxy(router adapter.Router, port uint16, isMixed bool) (func() error, error) {
|
2023-08-31 22:44:23 +08:00
|
|
|
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
|
2022-08-04 22:01:20 +08:00
|
|
|
}
|
2023-08-31 22:44:23 +08:00
|
|
|
if isKDE5 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
err = runAsUser("dbus-send", "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return func() error {
|
|
|
|
err = runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "'Proxy Settings'", "--key", "ProxyType", "0")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return runAsUser("dbus-send", "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''")
|
|
|
|
}, nil
|
2022-08-04 22:01:20 +08:00
|
|
|
}
|
2023-08-31 22:44:23 +08:00
|
|
|
return nil, E.New("unsupported desktop environment")
|
2022-07-14 20:30:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func setGnomeProxy(port uint16, proxyTypes ...string) error {
|
|
|
|
for _, proxyType := range proxyTypes {
|
2022-07-15 18:22:09 +08:00
|
|
|
err := runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "host", "127.0.0.1")
|
2022-07-14 20:30:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-15 18:22:09 +08:00
|
|
|
err = runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "port", F.ToString(port))
|
2022-07-14 20:30:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-31 22:44:23 +08:00
|
|
|
|
|
|
|
func setKDEProxy(port uint16, proxyTypes ...string) error {
|
|
|
|
for _, proxyType := range proxyTypes {
|
|
|
|
var proxyUrl string
|
|
|
|
if proxyType == "socks" {
|
|
|
|
proxyUrl = "socks://127.0.0.1:" + F.ToString(port)
|
|
|
|
} else {
|
|
|
|
proxyUrl = "http://127.0.0.1:" + F.ToString(port)
|
|
|
|
}
|
|
|
|
err := runAsUser(
|
|
|
|
"kwriteconfig5",
|
|
|
|
"--file",
|
|
|
|
"kioslaverc",
|
|
|
|
"--group",
|
|
|
|
"'Proxy Settings'",
|
|
|
|
"--key", proxyType+"Proxy",
|
|
|
|
proxyUrl,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|