(摘) 设置系统代理

声明:内容源自网络,版权归原作者所有。若有侵权请在网页聊天中联系我

Zen是一个过滤广告的软件,就是一个Web代理,所有请求经过它,它会判断是否为广告。

原本只是想汉化一下,它是使用的 Wails。发现它的设置系统代理的代码,位于 proxy 目录下:

# Windows设置代理

import (
	"fmt"
	"log"

	"golang.org/x/sys/windows"
	"golang.org/x/sys/windows/registry"
)

var (
	wininet                          = windows.NewLazySystemDLL("wininet.dll")
	internetSetOption                = wininet.NewProc("InternetSetOptionW")
	INTERNET_OPTION_SETTINGS_CHANGED = 39
	INTERNET_OPTION_REFRESH          = 37
)

func (p *Proxy) setSystemProxy() error {
	k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.ALL_ACCESS)
	if err != nil {
		return err
	}
	defer k.Close()

	if err := k.SetDWordValue("ProxyEnable", 1); err != nil {
		return err
	}

	if err := k.SetStringValue("ProxyServer", fmt.Sprintf("%s:%d", "127.0.0.1", p.port)); err != nil {
		if err := k.SetDWordValue("ProxyEnable", 0); err != nil {
			log.Printf("failed to disable proxy during error handling: %v", err)
		}
		return err
	}

	callInternetSetOption(INTERNET_OPTION_SETTINGS_CHANGED)
	callInternetSetOption(INTERNET_OPTION_REFRESH)

	return nil
}

func (p *Proxy) unsetSystemProxy() error {
	k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.ALL_ACCESS)
	if err != nil {
		return err
	}
	defer k.Close()

	if err := k.SetDWordValue("ProxyEnable", 0); err != nil {
		return err
	}

	callInternetSetOption(INTERNET_OPTION_SETTINGS_CHANGED)
	callInternetSetOption(INTERNET_OPTION_REFRESH)

	return nil
}

func callInternetSetOption(dwOption int) {
	ret, _, err := internetSetOption.Call(0, uintptr(dwOption), 0, 0)
	if ret == 0 {
		log.Printf("failed to call InternetSetOption with option %d: %v", dwOption, err)
	}
}
# Linux设置代理

import (
	"fmt"
	"os/exec"
	"strings"
)
func (p *Proxy) setSystemProxy() error {
	if binaryExists("gsettings") {
		commands := [][]string{
			[]string{"gsettings", "set", "org.gnome.system.proxy", "mode", "manual"},
			[]string{"gsettings", "set", "org.gnome.system.proxy.http", "host", "127.0.0.1"},
			[]string{"gsettings", "set", "org.gnome.system.proxy.http", "port", fmt.Sprint(p.port)},
			[]string{"gsettings", "set", "org.gnome.system.proxy.https", "host", "127.0.0.1"},
			[]string{"gsettings", "set", "org.gnome.system.proxy.https", "port", fmt.Sprint(p.port)},
		}

		for _, command := range commands {
			cmd := exec.Command(command[0], command[1:]...)
			out, err := cmd.CombinedOutput()
			if err != nil {
				return fmt.Errorf("%s: %v\n%s", strings.Join(command, " "), err, out)
			}
		}
		return nil
	}
	// TODO: add support for other desktop environments

	return fmt.Errorf("system proxy configuration is currently only supported on GNOME")
}

func (p *Proxy) unsetSystemProxy() error {
	command := []string{"gsettings", "set", "org.gnome.system.proxy", "mode", "none"}
	cmd := exec.Command(command[0], command[1:]...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return fmt.Errorf("%s: %v\n%s", strings.Join(command, " "), err, out)
	}

	return nil
}

func binaryExists(name string) bool {
	_, err := exec.LookPath(name)
	return err == nil
}

相关文章