-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.go
83 lines (73 loc) · 1.94 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
package main
import (
"context"
"crypto/tls"
"fmt"
"net"
"time"
)
// Connector opens 2 connections and forwards data from one to another
type Connector struct {
src, dst string
interval int
reconnect bool // Automatically reconnect to the local server
tlsConfig *tls.Config
}
func (c Connector) connectOutNode(fromCh, toCh chan Message) {
addr := c.src
for {
var msg *Message
if !c.reconnect {
m := <-fromCh // block until data are received from the remote node
msg = &m
}
conn, err := net.Dial("tcp", addr)
if err != nil {
errorLog.Printf("cannot connect to %s: %v, retrying in %v seconds\n", addr, err, c.interval)
disconnect(toCh)
time.Sleep(time.Duration(c.interval) * time.Second)
continue
}
debugLog.Print(fmt.Sprintf("connected to %s\n", addr))
ctx, cancel := context.WithCancel(context.Background())
n := Node{from: fromCh, to: toCh, cancel: cancel, addr: addr}
p := OutNode{Node: n, firstMessage: msg}
p.Wait(ctx, conn)
<-ctx.Done()
conn.Close()
}
}
func (c Connector) connectInNode(fromCh, toCh chan Message) {
addr := c.dst
for {
var conn net.Conn
var err error
if c.tlsConfig != nil {
conn, err = tls.Dial("tcp", addr, c.tlsConfig)
} else {
conn, err = net.Dial("tcp", addr)
}
if err != nil {
errorLog.Printf("cannot connect to %s: %v, retrying in %v seconds\n", addr, err, c.interval)
time.Sleep(time.Duration(c.interval) * time.Second)
continue
}
msg := fmt.Sprintf("connected to %s\n", addr)
if c.tlsConfig != nil {
msg = "securely " + msg
}
debugLog.Print(msg)
ctx, cancel := context.WithCancel(context.Background())
p := InNode{from: fromCh, to: toCh, cancel: cancel, addr: addr}
p.Wait(ctx, conn)
<-ctx.Done()
conn.Close()
}
}
// Connect the two destinations using a custom Pipe over channel
func (c Connector) Connect() {
fromCh := make(chan Message)
toCh := make(chan Message)
go c.connectInNode(fromCh, toCh)
c.connectOutNode(toCh, fromCh)
}