1
+
2
+ // Total.js client for Proxy
3
+ // The MIT License
4
+ // Copyright 2017-2023 (c) Peter Širka <petersirka@gmail.com>
5
+
6
+ const BUFFER_INIT = Buffer . alloc ( 1 ) ;
7
+ BUFFER_INIT . writeUInt8 ( 0 ) ;
8
+
9
+ const BUFFER_DATA = Buffer . alloc ( 1 ) ;
10
+ BUFFER_DATA . writeUInt8 ( 1 ) ;
11
+
12
+ const BUFFER_END = Buffer . alloc ( 1 ) ;
13
+ BUFFER_END . writeUInt8 ( 2 ) ;
14
+
15
+ const BUFFER_ERROR = Buffer . alloc ( 1 ) ;
16
+ BUFFER_END . writeUInt8 ( 3 ) ;
17
+
18
+ const BUFFER_ABORT = Buffer . alloc ( 1 ) ;
19
+ BUFFER_END . writeUInt8 ( 4 ) ;
20
+
21
+ const Pending = { } ;
22
+
23
+ function help ( ) {
24
+ console . log ( 'Proxy client arguments:' ) ;
25
+ console . log ( 'client [WSS proxy server] [port or ip:port]' ) ;
26
+ }
27
+
28
+ function log ( ) {
29
+
30
+ let arr = [ new Date ( ) . format ( 'yyyy-MM-dd HH:mm:ss' ) ] ;
31
+
32
+ for ( let index in arguments )
33
+ arr . push ( arguments [ index ] ) ;
34
+
35
+ console . log . apply ( console , arr ) ;
36
+ }
37
+
38
+ function connect ( Source , Target ) {
39
+
40
+ F . websocketclient ( function ( client ) {
41
+
42
+ client . options . type = 'binary' ;
43
+ client . connect ( Target . replace ( / ^ h t t p / i, 'ws' ) ) ;
44
+
45
+ client . on ( 'open' , function ( ) {
46
+ log ( '\x1b[42m[OK]\x1b[0m' , 'Proxy client successfully connected to \x1b[41m{0}\x1b[0m' . format ( Target ) ) ;
47
+ } ) ;
48
+
49
+ client . on ( 'close' , function ( ) {
50
+ log ( '\x1b[41m[NO]\x1b[0m' , 'Proxy client is disconnected.' ) ;
51
+ } ) ;
52
+
53
+ client . on ( 'message' , function ( msg ) {
54
+
55
+ let type = msg . readInt8 ( 0 ) ;
56
+ let id = msg . readInt32BE ( 1 ) ;
57
+ let data = msg . slice ( 5 ) ;
58
+ let req = Pending [ id ] ;
59
+
60
+ switch ( type ) {
61
+ case 0 : // init
62
+
63
+ let tmp = JSON . parse ( data . toString ( 'utf8' ) ) ;
64
+ let opt = { } ;
65
+ opt . hostname = Source [ 0 ] ;
66
+ opt . port = Source [ 1 ] ;
67
+ opt . headers = tmp . headers ;
68
+ opt . headers [ 'x-proxy' ] = 'Total.js' ;
69
+ opt . headers . host = Source [ 0 ] + ':' + Source [ 1 ] ;
70
+ opt . method = tmp . method ;
71
+ opt . path = tmp . url ;
72
+
73
+ log ( '\x1b[43m[' + opt . method + ']\x1b[0m' , '\x1b[34m' + tmp . ip + '\x1b[0m' , opt . path ) ;
74
+
75
+ req = Total . Http . request ( opt ) ;
76
+ req . ID = Buffer . alloc ( 4 ) ;
77
+ req . ID . writeUInt32BE ( id ) ;
78
+
79
+ Pending [ id ] = req ;
80
+
81
+ req . on ( 'error' , function ( err ) {
82
+ delete Pending [ id ] ;
83
+ client . send ( Buffer . concat ( [ BUFFER_ERROR , req . ID , Buffer . from ( err . toString ( ) , 'utf8' ) ] ) ) ;
84
+ } ) ;
85
+
86
+ req . on ( 'abort' , function ( ) {
87
+ delete Pending [ id ] ;
88
+ client . send ( Buffer . concat ( [ BUFFER_ABORT , req . ID ] ) ) ;
89
+ } ) ;
90
+
91
+ req . on ( 'response' , function ( res ) {
92
+
93
+ let obj = { } ;
94
+ obj . status = res . statusCode ;
95
+ obj . headers = res . headers ;
96
+
97
+ delete Pending [ id ] ;
98
+ res . req = req ;
99
+ client . send ( Buffer . concat ( [ BUFFER_INIT , req . ID , Buffer . from ( JSON . stringify ( obj ) ) ] ) ) ;
100
+ res . on ( 'data' , chunk => client . send ( Buffer . concat ( [ BUFFER_DATA , req . ID , chunk ] ) ) ) ;
101
+
102
+ res . on ( 'error' , function ( err ) {
103
+ delete Pending [ id ] ;
104
+ client . send ( Buffer . concat ( [ BUFFER_ERROR , req . ID , Buffer . from ( err . toString ( ) , 'utf8' ) ] ) ) ;
105
+ } ) ;
106
+
107
+ res . on ( 'abort' , function ( ) {
108
+ delete Pending [ id ] ;
109
+ client . send ( Buffer . concat ( [ BUFFER_ABORT , req . ID ] ) ) ;
110
+ } ) ;
111
+
112
+ res . on ( 'end' , ( ) => client . send ( Buffer . concat ( [ BUFFER_END , req . ID ] ) ) ) ;
113
+ } ) ;
114
+ break ;
115
+
116
+ case 1 : // data
117
+ req && req . write ( data ) ;
118
+ break ;
119
+
120
+ case 2 : // end
121
+ req && req . end ( ) ;
122
+ break ;
123
+ }
124
+
125
+ } ) ;
126
+ } ) ;
127
+ }
128
+
129
+ exports . init = function ( endpoint , port ) {
130
+ let Target = endpoint ;
131
+ let Source = port ;
132
+
133
+ if ( ! Target || ! Source ) {
134
+ help ( ) ;
135
+ return ;
136
+ }
137
+
138
+ Source = Source . split ( ':' ) ;
139
+
140
+ if ( ! Source [ 1 ] ) {
141
+ Source [ 1 ] = + Source [ 0 ] ;
142
+ Source [ 0 ] = '127.0.0.1' ;
143
+ }
144
+
145
+ connect ( Source , Target ) ;
146
+ } ;
0 commit comments