-
Notifications
You must be signed in to change notification settings - Fork 13
/
addr.go
467 lines (421 loc) · 9.19 KB
/
addr.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"fmt"
"net"
"strconv"
"strings"
"time"
"gvisor.dev/gvisor/pkg/tcpip"
)
/* SSH supports this syntax:
-R ... connections to given TCP port ... on the remote host are to be forwarded to the local side...
-L ... on the local host are to be forwarded to the given port... on the remote side...
-L [bind_address:]port:host:hostport
-R [bind_address:]port:host:hostport
-R [bind_address:]port
-L [bind_address:]port:remote_socket
-L local_socket:remote_socket
-L local_socket:host:hostport
-R [bind_address:]port:local_socket
-R remote_socket:host:hostport
-R remote_socket:local_socket
*/
type FwdAddr struct {
network string
bind defAddress
host defAddress
kaEnable bool
kaInterval time.Duration
proxyProtocol bool
}
type FwdAddrSlice []FwdAddr
func (f *FwdAddrSlice) String() string {
s := make([]string, 0, 10)
for _, fa := range *f {
x := fmt.Sprintf("%s://%s-%s",
fa.network,
fa.bind.String(),
fa.host.String())
s = append(s, x)
}
return strings.Join(s, " ")
}
func (f *FwdAddrSlice) Set(value string) error {
var (
bindPort, bindIP string
network string
rest string
hostIP, hostPort string
)
p := strings.SplitN(value, "://", 2)
switch len(p) {
case 2:
network = p[0]
rest = p[1]
case 1:
network = "tcp"
rest = p[0]
}
var fwa FwdAddr
switch network {
case "udprpc":
fwa.network = "udp"
fwa.kaEnable = true
fwa.kaInterval = 0
case "udpspp":
fwa.network = "udp"
fwa.proxyProtocol = true
case "tcppp":
fwa.network = "tcp"
fwa.proxyProtocol = true
case "tcp", "udp":
fwa.network = network
default:
return fmt.Errorf("unknown network type %q", network)
}
p = SplitHostPort(rest)
switch len(p) {
case 1:
bindPort = p[0]
case 2:
bindIP = p[0]
bindPort = p[1]
case 3:
bindPort = p[0]
hostIP = p[1]
hostPort = p[2]
case 4:
bindIP = p[0]
bindPort = p[1]
hostIP = p[2]
hostPort = p[3]
}
if bindIP != "" || bindPort != "" {
bind, err := ParseDefAddress(bindIP, bindPort)
if err != nil {
return err
}
fwa.bind = *bind
}
if bindPort != "" && hostPort == "" {
// in case only bindPort is set, and not hostPort, set the default:
hostPort = bindPort
}
if hostIP != "" || hostPort != "" {
host, err := ParseDefAddress(hostIP, hostPort)
if err != nil {
return err
}
fwa.host = *host
}
*f = append(*f, fwa)
return nil
}
// SetDefaultAddrs populates any unset endpoint with the provided default value.
// IPv6 values are only used if the one of the existing addresses is IPv6.
func (f *FwdAddrSlice) SetDefaultAddrs(bindAddrDef net.IP, bindAddr6Def net.IP, hostAddrDef net.IP, hostAddr6Def net.IP) {
for i := range *f {
fa := &(*f)[i]
if (fa.bind.static.Addr != "" && fa.bind.static.Addr.To4() == "") ||
(fa.host.static.Addr != "" && fa.host.static.Addr.To4() == "") {
// Bind and/or host is IPv6
fa.bind.SetDefaultAddr(bindAddr6Def)
fa.host.SetDefaultAddr(hostAddr6Def)
} else {
// Neither address is IPv6
fa.bind.SetDefaultAddr(bindAddrDef)
fa.host.SetDefaultAddr(hostAddrDef)
}
}
}
func (f *FwdAddr) BindAddr() (net.Addr, error) {
switch f.network {
case "tcp":
x := f.bind.GetTCPAddr()
if x == nil {
return nil, fmt.Errorf("dns lookup error of tcp addr: %w", f.bind.error)
}
return x, nil
case "udp":
x := f.bind.GetUDPAddr()
if x == nil {
return nil, fmt.Errorf("dns lookup error of udp addr: %w", f.bind.error)
}
return x, nil
}
return nil, fmt.Errorf("unknown network type: %v", f.network)
}
func (f *FwdAddr) HostAddr() (net.Addr, error) {
switch f.network {
case "tcp":
x := f.host.GetTCPAddr()
if x == nil {
return nil, fmt.Errorf("dns lookup error of tcp addr: %w", f.host.error)
}
return x, nil
case "udp":
x := f.host.GetUDPAddr()
if x == nil {
return nil, fmt.Errorf("dns lookup error of udp addr: %w", f.host.error)
}
return x, nil
}
return nil, fmt.Errorf("unknown network type: %v", f.network)
}
func FullAddressFromAddr(a net.Addr) *tcpip.FullAddress {
switch v := a.(type) {
case *net.TCPAddr:
return &tcpip.FullAddress{
Addr: tcpip.Address(v.IP),
Port: uint16(v.Port),
}
case *net.UDPAddr:
return &tcpip.FullAddress{
Addr: tcpip.Address(v.IP),
Port: uint16(v.Port),
}
}
return nil
}
func netAddrIP(a net.Addr) net.IP {
switch v := a.(type) {
case *net.TCPAddr:
return v.IP
case *net.UDPAddr:
return v.IP
}
return nil
}
func netAddrPort(a net.Addr) int {
switch v := a.(type) {
case *net.TCPAddr:
return v.Port
case *net.UDPAddr:
return v.Port
}
return 0
}
func netAddrSetPort(a net.Addr, port int) net.Addr {
switch v := a.(type) {
case *net.TCPAddr:
x := *v
x.Port = port
return &x
case *net.UDPAddr:
x := *v
x.Port = port
return &x
}
return nil
}
// Addr that can be set from flag.Var. For example:
// flag.Var(&metricAddr, "m", "Metrics address")
type AddrFlags struct {
net.Addr
}
func (a *AddrFlags) String() string {
if a.Addr == nil {
return ""
}
return fmt.Sprintf("%s://%s", a.Network(), a.Addr)
}
func (a *AddrFlags) Set(value string) error {
if addr, err := AddrFromString(value); err != nil {
return err
} else {
a.Addr = addr
return nil
}
}
func AddrFromString(value string) (net.Addr, error) {
p := strings.SplitN(value, "://", 2)
if len(p) != 2 {
return nil, fmt.Errorf("Address must be in form net://address, where net is one of unix/tcp/udp")
}
var addr net.Addr
switch p[0] {
case "tcp":
if v, err := net.ResolveTCPAddr(p[0], p[1]); err != nil {
return nil, err
} else {
addr = v
}
case "udp":
if v, err := net.ResolveUDPAddr(p[0], p[1]); err != nil {
return nil, err
} else {
addr = v
}
case "unix":
if v, err := net.ResolveUnixAddr(p[0], p[1]); err != nil {
return nil, err
} else {
addr = v
}
default:
return nil, fmt.Errorf("Address must be in form net://address, where net is one of unix/tcp/udp")
}
return addr, nil
}
func SplitHostPort(buf string) []string {
sliceOfParts := make([]string, 0)
part := make([]byte, 0)
var parens int
for _, c := range []byte(buf) {
switch {
case c == '[':
if parens > 0 {
part = append(part, c)
}
parens++
case c == ']':
if parens > 1 {
part = append(part, c)
}
parens--
case parens == 0 && c == ':':
sliceOfParts = append(sliceOfParts, string(part))
part = make([]byte, 0)
default:
part = append(part, c)
}
}
sliceOfParts = append(sliceOfParts, string(part))
return sliceOfParts
}
type IPFlag struct {
ip net.IP
}
func (f *IPFlag) String() string {
return fmt.Sprintf("%s", f.ip)
}
func (f *IPFlag) Set(value string) error {
var err error
f.ip, _, err = netParseOrResolveIP(value)
if err != nil {
return err
}
return nil
}
type IPPortRange struct {
network string
ipRange *net.IPNet
portMin uint16
portMax uint16
}
type IPPortRangeSlice []IPPortRange
func (p *IPPortRange) String() string {
a := []string{
p.network,
"://",
}
if p.ipRange.IP.To4() == nil {
a = append(a, "[")
}
a = append(a, p.ipRange.String())
if p.ipRange.IP.To4() == nil {
a = append(a, "]")
}
if p.portMin != 0 || p.portMax != 0 {
a = append(a, ":", fmt.Sprintf("%d-%d", p.portMin, p.portMax))
}
return strings.Join(a, "")
}
func (f *IPPortRangeSlice) String() string {
var a []string
for _, p := range *f {
a = append(a, p.String())
}
return strings.Join(a, ",")
}
func (f *IPPortRangeSlice) Set(commaValue string) error {
for _, value := range strings.Split(commaValue, ",") {
network, rest := "", ""
p := strings.SplitN(value, "://", 2)
switch len(p) {
case 2:
network = p[0]
rest = p[1]
case 1:
network = "tcp"
rest = p[0]
}
var (
pMin, pMax uint64
err1, err2 error
)
p = SplitHostPort(rest)
if len(p) < 1 || len(p) > 2 {
return fmt.Errorf("error parsing ipportrange")
}
host := p[0]
if len(p) == 2 {
portRange := p[1]
pn := strings.SplitN(portRange, "-", 2)
switch len(pn) {
case 2:
pMin, err1 = strconv.ParseUint(pn[0], 10, 16)
pMax, err2 = strconv.ParseUint(pn[1], 10, 16)
case 1:
pMin, err1 = strconv.ParseUint(pn[0], 10, 16)
pMax, err2 = pMin, err1
}
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
if pMax < pMin {
return fmt.Errorf("min port must be smaller equal than max")
}
}
_, ipnet, err := net.ParseCIDR(host)
if err != nil {
ip, _, err := netParseOrResolveIP(host)
if err != nil {
return err
}
var mask net.IPMask
if ip.To4() == nil {
mask = net.CIDRMask(128, 128)
} else {
mask = net.CIDRMask(32, 32)
}
ipnet = &net.IPNet{
IP: ip,
Mask: mask,
}
}
pr := IPPortRange{
network: network,
ipRange: ipnet,
portMin: uint16(pMin),
portMax: uint16(pMax),
}
*f = append(*f, pr)
}
return nil
}
func (f *IPPortRangeSlice) Contains(addr net.Addr) bool {
addrNetwork := addr.Network()
addrPort := uint16(netAddrPort(addr))
addrIP := netAddrIP(addr)
for _, p := range *f {
if p.network != addrNetwork {
// wrong proto
continue
}
if p.portMin != 0 || p.portMax != 0 {
if addrPort < p.portMin || addrPort > p.portMax {
// port out of range if ports are in the selector
continue
}
}
if !p.ipRange.Contains(addrIP) {
continue
}
return true
}
return false
}