/
config.go
292 lines (252 loc) · 7.13 KB
/
config.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2020 RethinkDNS and its authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package settings
import (
"errors"
"net"
"net/netip"
"net/url"
"strconv"
"strings"
"github.com/celzero/firestack/intra/log"
"golang.org/x/net/proxy"
)
var (
errDnsOptArg = errors.New("dnsopt: invalid arg")
)
// TODO: These modes could be covered by bit-flags instead.
// DNSModeNone does not redirect DNS queries sent to the tunnel.
const DNSModeNone = 0
// DNSModeIP redirects DNS requests sent to the IP endpoint set by VPN.
const DNSModeIP int = 1
// DNSModePort redirects all DNS requests on port 53.
const DNSModePort int = 2
// BlockModeNone filters no packet.
const BlockModeNone int = 0
// BlockModeFilter filters packets on connection establishment.
const BlockModeFilter int = 1
// BlockModeSink blackholes all packets.
const BlockModeSink int = 2
// BlockModeFilterProc determines owner-uid of a tcp/udp connection
// from procfs before filtering
const BlockModeFilterProc int = 3
// PtModeAuto does not enforce (but may still use) 6to4 protocol translation.
const PtModeAuto int = 0
// PtModeForce64 enforces 6to4 protocol translation.
const PtModeForce64 int = 1
// Android implements 464Xlat out-of-the-box, so this zero userspace impl
const PtModeNo46 int = 2
// msb to lsb: ipv6, ipv4, lwip(1) or netstack(0)
const Ns4 = 0b010 // 2
const Ns46 = 0b110 // 6
const Ns6 = 0b100 // 4
const IP4 = "4"
const IP46 = "46"
const IP6 = "6"
const NICID = 0x01
var Debug bool = false
func L3(engine int) string {
switch engine {
case Ns46:
return IP46
case Ns6:
return IP6
default:
return IP4
}
}
// TunMode specifies dns, firewall, xlat, and ip modes
type TunMode struct {
// DNSMode specifies the kind of DNS traffic to be trapped and routed to DoH servers
DNSMode int
// BlockMode instructs change in firewall behaviour.
BlockMode int
// PtMode determines 6to4 translation heuristics.
PtMode int
}
// SetMode re-assigns d to DNSMode, b to BlockMode, pt to NatPtMode.
func (t *TunMode) SetMode(d, b, pt int) {
t.DNSMode = d
t.BlockMode = b
t.PtMode = pt
}
// NewTunMode returns a new TunMode object.
// `d` sets dns-mode.
// `b` sets block-mode.
// `pt` sets natpt-mode.
func NewTunMode(d, b, pt int) *TunMode {
return &TunMode{
DNSMode: d,
BlockMode: b,
PtMode: pt,
}
}
// DefaultTunMode returns a new default TunMode with
// IP-only DNS capture and replay (not all DNS traffic but
// only the DNS traffic sent to [tcp/udp]handler.fakedns
// is captured and replayed to the remote DoH server)
// and with firewall disabled.
func DefaultTunMode() *TunMode {
return &TunMode{
DNSMode: DNSModeIP,
BlockMode: BlockModeNone,
PtMode: PtModeNo46,
}
}
// DNSOptions define https or socks5 proxy options
type DNSOptions struct {
ipp string
hostport string
hostips string
}
func (d *DNSOptions) String() string {
return d.AddrPort()
}
func (d *DNSOptions) AddrPort() string {
if len(d.ipp) > 0 {
return d.ipp
}
if len(d.hostport) > 0 {
return d.hostport
}
return ""
}
func (d *DNSOptions) ResolvedAddrs() string {
return d.hostips // TODO: may be ip:port
}
// Parse ip and port; where ip can be either ip:port or ip
func addrport(ip string, port string) (ipp netip.AddrPort, err error) {
var ipaddr netip.Addr
var p int
if ipaddr, err = netip.ParseAddr(ip); err == nil {
if p, err = strconv.Atoi(port); err == nil {
ipp = netip.AddrPortFrom(ipaddr.Unmap(), uint16(p))
return ipp, nil
}
} else if ipp, err = netip.ParseAddrPort(ip); err == nil {
return ipp, nil
}
return ipp, err
}
// NewDNSOptions returns a new DNSOpitons object.
func NewDNSOptions(ipport string) (*DNSOptions, error) {
var ipp netip.AddrPort
var err error
ip, port, err := net.SplitHostPort(ipport)
if err != nil {
return nil, err
}
if ipp, err = addrport(ip, port); err == nil {
return &DNSOptions{
ipp: ipp.String(),
}, nil
}
log.D("dnsopt(%s:%s); err(%v)", ip, port, err)
return nil, err
}
func NewDNSOptionsFromNetIp(ipp netip.AddrPort) (*DNSOptions, error) {
if !ipp.IsValid() {
return nil, errors.New("dnsopt: empty ipport")
}
return &DNSOptions{
ipp: ipp.String(),
}, nil
}
func NewDNSOptionsFromHostname(hostname, ipcsv string) (*DNSOptions, error) {
if len(hostname) <= 0 {
return nil, errDnsOptArg
}
domain, port, _ := net.SplitHostPort(hostname)
if len(domain) <= 0 {
domain = hostname
}
if len(port) == 0 {
port = "53"
}
return &DNSOptions{
hostport: net.JoinHostPort(domain, port),
hostips: ipcsv, // may be empty, and may be ip:port
}, nil
}
// ProxyOptions define https or socks5 proxy options
type ProxyOptions struct {
Auth *proxy.Auth
IP string // just the ip
Host string // just the hostname (no port)
Port string // just the port number
IPPort string // may be a url or ip:port
Scheme string // http, https, socks5, pip
Addrs []string // list of ips if ipport is a url; may be nil
}
// NewAuthProxyOptions returns a new ProxyOptions object with authentication object.
func NewAuthProxyOptions(scheme, username, password, ip, port string, addrs []string) *ProxyOptions {
var ippstr string
var ipstr string
var host string
ip = strings.TrimSuffix(ip, "/")
ipp, err := addrport(ip, port)
if err != nil {
log.I("proxyopt: scheme %s; ipport(%s:%s) is url?(%v)", scheme, ip, port, err)
if len(ip) > 0 {
// port is discarded, and expected to be in ip/url
ippstr = ip
host, port, _ = net.SplitHostPort(ip)
} else if len(port) > 0 {
// incoming ip,port is a wildcard address
ippstr = ":" + port
} else {
return nil
}
} else {
ippstr = ipp.String()
ipstr = ipp.Addr().String()
}
if len(username) <= 0 || len(password) <= 0 {
log.I("proxyopt: no user(%s) and/or pwd(%d)", username, len(password))
}
if len(scheme) <= 0 {
scheme = "http"
}
// todo: query unescape username and password?
auth := proxy.Auth{
User: username,
Password: password,
}
return &ProxyOptions{
Auth: &auth,
Host: host, // may be empty or hostname (without port)
IP: ipstr, // may be empty or ipaddr
Port: port, // port number
IPPort: ippstr, // mmay be ip4:port, [ip::6]:port, host:port, or :port
Scheme: scheme,
Addrs: addrs,
}
}
// NewProxyOptions returns a new ProxyOptions object.
func NewProxyOptions(ip string, port string) *ProxyOptions {
return NewAuthProxyOptions("" /*scheme*/, "" /*user*/, "" /*password*/, ip, port /*addrs*/, nil)
}
func (p *ProxyOptions) String() string {
return p.Auth.User + "," + p.Auth.Password + "," + p.IPPort
}
func (p *ProxyOptions) HasAuth() bool {
return len(p.Auth.User) > 0 && len(p.Auth.Password) > 0
}
func (p *ProxyOptions) FullUrl() string {
if p.HasAuth() {
// superuser.com/a/532530
usr := url.QueryEscape(p.Auth.User)
pwd := url.QueryEscape(p.Auth.Password)
return p.Scheme + "://" + usr + ":" + pwd + "@" + p.IPPort
} else if len(p.Auth.User) > 0 {
usr := url.QueryEscape(p.Auth.User)
return p.Scheme + "://" + usr + "@" + p.IPPort
}
return p.Url()
}
func (p *ProxyOptions) Url() string {
return p.Scheme + "://" + p.IPPort
}