/
roles.go
139 lines (124 loc) · 2.7 KB
/
roles.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
package connor
import (
"fmt"
"io"
"net"
"time"
)
// Logger is nil, set it to enable logging
var Logger interface {
Log(string)
}
func logf(m string, p ...interface{}) {
if Logger != nil {
Logger.Log(fmt.Sprintf(m, p...))
}
}
// Loop dial infinitely loops until it has a connection
func LoopDial(address string) net.Conn {
var conn net.Conn
var err error
for {
conn, err = net.Dial("tcp", address)
if err != nil {
logf(err.Error())
time.Sleep(time.Second)
continue
}
return conn
}
}
func TinyHandshakeDial(address string) net.Conn {
for {
conn := LoopDial(address)
err := TinyHandshake(conn, 0x01)
if err != nil {
logf(err.Error())
continue
}
return conn
}
}
func LoopListen(bind string, chanConn chan net.Conn) {
var listener net.Listener
var err error
for {
logf("Listen on %s", bind)
listener, err = net.Listen("tcp", bind)
if err != nil {
logf(err.Error())
time.Sleep(time.Second)
continue
}
for {
conn, err := listener.Accept()
if err != nil {
logf(err.Error())
continue
}
go func(conn net.Conn) {
logf("Got Connection on %s", bind)
chanConn <- conn
}(conn)
}
}
}
// TinyHandshakeListen binds to an address and sends the connection to the
// chanel, after successful handshake
func TinyHandshakeListen(bind string, chanConn chan net.Conn) {
localChanConn := make(chan net.Conn)
go LoopListen(bind, localChanConn)
for {
conn := <-localChanConn
go func(conn net.Conn) {
err := TinyHandshake(conn, 0x01)
if err != nil {
logf(err.Error())
return
}
chanConn <- conn
}(conn)
}
}
// Handshake writes one byte, and ensures that byte comes back. It
// is identical on both sides, the byte should be hardcoded as a
// version, not an echo
func TinyHandshake(conn net.Conn, msg byte) error {
logf("Handshake %s", conn.RemoteAddr())
n, err := conn.Write([]byte{msg})
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("could not write handshake byte")
}
in := []byte{0x00}
n, err = conn.Read(in) // Read a byte
if err != nil {
return err
}
if n != 1 {
return fmt.Errorf("could not read handshake byte")
}
if in[0] != msg {
return fmt.Errorf("handskake byte didn't match %x != %x", in, []byte{msg})
}
return nil
}
// BindConnections uses io.Copy in both directions, and closes both
// connections on return
func BindConnections(a net.Conn, b net.Conn) {
go copyThenClose(a, b)
go copyThenClose(b, a)
}
func copyThenClose(connA net.Conn, connB net.Conn) {
io.Copy(connA, connB)
// The error or number of bytes isn't really important
// Close both if either fail / close.
connA.Close()
connB.Close()
}
type FuncLogger func(string)
func (self FuncLogger) Log(msg string) {
self(msg)
}