-
Notifications
You must be signed in to change notification settings - Fork 5
/
function.go
165 lines (125 loc) · 3.35 KB
/
function.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
package stun
import (
"errors"
"fmt"
"net"
"os"
"time"
)
const STUN_SERVER = "118.89.173.65"
const STUN_PORT = 3478
const READ_TIME_OUT = 10
var NatType = map[string]string{
"UDP_BLOCKED": "Firewall blocks UDP",
"PUBLIC_IP": "Your IP is public on the Internet",
"SYMMETRIC_UDP_FIREWALL": "Firewall that allows UDP out, and responses have to come back to the source of the request",
"FULL_CONE": "Full Cone Nat",
"SYMMETRIC": "Symmetric NAT",
"PORT_RESTRICT": "Port Restrict Cone NAT",
"ADDR_RESTRICT": "(Address) Restrict Cone NAT",
}
func connect(ip string, port int) *net.UDPConn {
client := net.UDPAddr{}
client.IP = net.ParseIP("0.0.0.0")
server := net.UDPAddr{}
if ip == "" {
server.IP = net.ParseIP(STUN_SERVER)
} else {
server.IP = net.ParseIP(ip)
}
if port == 0 {
server.Port = STUN_PORT
} else {
server.Port = port
}
conn, err := net.DialUDP("udp", &client, &server)
check(err)
return conn
}
func check(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func read(conn *net.UDPConn) []byte {
err := conn.SetReadDeadline(time.Now().Add(time.Duration(READ_TIME_OUT * time.Second)))
check(err)
data := make([]byte, 4096)
n, _ := conn.Read(data)
return data[:n]
}
func write(conn *net.UDPConn, b []byte) error {
if _, err := conn.Write(b); err != nil {
fmt.Println("Send Msg Failed: " + err.Error())
return errors.New("Send Data Error")
}
return nil
}
func TestI(conn *net.UDPConn) *Message {
fmt.Println("Running TestI: Check UDP Response")
msg := NewBindRequest()
err := write(conn, msg.ToBytes())
check(err)
var resp Message
resp.FromBytes(read(conn))
fmt.Println("- Local Address:", conn.LocalAddr().String())
fmt.Println("- Mapped Address:", resp.GetMappedAddress().String())
fmt.Println("- Changed Address:", resp.GetChangedAddress().String())
return &resp
}
func TestII(conn *net.UDPConn) *Message {
fmt.Println("Running TestII: Check Changing IP & Port")
msg := NewChangeRequest(true, true)
err := write(conn, msg.ToBytes())
check(err)
var resp Message
resp.FromBytes(read(conn))
fmt.Println("- Receive Changing Response")
return &resp
}
func TestIII(conn *net.UDPConn) *Message {
fmt.Println("Running TestIII: Check Connect Changed Address & Change Port")
msg := NewChangeRequest(false, true)
err := write(conn, msg.ToBytes())
check(err)
var resp Message
resp.FromBytes(read(conn))
fmt.Println("- Receive Changing Response")
return &resp
}
func RunCheck(ip string) string {
conn1 := connect(ip, 0)
resp := TestI(conn1)
caddr := resp.GetChangedAddress()
m1addr := resp.GetMappedAddress()
if resp.Len() == 0 {
return NatType["UDP_BLOCKED"]
}
if conn1.LocalAddr().String() == resp.GetMappedAddress().String() {
resp = TestII(conn1)
if resp.Len() == 0 {
return NatType["SYMMETRIC_UDP_FIREWALL"]
}
return NatType["PUBLIC_IP"]
}
resp = TestII(conn1)
if resp.Len() > 0 {
return NatType["FULL_CONE"]
}
conn2 := connect((net.IP)(caddr.Ip).String(), int(BytesToUint16(caddr.Port)))
resp = TestI(conn2)
if resp.Len() == 0 {
return NatType["UDP_BLOCKED"]
}
m2addr := resp.GetMappedAddress()
if m1addr.String() != m2addr.String() {
return NatType["SYMMETRIC"]
}
resp = TestIII(conn1)
if resp.Len() == 0 {
return NatType["PORT_RESTRICT"]
} else {
return NatType["ADDR_RESTRICT"]
}
}