sing-box/common/settings/proxy_linux.go

167 lines
4.3 KiB
Go
Raw Permalink Normal View History

//go:build linux && !android
package settings
import (
2023-09-03 06:29:37 +00:00
"context"
2022-07-15 10:22:09 +00:00
"os"
"os/exec"
2022-07-15 10:22:09 +00:00
"strings"
"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-09-03 06:29:37 +00:00
M "github.com/sagernet/sing/common/metadata"
2023-03-13 03:31:36 +00:00
"github.com/sagernet/sing/common/shell"
)
2023-09-03 06:29:37 +00:00
type LinuxSystemProxy struct {
hasGSettings bool
hasKWriteConfig5 bool
sudoUser string
serverAddr M.Socksaddr
supportSOCKS bool
isEnabled bool
}
2023-09-03 06:29:37 +00:00
func NewSystemProxy(ctx context.Context, serverAddr M.Socksaddr, supportSOCKS bool) (*LinuxSystemProxy, error) {
hasGSettings := common.Error(exec.LookPath("gsettings")) == nil
hasKWriteConfig5 := common.Error(exec.LookPath("kwriteconfig5")) == nil
var sudoUser string
2022-07-15 10:22:09 +00:00
if os.Getuid() == 0 {
sudoUser = os.Getenv("SUDO_USER")
}
2023-09-03 06:29:37 +00:00
if !hasGSettings && !hasKWriteConfig5 {
return nil, E.New("unsupported desktop environment")
}
return &LinuxSystemProxy{
hasGSettings: hasGSettings,
hasKWriteConfig5: hasKWriteConfig5,
sudoUser: sudoUser,
serverAddr: serverAddr,
supportSOCKS: supportSOCKS,
}, nil
2022-07-15 10:22:09 +00:00
}
2023-09-03 06:29:37 +00:00
func (p *LinuxSystemProxy) IsEnabled() bool {
return p.isEnabled
}
2023-09-03 06:29:37 +00:00
func (p *LinuxSystemProxy) Enable() error {
if p.hasGSettings {
err := p.runAsUser("gsettings", "set", "org.gnome.system.proxy.http", "enabled", "true")
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-03 06:29:37 +00:00
if p.supportSOCKS {
err = p.setGnomeProxy("ftp", "http", "https", "socks")
} else {
2023-09-03 06:29:37 +00:00
err = p.setGnomeProxy("http", "https")
}
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-03 06:29:37 +00:00
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy", "use-same-proxy", F.ToString(p.supportSOCKS))
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-03 06:29:37 +00:00
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy", "mode", "manual")
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2022-08-04 14:01:20 +00:00
}
2023-09-03 06:29:37 +00:00
if p.hasKWriteConfig5 {
2023-09-30 08:44:58 +00:00
err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "1")
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-03 06:29:37 +00:00
if p.supportSOCKS {
err = p.setKDEProxy("ftp", "http", "https", "socks")
} else {
2023-09-03 06:29:37 +00:00
err = p.setKDEProxy("http", "https")
}
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-30 08:44:58 +00:00
err = p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "Authmode", "0")
if err != nil {
2023-09-03 06:29:37 +00:00
return err
}
2023-09-03 06:29:37 +00:00
err = p.runAsUser("dbus-send", "--type=signal", "/KIO/Scheduler", "org.kde.KIO.Scheduler.reparseSlaveConfiguration", "string:''")
if err != nil {
2023-09-03 06:29:37 +00: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
}
}
if p.hasKWriteConfig5 {
2023-09-30 08:44:58 +00:00
err := p.runAsUser("kwriteconfig5", "--file", "kioslaverc", "--group", "Proxy Settings", "--key", "ProxyType", "0")
2023-09-03 06:29:37 +00: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 14:01:20 +00:00
}
}
2023-09-03 06:29:37 +00:00
func (p *LinuxSystemProxy) setGnomeProxy(proxyTypes ...string) error {
for _, proxyType := range proxyTypes {
2023-09-03 06:29:37 +00:00
err := p.runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "host", p.serverAddr.AddrString())
if err != nil {
return err
}
2023-09-03 06:29:37 +00:00
err = p.runAsUser("gsettings", "set", "org.gnome.system.proxy."+proxyType, "port", F.ToString(p.serverAddr.Port))
if err != nil {
return err
}
}
return nil
}
2023-09-03 06:29:37 +00:00
func (p *LinuxSystemProxy) setKDEProxy(proxyTypes ...string) error {
for _, proxyType := range proxyTypes {
var proxyUrl string
if proxyType == "socks" {
2023-09-03 06:29:37 +00:00
proxyUrl = "socks://" + p.serverAddr.String()
} else {
2023-09-03 06:29:37 +00:00
proxyUrl = "http://" + p.serverAddr.String()
}
2023-09-03 06:29:37 +00:00
err := p.runAsUser(
"kwriteconfig5",
"--file",
"kioslaverc",
"--group",
2023-09-03 06:29:37 +00:00
"Proxy Settings",
"--key", proxyType+"Proxy",
proxyUrl,
)
if err != nil {
return err
}
}
return nil
}