-
Notifications
You must be signed in to change notification settings - Fork 0
/
winproxy.go
69 lines (55 loc) · 1.43 KB
/
winproxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package winproxy
import (
"github.com/fhluo/winproxy/settings"
"strings"
)
type Settings struct {
base *settings.DefaultConnectionSettings
Proxy bool
ProxyAddress string
Script bool
ScriptAddress string
AutoDetect bool
BypassList []string
}
// ReadSettings reads the settings from the registry.
func ReadSettings() (s Settings, err error) {
base, err := settings.Read()
if err != nil {
return
}
s = Settings{
base: base,
Proxy: base.Flags&settings.FlagProxy != 0,
Script: base.Flags&settings.FlagAutoProxyURL != 0,
AutoDetect: base.Flags&settings.FlagAutoDetect != 0,
ProxyAddress: base.ProxyAddress,
BypassList: strings.Split(strings.TrimSpace(base.BypassList), ";"),
ScriptAddress: base.ScriptAddress,
}
return
}
func (s Settings) setFlag(flag int32, v bool) {
if v {
s.base.Flags |= flag
} else {
s.base.Flags &^= flag
}
}
// Apply writes the settings to the registry.
func (s Settings) Apply() error {
if s.base == nil {
s.base = settings.New()
}
s.base.Version++
s.setFlag(settings.FlagProxy, s.Proxy)
s.setFlag(settings.FlagAutoProxyURL, s.Script)
s.setFlag(settings.FlagAutoDetect, s.AutoDetect)
s.base.ProxyAddress = s.ProxyAddress
for i := range s.BypassList {
s.BypassList[i] = strings.TrimSpace(s.BypassList[i])
}
s.base.BypassList = strings.Join(s.BypassList, ";")
s.base.ScriptAddress = s.ScriptAddress
return settings.Write(s.base)
}