-
Notifications
You must be signed in to change notification settings - Fork 68
/
overlay-adnl.go
375 lines (311 loc) · 9.47 KB
/
overlay-adnl.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package overlay
import (
"bytes"
"context"
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/xssnick/tonutils-go/adnl"
"github.com/xssnick/tonutils-go/adnl/rldp"
"github.com/xssnick/tonutils-go/adnl/rldp/raptorq"
"github.com/xssnick/tonutils-go/tl"
"reflect"
"sync"
"time"
)
type CertCheckResult int
const CertCheckResultForbidden CertCheckResult = 0
const CertCheckResultTrusted CertCheckResult = 1
const CertCheckResultNeedCheck CertCheckResult = 2
type fecBroadcastStream struct {
decoder *raptorq.Decoder
finishedAt *time.Time
lastMessageAt time.Time
lastCompleteAt time.Time
source ed25519.PublicKey
trusted bool
mx sync.Mutex
}
type ADNLOverlayWrapper struct {
overlayId []byte
authorizedKeys map[string]int32
maxUnauthSize int32
allowFEC bool
trustUnauthorized bool
customHandler func(msg *adnl.MessageCustom) error
queryHandler func(msg *adnl.MessageQuery) error
disconnectHandler func(addr string, key ed25519.PublicKey)
broadcastStreams map[string]*fecBroadcastStream
streamsMx sync.RWMutex
broadcastHandler func(msg tl.Serializable, trusted bool) error
*ADNLWrapper
}
// WithOverlay - creates basic overlay with restrictive broadcast settings
func (a *ADNLWrapper) WithOverlay(id []byte) *ADNLOverlayWrapper {
return a.CreateOverlayWithSettings(id, 0, true, false)
}
func (a *ADNLWrapper) CreateOverlayWithSettings(id []byte, maxUnauthBroadcastSize int32,
allowBroadcastFEC bool, trustUnauthorizedBroadcast bool) *ADNLOverlayWrapper {
a.mx.Lock()
defer a.mx.Unlock()
strId := hex.EncodeToString(id)
w := a.overlays[strId]
if w != nil {
return w
}
w = &ADNLOverlayWrapper{
overlayId: id,
ADNLWrapper: a,
broadcastStreams: map[string]*fecBroadcastStream{},
allowFEC: allowBroadcastFEC,
maxUnauthSize: maxUnauthBroadcastSize,
trustUnauthorized: trustUnauthorizedBroadcast,
}
a.overlays[strId] = w
return w
}
func (a *ADNLOverlayWrapper) SetAuthorizedKeys(keysWithMaxLen map[string]int32) {
a.mx.Lock()
defer a.mx.Unlock()
// reset and copy
a.authorizedKeys = map[string]int32{}
for k, v := range keysWithMaxLen {
a.authorizedKeys[k] = v
}
}
func (a *ADNLWrapper) UnregisterOverlay(id []byte) {
a.mx.Lock()
defer a.mx.Unlock()
delete(a.overlays, hex.EncodeToString(id))
}
func (a *ADNLOverlayWrapper) SendCustomMessage(ctx context.Context, req tl.Serializable) error {
return a.ADNLWrapper.SendCustomMessage(ctx, []tl.Serializable{Query{Overlay: a.overlayId}, req})
}
func (a *ADNLOverlayWrapper) Query(ctx context.Context, req, result tl.Serializable) error {
return a.ADNLWrapper.Query(ctx, []tl.Serializable{Query{Overlay: a.overlayId}, req}, result)
}
func (a *ADNLOverlayWrapper) GetRandomPeers(ctx context.Context) ([]Node, error) {
var res NodesList
err := a.Query(ctx, GetRandomPeers{}, &res)
if err != nil {
return nil, err
}
return res.List, nil
}
func (a *ADNLOverlayWrapper) SetCustomMessageHandler(handler func(msg *adnl.MessageCustom) error) {
a.customHandler = handler
}
func (a *ADNLOverlayWrapper) SetQueryHandler(handler func(msg *adnl.MessageQuery) error) {
a.queryHandler = handler
}
func (a *ADNLOverlayWrapper) SetDisconnectHandler(handler func(addr string, key ed25519.PublicKey)) {
a.disconnectHandler = handler
}
func (a *ADNLOverlayWrapper) SetBroadcastHandler(handler func(msg tl.Serializable, trusted bool) error) {
a.broadcastHandler = handler
}
func (a *ADNLOverlayWrapper) Close() {
a.ADNLWrapper.UnregisterOverlay(a.overlayId)
}
func (a *ADNLOverlayWrapper) checkRules(keyId string, dataSize int32, isFEC bool) CertCheckResult {
if dataSize == 0 {
return CertCheckResultForbidden
}
a.mx.RLock()
maxSz, authorized := a.authorizedKeys[keyId]
a.mx.RUnlock()
if authorized {
if maxSz >= dataSize {
return CertCheckResultTrusted
}
return CertCheckResultForbidden // too big size of data
}
if a.maxUnauthSize < dataSize {
return CertCheckResultForbidden // too big size of data
}
if !a.allowFEC && isFEC {
return CertCheckResultForbidden
}
if a.trustUnauthorized {
return CertCheckResultTrusted
}
return CertCheckResultNeedCheck
}
func (a *ADNLOverlayWrapper) processFECBroadcast(t *BroadcastFEC) error {
broadcastHash, err := t.CalcID()
if err != nil {
return fmt.Errorf("failed to calc broadcast hash: %w", err)
}
id := string(broadcastHash)
a.streamsMx.RLock()
stream := a.broadcastStreams[id]
a.streamsMx.RUnlock()
partDataHasher := sha256.New()
partDataHasher.Write(t.Data)
partDataHash := partDataHasher.Sum(nil)
partHash, err := tl.Hash(&BroadcastFECPartID{
BroadcastHash: broadcastHash,
DataHash: partDataHash,
Seqno: t.Seqno,
})
if err != nil {
return fmt.Errorf("failed to compute hash id of the part: %w", err)
}
toSign, err := tl.Serialize(&BroadcastToSign{
Hash: partHash,
Date: t.Date,
}, true)
if err != nil {
return fmt.Errorf("failed to serialize broadcast for sign check: %w", err)
}
sourceKey, ok := t.Source.(adnl.PublicKeyED25519)
if !ok {
return fmt.Errorf("invalid signer key format")
}
if !ed25519.Verify(sourceKey.Key, toSign, t.Signature) {
return fmt.Errorf("invalid broadcast signature")
}
if stream == nil {
fec, ok := t.FEC.(rldp.FECRaptorQ)
if !ok {
return fmt.Errorf("not supported fec type")
}
if fec.DataSize != t.DataSize {
return fmt.Errorf("incorrect data size")
}
srcId, err := tl.Hash(t.Source)
if err != nil {
return fmt.Errorf("source key id serialize failed: %w", err)
}
checkRes := a.checkRules(string(srcId), t.DataSize, true)
if checkRes != CertCheckResultTrusted && t.Certificate != nil {
var issuerId []byte
var certRes CertCheckResult
switch crt := t.Certificate.(type) {
case CheckableCert:
certRes, err = crt.Check(srcId, a.overlayId, t.DataSize, true)
if err != nil {
return fmt.Errorf("cert check failed: %w", err)
}
if certRes == CertCheckResultForbidden {
break
}
var issuedBy any
switch cert := crt.(type) {
case Certificate:
issuedBy = cert.IssuedBy
case CertificateV2:
issuedBy = cert.IssuedBy
}
issuerId, err = tl.Hash(issuedBy)
if err != nil {
return fmt.Errorf("issuer key id serialize failed: %w", err)
}
case CertificateEmpty:
default:
return fmt.Errorf("not supported cert type %s", reflect.TypeOf(t.Certificate).String())
}
if issuerId != nil {
issuerRes := a.checkRules(string(issuerId), t.DataSize, true)
if issuerRes > certRes {
// we consider minimal of these 2
issuerRes = certRes
}
if issuerRes > checkRes {
// we consider maximal of these 2
checkRes = issuerRes
}
}
}
if checkRes == CertCheckResultForbidden {
return fmt.Errorf("not allowed")
}
dec, err := raptorq.NewRaptorQ(uint32(fec.SymbolSize)).CreateDecoder(uint32(fec.DataSize))
if err != nil {
return fmt.Errorf("failed to init raptorq decoder: %w", err)
}
stream = &fecBroadcastStream{
decoder: dec,
lastMessageAt: time.Now(),
source: sourceKey.Key,
trusted: checkRes == CertCheckResultTrusted,
}
a.streamsMx.Lock()
// check again because of possible concurrency
if a.broadcastStreams[id] != nil {
stream = a.broadcastStreams[id]
} else {
a.broadcastStreams[id] = stream
}
a.streamsMx.Unlock()
} else if !bytes.Equal(stream.source, sourceKey.Key) {
return fmt.Errorf("malformed source")
}
stream.mx.Lock()
defer stream.mx.Unlock()
if stream.finishedAt != nil {
var received tl.Serializable = FECCompleted{
Hash: broadcastHash,
}
// got packet for a finished stream, let them know that it is received
err := a.ADNL.SendCustomMessage(context.Background(), received)
if err != nil {
return fmt.Errorf("failed to send overlay fec received message: %w", err)
}
return nil
}
tm := time.Now()
stream.lastMessageAt = tm
canTryDecode, err := stream.decoder.AddSymbol(uint32(t.Seqno), t.Data)
if err != nil {
return fmt.Errorf("failed to add raptorq symbol %d: %w", t.Seqno, err)
}
if canTryDecode {
decoded, data, err := stream.decoder.Decode()
if err != nil {
return fmt.Errorf("failed to decode raptorq packet: %w", err)
}
// it may not be decoded due to unsolvable math system, it means we need more symbols
if decoded {
stream.finishedAt = &tm
stream.decoder = nil
a.streamsMx.Lock()
if len(a.broadcastStreams) > 100 {
for sID, s := range a.broadcastStreams {
// remove streams that was finished more than 180 sec ago and stuck streams when it was no messages for 60 sec.
if s.lastMessageAt.Add(40*time.Second).Before(tm) ||
(s.finishedAt != nil && s.finishedAt.Add(90*time.Second).Before(tm)) {
delete(a.broadcastStreams, sID)
}
}
}
a.streamsMx.Unlock()
dHash := sha256.New()
dHash.Write(data)
if !bytes.Equal(dHash.Sum(nil), t.DataHash) {
return fmt.Errorf("incorrect data hash")
}
var res any
_, err = tl.Parse(&res, data, true)
if err != nil {
return fmt.Errorf("failed to parse decoded broadcast message: %w", err)
}
var complete tl.Serializable = FECCompleted{
Hash: broadcastHash,
}
err = a.ADNL.SendCustomMessage(context.Background(), complete)
if err != nil {
return fmt.Errorf("failed to send rldp complete message: %w", err)
}
if bHandler := a.broadcastHandler; bHandler != nil {
// handle result
err = bHandler(res, stream.trusted)
if err != nil {
return fmt.Errorf("failed to process broadcast message: %w", err)
}
}
}
}
return nil
}