forked from AlexRuzin/websock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
724 lines (610 loc) · 20.5 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
* Copyright (c) 2017 AlexRuzin ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package websock
import (
"io"
"time"
"bytes"
"strings"
"crypto"
"strconv"
"net"
"net/url"
"net/http"
"io/ioutil"
"github.com/AlexRuzin/util"
"github.com/wsddn/go-ecdh"
"github.com/tatsushid/go-fastping"
)
type NetChannelClient struct {
/* Server connection parameters */
inputURI string
port int16
path string
host string
controllerURL *url.URL
/* Circuit tests */
testCircuit bool
pingServer bool
/* Identifiers for the client */
clientId []byte
clientIdString string
/* ECDH secret */
secret []byte
/* States and configuration */
flags FlagVal
connected bool
/* Data coming in from the server */
responseData *bytes.Buffer
/* Request elements */
transport *http.Transport
request *http.Request
getReqKilled bool
/* Main config */
config *ProtocolConfig
}
type transferUnit struct {
GlobalIP string
LocalIP string
TimeStamp string
ClientID string
Data []byte
DecryptedSum string
Direction FlagVal
Flags FlagVal
}
func (f *NetChannelClient) Read(p []byte) (read int, err error) {
read, err = f.readInternal(p)
if err != io.EOF {
return 0, err
}
return
}
func (f *NetChannelClient) Write(p []byte) (written int, err error) {
written, err = f.writeInternal(p)
if err != io.EOF {
return 0, err
}
return written, io.EOF
}
func (f *NetChannelClient) Len() int {
if f.connected == false {
return 0
} else if f.responseData == nil {
return 0
}
return f.responseData.Len()
}
func (f *NetChannelClient) Wait(timeoutMilliseconds time.Duration) (responseLen int, err error) {
responseLen = 0
err = WAIT_TIMEOUT_REACHED
for i := timeoutMilliseconds / 100; i != 0; i -= 1 {
if f.connected == false {
err = WAIT_CLOSED
responseLen = -1
break
}
if f.Len() > 0 {
responseLen = f.Len()
err = WAIT_DATA_RECEIVED
break
}
util.Sleep(100 * time.Millisecond)
}
return
}
func BuildChannel(gateURI string, flags FlagVal) (*NetChannelClient, error) {
if (flags & FLAG_DO_NOT_USE) == 1 {
return nil, util.RetErrStr("Invalid flag: FLAG_DO_NOT_USE")
}
if !((flags & FLAG_ENCRYPT) > 1) {
return nil, util.RetErrStr("FLAG_ENCRYPT is a mandatory switch for the `flags` parameter")
}
/*
* Parse the primary configuration and set it as a reference to masterConfig
*/
var (
tmpConfig *ProtocolConfig
configParseStatus error
)
tmpConfig, configParseStatus = parseConfig()
if configParseStatus != nil {
return nil, configParseStatus
}
if testCharSetPKE(tmpConfig.PostBodyKeyCharset) == false {
return nil, util.RetErrStr("PANIC: POST_BODY_KEY_CHARSET contains non-unique elements")
}
mainURL, err := url.Parse(gateURI)
if err != nil {
return nil, err
}
if mainURL.Scheme != "http" {
return nil, util.RetErrStr("HTTP scheme must not use TLS")
}
port, _ := strconv.Atoi(mainURL.Port())
var ioChannel = &NetChannelClient{
controllerURL: mainURL,
inputURI: gateURI,
port: int16(port),
flags: flags,
connected: false,
path: mainURL.Path,
host: mainURL.Host,
secret: nil,
responseData: nil,
transport: nil,
request: nil,
config: tmpConfig,
testCircuit: false,
pingServer: false,
getReqKilled: false,
}
if (flags & FLAG_TEST_CIRCUIT) > 0 {
ioChannel.testCircuit = true
}
if (flags & FLAG_PING_SERVER) > 0 {
ioChannel.pingServer = true
}
if (ioChannel.flags & FLAG_DEBUG) > 1 {
util.DebugOut("NetChannelClient structure initialized")
}
return ioChannel, nil
}
func (f *NetChannelClient) InitializeCircuit() error {
/*
* Determine if we can pull anything from the target URI
*/
if f.pingServer == true {
if checkServerStatus := checkServerAliveStatus(f.controllerURL.String()); checkServerStatus != ERROR_SERVER_UP {
return checkServerStatus
}
}
/* Transmit and receive public keys, generate secret */
if pkeStatus := f.initializePKE(); pkeStatus != nil {
return pkeStatus
}
f.connected = true
/*
* Test the circuit
*/
if f.testCircuit == true {
if circuitStatus := f.testCircuitRoutine(); circuitStatus != nil {
f.Close()
return circuitStatus
}
}
/*
* Keep sending POSTs until some data is written to the controller write interface
*/
checkWriteThread(f)
util.Sleep(100 * time.Millisecond)
return nil
}
func checkServerAliveStatus(URI string) error {
var (
parsedURI *url.URL
parseStatus error
remoteAddr *net.IPAddr
reachable bool = false
)
parsedURI, parseStatus = url.Parse(URI)
if parseStatus != nil {
return ERROR_INVALID_URI
}
if parsedURI.Hostname() != "" {
remoteAddr, parseStatus = net.ResolveIPAddr("ip4:icmp", parsedURI.Hostname())
if parseStatus != nil {
return ERROR_INVALID_URI
}
} else {
q := net.ParseIP(parsedURI.Host)
remoteAddr = &net.IPAddr{
IP: q,
Zone: "",
}
}
/* Test ping */
const numPings = 5
if serverStatus := func (addr net.IPAddr) error {
var ping = fastping.NewPinger()
ping.AddIPAddr(&addr)
ping.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
reachable = true
}
for i := 0; i != numPings; i += 1 {
if err := ping.Run(); err != nil {
reachable = false
}
}
if reachable == false {
return ERROR_SERVER_DOWN
}
return ERROR_SERVER_UP
} (*remoteAddr); serverStatus != ERROR_SERVER_UP {
return serverStatus
}
var (
response *http.Response
responseStatus error
)
if response, responseStatus = http.Get(URI); responseStatus != nil || response == nil {
return ERROR_SERVER_DOWN
}
return ERROR_SERVER_UP
}
func checkWriteThread(client *NetChannelClient) {
/*
* Periodically check to see if the server has any data to be sent to the
* socket. This is the primary i/o subsystem
*/
go func (client *NetChannelClient) {
for {
client.getReqKilled = false
read, _, err := client.writeStream(nil, FLAG_CHECK_STREAM_DATA)
if err == io.EOF && read == 0 {
/* Connection is closed due to a Write() request */
if (client.flags & FLAG_DEBUG) > 0 && read == 0 && client.getReqKilled == false {
util.DebugOut("[" + time.Now().String() + "] FLAG_CHECK_STREAM_DATA: Keep-alive -- no data")
}
util.Sleep(100 * time.Millisecond)
continue
} else if read != 0 {
/* Data inbound from server */
util.Sleep(100 * time.Millisecond)
continue
}
/* Some other error -- i.e. the server terminates the socket */
client.Close()
return
}
} (client)
}
func (f *NetChannelClient) initializePKE() (error) {
/*
* Generate keypair, construct HTTP POST request parameter map
*/
var ( /* Output reserved for keypair/post request generate method */
curve ecdh.ECDH
request map[string]string
curveStatus error = nil
clientPrivateKey crypto.PrivateKey
)
curve, request, clientPrivateKey, curveStatus = f.generateCurvePostRequest()
if curveStatus != nil {
return curveStatus
}
/* Perform HTTP TX, receive the public key from the server */
body, initStatus := f.sendTransmission(f.config.HTTPVerb/* POST */, f.inputURI, request)
if initStatus != nil {
return initStatus
}
if len(body) == 0 {
return util.RetErrStr("server has returned a null length public key")
}
/*
* Decode the public key returned by the server and create a secret key
*/
f.secret, initStatus = f.decodeServerPubkeyGenSecret(body, clientPrivateKey, curve)
if initStatus != nil {
return initStatus
}
if (f.flags & FLAG_DEBUG) > 0 {
util.DebugOut("Client-side secret:")
util.DebugOutHex(f.secret)
}
return nil
}
func (f *NetChannelClient) Close() {
f.writeStream(nil, FLAG_TERMINATE_CONNECTION)
f.connected = false
}
func (f *NetChannelClient) readInternal(p []byte) (int, error) {
if f.connected == false {
return 0, util.RetErrStr("readInternal(): client not connected")
}
if f.Len() == 0 {
return 0, io.EOF
}
read, err := f.readStream(p, 0)
if err != io.EOF {
return 0, err
}
return read, io.EOF
}
func (f *NetChannelClient) writeInternal(p []byte) (int, error) {
if f.connected == false {
return 0, util.RetErrStr("writeInternal(): client not connected")
}
if f.transport != nil {
f.transport.CancelRequest(f.request)
}
_, wrote, err := f.writeStream(p, 0)
if err != io.EOF {
return 0, err
}
return wrote, io.EOF
}
func (f *NetChannelClient) testCircuitRoutine() error {
if _, _, err := f.writeStream(nil, FLAG_TEST_CONNECTION); err != io.EOF {
return err
}
if f.responseData.Len() == 0 {
return util.RetErrStr("testCircuit() failed on the server side")
}
var responseData = make([]byte, f.responseData.Len())
read, err := f.readStream(responseData, FLAG_TEST_CONNECTION)
if err != io.EOF || read != len(f.config.TestStream) {
return util.RetErrStr("testCircuit() invalid response from server side")
}
if !util.IsAsciiPrintable(string(responseData)) ||
strings.Compare(string(responseData), f.config.TestStream) != 0 {
return util.RetErrStr("testCircuit() data corruption from server side")
}
return nil
}
func (f *NetChannelClient) writeStream(rawData []byte, flags FlagVal) (read int, written int, err error) {
if !((flags & FLAG_TEST_CONNECTION) > 0) && f.connected == false {
return 0,0, util.RetErrStr("writeStream(): client not connected")
}
if (flags & FLAG_TERMINATE_CONNECTION) > 0 {
rawData, _ = returnCommandString(FLAG_TERMINATE_CONNECTION, *f.config)
}
if rawData == nil && (flags & FLAG_CHECK_STREAM_DATA) > 0 {
rawData, _ = returnCommandString(FLAG_CHECK_STREAM_DATA, *f.config)
}
/* Generate parameters */
var (
parmMap = make(map[string]string)
genPostStatus error
)
if parmMap, genPostStatus = f.generatePOSTrequest(rawData, flags); genPostStatus != nil {
return 0, 0, genPostStatus
}
/* Transmit */
var body []byte
body, sendStatus := f.sendTransmission(f.config.HTTPVerb, f.inputURI, parmMap)
if sendStatus == nil && body == nil {
/* This is the case in which Write() abruptly forces an HTTP channel to close */
return 0,0, io.EOF
}
read = len(body)
written = len(rawData)
if read != 0 {
/* Decode the body (TransferUnit) and store in NetChannelClient.ResponseData */
if _, err = f.processHTTPresponse(body, flags); err != nil {
return 0, 0, err
}
return read, written, io.EOF
}
return 0, written, io.EOF
}
func (f *NetChannelClient) processHTTPresponse(body []byte, flags FlagVal) (written int, err error) {
/* Decode the body (TransferUnit) and store in NetChannelClient.ResponseData */
clientId, rawData, _, err := decryptData(string(body), f.secret)
if err != nil {
return 0, err
}
if strings.Compare(clientId, f.clientIdString) != 0 {
return 0, util.RetErrStr("Invalid server response")
}
if (f.flags & FLAG_COMPRESS) > 0 && !((flags & FLAG_TEST_CONNECTION) > 0) {
var (
streamStatus error = nil
decompressed []byte
)
decompressed, streamStatus = util.DecompressStream(rawData)
if streamStatus != nil && len(decompressed) == 0 {
return 0, err
}
rawData = decompressed
}
/* Write either the compressed or decompressed stream */
if f.responseData == nil {
f.responseData = &bytes.Buffer{}
}
if written, err = f.responseData.Write(rawData); err != nil {
return written, err
}
return written, nil
}
func (f *NetChannelClient) generatePOSTrequest(rawData []byte, flags FlagVal) (map[string]string, error) {
if len(rawData) == 0 && flags != 0 {
var (
err error
tmp []byte
)
if tmp, err = returnCommandString(flags, *f.config); err == nil {
rawData = tmp
}
}
if len(rawData) == 0 {
return nil, util.RetErrStr("No input data")
}
var (
encrypted []byte
processStatus error
)
if encrypted, processStatus = f.compressEncryptData(rawData, flags); processStatus != nil {
return nil, processStatus
}
var parmMap = make(map[string]string)
/* key = b64(ClientIdString) value = b64(JSON(<data>)) */
value := util.B64E(encrypted)
key := util.B64E([]byte(f.clientIdString))
parmMap[key] = value
return parmMap, nil
}
func (f *NetChannelClient) compressEncryptData(rawData []byte, flags FlagVal) (encrypted []byte, err error) {
err = nil
/* Check for high-entropy compression inflation and generate a compression stream */
var (
compressionFlag FlagVal = 0
deflateStatus error = nil
txData = rawData
)
if (f.flags & FLAG_COMPRESS) > 0 && len(rawData) > util.GetCompressedSize(rawData) &&
!((flags & FLAG_TEST_CONNECTION) > 0) /* Compression is not required for testing the circuit */ {
compressionFlag |= FLAG_COMPRESS
txData, deflateStatus = util.CompressStream(txData)
if deflateStatus != nil {
return nil, deflateStatus
}
}
f.flags |= FLAG_DIRECTION_TO_SERVER
encrypted, err = encryptData(txData, f.secret, FLAG_DIRECTION_TO_SERVER, compressionFlag, f.clientIdString)
if err != nil {
return nil, err
}
return
}
func (f *NetChannelClient) readStream(p []byte, flags FlagVal) (read int, err error) {
if !((flags & FLAG_TEST_CONNECTION) > 0) && f.connected == false {
return 0, util.RetErrStr("readStream: client not connected")
}
if f.responseData == nil {
return 0, io.EOF
}
read = f.responseData.Len()
if read == 0 {
return 0, io.EOF
}
f.responseData.Read(p)
return read, io.EOF
}
func (f* NetChannelClient) sendTransmission(verb string, URI string, params map[string]string) ([]byte, error) {
var (
req *http.Request
resp *http.Response
reqError error
)
if req, reqError = f.generateHTTPheaders(URI, verb, params); reqError != nil {
return nil, reqError
}
/*
* This method invokes a thread which waits for a Write() call, and terminates the read request
* coming from the client to server. Consequently, the only type of request which ought to be
* terminated is a FLAG_CHECK_STREAM_DATA request, which, upon termination, does not contain
* data. If it does contain data, then a Write() was not called
*/
resp, stopStatus := f.waitForWriteTxCancel(req)
if resp == nil && stopStatus == nil{
/*
* Graceful termination of the FLAG_CHECK_STREAM_DATA request. There is no response, as a
* consequence of the abrupt termination of the stream. There is no data to be returned
*/
return nil, nil
}
/*
* The FLAG_CHECK_STREAM_DATA request was terminated, but not by force due to Write(), but because
* the server supplemented data on the stream. Check for a normal response
*/
if resp.Status != "200 OK" {
return nil, util.RetErrStr("HTTP 200 OK not returned")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func (f *NetChannelClient) waitForWriteTxCancel(httpRequestInput *http.Request) (*http.Response, error) {
var (
tr = &http.Transport{}
httpClient = &http.Client{Transport: tr}
respIo = make(chan *http.Response)
)
f.request = httpRequestInput
f.transport = tr
go func (httpRequest *http.Request) {
util.Sleep(10 * time.Millisecond)
var (
response *http.Response
rxStatus error = nil
)
if response, rxStatus = httpClient.Do(httpRequest); rxStatus != nil {
/* If cancelled, the channel will close and the HTTP request will be cleaned up */
close(respIo)
return
}
/*
* In the instance of a regular transmit, this object should be passed, and this method
* should ultimately server no purpose.
*/
respIo <- response
} (f.request)
resp, ok := <- respIo
if !ok {
/* Forced write request -- the request is cancelled, so permit another transmit */
f.transport = nil
f.request = nil
f.getReqKilled = true
return nil, nil
} else {
defer close(respIo)
}
/*
* The timeout for FLAG_CHECK_STREAM_DATA was not reached, and the server has transmitted data
* in the meantime, meaning a response body should exist.
*/
return resp, nil
}
func (f *NetChannelClient) generateHTTPheaders(URI string, verb string,
formMap map[string]string) (*http.Request, error) {
form := url.Values{}
for k, v := range formMap {
form.Set(k, v)
}
formEncoded := form.Encode()
var (
req *http.Request
reqStatus error
)
if req, reqStatus = http.NewRequest(verb /* POST */, URI, strings.NewReader(formEncoded)); reqStatus != nil {
return nil, reqStatus
}
/*
* "application/x-www-form-urlencoded"
*
* Most common ever Content-Type
*/
req.Header.Set("Content-Type", f.config.ContentType)
req.Header.Set("Connection", "close")
/*
* "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
* (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
*
* Most common ever UA
*/
req.Header.Set("User-Agent", f.config.UserAgent)
/* Set the domain/IP */
var (
parsedURI *url.URL
parseError error
)
if parsedURI, parseError = url.Parse(URI); parseError != nil {
return nil, parseError
}
req.Header.Set("Host", parsedURI.Hostname())
return req, nil
}
/* EOF */