forked from mcclymont/tcp2ws-go
/
tcp2ws.go
168 lines (154 loc) · 3.69 KB
/
tcp2ws.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
package main
import (
"encoding/base64"
"flag"
"fmt"
"github.com/gorilla/websocket"
"net"
"net/url"
"os"
)
const (
CONN_TYPE = "tcp"
BUFFER_SIZE = 1024
)
func main() {
rhost := flag.String("rhost", "REQUIRED", "URL of upstream websocket server. Format is 'ws://HOST:PORT/PATH'")
lhost := flag.String("lhost", "localhost:3333", "TCP HOST:PORT to listen on")
flag.Parse()
if *rhost == "REQUIRED" {
fmt.Println("Usage:")
flag.PrintDefaults()
os.Exit(1)
}
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, *lhost)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on " + *lhost)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
// Handle connections in a new goroutine.
go handleRequest(conn, *rhost)
}
}
// Handles incoming requests.
func handleRequest(conn net.Conn, rhost string) {
fmt.Println("TCP connection received")
defer func() { fmt.Println("Connection closed") }()
defer conn.Close()
ws_url, err := url.Parse(rhost)
if err != nil {
fmt.Println(err.Error())
return
}
wsConn, err := net.Dial(CONN_TYPE, ws_url.Host)
if err != nil {
fmt.Println(err.Error())
return
}
defer wsConn.Close()
REQUEST_HEADERS := map[string][]string{
"Sec-Websocket-Protocol": []string{"binary", "base64"},
}
ws, response, err := websocket.NewClient(wsConn, ws_url, REQUEST_HEADERS, BUFFER_SIZE, BUFFER_SIZE)
if err != nil {
fmt.Println(err.Error())
return
}
defer ws.Close()
protocol_arr := response.Header["Sec-Websocket-Protocol"]
var protocol string
if len(protocol_arr) > 0 {
protocol = protocol_arr[0]
}
var messageType int
if protocol == "base64" {
messageType = websocket.TextMessage
} else if protocol == "binary" {
messageType = websocket.BinaryMessage
} else {
fmt.Println("Expected Sec-Websocket-Protocol header value of binary or base64, got:")
fmt.Println(response.Header)
return
}
fmt.Println("Websocket connection established, proxying...")
var stopChan = make(chan int)
go pipe_to_ws(conn, ws, messageType, stopChan)
pipe_to_net(conn, ws, messageType, stopChan)
}
func pipe_to_net(netConn net.Conn, wsConn *websocket.Conn, protocolType int, stopChan chan int) {
for {
select {
case <-stopChan:
return
default:
{
_, p, err := wsConn.ReadMessage()
if err != nil {
stopChan <- 1
return
}
if protocolType == websocket.BinaryMessage {
_, err = netConn.Write(p)
if err != nil {
stopChan <- 1
return
}
} else if protocolType == websocket.TextMessage {
data := make([]byte, base64.StdEncoding.DecodedLen(len(p)))
n, err := base64.StdEncoding.Decode(data, p)
if err != nil {
stopChan <- 1
return
}
_, err = netConn.Write(data[:n])
if err != nil {
stopChan <- 1
return
}
}
}
}
}
}
func pipe_to_ws(netConn net.Conn, wsConn *websocket.Conn, protocolType int, stopChan chan int) {
for {
select {
case <-stopChan:
return
default:
{
data := make([]byte, BUFFER_SIZE)
n, err := netConn.Read(data)
if err != nil {
stopChan <- 1
return
}
if protocolType == websocket.BinaryMessage {
err = wsConn.WriteMessage(protocolType, data[:n])
if err != nil {
stopChan <- 1
return
}
} else if protocolType == websocket.TextMessage {
encodedData := []byte(base64.StdEncoding.EncodeToString(data[:n]))
err = wsConn.WriteMessage(protocolType, encodedData)
if err != nil {
stopChan <- 1
return
}
}
}
}
}
}