-
Notifications
You must be signed in to change notification settings - Fork 127
/
one.go
115 lines (90 loc) · 2.01 KB
/
one.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// date : 2016-05-13
// author: xjdrew
//
package kone
import (
"net"
"sync"
"github.com/op/go-logging"
"github.com/xjdrew/kone/tcpip"
)
var logger = logging.MustGetLogger("kone")
type One struct {
// tun ip
ip net.IP
// tun virtual network
subnet *net.IPNet
rule *Rule
dnsTable *DnsTable
proxies *Proxies
dns *Dns
tcpRelay *TCPRelay
udpRelay *UDPRelay
tun *TunDriver
manager *Manager
}
func (one *One) Serve() {
var wg sync.WaitGroup
runAndWait := func(f func() error) {
defer wg.Done()
err := f()
logger.Errorf("%v", err)
}
wg.Add(5)
go runAndWait(one.dnsTable.Serve)
go runAndWait(one.dns.Serve)
go runAndWait(one.tcpRelay.Serve)
go runAndWait(one.udpRelay.Serve)
go runAndWait(one.tun.Serve)
if one.manager != nil {
wg.Add(1)
go runAndWait(one.manager.Serve)
}
wg.Wait()
}
func (one *One) Reload(cfg *KoneConfig) error {
one.rule = NewRule(cfg.Rule)
one.dnsTable.ClearNonProxyDomain()
return nil
}
func FromConfig(cfg *KoneConfig) (*One, error) {
ip, subnet, _ := net.ParseCIDR(cfg.Core.Network)
logger.Infof("[tun] ip:%s, subnet: %s", ip, subnet)
one := &One{
ip: ip.To4(),
subnet: subnet,
}
// new rule
one.rule = NewRule(cfg.Rule)
// new dns cache
one.dnsTable = NewDnsTable(ip, subnet)
var err error
// new dns
if one.dns, err = NewDns(one, cfg.Core); err != nil {
return nil, err
}
if one.proxies, err = NewProxies(one, cfg.Proxy); err != nil {
return nil, err
}
one.tcpRelay = NewTCPRelay(one, cfg.Core)
one.udpRelay = NewUDPRelay(one, cfg.Core)
filters := map[tcpip.IPProtocol]PacketFilter{
tcpip.ICMP: PacketFilterFunc(icmpFilterFunc),
tcpip.TCP: one.tcpRelay,
tcpip.UDP: one.udpRelay,
}
if one.tun, err = NewTunDriver(ip, subnet, filters); err != nil {
return nil, err
}
// set tun as all IP-CIDR rule output
for _, pattern := range one.rule.patterns {
switch p := pattern.(type) {
case IPCIDRPattern:
one.tun.AddRoute(p.ipNet)
}
}
// new manager
one.manager = NewManager(one, cfg)
return one, nil
}