-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
163 lines (140 loc) · 3.41 KB
/
utils.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 main
import (
"bytes"
"fmt"
"hash/crc32"
"time"
"net"
)
func iclock() uint32 {
return uint32((time.Now().UnixNano() / 1000000) & 0xffffffff)
}
// 这里优先级:外网IP、内网IP、loopback
func getOutboundIp() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
errl.Println(err, addrs)
}
// 查找公网IP
for _, addr := range addrs {
aip := addr.(*net.IPNet).IP
if !aip.IsLoopback() && !isReservedIp(aip) {
// 现在还是需要排除IPv6
if len(aip.String()) > len("255.255.255.255") {
} else {
return aip.String()
}
}
}
// 查找内网IP
for _, addr := range addrs {
aip := addr.(*net.IPNet).IP
if !aip.IsLoopback() && isReservedIp(aip) {
return aip.String()
}
}
// 查找loopback IP
for _, addr := range addrs {
aip := addr.(*net.IPNet).IP
if aip.IsLoopback() {
return aip.String()
}
}
return ""
}
// func init() { info.Println(getOutboundIp()) }
func ip2long(ip net.IP) uint32 {
return uint32(ip[0])<<24 | uint32(ip[1])<<16 | uint32(ip[2])<<8 | uint32(ip[3])
}
func isReservedIp(ip net.IP) bool {
// ip range
// 10.0.0.0--10.255.255.255
// 172.16.0.0--172.31.255.255
// 192.168.0.0--192.168.255.255
rips := []net.IP{
net.ParseIP("10.0.0.0"),
net.ParseIP("10.255.255.255"),
net.ParseIP("172.16.0.0"),
net.ParseIP("172.31.255.255"),
net.ParseIP("192.168.0.0"),
net.ParseIP("192.168.255.255"),
}
ipn := ip2long(ip)
for idx := 0; idx < len(rips); idx += 2 {
ipc0 := ip2long(rips[idx])
ipc1 := ip2long(rips[idx+1])
if ipn >= ipc0 && ipn <= ipc1 {
return true
}
}
return false
}
func isReservedIpStr(ip string) bool {
return isReservedIp(net.ParseIP(ip))
}
// a tool function
func makeKcpConv(friendId string, remip string, remport string) uint32 {
// crc32: toxid+host+port+time
data := fmt.Sprintf("%s@%s:%s@%d", friendId, remip, remport, time.Now().UnixNano())
conv := crc32.ChecksumIEEE(bytes.NewBufferString(data).Bytes())
return conv
}
func ListenUdpPortAuto(bport int, host string) (net.PacketConn, error) {
var pc net.PacketConn
var err error
for port := bport; port < 65536; port++ {
pc, err = net.ListenPacket("udp", fmt.Sprintf("%s:%d", host, port))
if err == nil {
return pc, nil
}
}
return nil, err
}
func kcp_reset_by_newest_rcvbuf(kcp *KCP) *KCP {
debug.Println(len(kcp.rcv_buf), len(kcp.rcv_queue))
kcpnxt := kcp.rcv_nxt
segsn := kcp.rcv_buf[len(kcp.rcv_buf)-1].sn
if kcpnxt == 0 && segsn > 0 {
kcp.rcv_nxt = segsn
} else if kcpnxt > 0 && segsn == 0 {
kcp.rcv_nxt = segsn
}
return kcp
}
func kcp_reset_by_seg(kcp *KCP, seg *segment) *KCP {
debug.Println(len(kcp.rcv_buf), len(kcp.rcv_queue))
kcpnxt := kcp.rcv_nxt
segsn := seg.sn
if kcpnxt == 0 && segsn > 0 {
kcp.rcv_nxt = segsn
} else if kcpnxt > 0 && segsn == 0 {
kcp.rcv_nxt = segsn
kcp.rcv_buf = kcp.rcv_buf[:0]
kcp.rcv_queue = kcp.rcv_queue[:0]
}
return kcp
}
func kcp_parse_segment(buf []byte) *segment {
data := make([]byte, 32)
copy(data, buf)
var ts, sn, length, una, conv uint32
var wnd uint16
var cmd, frg uint8
data = ikcp_decode32u(data, &conv)
data = ikcp_decode8u(data, &cmd)
data = ikcp_decode8u(data, &frg)
data = ikcp_decode16u(data, &wnd)
data = ikcp_decode32u(data, &ts)
data = ikcp_decode32u(data, &sn)
data = ikcp_decode32u(data, &una)
data = ikcp_decode32u(data, &length)
seg := &segment{}
seg.conv = conv
seg.cmd = cmd
seg.frg = frg
seg.wnd = wnd
seg.ts = ts
seg.sn = sn
seg.una = una
return seg
}