-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.go
288 lines (249 loc) · 5.87 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
package spotlib
import (
"context"
"fmt"
"io"
"strings"
"sync/atomic"
"time"
"github.com/KarpelesLab/rest"
"github.com/KarpelesLab/spotproto"
"github.com/coder/websocket"
)
type conn struct {
host string
wr chan []byte
c *Client
}
func (c *Client) getConn(host string) *conn {
c.connsLk.Lock()
defer c.connsLk.Unlock()
if v, ok := c.conns[host]; ok {
return v
}
return nil
}
func (c *Client) regConn(co *conn) {
c.connsLk.Lock()
defer c.connsLk.Unlock()
c.conns[co.host] = co
}
func (c *Client) unregConn(co *conn) bool {
c.connsLk.Lock()
defer c.connsLk.Unlock()
if v, ok := c.conns[co.host]; ok && v == co {
delete(c.conns, co.host)
return true
}
return false
}
func getHosts(ctx context.Context) ([]string, uint32, error) {
// call Spot:connect API to fetch hosts we can connect to
var res *struct {
Hosts []string `json:"hosts"`
MinConn uint32 `json:"min_conn"`
}
err := rest.Apply(ctx, "Spot:connect", "GET", nil, &res)
if err != nil {
return nil, 0, err
}
return res.Hosts, res.MinConn, nil
}
func (c *Client) runConnect() error {
hosts, minConn, err := getHosts(context.Background())
if err != nil {
return err
}
if minConn == 0 {
minConn = uint32(len(hosts))
}
if len(hosts) > 10 {
hosts = hosts[:10]
}
c.minConn = minConn
for _, h := range hosts {
if c.getConn(h) == nil {
c.logf("connecting to host: %s", h)
co := &conn{
host: h,
c: c,
wr: make(chan []byte),
}
c.regConn(co)
go co.run()
// delay things a bit so we don't perform too many handshakes at the same time
time.Sleep(2 * time.Second)
}
}
return nil
}
func (co *conn) run() {
defer co.c.unregConn(co)
atomic.AddUint32(&co.c.connCnt, 1)
defer atomic.AddUint32(&co.c.connCnt, ^uint32(0))
failGiveup := 0
for atomic.LoadUint32(&co.c.closed) == 0 {
c, err := co.dial()
if err != nil {
co.c.logf("failed to connect to server: %s", err)
failGiveup += 1
if failGiveup > 10 {
// give up so we can have a better connection later
return
}
time.Sleep(2 * time.Second)
continue
}
failGiveup = 0
err = co.handle(c)
if err != nil {
if err != io.EOF {
co.c.logf("error during communications with server: %s", err)
}
}
// retry connection immediately
}
}
func (co *conn) handle(c *websocket.Conn) error {
defer c.CloseNow()
c.SetReadLimit(1024 * 1024) // 1MB max packet size
if err := co.handshake(c); err != nil {
return err
}
co.c.onlineIncr()
defer co.c.onlineDecr()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go co.handleWrites(ctx, cancel, c)
for {
mt, dat, err := c.Read(ctx)
if err != nil {
return fmt.Errorf("while reading packet: %w", err)
}
switch mt {
case websocket.MessageBinary:
err = co.handlePacket(dat)
if err != nil {
return err
}
case websocket.MessageText:
// TODO handle text messages. For now we do nothing of these
}
}
}
func (co *conn) handleWrites(ctx context.Context, cancel func(), wsc *websocket.Conn) {
defer cancel()
for {
select {
case <-ctx.Done():
return
case _, _ = <-co.c.alive:
return
case msg := <-co.c.mWrQ:
// write message
buf := msg.Bytes()
buf = append([]byte{spotproto.InstantMsg}, buf...)
wsc.Write(ctx, websocket.MessageBinary, buf)
case buf := <-co.wr:
wsc.Write(ctx, websocket.MessageBinary, buf)
}
}
}
func (co *conn) handshake(c *websocket.Conn) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
for {
mt, dat, err := c.Read(ctx)
if err != nil {
return fmt.Errorf("while reading packet: %w", err)
}
switch mt {
case websocket.MessageBinary:
pkt, err := spotproto.Parse(dat, true)
if err != nil {
return fmt.Errorf("parse error: %w", err)
}
switch obj := pkt.(type) {
case *spotproto.HandshakeRequest:
if obj.Ready {
co.c.logf("authentication done, connected as c.%s", obj.ClientId)
return nil
}
if obj.Groups != nil {
// need to re-compute key
co.c.handleGroups(obj.Groups)
}
// generate response
res, err := obj.Respond(nil, co.c.s)
if err != nil {
return err
}
res.ID = co.c.idBin
// send
buf := append([]byte{spotproto.Handshake}, res.Bytes()...)
c.Write(ctx, websocket.MessageBinary, buf)
default:
co.c.logf("unsupported handshake packet type %T", obj)
}
case websocket.MessageText:
// TODO handle text messages. For now we do nothing of these
}
}
}
func (co *conn) dial() (*websocket.Conn, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
u := "wss://" + co.host + "/_websocket"
co.c.logf("dialing via websocket: %s", u)
c, _, err := websocket.Dial(ctx, u, nil)
return c, err
}
func (co *conn) handlePacket(dat []byte) error {
pkt, err := spotproto.Parse(dat, true)
if err != nil {
return fmt.Errorf("parse error: %w", err)
}
switch obj := pkt.(type) {
case *spotproto.HandshakeRequest:
if obj.Ready {
return nil
}
if obj.Groups != nil {
// need to re-compute key
co.c.handleGroups(obj.Groups)
}
// generate response
res, err := obj.Respond(nil, co.c.s)
if err != nil {
return err
}
res.ID = co.c.idBin
// send
buf := append([]byte{spotproto.Handshake}, res.Bytes()...)
co.wr <- buf
return nil
case *spotproto.Message:
// Recipient:c:4p84:conn-ubzvcl-h7yv-g7pe-tfp6-mddcsqtu/699ec197-d329-45bd-9306-b29f2ff99ac9
rcv := obj.Recipient
pos := strings.IndexByte(rcv, '/')
if pos == -1 {
return nil
}
rcv = rcv[pos+1:]
if pos2 := strings.IndexByte(rcv, '/'); pos2 != -1 {
rcv = rcv[:pos2]
}
q := co.c.takeInQ(rcv)
if q != nil {
q <- obj
} else if h := co.c.getHandler(rcv); h != nil {
go co.c.runHandler(obj, h)
} else {
co.c.logf("unable to route packet targetted to %s", obj.Recipient)
}
return nil
default:
co.c.logf("unsupported packet type %T", obj)
return nil
}
}