-
Notifications
You must be signed in to change notification settings - Fork 251
/
ws2tcp.go
135 lines (116 loc) · 2.9 KB
/
ws2tcp.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
package expose
import (
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
// Accept websocket connections at path / and forward them to the local port.
func websocketToTCPHandlerFunc(targetPort uint16) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
if !websocket.IsWebSocketUpgrade(r) {
http.NotFound(w, r)
return
}
upgrader := websocket.Upgrader{
Subprotocols: websocket.Subprotocols(r),
CheckOrigin: func(r *http.Request) bool {
return true
},
}
wsconn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, fmt.Sprintf("Could not upgrade: %s", err), 500)
return
}
defer wsconn.Close()
// only one thing is allowed to write to the wsconn at a time
var writeLock sync.Mutex
tcpconn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", targetPort))
if err != nil {
http.Error(w, fmt.Sprintf("Could not connect to TCP port: %s", err), 502)
return
}
defer tcpconn.Close()
// when it's time to tear down the connection, tear down both sides
kill := func() {
tcpconn.Close()
wsconn.Close()
}
// Handle control messages
wsconn.SetPingHandler(func(appData string) error {
writeLock.Lock()
defer writeLock.Unlock()
return wsconn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(5*time.Second))
})
// send keepalives every 10s
ticker := time.NewTicker(10 * time.Second)
go func() {
for {
<-ticker.C
writeLock.Lock()
err := wsconn.WriteControl(websocket.PingMessage, nil, time.Now().Add(5*time.Second))
writeLock.Unlock()
if err != nil {
kill()
return
}
}
}()
defer ticker.Stop()
wsconn.SetCloseHandler(func(code int, text string) error {
kill()
return nil
})
// bridge the two connections together, breaking the connection entirely
// when any piece fails and completing this function when both directions
// are closed
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer kill()
for {
messageType, payload, err := wsconn.ReadMessage()
if err != nil {
// typically this means the connection was closed, but
// regardless of the error we want to close the TCP connection.
return
}
if messageType != websocket.BinaryMessage {
// this is a protocol error; we don't support Text
return
}
_, err = tcpconn.Write(payload)
if err != nil {
// An error occurred before the full payload could
// be written; close the connection
return
}
}
}()
go func() {
defer kill()
var buf []byte = make([]byte, 1024*512)
for {
n, err := tcpconn.Read(buf)
if err != nil {
return
}
var data = buf[:n]
writeLock.Lock()
err = wsconn.WriteMessage(websocket.BinaryMessage, data)
writeLock.Unlock()
if err != nil {
return
}
}
}()
wg.Wait()
}
}