forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_router.go
66 lines (53 loc) · 1.26 KB
/
no_router.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nat
import (
"errors"
"net"
"time"
)
var (
errNoRouterCantMapPorts = errors.New("can't map ports without a known router")
errFetchingIP = errors.New("getting outbound IP failed")
_ Router = &noRouter{}
)
const googleDNSServer = "8.8.8.8:80"
type noRouter struct {
ip net.IP
ipErr error
}
func (noRouter) SupportsNAT() bool {
return false
}
func (noRouter) MapPort(_ string, intPort, extPort uint16, _ string, _ time.Duration) error {
return errNoRouterCantMapPorts
}
func (noRouter) UnmapPort(string, uint16, uint16) error {
return nil
}
func (r noRouter) ExternalIP() (net.IP, error) {
return r.ip, r.ipErr
}
func getOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", googleDNSServer)
if err != nil {
return nil, err
}
addr := conn.LocalAddr()
if err := conn.Close(); err != nil {
return nil, err
}
udpAddr, ok := addr.(*net.UDPAddr)
if !ok {
return nil, errFetchingIP
}
return udpAddr.IP, nil
}
// NewNoRouter returns a router that assumes the network is public
func NewNoRouter() Router {
ip, err := getOutboundIP()
return &noRouter{
ip: ip,
ipErr: err,
}
}