-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
260 lines (214 loc) · 5.41 KB
/
message.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
/*
This file defines all Messages exchanged between router and proxy.
*/
package gobonding
import (
"crypto"
crand "crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/binary"
"fmt"
"log"
"math"
"math/rand"
"time"
"golang.org/x/net/ipv4"
)
const (
MTU = 1500
// BSIZE is size of buffer to receive packets
// (little bit bigger than maximum)
BUFFERSIZE = MTU + 218 + 2 + 8
SOCKET_BUFFER = 1024 * 1024
)
type Message interface {
Buffer() []byte
String() string
}
/*
A tunneled IP package
*/
type Chunk struct {
Data [BUFFERSIZE]byte
Size uint16
Age Wrapped
}
func (msg *Chunk) Buffer() []byte {
return msg.Data[:msg.Size]
}
func (msg *Chunk) IPData() []byte {
return msg.Data[:msg.Size-2]
}
func (msg *Chunk) Gather(size uint16) {
msg.Size = size
msg.Age = Wrapped(binary.BigEndian.Uint16(msg.Data[msg.Size-2 : msg.Size]))
}
func (msg *Chunk) Set(age Wrapped, size uint16) {
msg.Age = age
msg.Size = size + 2
binary.BigEndian.PutUint16(msg.Data[msg.Size-2:msg.Size], uint16(msg.Age))
}
func (msg *Chunk) String() string {
//return fmt.Sprintf("Chunk(%v) %v", msg.Age, msg.Size)
header, err := ipv4.ParseHeader(msg.Data[0:])
if err != nil {
return "Error parsing buffer"
} else {
return fmt.Sprintf("Packet %v: %v", msg.Size, header)
}
}
/*
Heartbeat test: PingMsg is a request for a Pong response
*/
type PingMsg struct {
}
func (msg *PingMsg) Buffer() []byte {
return []byte{0, 'i'}
}
func (msg *PingMsg) String() string {
return "Ping"
}
/*
Heartbeat test: PongMsg is a response for a Ping request
*/
type PongMsg struct {
}
func Pong() *PongMsg {
return &PongMsg{}
}
func (msg *PongMsg) Buffer() []byte {
buffer := []byte{0, 'o'}
return buffer
}
func (msg *PongMsg) String() string {
return "Pong"
}
/*
Sends the receiving speed in bytes/sec to the peer.
*/
type SpeedMsg struct {
Speed float32
}
func SpeedFromChunk(chunk *Chunk) *SpeedMsg {
speed := math.Float32frombits(binary.BigEndian.Uint32(chunk.Data[2:]))
return &SpeedMsg{Speed: speed}
}
func (msg *SpeedMsg) Buffer() []byte {
buffer := []byte{0, 's', 1, 2, 3, 4}
binary.BigEndian.PutUint32(buffer[2:], math.Float32bits(msg.Speed))
return buffer
}
func (msg *SpeedMsg) String() string {
return fmt.Sprintf("Speed: %v", msg.Speed)
}
var epoch = time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)
var Epoch = epoch
/*
Sends data to the peer for measuring the receiving speed.
*/
type SpeedTestMsg struct {
// the age of the last Chunk package of this speed test
Age Wrapped
// the local start time
Timestamp time.Duration
// the transferred bytes
Size uint32
}
func SpeedTest(age Wrapped, ts time.Time, size uint32) *SpeedTestMsg {
return &SpeedTestMsg{Age: age, Timestamp: ts.Sub(epoch), Size: size}
}
func SpeedTestFromChunk(chunk *Chunk) *SpeedTestMsg {
age := Wrapped(binary.BigEndian.Uint16(chunk.Data[2:4]))
ts := time.Duration(binary.BigEndian.Uint64(chunk.Data[4:12]))
size := binary.BigEndian.Uint32(chunk.Data[12:16])
return &SpeedTestMsg{Age: age, Timestamp: ts, Size: size}
}
func (m *SpeedTestMsg) Buffer() []byte {
buffer := []byte{0, 'b', 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4}
binary.BigEndian.PutUint16(buffer[2:4], uint16(m.Age))
binary.BigEndian.PutUint64(buffer[4:12], uint64(m.Timestamp))
binary.BigEndian.PutUint32(buffer[12:16], uint32(m.Size))
return buffer
}
func (msg *SpeedTestMsg) String() string {
ts := epoch.Add(msg.Timestamp)
return fmt.Sprintf("SpeedTest: %v %v %v", msg.Age, ts, msg.Size)
}
/*
Authentification challenge
*/
type ChallengeMsg struct {
Challenge string
}
func Challenge() *ChallengeMsg {
return &ChallengeMsg{Challenge: fmt.Sprintf("HEL%v%v", time.Now(), rand.Int())}
}
func ChallengeFromChunk(chunk *Chunk, size int) *ChallengeMsg {
return &ChallengeMsg{Challenge: string(chunk.Data[2:size])}
}
func (m *ChallengeMsg) Buffer() []byte {
return append([]byte{0, 'c'}, []byte(m.Challenge)...)
}
func (m *ChallengeMsg) CreateResponse(privKey *rsa.PrivateKey) *ChallengeResponseMsg {
h := sha256.New()
h.Write([]byte(m.Challenge))
hash := h.Sum(nil)
response, err := rsa.SignPSS(crand.Reader, privKey, crypto.SHA256, hash, nil)
if err == nil {
err = rsa.VerifyPSS(&privKey.PublicKey, crypto.SHA256, hash, response, nil)
if err != nil {
log.Printf("!!!Wrong Verify %v\n", err)
}
return &ChallengeResponseMsg{Response: string(response)}
}
return &ChallengeResponseMsg{Response: ""}
}
func (m *ChallengeMsg) Verify(pubKey *rsa.PublicKey, chunk *Chunk, size int) error {
h := sha256.New()
h.Write([]byte(m.Challenge))
hash := h.Sum(nil)
response := chunk.Data[2:size]
return rsa.VerifyPSS(pubKey, crypto.SHA256, hash, response, nil)
}
func (msg *ChallengeMsg) String() string {
return fmt.Sprintf("Challenge: %v", msg.Challenge)
}
/*
Authentification Response
*/
type ChallengeResponseMsg struct {
Response string
}
func (m *ChallengeResponseMsg) Buffer() []byte {
return append([]byte{0, 'r'}, []byte(m.Response)...)
}
func (msg *ChallengeResponseMsg) String() string {
return fmt.Sprintf("ChallengeResponseMsg: %v", []byte(msg.Response))
}
/*
A logical clock that can wrap.
Used for ordering chunk Messages.
*/
type Wrapped uint16
func (wrp Wrapped) Less(other Wrapped) bool {
if wrp < 0x1000 && other > 0xf000 {
// Overflow case
return false
}
if other < 0x1000 && wrp > 0xf000 {
// Overflow case
return true
}
if other == 0 && wrp != 0 {
return true
}
return wrp < other && wrp != 0
}
func (wrp Wrapped) Inc() Wrapped {
(wrp)++
if wrp == 0 {
wrp++
}
return wrp
}