This repository has been archived by the owner on Oct 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
booster.go
414 lines (345 loc) · 9.7 KB
/
booster.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
/*
Copyright (C) 2018 Daniel Morandini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Package booster provides the higher interface for dealing with booster instances
// that follow the booster protocol. It wraps together node, proxy, network.
package booster
import (
"context"
"crypto/sha1"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"sync"
"time"
"github.com/danielmorandini/booster/log"
"github.com/danielmorandini/booster/network"
"github.com/danielmorandini/booster/network/packet"
"github.com/danielmorandini/booster/node"
"github.com/danielmorandini/booster/protocol"
"github.com/danielmorandini/booster/pubsub"
"github.com/danielmorandini/booster/socks5"
)
// Proxy wraps that booster requires a proxy to implement.
type Proxy interface {
Sub(cmd *pubsub.Command) (pubsub.CancelFunc, error)
// ListenAndServe should starts the actual proxy server, announcing it to the local
// address.
ListenAndServe(ctx context.Context, port int) error
// Proto returns the string representation of the protocol used by the proxy.
// Example: socks5.
Proto() string
}
// PubSub describes the required functionalities of a publication/subscription object.
type PubSub interface {
Sub(cmd *pubsub.Command) (pubsub.CancelFunc, error)
Pub(message interface{}, topic string)
}
type SendConsumeCloser interface {
SendCloser
Consume() (<-chan *packet.Packet, error)
}
type SendCloser interface {
Close() error
Send(p *packet.Packet) error
}
// Booster wraps the parts that compose a booster node together.
type Booster struct {
ID string
Proxy Proxy
PubSub
Netconfig network.Config
stop chan struct{}
restart chan struct{}
}
var DefaultNetConfig = network.Config{
TagSet: packet.TagSet{
PacketOpeningTag: protocol.PacketOpeningTag,
PacketClosingTag: protocol.PacketClosingTag,
ModuleOpeningTag: protocol.ModuleOpeningTag,
ModuleClosingTag: protocol.ModuleClosingTag,
Separator: protocol.Separator,
},
}
// New creates a new configured booster node. Creates a network configuration
// based in the information contained in the protocol package.
//
// The internal proxy is configured to use the node dispatcher as network
// dialer.
func New(pport, bport int) (*Booster, error) {
b := new(Booster)
pp := strconv.Itoa(pport)
bp := strconv.Itoa(bport)
rn, err := node.New("localhost", pp, bp, true)
if err != nil {
return nil, err
}
id := sha1Hash([]byte(strconv.Itoa(pport)), []byte(strconv.Itoa(bport)))
n := NewNet(rn, id)
pubsub := pubsub.New()
dialer := node.NewDispatcher(n)
proxy := socks5.New(dialer)
Nets.Set(id, n)
b.ID = id
b.Proxy = proxy
b.PubSub = pubsub
b.Netconfig = DefaultNetConfig
b.stop = make(chan struct{})
b.restart = make(chan struct{})
b.Net().HeartbeatTTL = time.Second * 8
b.Net().DialTimeout = time.Second * 4
return b, nil
}
func (b *Booster) Net() *Network {
return Nets.Get(b.ID)
}
// Run starts the proxy and booster node.
//
// This is a blocking routine that can be stopped using the Close() method.
// Traps INTERRUPT signals.
func (b *Booster) Run() error {
// trap exit signals
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for sig := range c {
log.Info.Printf("booster: signal (%v) received: exiting...", sig)
b.Close()
return
}
}()
errc := make(chan error)
var ctx context.Context
var cancel context.CancelFunc
run := func() {
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
errc <- b.run(ctx)
}
for {
go run()
select {
case err := <-errc:
cancel()
return err
case <-b.stop:
cancel()
<-errc
return fmt.Errorf("booster: stopped")
case <-b.restart:
cancel()
<-errc
}
}
}
func (b *Booster) run(ctx context.Context) error {
_, pport, _ := net.SplitHostPort(Nets.Get(b.ID).LocalNode.PAddr.String())
_, bport, _ := net.SplitHostPort(Nets.Get(b.ID).LocalNode.BAddr.String())
pp, _ := strconv.Atoi(pport)
bp, _ := strconv.Atoi(bport)
errc := make(chan error, 4)
defer close(errc)
var wg sync.WaitGroup
go func() {
wg.Add(1)
errc <- b.ListenAndServe(ctx, bp)
wg.Done()
}()
go func() {
wg.Add(1)
errc <- b.Proxy.ListenAndServe(ctx, pp)
wg.Done()
}()
go func() {
wg.Add(1)
errc <- b.UpdateRoot(ctx)
wg.Done()
}()
go func() {
wg.Add(1)
errc <- Nets.Get(b.ID).TraceNodes(ctx, b)
wg.Done()
}()
// read only the first message that arrives
err := <-errc
if ctx.Err() == nil {
// it means that one of the rountines up here failed, but no close
// was manually called
b.Close()
}
// wait for every rountine to return before quitting
wg.Wait()
return err
}
// Close stops the Run routine. It drops the whole booster network, preparing for the
// node to reset or stop.
func (b *Booster) Close() error {
log.Info.Println("booster: closing...")
b.stop <- struct{}{}
return nil
}
// restart restarts the Run routine.
func (b *Booster) Restart() error {
log.Info.Println("booster: restarting...")
Nets.Close(b.ID)
b.restart <- struct{}{}
return nil
}
// ListenAndServe shows to the network, listening for incoming tcp connections an
// turning them into booster connections.
func (b *Booster) ListenAndServe(ctx context.Context, port int) error {
p := strconv.Itoa(port)
ln, err := network.Listen("tcp", ":"+p, b.Netconfig)
if err != nil {
return err
}
defer ln.Close()
log.Info.Printf("booster: listening on port: %v", p)
errc := make(chan error)
defer close(errc)
go func() {
for {
conn, err := ln.Accept()
if err != nil {
errc <- fmt.Errorf("booster: cannot accept conn: %v", err)
return
}
// send hello message first.
if err := b.SendHello(ctx, conn); err != nil {
errc <- err
return
}
go b.Handle(ctx, conn)
}
}()
select {
case err := <-errc:
return err
case <-ctx.Done():
ln.Close()
<-errc // wait for listener to return
return ctx.Err()
}
}
// DialContext dials a new connection to addr and wraps the connection around
// a booster connection. Consumes the first hello message received.
func (b *Booster) DialContext(ctx context.Context, netwrk, addr string) (*Conn, error) {
ctx, cancel := context.WithTimeout(ctx, b.Net().DialTimeout)
defer cancel()
dialer := network.NewDialer(new(net.Dialer), b.Netconfig)
conn, err := dialer.DialContext(ctx, netwrk, addr)
if err != nil {
return nil, err
}
return b.RecvHello(ctx, conn)
}
// Wire connects the target and the local node together, adding the remote booster instance
// as a new connection of the network.
func (b *Booster) Wire(ctx context.Context, network, target string) (*Conn, error) {
// connect to the target node. The node stored in conn will not
// trigger the tracer (i.e. ToBeTraced == false), so it is ok
// to just close the connection in case of failure.
conn, err := b.DialContext(ctx, network, target)
if err != nil {
return nil, err
}
fail := func(err error) (*Conn, error) {
conn.Close()
return nil, err
}
// if AddConn returns an error, chances are that the connection is
// already present and active.
err = b.Net().AddConn(conn)
if err != nil {
return fail(err)
}
// compose the notify packet which tells the receiver to start sending
// information notifications when its state changes
p, err := b.Net().EncodeDefault(nil, protocol.MessageNotify)
if err != nil {
return fail(err)
}
if err = conn.Send(p); err != nil {
return fail(err)
}
log.Info.Printf("booster: -> wire: %v", target)
// inject the heartbeat message in the connection
p, err = b.Net().composeHeartbeat(nil)
if err != nil {
return fail(err)
}
if err = conn.Send(p); err != nil {
return fail(err)
}
// start the timer that, when done, will close the connection if
// no heartbeat message is received in time
conn.HeartbeatTimer = time.AfterFunc(Nets.Get(b.ID).HeartbeatTTL*2, func() {
// do not close the node multiple times.
if conn.Conn != nil {
log.Info.Printf("booster: no heartbeat received from conn %v: timer expired", conn.ID)
conn.Close()
}
})
// set the connection as active
conn.RemoteNode.SetIsActive(true)
conn.RemoteNode.ToBeTraced = true
// handle the newly added connection in a different goroutine.
go b.Handle(ctx, conn)
return conn, nil
}
// UpdateRoot subscribes to the local proxy updating the root node information with the
// updated data.
func (b *Booster) UpdateRoot(ctx context.Context) error {
errc := make(chan error)
cancel, err := b.Proxy.Sub(&pubsub.Command{
Topic: socks5.TopicTunnelEvents,
Run: func(i interface{}) error {
p, ok := i.(protocol.PayloadProxyUpdate)
if !ok {
return fmt.Errorf("update root: unable to recognise payload: %v", p)
}
node := Nets.Get(b.ID).LocalNode
if err := b.UpdateNode(node, p, true); err != nil {
log.Error.Printf("booster: %v", err)
}
return nil
},
PostRun: func(err error) {
if err != nil {
log.Error.Printf("booster: update root: %v", err)
errc <- err
}
},
})
if err != nil {
log.Error.Printf("booster: update root: %v", err)
return err
}
defer cancel()
select {
case err := <-errc:
return err
case <-ctx.Done():
cancel()
return ctx.Err()
}
}
func sha1Hash(images ...[]byte) string {
h := sha1.New()
for _, image := range images {
h.Write(image)
}
return fmt.Sprintf("%x", h.Sum(nil))
}