-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.go
317 lines (248 loc) · 7.49 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
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"bytes"
"encoding/binary"
"fmt"
"net"
)
const (
PDU_LISTEN_REQUEST = 1
PDU_LISTEN_RESPONSE = 2
PDU_TUNNEL_CONNECT_REQUEST = 3
PDU_TUNNEL_CONNECT_RESPONSE = 4
PDU_TUNNEL_DATA_INDICATION = 5
PDU_TUNNEL_DISCONNECT_REQUEST = 6
PDU_TUNNEL_DISCONNECT_RESPONSE = 7
)
type Serializable interface {
GetSerialType() int
GetSerialLength() uint32
SerializeTo(w *bytes.Buffer)
SerializeFrom(r *bytes.Buffer)
}
func serializeUInt32To(v uint32, w *bytes.Buffer) {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, v)
w.Write(b)
}
func serializeUInt32From(r *bytes.Buffer) uint32 {
b := make([]byte, 4)
r.Read(b)
return binary.BigEndian.Uint32(b)
}
func getStringSerialLength(s string) uint32 {
return uint32(4 + len([]byte(s)))
}
func serializeStringTo(s string, w *bytes.Buffer) {
l := uint32(len([]byte(s)))
serializeUInt32To(l, w)
w.Write([]byte(s))
}
func serializeStringFrom(r *bytes.Buffer) string {
l := serializeUInt32From(r)
b := make([]byte, int(l))
r.Read(b)
return string(b)
}
func getPduSerialLength(pdu Serializable) uint32 {
return 1 + pdu.GetSerialLength()
}
func serializePduTo(pdu Serializable, w *bytes.Buffer) {
w.WriteByte(byte(pdu.GetSerialType()))
pdu.SerializeTo(w)
}
func serializePduFrom(r *bytes.Buffer) Serializable {
t, _ := r.ReadByte()
switch int(t) {
case PDU_LISTEN_REQUEST:
pdu := &ListenRequest{}
pdu.SerializeFrom(r)
return pdu
case PDU_LISTEN_RESPONSE:
pdu := &ListenResponse{}
pdu.SerializeFrom(r)
return pdu
case PDU_TUNNEL_CONNECT_REQUEST:
pdu := &TunnelConnectRequest{}
pdu.SerializeFrom(r)
return pdu
case PDU_TUNNEL_CONNECT_RESPONSE:
pdu := &TunnelConnectResponse{}
pdu.SerializeFrom(r)
return pdu
case PDU_TUNNEL_DATA_INDICATION:
pdu := &TunnelDataIndication{}
pdu.SerializeFrom(r)
return pdu
case PDU_TUNNEL_DISCONNECT_REQUEST:
pdu := &TunnelDisconnectRequest{}
pdu.SerializeFrom(r)
return pdu
case PDU_TUNNEL_DISCONNECT_RESPONSE:
pdu := &TunnelDisconnectResponse{}
pdu.SerializeFrom(r)
return pdu
}
fmt.Printf("Invalid protocol data\n")
return nil
}
func sendPdu(conn net.Conn, pdu Serializable) error {
l := getPduSerialLength(pdu)
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, l)
_, err := conn.Write(b)
if err != nil {
return err
}
buf := bytes.NewBuffer(nil)
serializePduTo(pdu, buf)
_, err = conn.Write(buf.Bytes())
return err
}
/////////////////////////////////////////////////////////////////////////////
type ListenRequest struct {
proxyAddress string
proxyPort int
}
func (pdu *ListenRequest) GetSerialType() int {
return PDU_LISTEN_REQUEST
}
func (pdu *ListenRequest) GetSerialLength() uint32 {
return 4 + getStringSerialLength(pdu.proxyAddress)
}
func (pdu *ListenRequest) SerializeTo(w *bytes.Buffer) {
serializeStringTo(pdu.proxyAddress, w)
serializeUInt32To(uint32(pdu.proxyPort), w)
}
func (pdu *ListenRequest) SerializeFrom(r *bytes.Buffer) {
pdu.proxyAddress = serializeStringFrom(r)
pdu.proxyPort = int(serializeUInt32From(r))
}
/////////////////////////////////////////////////////////////////////////////
type ListenResponse struct {
proxyAddress string
proxyPort int
tunnelAddress string
tunnelPort int
}
func (pdu *ListenResponse) GetSerialType() int {
return PDU_LISTEN_RESPONSE
}
func (pdu *ListenResponse) GetSerialLength() uint32 {
return 8 + getStringSerialLength(pdu.proxyAddress) + getStringSerialLength(pdu.tunnelAddress)
}
func (pdu *ListenResponse) SerializeTo(w *bytes.Buffer) {
serializeStringTo(pdu.proxyAddress, w)
serializeUInt32To(uint32(pdu.proxyPort), w)
serializeStringTo(pdu.tunnelAddress, w)
serializeUInt32To(uint32(pdu.tunnelPort), w)
}
func (pdu *ListenResponse) SerializeFrom(r *bytes.Buffer) {
pdu.proxyAddress = serializeStringFrom(r)
pdu.proxyPort = int(serializeUInt32From(r))
pdu.tunnelAddress = serializeStringFrom(r)
pdu.tunnelPort = int(serializeUInt32From(r))
}
/////////////////////////////////////////////////////////////////////////////
// listener -> proxy
type TunnelConnectRequest struct {
dataConnectionHandle uint32
clientAddress string
proxyAddress string
proxyPort int
}
func (pdu *TunnelConnectRequest) GetSerialType() int {
return PDU_TUNNEL_CONNECT_REQUEST
}
func (pdu *TunnelConnectRequest) GetSerialLength() uint32 {
return 4 +
getStringSerialLength(pdu.clientAddress) +
getStringSerialLength(pdu.proxyAddress) +
4
}
func (pdu *TunnelConnectRequest) SerializeTo(w *bytes.Buffer) {
serializeUInt32To(uint32(pdu.dataConnectionHandle), w)
serializeStringTo(pdu.clientAddress, w)
serializeStringTo(pdu.proxyAddress, w)
serializeUInt32To(uint32(pdu.proxyPort), w)
}
func (pdu *TunnelConnectRequest) SerializeFrom(r *bytes.Buffer) {
pdu.dataConnectionHandle = Handle(serializeUInt32From(r))
pdu.clientAddress = serializeStringFrom(r)
pdu.proxyAddress = serializeStringFrom(r)
pdu.proxyPort = int(serializeUInt32From(r))
}
/////////////////////////////////////////////////////////////////////////////
type TunnelConnectResponse struct {
dataConnectionHandle uint32
proxyConnectionHandle uint32
}
func (pdu *TunnelConnectResponse) GetSerialType() int {
return PDU_TUNNEL_CONNECT_RESPONSE
}
func (pdu *TunnelConnectResponse) GetSerialLength() uint32 {
return 8
}
func (pdu *TunnelConnectResponse) SerializeTo(w *bytes.Buffer) {
serializeUInt32To(uint32(pdu.dataConnectionHandle), w)
serializeUInt32To(uint32(pdu.proxyConnectionHandle), w)
}
func (pdu *TunnelConnectResponse) SerializeFrom(r *bytes.Buffer) {
pdu.dataConnectionHandle = serializeUInt32From(r)
pdu.proxyConnectionHandle = serializeUInt32From(r)
}
/////////////////////////////////////////////////////////////////////////////
type TunnelDataIndication struct {
peerConnectionHandle uint32
data []byte
}
func (pdu *TunnelDataIndication) GetSerialType() int {
return PDU_TUNNEL_DATA_INDICATION
}
func (pdu *TunnelDataIndication) GetSerialLength() uint32 {
return uint32(4 + 4 + len(pdu.data))
}
func (pdu *TunnelDataIndication) SerializeTo(w *bytes.Buffer) {
serializeUInt32To(uint32(pdu.peerConnectionHandle), w)
serializeUInt32To(uint32(len(pdu.data)), w)
w.Write(pdu.data)
}
func (pdu *TunnelDataIndication) SerializeFrom(r *bytes.Buffer) {
pdu.peerConnectionHandle = serializeUInt32From(r)
l := serializeUInt32From(r)
pdu.data = make([]byte, int(l))
r.Read(pdu.data)
}
/////////////////////////////////////////////////////////////////////////////
type TunnelDisconnectRequest struct {
peerConnectionHandle uint32
}
func (pdu *TunnelDisconnectRequest) GetSerialType() int {
return PDU_TUNNEL_DISCONNECT_REQUEST
}
func (pdu *TunnelDisconnectRequest) GetSerialLength() uint32 {
return 4
}
func (pdu *TunnelDisconnectRequest) SerializeTo(w *bytes.Buffer) {
serializeUInt32To(uint32(pdu.peerConnectionHandle), w)
}
func (pdu *TunnelDisconnectRequest) SerializeFrom(r *bytes.Buffer) {
pdu.peerConnectionHandle = serializeUInt32From(r)
}
/////////////////////////////////////////////////////////////////////////////
type TunnelDisconnectResponse struct {
peerConnectionHandle uint32
}
func (pdu *TunnelDisconnectResponse) GetSerialType() int {
return PDU_TUNNEL_DISCONNECT_RESPONSE
}
func (pdu *TunnelDisconnectResponse) GetSerialLength() uint32 {
return 4
}
func (pdu *TunnelDisconnectResponse) SerializeTo(w *bytes.Buffer) {
serializeUInt32To(uint32(pdu.peerConnectionHandle), w)
}
func (pdu *TunnelDisconnectResponse) SerializeFrom(r *bytes.Buffer) {
pdu.peerConnectionHandle = serializeUInt32From(r)
}
/////////////////////////////////////////////////////////////////////////////