forked from openp2p-cn/openp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.go
306 lines (272 loc) · 7.9 KB
/
protocol.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"hash/crc64"
"math/big"
"net"
"time"
)
const OpenP2PVersion = "0.96.1"
const ProducnName string = "openp2p"
type openP2PHeader struct {
DataLen uint32
MainType uint16
SubType uint16
}
var openP2PHeaderSize = binary.Size(openP2PHeader{})
type PushHeader struct {
From uint64
To uint64
}
var PushHeaderSize = binary.Size(PushHeader{})
type overlayHeader struct {
id uint64
}
var overlayHeaderSize = binary.Size(overlayHeader{})
func decodeHeader(data []byte) (*openP2PHeader, error) {
head := openP2PHeader{}
rd := bytes.NewReader(data)
err := binary.Read(rd, binary.LittleEndian, &head)
if err != nil {
return nil, err
}
return &head, nil
}
func encodeHeader(mainType uint16, subType uint16, len uint32) []byte {
head := openP2PHeader{
len,
mainType,
subType,
}
headBuf := new(bytes.Buffer)
err := binary.Write(headBuf, binary.LittleEndian, head)
if err != nil {
return []byte("")
}
return headBuf.Bytes()
}
// Message type
const (
MsgLogin = 0
MsgHeartbeat = 1
MsgNATDetect = 2
MsgPush = 3
MsgP2P = 4
MsgRelay = 5
MsgReport = 6
)
const (
MsgPushRsp = 0
MsgPushConnectReq = 1
MsgPushConnectRsp = 2
MsgPushHandshakeStart = 3
MsgPushAddRelayTunnelReq = 4
MsgPushAddRelayTunnelRsp = 5
MsgPushUpdate = 6
MsgPushReportApps = 7
MsgPushQuicConnect = 8
)
// MsgP2P sub type message
const (
MsgPunchHandshake = iota
MsgPunchHandshakeAck
MsgTunnelHandshake
MsgTunnelHandshakeAck
MsgTunnelHeartbeat
MsgTunnelHeartbeatAck
MsgOverlayConnectReq
MsgOverlayConnectRsp
MsgOverlayDisconnectReq
MsgOverlayData
MsgRelayData
MsgRelayHeartbeat
MsgRelayHeartbeatAck
)
// MsgRelay sub type message
const (
MsgRelayNodeReq = iota
MsgRelayNodeRsp
)
// MsgReport sub type message
const (
MsgReportBasic = iota
MsgReportQuery
MsgReportConnect
)
const (
ReadBuffLen = 1024
NetworkHeartbeatTime = time.Second * 30 // TODO: server no response hb, save flow
TunnelHeartbeatTime = time.Second * 15
TunnelIdleTimeout = time.Minute
SymmetricHandshakeNum = 800 // 0.992379
// SymmetricHandshakeNum = 1000 // 0.999510
SymmetricHandshakeInterval = time.Millisecond
SymmetricHandshakeAckTimeout = time.Second * 11
PeerAddRelayTimeount = time.Second * 20
CheckActiveTimeout = time.Second * 5
PaddingSize = 16
AESKeySize = 16
MaxRetry = 10
RetryInterval = time.Second * 30
PublicIPEchoTimeout = time.Second * 3
NatTestTimeout = time.Second * 10
)
// error message
var (
// ErrorS2S string = "s2s is not supported"
// ErrorHandshake string = "handshake error"
ErrorS2S = errors.New("s2s is not supported")
ErrorHandshake = errors.New("handshake error")
)
// NATNone has public ip
const (
NATNone = 0
NATCone = 1
NATSymmetric = 2
)
func newMessage(mainType uint16, subType uint16, packet interface{}) ([]byte, error) {
data, err := json.Marshal(packet)
if err != nil {
return nil, err
}
// gLog.Println(LevelINFO,"write packet:", string(data))
head := openP2PHeader{
uint32(len(data)),
mainType,
subType,
}
headBuf := new(bytes.Buffer)
err = binary.Write(headBuf, binary.LittleEndian, head)
if err != nil {
return nil, err
}
writeBytes := append(headBuf.Bytes(), data...)
return writeBytes, nil
}
func nodeNameToID(name string) uint64 {
return crc64.Checksum([]byte(name), crc64.MakeTable(crc64.ISO))
}
type PushConnectReq struct {
From string `json:"from,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
Token uint64 `json:"token,omitempty"`
ConeNatPort int `json:"coneNatPort,omitempty"`
NatType int `json:"natType,omitempty"`
FromIP string `json:"fromIP,omitempty"`
ID uint64 `json:"id,omitempty"`
}
type PushConnectRsp struct {
Error int `json:"error,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Detail string `json:"detail,omitempty"`
NatType int `json:"natType,omitempty"`
ConeNatPort int `json:"coneNatPort,omitempty"`
FromIP string `json:"fromIP,omitempty"`
ID uint64 `json:"id,omitempty"`
}
type PushRsp struct {
Error int `json:"error,omitempty"`
Detail string `json:"detail,omitempty"`
}
type LoginRsp struct {
Error int `json:"error,omitempty"`
Detail string `json:"detail,omitempty"`
Ts uint64 `json:"ts,omitempty"`
}
type NatDetectReq struct {
SrcPort int `json:"srcPort,omitempty"`
EchoPort int `json:"echoPort,omitempty"`
}
type NatDetectRsp struct {
IP string `json:"IP,omitempty"`
Port int `json:"port,omitempty"`
IsPublicIP int `json:"isPublicIP,omitempty"`
}
type P2PHandshakeReq struct {
ID uint64 `json:"id,omitempty"`
}
type OverlayConnectReq struct {
ID uint64 `json:"id,omitempty"`
User string `json:"user,omitempty"`
Password string `json:"password,omitempty"`
DstIP string `json:"dstIP,omitempty"`
DstPort int `json:"dstPort,omitempty"`
Protocol string `json:"protocol,omitempty"`
RelayTunnelID uint64 `json:"relayTunnelID,omitempty"` // if not 0 relay
AppID uint64 `json:"appID,omitempty"`
}
type OverlayDisconnectReq struct {
ID uint64 `json:"id,omitempty"`
}
type TunnelMsg struct {
ID uint64 `json:"id,omitempty"`
}
type RelayNodeReq struct {
PeerNode string `json:"peerNode,omitempty"`
}
type RelayNodeRsp struct {
RelayName string `json:"relayName,omitempty"`
RelayToken uint64 `json:"relayToken,omitempty"`
}
type AddRelayTunnelReq struct {
From string `json:"from,omitempty"`
RelayName string `json:"relayName,omitempty"`
RelayToken uint64 `json:"relayToken,omitempty"`
AppID uint64 `json:"appID,omitempty"`
AppKey uint64 `json:"appKey,omitempty"`
}
type RelayHeartbeat struct {
RelayTunnelID uint64 `json:"relayTunnelID,omitempty"`
AppID uint64 `json:"appID,omitempty"`
}
type ReportBasic struct {
OS string `json:"os,omitempty"`
Mac string `json:"mac,omitempty"`
LanIP string `json:"lanIP,omitempty"`
IPv6 string `json:"IPv6,omitempty"`
Version string `json:"version,omitempty"`
NetInfo NetInfo `json:"netInfo,omitempty"`
}
type ReportConnect struct {
Error string `json:"error,omitempty"`
Protocol string `json:"protocol,omitempty"`
SrcPort int `json:"srcPort,omitempty"`
NatType int `json:"natType,omitempty"`
PeerNode string `json:"peerNode,omitempty"`
DstPort int `json:"dstPort,omitempty"`
DstHost string `json:"dsdtHost,omitempty"`
PeerUser string `json:"peerUser,omitempty"`
PeerNatType int `json:"peerNatType,omitempty"`
PeerIP string `json:"peerIP,omitempty"`
ShareBandwidth int `json:"shareBandWidth,omitempty"`
RelayNode string `json:"relayNode,omitempty"`
Version string `json:"version,omitempty"`
}
type UpdateInfo struct {
Error int `json:"error,omitempty"`
ErrorDetail string `json:"errorDetail,omitempty"`
Url string `json:"url,omitempty"`
}
type NetInfo struct {
IP net.IP `json:"ip"`
IPDecimal *big.Int `json:"ip_decimal"`
Country string `json:"country,omitempty"`
CountryISO string `json:"country_iso,omitempty"`
CountryEU *bool `json:"country_eu,omitempty"`
RegionName string `json:"region_name,omitempty"`
RegionCode string `json:"region_code,omitempty"`
MetroCode uint `json:"metro_code,omitempty"`
PostalCode string `json:"zip_code,omitempty"`
City string `json:"city,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Timezone string `json:"time_zone,omitempty"`
ASN string `json:"asn,omitempty"`
ASNOrg string `json:"asn_org,omitempty"`
Hostname string `json:"hostname,omitempty"`
}