mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-26 12:41:45 +00:00
50 lines
993 B
Go
50 lines
993 B
Go
|
package proxy
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"golang.org/x/sys/windows/registry"
|
||
|
)
|
||
|
|
||
|
func ReadSystemProxy() (*Proxy, error) {
|
||
|
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.QUERY_VALUE)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer key.Close()
|
||
|
|
||
|
enabled, _, err := key.GetIntegerValue("proxyEnable")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
server, _, err := key.GetStringValue("proxyServer")
|
||
|
if err != nil && !errors.Is(err, registry.ErrNotExist) {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &Proxy{Enabled: enabled != 0, Server: server}, nil
|
||
|
}
|
||
|
|
||
|
func SetSystemProxy(state bool) error {
|
||
|
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Internet Settings`, registry.SET_VALUE)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer key.Close()
|
||
|
|
||
|
var value uint32
|
||
|
if state {
|
||
|
value = 1
|
||
|
} else {
|
||
|
value = 0
|
||
|
}
|
||
|
|
||
|
err = key.SetDWordValue("proxyEnable", value)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|