-
Notifications
You must be signed in to change notification settings - Fork 13
/
nat.go
163 lines (135 loc) · 2.97 KB
/
nat.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
package mc
import (
"errors"
"fmt"
multiaddr "github.com/multiformats/go-multiaddr"
"io/ioutil"
"log"
"net"
"net/http"
"regexp"
"strings"
)
var (
BadAddressSpec = errors.New("Bad NAT address specification")
)
type NATConfig struct {
Opt int
spec string // address spec when option = NATConfigManual
addr multiaddr.Multiaddr // public address when option = NATConfigManual
}
const (
NATConfigNone = iota
NATConfigAuto
NATConfigManual
)
var natConfigString = []string{"none", "auto", "manual"}
func (cfg *NATConfig) String() string {
switch cfg.Opt {
case NATConfigManual:
return cfg.spec
default:
return natConfigString[cfg.Opt]
}
}
func (cfg *NATConfig) PublicAddr(base multiaddr.Multiaddr) (multiaddr.Multiaddr, error) {
if cfg.Opt != NATConfigManual {
return nil, BadAddressSpec
}
if cfg.addr != nil {
return cfg.addr, nil
}
ix := strings.LastIndex(cfg.spec, ":")
switch {
case ix < 0:
addr, err := GetPublicIP()
if err != nil {
return nil, err
}
port, err := base.ValueForProtocol(multiaddr.P_TCP)
if err != nil {
return nil, err
}
return cfg.makePublicAddr(addr, port)
case cfg.spec[:ix] == "*":
addr, err := GetPublicIP()
if err != nil {
return nil, err
}
port := cfg.spec[ix+1:]
return cfg.makePublicAddr(addr, port)
default:
addr, port := cfg.spec[:ix], cfg.spec[ix+1:]
return cfg.makePublicAddr(addr, port)
}
}
func (cfg *NATConfig) makePublicAddr(addr, port string) (multiaddr.Multiaddr, error) {
maddr, err := ParseAddress(fmt.Sprintf("/ip4/%s/tcp/%s", addr, port))
if err != nil {
return nil, err
}
cfg.addr = maddr
return maddr, nil
}
func (cfg *NATConfig) Clear() {
cfg.addr = nil
}
var natcfgrx *regexp.Regexp
func init() {
rx, err := regexp.Compile("([*])|([*]:[0-9]+)|([0-9]{1,3}([.][0-9]{1,3}){3}:[0-9]+)")
if err != nil {
log.Fatal(err)
}
natcfgrx = rx
}
func NATConfigFromString(str string) (cfg NATConfig, err error) {
switch str {
case "none":
cfg.Opt = NATConfigNone
return cfg, nil
case "auto":
cfg.Opt = NATConfigAuto
return cfg, nil
default:
if !natcfgrx.Match([]byte(str)) {
return cfg, BadAddressSpec
}
cfg.Opt = NATConfigManual
cfg.spec = str
return cfg, nil
}
}
var ifconfigSvc = []string{
"http://ifconfig.mediachain.io/ip",
"http://ifconfig.co/ip"}
var IfconfigError = errors.New("ifconfig service error")
func GetPublicIP() (string, error) {
for _, url := range ifconfigSvc {
ip, err := doGetPublicIP(url)
if err != nil {
log.Printf("Error getting IP from %s: %s", url, err.Error())
continue
}
return ip, nil
}
return "", IfconfigError
}
func doGetPublicIP(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return "", IfconfigError
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
ipstr := strings.TrimSpace(string(body))
if net.ParseIP(ipstr) == nil {
return "", IfconfigError
}
return ipstr, nil
}