-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnat.go
238 lines (198 loc) · 4.61 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
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
package main
import (
"context"
"github.com/NebulousLabs/go-upnp"
"github.com/jackpal/gateway"
"github.com/jackpal/go-nat-pmp"
"log"
"net"
"time"
)
func upnpDiscover() (*upnp.IGD, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client, err := upnp.DiscoverCtx(ctx)
if err != nil {
log.Println(err)
return nil, err
}
return client, nil
}
func upnpExternalIP(client *upnp.IGD) (net.IP, error) {
ip, err := client.ExternalIP()
if err != nil {
log.Println(err)
return net.ParseIP("0.0.0.0"), err
}
log.Printf("external ip: %s\n", ip)
return net.ParseIP(ip), nil
}
func upnpOpen(client *upnp.IGD, port uint16) error {
err := client.Forward(port, "party-line")
if err != nil {
log.Println(err)
return err
}
log.Printf("port (0x%x) open\n", port)
return nil
}
func upnpClose(client *upnp.IGD, port uint16) error {
err := client.Clear(port)
if err != nil {
log.Println(err)
return err
}
log.Printf("port (0x%x) closed\n", port)
return nil
}
func pmpDiscover() (*natpmp.Client, error) {
gatewayIP, err := gateway.DiscoverGateway()
if err != nil {
log.Println(err)
return nil, err
}
client := natpmp.NewClient(gatewayIP)
return client, nil
}
func pmpExternalIP(client *natpmp.Client) (net.IP, error) {
response, err := client.GetExternalAddress()
if err != nil {
log.Println(err)
return net.ParseIP("0.0.0.0"), err
}
extBytes := response.ExternalIPAddress
return net.IPv4(extBytes[0], extBytes[1], extBytes[2], extBytes[3]), nil
}
func pmpOpen(client *natpmp.Client, port uint16) error {
_, err := client.AddPortMapping("udp", int(port), int(port), 30*24*60*60)
if err != nil {
log.Println(err)
return err
}
return nil
}
func pmpClose(client *natpmp.Client, port uint16) error {
_, err := client.AddPortMapping("udp", int(port), 0, 0)
if err != nil {
log.Println(err)
return err
}
return nil
}
func getOutboundIP() (net.IP, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Println(err)
return net.ParseIP("0.0.0.0"), err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}
func isPrivateIP(ip net.IP) bool {
var privateIPBlocks []*net.IPNet
privs := []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
}
for _, cidr := range privs {
_, block, _ := net.ParseCIDR(cidr)
privateIPBlocks = append(privateIPBlocks, block)
}
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return false
}
var closeUpnp bool = false
var closePmp bool = false
var openedPort uint16 = 0
var upnpClient *upnp.IGD
var pmpClient *natpmp.Client
func natCleanup() {
if closeUpnp {
upnpClose(upnpClient, openedPort)
} else if closePmp {
pmpClose(pmpClient, openedPort)
}
}
func tryNatPMP(extIP *net.IP, port uint16, successChan chan bool) {
log.Println("trying pmp...")
log.Println("untested!!! let me know if it works")
var err error
pmpClient, err = pmpDiscover()
if err != nil {
log.Println("error with pmp", err)
successChan <- false
}
*extIP, err = pmpExternalIP(pmpClient)
if err != nil {
log.Println("error with pmp", err)
successChan <- false
}
err = pmpOpen(pmpClient, port)
if err != nil {
log.Println("error with pmp", err)
successChan <- false
}
successChan <- true
}
func natStuff(port uint16) net.IP {
log.SetFlags(log.LstdFlags | log.Lshortfile)
ip, err := getOutboundIP()
if err != nil {
log.Fatal("error getting outbound ip", err)
}
openedPort = port
var extIP net.IP = ip
var successChan chan bool
if isPrivateIP(ip) {
log.Println("private ip detected")
log.Println("trying upnp...")
upnpClient, err = upnpDiscover()
if err != nil {
log.Println("error with upnp (discover)", err)
goto pmp
}
extIP, err = upnpExternalIP(upnpClient)
if err != nil {
log.Println("error with upnp (ext ip)", err)
goto pmp
}
err = upnpOpen(upnpClient, port)
if err != nil {
log.Println("error with upnp (open)", err)
goto pmp
}
closeUpnp = true
goto natSuccess
pmp:
successChan = make(chan bool)
go tryNatPMP(&extIP, port, successChan)
select {
case result := <-successChan:
if result {
closePmp = true
goto natSuccess
} else {
goto natFail
}
case <-time.After(30 * time.Second):
log.Println("nat pmp timed out :(")
goto natFail
}
goto natSuccess
natFail:
log.Println("could not map port with upnp or pmp")
log.Fatal("goodbye")
natSuccess:
}
log.Println(extIP)
return extIP
}