-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpproxy.go
181 lines (153 loc) · 3.66 KB
/
tcpproxy.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
package server
import (
"crypto/sha256"
"crypto/subtle"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/gorilla/websocket"
"github.com/moisespsena-go/httpdx/internal"
)
// BufferSize for coybuffer and websocket
const BufferSize = 256 * 1024
// Handler handlers
type Handler struct {
handlers map[string]*TCPSocketConfig
upgrader websocket.Upgrader
dialTimeout time.Duration
writeTimeout time.Duration
enableCompression bool
}
// New new handler
func New(
handlers map[string]*TCPSocketConfig,
handshakeTimeout time.Duration,
dialTimeout time.Duration,
writeTimeout time.Duration,
enableCompression bool) *Handler {
upgrader := websocket.Upgrader{
EnableCompression: enableCompression,
ReadBufferSize: BufferSize,
WriteBufferSize: BufferSize,
HandshakeTimeout: handshakeTimeout,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
return &Handler{
handlers: handlers,
upgrader: upgrader,
dialTimeout: dialTimeout,
writeTimeout: writeTimeout,
}
}
// Proxy proxy handler
func (h *Handler) Proxy() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
wc, err := h.upgrader.Upgrade(w, r, nil)
if err != nil {
http.Error(w, "WEBSOCKET failed: "+err.Error(), http.StatusPreconditionFailed)
return
}
fail := func(msg string) {
wc.WriteMessage(websocket.TextMessage, []byte("ERROR: "+msg))
wc.Close()
}
name := r.URL.Query().Get("name")
if name == "" {
fail("name is blank")
return
}
if name == internal.TestRoute {
wc.PingHandler()("!!test!!")
return
}
sck := h.handlers[name]
if sck.Addr == "" {
fail(fmt.Sprintf("%q is not registered"))
return
}
if sck.Auth != nil && !sck.Auth.Disabled {
username, password, _ := r.BasicAuth()
// Calculate SHA-256 hashes for the provided and expected
// usernames and passwords.
usernameHash := sha256.Sum256([]byte(username))
passwordHash := sha256.Sum256([]byte(password))
expectedUsernameHash := sha256.Sum256([]byte(sck.Auth.User))
expectedPasswordHash := sha256.Sum256([]byte(sck.Auth.Password))
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1)
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1)
if !usernameMatch || !passwordMatch {
fail("invalid username or password")
return
}
}
s, err := net.DialTimeout("tcp", sck.Addr, h.dialTimeout)
if err != nil {
http.Error(w, fmt.Sprintf("Could not connect upstream: %v", err), 500)
return
}
rwc := &wsConnRW{c: wc}
doneCh := make(chan bool)
// websocket -> server
go func() {
defer func() { doneCh <- true }()
io.Copy(s, rwc)
}()
// server -> websocket
go func() {
defer func() { doneCh <- true }()
io.Copy(rwc, s)
}()
<-doneCh
s.Close()
wc.Close()
<-doneCh
}
}
type wsConnRW struct {
c *websocket.Conn
r io.Reader
}
func (w *wsConnRW) Read(p []byte) (n int, err error) {
start:
if w.r != nil {
if n, err = w.r.Read(p); err == io.EOF {
w.r = nil
} else {
return
}
}
var (
mt int
r io.Reader
)
for {
mt, r, err = w.c.NextReader()
if websocket.IsCloseError(err,
websocket.CloseNormalClosure, // Normal.
websocket.CloseAbnormalClosure, // OpenSSH killed proxy client.
) {
return 0, io.EOF
}
if err != nil {
return
}
if mt != websocket.BinaryMessage {
continue
}
w.r = r
goto start
}
}
func (w *wsConnRW) Write(p []byte) (n int, err error) {
if err = w.c.WriteMessage(websocket.BinaryMessage, p); err == nil {
return len(p), nil
}
return
}
func (w *wsConnRW) Close() error {
return w.c.Close()
}