This repository has been archived by the owner on Jun 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
375 lines (313 loc) · 7.35 KB
/
client.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package remoton
import (
"bufio"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"runtime"
"time"
jswebsocket "github.com/gopherjs/websocket"
"github.com/pierrec/lz4"
"golang.org/x/net/websocket"
)
type ErrHTTP struct {
Code int
Msg string
}
func (c ErrHTTP) Error() string {
return c.Msg
}
//Client allow creation of sessions
type Client struct {
Prefix string
TLSConfig *tls.Config
Origin string
}
//SessionClient it's a session where we can create Services a services it's
//a connection -net.Conn-
type SessionClient struct {
*Client
ID string
AuthToken string
//WSURL web socket url by default it try
//to guess from baseUrl
WSURL string
APIURL string
hclient *http.Client
}
//SessionListen tunnel type websocket by default
type SessionListen struct {
*SessionClient
service string
}
//Accept implements the net.Accept for Websocket
func (c *SessionListen) Accept() (net.Conn, error) {
return c.dialWebsocket(c.service, "/listen")
}
//Accept implements the net.Accept for TCP
func (c *SessionListen) AcceptTCP() (net.Conn, error) {
return c.dialTCP(c.service, "/listen")
}
func (c *SessionListen) Close() error {
c.Destroy()
return nil
}
func (c *SessionListen) Addr() net.Addr {
return nil
}
//SessionListenTCP tunnel type TCP
type SessionListenTCP struct {
*SessionClient
service string
}
func (c *SessionListenTCP) Accept() (net.Conn, error) {
return c.dialTCP(c.service, "/listen")
}
func (c *SessionListenTCP) Close() error {
c.Destroy()
return nil
}
func (c *SessionListenTCP) Addr() net.Addr {
return nil
}
//NewSession create a session on server
func (c *Client) NewSession(_url string, authToken string) (*SessionClient, error) {
hclient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: c.TLSConfig,
},
}
req, err := http.NewRequest("POST", _url+c.Prefix+"/session", nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Auth-Token", authToken)
resp, err := hclient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, ErrHTTP{resp.StatusCode, resp.Status}
}
session := &SessionClient{Client: c, APIURL: _url, hclient: hclient}
if err := json.NewDecoder(resp.Body).Decode(session); err != nil {
return nil, err
}
return session, nil
}
//Destroy the current session this not close active connections
func (c *SessionClient) Destroy() {
req, _ := http.NewRequest("DELETE", c.APIURL+"/session/"+c.ID, nil)
req.Header.Set("X-Auth-Session", c.AuthToken)
c.hclient.Do(req)
}
//Dial create a new *service* -net.Conn- Websocket
func (c *SessionClient) Dial(service string) (net.Conn, error) {
if runtime.GOARCH == "js" {
return c.dialWebsocketJS(service, "/dial")
}
return c.dialWebsocket(service, "/dial")
}
//Dial create a new *service* -net.Conn- TCP
func (c *SessionClient) DialTCP(service string) (net.Conn, error) {
return c.dialTCP(service, "/dial")
}
//Listen implementes net.Listener for Websocket connections
func (c *SessionClient) Listen(service string) net.Listener {
return &SessionListen{c, service}
}
//Listen implementes net.Listener for TCP connections
func (c *SessionClient) ListenTCP(service string) net.Listener {
return &SessionListenTCP{c, service}
}
func (c *SessionClient) dialTCP(service string, action string) (net.Conn, error) {
burl, err := url.Parse(c.APIURL)
if err != nil {
return nil, err
}
burl.Path += fmt.Sprintf("%s/session/%s/conn/%s%s/tcp", c.Prefix, c.ID, service, action)
req, err := http.NewRequest("GET", burl.String(), nil)
req.Header.Set("X-Auth-Session", c.AuthToken)
if err != nil {
return nil, err
}
var conn net.Conn
if burl.Scheme == "https" {
conn, err = tls.Dial("tcp", burl.Host, c.TLSConfig)
} else {
conn, err = net.Dial("tcp", burl.Host)
}
if err != nil {
return nil, err
}
br := bufio.NewReader(conn)
bw := bufio.NewWriter(conn)
bw.WriteString("GET " + burl.RequestURI() + " HTTP/1.1\r\n")
bw.WriteString("Host: " + burl.Host + "\r\n")
bw.WriteString("Connection: Upgrade\r\n")
header := http.Header{}
header.Set("X-Auth-Session", c.AuthToken)
err = header.Write(bw)
if err != nil {
return nil, err
}
bw.WriteString("\r\n")
if err = bw.Flush(); err != nil {
return nil, err
}
resp, err := http.ReadResponse(br, &http.Request{Method: "GET"})
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("sessionClient.dialTCP: http response error %s", resp.Status)
}
return conn, nil
}
func (c *SessionClient) dialWebsocketJS(service string, action string) (net.Conn, error) {
var wsurl string
if c.WSURL == "" {
burl, err := url.Parse(c.APIURL)
if err != nil {
return nil, err
}
if burl.Scheme == "https" {
burl.Scheme = "wss"
} else {
burl.Scheme = "ws"
}
wsurl = burl.String()
} else {
wsurl = c.WSURL
}
wsurl += fmt.Sprintf(c.Prefix+"/session/%s/conn/%s%s/websocket",
c.ID,
service,
action)
return jswebsocket.Dial(wsurl)
}
func (c *SessionClient) dialWebsocket(service string, action string) (*websocket.Conn, error) {
var origin string
var wsurl string
useTls := false
if c.Origin == "" {
origin = "http://localhost"
} else {
origin = c.Origin
}
if c.WSURL == "" {
burl, err := url.Parse(c.APIURL)
if err != nil {
return nil, err
}
if burl.Scheme == "https" {
burl.Scheme = "wss"
useTls = true
} else {
burl.Scheme = "ws"
}
wsurl = burl.String()
} else {
wsurl = c.WSURL
}
conf, err := websocket.NewConfig(wsurl, origin)
if err != nil {
return nil, err
}
conf.Protocol = []string{"binary"}
conf.Location.Path = fmt.Sprintf(
c.Prefix+"/session/%s/conn/%s%s/websocket", c.ID, service, action,
)
//TODO use root cert
if useTls {
conf.TlsConfig = c.TLSConfig
}
wsconn, err := websocket.DialConfig(conf)
if err != nil {
return nil, err
}
return wsconn, nil
}
func CompressConnection(conn net.Conn) net.Conn {
src, dst := net.Pipe()
go cpdeflate(src, conn)
go cpflate(conn, src)
return dst
}
func cpflate(dst, src io.ReadWriteCloser) {
w := lz4.NewWriter(dst)
errc := make(chan error)
rbuf := make(chan []byte)
go func() {
for {
buf := make([]byte, 32*512)
rlen, err := src.Read(buf)
if err != nil {
errc <- err
}
rbuf <- buf[0:rlen]
}
}()
loop:
for {
select {
case <-time.Tick(time.Millisecond * 16):
w.Flush()
case buf := <-rbuf:
_, err := w.Write(buf)
if err != nil {
break loop
}
w.Flush()
case <-errc:
break loop
}
}
}
func cpdeflate(dst, src io.ReadWriteCloser) {
r := lz4.NewReader(src)
io.Copy(dst, r)
}
//NetCopy code from io.Copy but with deadline
func NetCopy(dst net.Conn, src net.Conn, deadline time.Duration) (written int64, err error) {
// If the reader has a WriteTo method, use it to do the copy.
// Avoids an allocation and a copy.
if wt, ok := src.(io.WriterTo); ok {
return wt.WriteTo(dst)
}
// Similarly, if the writer has a ReadFrom method, use it to do the copy.
if rt, ok := dst.(io.ReaderFrom); ok {
return rt.ReadFrom(src)
}
buf := make([]byte, 32*1024)
for {
src.SetReadDeadline(time.Now().Add(deadline))
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return written, err
}