forked from schollz/croc
/
relay.go
218 lines (197 loc) · 4.92 KB
/
relay.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
package croc
import (
"net"
"strings"
"sync"
"time"
log "github.com/cihub/seelog"
"github.com/pkg/errors"
)
func (c *Croc) startRelay() {
ports := c.TcpPorts
var wg sync.WaitGroup
wg.Add(len(ports))
for _, port := range ports {
go func(port string, wg *sync.WaitGroup) {
defer wg.Done()
log.Debugf("listening on port %s", port)
if err := c.listener(port); err != nil {
log.Error(err)
return
}
}(port, &wg)
}
wg.Wait()
}
func (c *Croc) listener(port string) (err error) {
server, err := net.Listen("tcp", "0.0.0.0:"+port)
if err != nil {
return errors.Wrap(err, "Error listening on :"+port)
}
defer server.Close()
// spawn a new goroutine whenever a client connects
for {
connection, err := server.Accept()
if err != nil {
return errors.Wrap(err, "problem accepting connection")
}
log.Debugf("client %s connected", connection.RemoteAddr().String())
go func(port string, connection net.Conn) {
errCommunication := c.clientCommuncation(port, connection)
if errCommunication != nil {
log.Warnf("relay-%s: %s", connection.RemoteAddr().String(), errCommunication.Error())
}
}(port, connection)
}
}
func (c *Croc) clientCommuncation(port string, connection net.Conn) (err error) {
var con1, con2 net.Conn
// get the channel and UUID from the client
err = sendMessage("channel and uuid?", connection)
if err != nil {
return
}
channel, err := receiveMessage(connection)
if err != nil {
return
}
uuid, err := receiveMessage(connection)
if err != nil {
return
}
log.Debugf("%s connected to port %s on channel %s and uuid %s", connection.RemoteAddr().String(), port, channel, uuid)
// validate channel and UUID
c.rs.Lock()
if _, ok := c.rs.channel[channel]; !ok {
c.rs.Unlock()
err = errors.Errorf("channel %s does not exist", channel)
return
}
if uuid != c.rs.channel[channel].uuids[0] &&
uuid != c.rs.channel[channel].uuids[1] {
c.rs.Unlock()
err = errors.Errorf("uuid '%s' is invalid", uuid)
return
}
role := 0
if uuid == c.rs.channel[channel].uuids[1] {
role = 1
}
if _, ok := c.rs.channel[channel].connection[port]; !ok {
c.rs.channel[channel].connection[port] = [2]net.Conn{nil, nil}
}
con1 = c.rs.channel[channel].connection[port][0]
con2 = c.rs.channel[channel].connection[port][1]
if role == 0 {
con1 = connection
} else {
con2 = connection
}
log.Debug(c.rs.channel[channel].connection[port])
c.rs.channel[channel].connection[port] = [2]net.Conn{con1, con2}
ports := c.rs.channel[channel].Ports
c.rs.Unlock()
if con1 != nil && con2 != nil {
log.Debugf("beginning the piping")
var wg sync.WaitGroup
wg.Add(1)
// start piping
go func(con1 net.Conn, con2 net.Conn, wg *sync.WaitGroup) {
pipe(con1, con2)
wg.Done()
log.Debug("done piping")
}(con1, con2, &wg)
if port == ports[0] {
// then set transfer ready
c.rs.Lock()
c.rs.channel[channel].TransferReady = true
c.rs.channel[channel].websocketConn[0].WriteJSON(c.rs.channel[channel])
c.rs.channel[channel].websocketConn[1].WriteJSON(c.rs.channel[channel])
c.rs.Unlock()
log.Debugf("sent ready signal")
}
wg.Wait()
log.Debugf("finished transfer")
}
log.Debug("finished client communication")
return
}
func sendMessage(message string, connection net.Conn) (err error) {
message = fillString(message, bufferSize)
_, err = connection.Write([]byte(message))
return
}
func receiveMessage(connection net.Conn) (s string, err error) {
messageByte := make([]byte, bufferSize)
err = connection.SetReadDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
return
}
err = connection.SetDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
return
}
err = connection.SetWriteDeadline(time.Now().Add(60 * time.Minute))
if err != nil {
return
}
_, err = connection.Read(messageByte)
if err != nil {
return
}
s = strings.TrimRight(string(messageByte), ":")
return
}
func fillString(returnString string, toLength int) string {
for {
lengthString := len(returnString)
if lengthString < toLength {
returnString = returnString + ":"
continue
}
break
}
return returnString
}
// chanFromConn creates a channel from a Conn object, and sends everything it
// Read()s from the socket to the channel.
func chanFromConn(conn net.Conn) chan []byte {
c := make(chan []byte)
go func() {
b := make([]byte, bufferSize)
for {
n, err := conn.Read(b)
if n > 0 {
res := make([]byte, n)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(res, b[:n])
c <- res
}
if err != nil {
c <- nil
break
}
}
}()
return c
}
// pipe creates a full-duplex pipe between the two sockets and
// transfers data from one to the other.
func pipe(conn1 net.Conn, conn2 net.Conn) {
chan1 := chanFromConn(conn1)
chan2 := chanFromConn(conn2)
for {
select {
case b1 := <-chan1:
if b1 == nil {
return
}
conn2.Write(b1)
case b2 := <-chan2:
if b2 == nil {
return
}
conn1.Write(b2)
}
}
}