-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
tun.go
158 lines (129 loc) · 3.74 KB
/
tun.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package libbox
import (
"net"
"net/netip"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)
type TunOptions interface {
GetInet4Address() RoutePrefixIterator
GetInet6Address() RoutePrefixIterator
GetDNSServerAddress() (string, error)
GetMTU() int32
GetAutoRoute() bool
GetStrictRoute() bool
GetInet4RouteAddress() RoutePrefixIterator
GetInet6RouteAddress() RoutePrefixIterator
GetInet4RouteExcludeAddress() RoutePrefixIterator
GetInet6RouteExcludeAddress() RoutePrefixIterator
GetInet4RouteRange() RoutePrefixIterator
GetInet6RouteRange() RoutePrefixIterator
GetIncludePackage() StringIterator
GetExcludePackage() StringIterator
IsHTTPProxyEnabled() bool
GetHTTPProxyServer() string
GetHTTPProxyServerPort() int32
}
type RoutePrefix struct {
address netip.Addr
prefix int
}
func (p *RoutePrefix) Address() string {
return p.address.String()
}
func (p *RoutePrefix) Prefix() int32 {
return int32(p.prefix)
}
func (p *RoutePrefix) Mask() string {
var bits int
if p.address.Is6() {
bits = 128
} else {
bits = 32
}
return net.IP(net.CIDRMask(p.prefix, bits)).String()
}
func (p *RoutePrefix) String() string {
return netip.PrefixFrom(p.address, p.prefix).String()
}
type RoutePrefixIterator interface {
Next() *RoutePrefix
HasNext() bool
}
func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
return &RoutePrefix{
address: prefix.Addr(),
prefix: prefix.Bits(),
}
}))
}
var _ TunOptions = (*tunOptions)(nil)
type tunOptions struct {
*tun.Options
routeRanges []netip.Prefix
option.TunPlatformOptions
}
func (o *tunOptions) GetInet4Address() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4Address)
}
func (o *tunOptions) GetInet6Address() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6Address)
}
func (o *tunOptions) GetDNSServerAddress() (string, error) {
if len(o.Inet4Address) == 0 || o.Inet4Address[0].Bits() == 32 {
return "", E.New("need one more IPv4 address for DNS hijacking")
}
return o.Inet4Address[0].Addr().Next().String(), nil
}
func (o *tunOptions) GetMTU() int32 {
return int32(o.MTU)
}
func (o *tunOptions) GetAutoRoute() bool {
return o.AutoRoute
}
func (o *tunOptions) GetStrictRoute() bool {
return o.StrictRoute
}
func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4RouteAddress)
}
func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6RouteAddress)
}
func (o *tunOptions) GetInet4RouteExcludeAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4RouteExcludeAddress)
}
func (o *tunOptions) GetInet6RouteExcludeAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6RouteExcludeAddress)
}
func (o *tunOptions) GetInet4RouteRange() RoutePrefixIterator {
return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
return it.Addr().Is4()
}))
}
func (o *tunOptions) GetInet6RouteRange() RoutePrefixIterator {
return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
return it.Addr().Is6()
}))
}
func (o *tunOptions) GetIncludePackage() StringIterator {
return newIterator(o.IncludePackage)
}
func (o *tunOptions) GetExcludePackage() StringIterator {
return newIterator(o.ExcludePackage)
}
func (o *tunOptions) IsHTTPProxyEnabled() bool {
if o.TunPlatformOptions.HTTPProxy == nil {
return false
}
return o.TunPlatformOptions.HTTPProxy.Enabled
}
func (o *tunOptions) GetHTTPProxyServer() string {
return o.TunPlatformOptions.HTTPProxy.Server
}
func (o *tunOptions) GetHTTPProxyServerPort() int32 {
return int32(o.TunPlatformOptions.HTTPProxy.ServerPort)
}