sing-box/common/settings/proxy_linux.go

81 lines
2 KiB
Go
Raw Permalink Normal View History

//go:build linux && !android
package settings
import (
2022-07-15 10:22:09 +00:00
"os"
"os/exec"
2022-07-15 10:22:09 +00:00
"strings"
2022-08-04 14:01:20 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/common"
2022-07-15 10:22:09 +00:00
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
2023-03-13 03:31:36 +00:00
"github.com/sagernet/sing/common/shell"
)
2022-07-15 10:22:09 +00:00
var (
hasGSettings bool
sudoUser string
)
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 {
2023-03-13 03:31:36 +00:00
return shell.Exec(name, args...).Attach().Run()
2022-07-15 10:22:09 +00:00
} else if sudoUser != "" {
2023-03-13 03:31:36 +00:00
return shell.Exec("su", "-", sudoUser, "-c", F.ToString(name, " ", strings.Join(args, " "))).Attach().Run()
2022-07-15 10:22:09 +00:00
} else {
return E.New("set system proxy: unable to set as root")
}
}
2022-08-04 14:01:20 +00:00
func SetSystemProxy(router adapter.Router, port uint16, isMixed bool) (func() error, error) {
if !hasGSettings {
return nil, E.New("unsupported desktop environment")
}
2022-08-04 14:01:20 +00:00
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 {
2022-08-04 14:01:20 +00:00
err = setGnomeProxy(port, "http", "https")
}
2022-08-04 14:01:20 +00:00
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
}
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")
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))
if err != nil {
return err
}
}
return nil
}