-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.go
422 lines (361 loc) · 10.1 KB
/
connect.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2021-22 Kirill Scherba <kirill@scherba.ru>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Connect to teonet module
package teonet
import (
"bytes"
"errors"
"fmt"
"reflect"
"time"
"github.com/kirill-scherba/bslice"
"github.com/teonet-go/tru"
)
// nMODULEcon is current module name
var nMODULEcon = "connect"
// Reconnect teonet at error after 1 second
const teonetReconnectAfter = 1 * time.Second
// Teoauth commands
const (
// CmdConnect send <cmd byte, data ConnectData> to teonet auth server to
// connect to teonet; receive <cmd byte, data ConnectData> from teonet auth
// server when connection established
CmdConnect AuthCmd = iota
// CmdConnectTo send <cmd byte, data ConnectToData> to teonet auth server to
// connect to peer
CmdConnectTo
// CmdConnectToPeer command send by teonet auth to server to receive
// connection from client
CmdConnectToPeer
// CmdResendConnectTo need to resend CmdConnectTo data from rauth to auth servers
// to find peer and send command data to it
CmdResendConnectTo
// CmdResendConnectToPeerto need to resend CmdConnectToPeer data from rauth
// to auth servers to find client and send command data to it
CmdResendConnectToPeer
// CmdGetIP used in rauth and return channels IP:Port
CmdGetIP
)
// Connet error
var (
ErrIncorrectServerKey = errors.New("incorrect server key received")
ErrIncorrectPublicKey = errors.New("incorrect public key received")
ErrTimeout = errors.New("timeout")
)
// AuthCmd auth command type
type AuthCmd byte
// String return string with command name
func (c AuthCmd) String() string {
switch c {
case CmdConnect:
return "CmdConnect"
case CmdConnectTo:
return "CmdConnectTo"
case CmdConnectToPeer:
return "CmdConnectToPeer"
case CmdResendConnectTo:
return "CmdResendConnectTo"
case CmdResendConnectToPeer:
return "CmdResendConnectToPeer"
case CmdGetIP:
return "CmdGetIP"
}
return "not defined"
}
// ConnectIpPort
type ConnectIpPort struct {
IP string
Port int
}
// ExcludeIPs
type ExcludeIPs struct {
IPs []string
}
// exclude IPs from NodeAddr slice
func (c ConnectIpPort) exclude(nodesin []NodeAddr, excludeIPs ...string) (nodes []NodeAddr) {
nodes = nodesin
for i := range excludeIPs {
for j := range nodes {
if nodes[j].IP == excludeIPs[i] {
nodes = append(nodes[:j], nodes[j+1:]...)
nodes = c.exclude(nodes, excludeIPs...)
return
}
}
}
return
}
// getAddrFromHTTP get connection nodes by URL, remove excludeIPs and random
// select one node
func (c *ConnectIpPort) getAddrFromHTTP(url string, excludeIPs ...string) (err error) {
// Get connection nodes by URL
n, err := Nodes(url)
if err != nil {
return
}
// Exclude from Nodes list by IPs
n.address = c.exclude(n.address, excludeIPs...)
// Return error if nodes list is empty
l := len(n.address)
if l == 0 {
err = errors.New("empty list of nodes returned")
return
}
// Get random node
i := 0
if l > 1 {
i = rnd.Intn(l)
}
c.IP = n.address[i].IP
c.Port = int(n.address[i].Port)
log.Debugv.Printf("\n%s\nnum nodes -> %d, i -> %d, connect to: %s:%d\n",
n.String(), l, i, c.IP, c.Port)
return
}
// Connect to Teonet.
// Attributes parameter by type:
//
// type eExcludeIPs - struct with IPs slice to exclude from
// type ConnectIpPort - struct with IP and Port
// type string - RHost URL
// type int - directConnectDelay in millisecond to execute direct connect to peers
func (teo *Teonet) Connect(attr ...interface{}) (err error) {
// During Connet to Teonet client send request to Teonet auth server:
//
// - Client call Connect (and wait answer inside Connect function)
// - Server call ConnectProcess
// - Client got answer (inside Connect function) and create teonet channel
//
teo.Log().Connect.Println(nMODULEcon, "to remote teonet node", attr)
// Parse attr by type, it may be:
//
// - String with URL,
// - ConnectIpPort struct with IP and Port
// - ExcludeIPs struct with IPs slice to exclude from
// - Int integer directConnectDelay to execute direct connect to peers
//
// If attr string present than connect to URL by http get list of
// available nodes remove ExludeIPs and select one of it
var con = ConnectIpPort{"95.217.18.68", 8000}
var excl ExcludeIPs
var url string
for i := range attr {
switch v := attr[i].(type) {
case ExcludeIPs:
excl = v
case ConnectIpPort:
con = v
case string:
switch {
case v == teo.connectURL.rauthPage:
url = teo.connectURL.rauthURL
case len(v) > 0:
url = v
default:
url = teo.connectURL.authURL
}
}
}
// Set default address if attr omitted
if len(url) == 0 {
url = teo.connectURL.authURL
}
// Connect to rauth https server and get auth ip:port to connect
if len(url) > 0 {
err = con.getAddrFromHTTP(url, excl.IPs...)
if err != nil {
return
}
}
// Connect to tru auth node and create new teonet channel if connected
ch, err := teo.tru.Connect(fmt.Sprintf("%s:%d", con.IP, con.Port))
if err != nil {
return
}
auth := teo.channels.new(ch)
teo.setAuth(auth)
// Create channel to wait end of connection
var chanWait = make(chanWait)
defer close(chanWait)
// Subscribe to teo.auth channel to get and process messages from teonet
// server. Subscribers reader shound return true if packet processed by this
// reader
var subs *subscribeData
subs = teo.subscribe(auth, func(teo *Teonet, c *Channel, p *Packet, e *Event) bool {
// Disconnect r-host processing
if e.Event == EventTeonetDisconnected {
log.Connect.Println("disconnected from teonet")
teo.Unsubscribe(subs)
teo.setAuth(nil)
select {
case <-teo.closing:
return true
default:
// Reconnect
go func() {
// wait while exit when closing
time.Sleep(20 * time.Millisecond)
// reconnect while connected
for {
log.Debug.Println("reconnect to teonet")
err := teo.Connect(attr...)
if err == nil {
break
}
time.Sleep(teonetReconnectAfter)
}
}()
}
return true
}
// Skip not Data Event
if e.Event != EventData {
return false
}
// Commands from teonet server processing
cmd := teo.Command(p.Data())
switch AuthCmd(cmd.Cmd) {
// Client got answer to cmdConnect(connect to teonet server)
case CmdConnect:
// Check if chanW chanal is open
ok := chanWait.IsOpen()
if !ok {
return false
}
// Send to channel
chanWait <- cmd.Data
// Client got answer to cmdConnectTo(connect to peer)
case CmdConnectTo:
teo.processCmdConnectTo(cmd.Data)
// Peer got CmdConnectToPeer command
case CmdConnectToPeer:
teo.processCmdConnectToPeer(cmd.Data)
// This commands (and empty body) added to remove "not defined" error
// from default case
case CmdResendConnectTo, CmdResendConnectToPeer, CmdGetIP:
return false
// Not defined commands
default:
log.Error.Println("Got not defined command", cmd.Cmd)
return false
}
return true
})
defer func() {
if err != nil {
teo.Unsubscribe(subs)
}
}()
// Connect data
conIn := ConnectData{
PubliKey: teo.config.getPublicKey(), // []byte("PublicKey"),
Address: []byte(teo.Address()), // []byte("Address"),
ServerKey: teo.config.ServerPublicKeyData, // []byte("ServerKey"),
ServerAddress: nil,
}
// Marshal data
data, err := conIn.MarshalBinary()
if err != nil {
return
}
// Send to teoauth
_, err = teo.Command(CmdConnect, data).Send(auth)
if err != nil {
return
}
// Wait Connect answer data processed in subscribe callback
select {
case data = <-chanWait:
case <-time.After(tru.ClientConnectTimeout):
err = ErrTimeout
return
}
// Unmarshal data
var conOut ConnectData
conOut.UnmarshalBinary(data)
if err != nil {
return
}
// Check server error
if len(conOut.Err) > 0 {
err = errors.New(string(conOut.Err))
return
}
// Check received data
if !reflect.DeepEqual(conOut.PubliKey, teo.GetPublicKey()) {
err = ErrIncorrectPublicKey
return
}
// Update config data and save config to file
addr := string(conOut.Address)
teo.config.ServerPublicKeyData = conOut.ServerKey
// teo.config.Address = addr
teo.setAddress(addr)
teo.config.save()
teo.SetConnected(auth, string(conOut.ServerAddress))
// Connected to teonet, show log message and send Event to main reader
log.Connect.Printf("teonet address: %s\n", conOut.Address)
reader(teo, auth, nil, &Event{EventTeonetConnected, nil})
return
}
// SetConnected set address to channel, add channel to channels list and send
// event to main teonet reader
func (teo *Teonet) SetConnected(c *Channel, addr string) {
c.a = addr
teo.channels.add(c)
reader(teo, c, nil, &Event{EventConnected, nil})
}
// ConnectData teonet connect data
type ConnectData struct {
PubliKey []byte // Client public key (generated from private key)
Address []byte // Client address (received after connect if empty)
ServerKey []byte // Server public key (send if exists or received in connect if empty)
ServerAddress []byte // Server address (received after connect)
Err []byte // Error of connect data processing
bslice.ByteSlice
}
// MarshalBinary binary marshal ConnectData
func (c ConnectData) MarshalBinary() (data []byte, err error) {
buf := new(bytes.Buffer)
c.WriteSlice(buf, c.PubliKey)
c.WriteSlice(buf, c.Address)
c.WriteSlice(buf, c.ServerKey)
c.WriteSlice(buf, c.ServerAddress)
c.WriteSlice(buf, c.Err)
data = buf.Bytes()
return
}
// UnmarshalBinary binary unmarshal ConnectData
func (c *ConnectData) UnmarshalBinary(data []byte) (err error) {
buf := bytes.NewBuffer(data)
c.PubliKey, err = c.ReadSlice(buf)
if err != nil {
return
}
c.Address, err = c.ReadSlice(buf)
if err != nil {
return
}
c.ServerKey, err = c.ReadSlice(buf)
if err != nil {
return
}
c.ServerAddress, err = c.ReadSlice(buf)
if err != nil {
return
}
c.Err, err = c.ReadSlice(buf)
return
}
// String return string with ConnectData
func (c ConnectData) String() string {
return fmt.Sprintf("len: %d\nkey: %x\naddress: %s\nserver key: %x\nserver address: %s\nerror: %s",
len(c.PubliKey)+len(c.Address)+len(c.ServerKey)+len(c.ServerAddress)+len(c.Err),
c.PubliKey,
c.Address,
c.ServerKey,
c.ServerAddress,
c.Err,
)
}