-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner.go
86 lines (75 loc) · 1.71 KB
/
runner.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"net"
"github.com/songgao/vpnroutesd/config"
"github.com/songgao/vpnroutesd/dns"
"github.com/songgao/vpnroutesd/sys"
"go.uber.org/zap"
)
type runResult struct {
config string
dns string
routes string
}
func dedupIPs(ips ...[]net.IP) []net.IP {
m := make(map[string]net.IP)
for _, l := range ips {
for _, ip := range l {
m[ip.String()] = ip
}
}
ret := make([]net.IP, 0, len(m))
for _, ip := range m {
ret = append(ret, ip)
}
return ret
}
func run(logger *zap.Logger) (result runResult) {
logger.Debug("+ run")
defer logger.Debug("- run")
cfg, cfgChanged, err := config.Load(logger, *fConfig)
if err != nil {
logger.Sugar().Errorf("loading config error: %v", err.Error())
result.config = "ERR"
return
}
if cfgChanged {
result.config = "CHANGE DETECTED"
} else {
result.config = "UNCHANGED"
}
logger.Sugar().Debugf("using config: %s", cfg)
domainIPs, dnsChanged, err := dns.GetIPs(logger, cfg.DNSServer, cfg.VPNDomains)
if err != nil {
logger.Sugar().Errorf("dns.GetIPs error: %v", err)
result.dns = "ERR"
return result
}
if dnsChanged {
result.dns = "CHANGED"
} else {
result.dns = "UNCHANGED"
}
logger.Sugar().Debugf("IPs from DNS: %s", dedupIPs(domainIPs))
args := sys.ApplyRoutesArgs{
VPNIPs: dedupIPs(cfg.VPNIPs, domainIPs),
}
if len(*fPrimaryIfce) > 0 && len(*fVPNIfce) > 0 {
args.Interfaces = &sys.InterfaceNames{
Primary: *fPrimaryIfce,
VPN: *fVPNIfce,
}
}
routesChanged, err := sys.ApplyRoutes(logger, args)
if err != nil {
logger.Sugar().Errorf("ApplyRoutes error: %v", err)
result.routes = "ERR"
return result
}
if routesChanged {
result.routes = "CHANGED"
} else {
result.routes = "UNCHANGED"
}
return result
}