2022-07-14 12:30:57 +00:00
|
|
|
//go:build linux && !android
|
|
|
|
|
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
2022-07-15 10:22:09 +00:00
|
|
|
"os"
|
2022-07-14 12:30:57 +00:00
|
|
|
"os/exec"
|
2022-07-15 10:22:09 +00:00
|
|
|
"strings"
|
2022-07-14 12:30:57 +00:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-15 10:22:09 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-14 12:30:57 +00:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
)
|
|
|
|
|
2022-07-15 10:22:09 +00:00
|
|
|
var (
|
|
|
|
hasGSettings bool
|
|
|
|
sudoUser string
|
|
|
|
)
|
2022-07-14 12:30:57 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
hasGSettings = common.Error(exec.LookPath("gsettings")) == nil
|
2022-07-15 10:22:09 +00:00
|
|
|
if os.Getuid() == 0 {
|
|
|
|
sudoUser = os.Getenv("SUDO_USER")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAsUser(name string, args ...string) error {
|
|
|
|
if os.Getuid() != 0 {
|
|
|
|
return runCommand(name, args...)
|
|
|
|
} else if sudoUser != "" {
|
|
|
|
return runCommand("su", "-", sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " ")))
|
|
|
|
} else {
|
|
|
|
return E.New("set system proxy: unable to set as root")
|
|
|
|
}
|
2022-07-14 12:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ClearSystemProxy() error {
|
|
|
|
if hasGSettings {
|
2022-07-15 10:22:09 +00:00
|
|
|
return runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "none")
|
2022-07-14 12:30:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSystemProxy(port uint16, mixed bool) error {
|
|
|
|
if hasGSettings {
|
2022-07-15 10:22:09 +00:00
|
|
|
err := runAsUser("gsettings", "set", "org.gnome.system.proxy.http", "enabled", "true")
|
2022-07-14 12:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mixed {
|
|
|
|
err = setGnomeProxy(port, "ftp", "http", "https", "socks")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = setGnomeProxy(port, "http", "https")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-15 10:22:09 +00:00
|
|
|
err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "use-same-proxy", F.ToString(mixed))
|
2022-07-14 12:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-15 10:22:09 +00:00
|
|
|
err = runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "manual")
|
2022-07-14 12:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("set system proxy: unsupported desktop environment")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setGnomeProxy(port uint16, proxyTypes ...string) error {
|
|
|
|
for _, proxyType := range proxyTypes {
|
2022-07-15 10:22:09 +00:00
|
|
|
err := runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "host", "127.0.0.1")
|
2022-07-14 12:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-15 10:22:09 +00:00
|
|
|
err = runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "port", F.ToString(port))
|
2022-07-14 12:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|