total5 0.0.17-1 → 0.0.17-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aimodel.js +4 -0
- package/bin/total5 +1 -1
- package/changelog.txt +6 -0
- package/components.js +1 -1
- package/global.js +2 -0
- package/index.js +11 -10
- package/package.json +1 -1
- package/proxy.js +277 -0
- package/websocket.js +4 -3
- package/proxyclient.js +0 -145
package/aimodel.js
CHANGED
|
@@ -78,6 +78,10 @@ AI.prototype.assistant = function(content, merge) {
|
|
|
78
78
|
return this.message('assistant', content, merge);
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
AI.prototype.tool = function(content, merge) {
|
|
82
|
+
return this.message('tool', content, merge);
|
|
83
|
+
};
|
|
84
|
+
|
|
81
85
|
AI.prototype.promise = function($) {
|
|
82
86
|
const t = this;
|
|
83
87
|
return new Promise(function(resolve, reject) {
|
package/bin/total5
CHANGED
package/changelog.txt
CHANGED
|
@@ -9,6 +9,12 @@
|
|
|
9
9
|
- fixed `mail` priority
|
|
10
10
|
- fixed `kB` and `GB` units in the routing as upload limits
|
|
11
11
|
- `DEBUG` is set according to the `process.env.NODE_ENV` variable
|
|
12
|
+
- fixed server pinging of all WebSocket connections
|
|
13
|
+
- improved code
|
|
14
|
+
- fixed typo in send method data assignment
|
|
15
|
+
- added `PROXYCLIENT(proxyserver, ip:port, [logger])` method
|
|
16
|
+
- added `PROXYSERVER(endpoint, socketendpoint, [logger])` method
|
|
17
|
+
- added `AIMODEL.tool(content, [merge])` method.
|
|
12
18
|
|
|
13
19
|
========================
|
|
14
20
|
0.0.16
|
package/components.js
CHANGED
|
@@ -233,7 +233,7 @@ Instance.prototype.input = function(input, data, callback) {
|
|
|
233
233
|
Instance.prototype.send = function(output, data) {
|
|
234
234
|
let msg = data instanceof Message ? data : this.newmessage();
|
|
235
235
|
msg.output = output;
|
|
236
|
-
msg.data = data;
|
|
236
|
+
msg.data = data;
|
|
237
237
|
msg.send(output);
|
|
238
238
|
};
|
|
239
239
|
|
package/global.js
CHANGED
|
@@ -12,6 +12,8 @@ global.EMIT = (name, a, b, c, d, e, f, g) => F.emit(name, a, b, c, d, e, f, g);
|
|
|
12
12
|
global.OFF = (name, fn) => F.off(name, fn);
|
|
13
13
|
global.ROUTE = F.TRouting.route;
|
|
14
14
|
global.PROXY = F.TRouting.proxy;
|
|
15
|
+
global.PROXYCLIENT = F.TProxy.client;
|
|
16
|
+
global.PROXYSERVER = F.TProxy.server;
|
|
15
17
|
global.print = console.log;
|
|
16
18
|
global.Utils = F.TUtils;
|
|
17
19
|
global.LOAD = F.load;
|
package/index.js
CHANGED
|
@@ -1148,10 +1148,10 @@ F.console = function() {
|
|
|
1148
1148
|
if (!F.config.$imprint)
|
|
1149
1149
|
return;
|
|
1150
1150
|
|
|
1151
|
-
|
|
1152
|
-
|
|
1151
|
+
let memory = process.memoryUsage();
|
|
1152
|
+
let nodemodules = require.resolve('./index');
|
|
1153
1153
|
|
|
1154
|
-
nodemodules =
|
|
1154
|
+
nodemodules = F.Path.dirname(nodemodules);
|
|
1155
1155
|
|
|
1156
1156
|
print('====================================================');
|
|
1157
1157
|
print('PID : ' + process.pid);
|
|
@@ -1171,19 +1171,19 @@ F.console = function() {
|
|
|
1171
1171
|
// global.THREAD && print('Thread : ' + global.THREAD);
|
|
1172
1172
|
print('====================================================');
|
|
1173
1173
|
F.config.$root && print('Root : ' + F.config.$root);
|
|
1174
|
-
print('Directory : ' + process.cwd());
|
|
1175
|
-
print('node_modules : ' + nodemodules);
|
|
1174
|
+
print('Directory : ' + F.Path.join(process.cwd(), F.Path.sep));
|
|
1175
|
+
print('node_modules : ' + F.Path.join(nodemodules, F.Path.sep));
|
|
1176
1176
|
print('====================================================\n');
|
|
1177
1177
|
|
|
1178
1178
|
if (!F.isWorker && F.server) {
|
|
1179
1179
|
|
|
1180
|
-
|
|
1180
|
+
let hostname = F.unixsocket ? ('Socket: ' + F.unixsocket) : '{2}://{0}:{1}/'.format(F.config.$ip, F.config.$port, F.isHTTPS ? 'https' : 'http');
|
|
1181
1181
|
|
|
1182
1182
|
if (!F.unixsocket && F.ip === '0.0.0.0') {
|
|
1183
|
-
|
|
1183
|
+
let ni = F.Os.networkInterfaces();
|
|
1184
1184
|
if (ni.en0) {
|
|
1185
|
-
for (
|
|
1186
|
-
|
|
1185
|
+
for (let i = 0; i < ni.en0.length; i++) {
|
|
1186
|
+
let nii = ni.en0[i];
|
|
1187
1187
|
// nii.family === 'IPv6' ||
|
|
1188
1188
|
if (nii.family === 'IPv4') {
|
|
1189
1189
|
hostname += '\n{2}://{0}:{1}/'.format(nii.address, F.port, F.isHTTPS ? 'https' : 'http');
|
|
@@ -1215,7 +1215,7 @@ F.loadservices = function() {
|
|
|
1215
1215
|
flow.service(F.internal.ticks);
|
|
1216
1216
|
}
|
|
1217
1217
|
|
|
1218
|
-
if (F.internal.ticks == 6 || F.internal.ticks == 12)
|
|
1218
|
+
if (F.internal.ticks == 3 || F.internal.ticks == 6 || F.internal.ticks == 12)
|
|
1219
1219
|
F.TWebSocket.ping();
|
|
1220
1220
|
|
|
1221
1221
|
// 1 minute
|
|
@@ -3025,6 +3025,7 @@ process.on('message', function(msg, h) {
|
|
|
3025
3025
|
F.proxy = F.TRouting.proxy;
|
|
3026
3026
|
|
|
3027
3027
|
// Needed "F"
|
|
3028
|
+
F.TProxy = require('./proxy');
|
|
3028
3029
|
F.TFlow = require('./flow');
|
|
3029
3030
|
F.TTMS = require('./tms');
|
|
3030
3031
|
F.TCMS = require('./cms');
|
package/package.json
CHANGED
package/proxy.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
// Total.js client for Proxy
|
|
2
|
+
// The MIT License
|
|
3
|
+
// Copyright 2017-2026 (c) Peter Širka <petersirka@gmail.com>
|
|
4
|
+
|
|
5
|
+
// Client implementation
|
|
6
|
+
(function() {
|
|
7
|
+
|
|
8
|
+
const BUFFER_INIT = Buffer.alloc(1);
|
|
9
|
+
BUFFER_INIT.writeUInt8(0);
|
|
10
|
+
|
|
11
|
+
const BUFFER_DATA = Buffer.alloc(1);
|
|
12
|
+
BUFFER_DATA.writeUInt8(1);
|
|
13
|
+
|
|
14
|
+
const BUFFER_END = Buffer.alloc(1);
|
|
15
|
+
BUFFER_END.writeUInt8(2);
|
|
16
|
+
|
|
17
|
+
const BUFFER_ERROR = Buffer.alloc(1);
|
|
18
|
+
BUFFER_END.writeUInt8(3);
|
|
19
|
+
|
|
20
|
+
const BUFFER_ABORT = Buffer.alloc(1);
|
|
21
|
+
BUFFER_END.writeUInt8(4);
|
|
22
|
+
|
|
23
|
+
const REG_ANSI = /\x1B\[[0-?]*[ -/]*[@-~]/g;
|
|
24
|
+
const Pending = {};
|
|
25
|
+
const EnabledColors = process.stdout.isTTY && process.stdout.hasColors();
|
|
26
|
+
|
|
27
|
+
function help() {
|
|
28
|
+
console.log('Proxy client arguments:');
|
|
29
|
+
console.log('client [WSS proxy server] [port or ip:port]');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function log() {
|
|
33
|
+
|
|
34
|
+
let arr = [new Date().format('yyyy-MM-dd HH:mm:ss')];
|
|
35
|
+
|
|
36
|
+
for (let index in arguments)
|
|
37
|
+
arr.push(arguments[index]);
|
|
38
|
+
|
|
39
|
+
console.log.apply(console, arr);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function connect(Source, Target, logger) {
|
|
43
|
+
|
|
44
|
+
let client = F.websocketclient();
|
|
45
|
+
|
|
46
|
+
client.options.type = 'binary';
|
|
47
|
+
client.connect(Target.replace(/^http/i, 'ws'));
|
|
48
|
+
|
|
49
|
+
client.on('open', function() {
|
|
50
|
+
logger && logger('[OK]', 'Proxy client successfully connected to ' + Target);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
client.on('close', function() {
|
|
54
|
+
logger && logger('[NO]', 'Proxy client is disconnected.');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
client.on('message', function(msg) {
|
|
58
|
+
|
|
59
|
+
let type = msg.readInt8(0);
|
|
60
|
+
let id = msg.readInt32BE(1);
|
|
61
|
+
let data = msg.slice(5);
|
|
62
|
+
let req = Pending[id];
|
|
63
|
+
|
|
64
|
+
switch (type) {
|
|
65
|
+
case 0: // init
|
|
66
|
+
|
|
67
|
+
let tmp = JSON.parse(data.toString('utf8'));
|
|
68
|
+
let opt = {};
|
|
69
|
+
opt.hostname = Source[0];
|
|
70
|
+
opt.port = Source[1];
|
|
71
|
+
opt.headers = tmp.headers;
|
|
72
|
+
opt.headers['x-proxy'] = 'Total.js';
|
|
73
|
+
opt.headers.host = Source[0] + ':' + Source[1];
|
|
74
|
+
opt.method = tmp.method;
|
|
75
|
+
opt.path = tmp.url;
|
|
76
|
+
|
|
77
|
+
logger && logger('[' + opt.method + ']',tmp.ip, opt.path);
|
|
78
|
+
|
|
79
|
+
req = Total.Http.request(opt);
|
|
80
|
+
req.ID = Buffer.alloc(4);
|
|
81
|
+
req.ID.writeUInt32BE(id);
|
|
82
|
+
|
|
83
|
+
Pending[id] = req;
|
|
84
|
+
|
|
85
|
+
req.on('error', function(err) {
|
|
86
|
+
delete Pending[id];
|
|
87
|
+
client.send(Buffer.concat([BUFFER_ERROR, req.ID, Buffer.from(err.toString(), 'utf8')]));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
req.on('abort', function() {
|
|
91
|
+
delete Pending[id];
|
|
92
|
+
client.send(Buffer.concat([BUFFER_ABORT, req.ID]));
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
req.on('response', function(res) {
|
|
96
|
+
|
|
97
|
+
let obj = {};
|
|
98
|
+
obj.status = res.statusCode;
|
|
99
|
+
obj.headers = res.headers;
|
|
100
|
+
|
|
101
|
+
delete Pending[id];
|
|
102
|
+
res.req = req;
|
|
103
|
+
client.send(Buffer.concat([BUFFER_INIT, req.ID, Buffer.from(JSON.stringify(obj))]));
|
|
104
|
+
res.on('data', chunk => client.send(Buffer.concat([BUFFER_DATA, req.ID, chunk])));
|
|
105
|
+
|
|
106
|
+
res.on('error', function(err) {
|
|
107
|
+
delete Pending[id];
|
|
108
|
+
client.send(Buffer.concat([BUFFER_ERROR, req.ID, Buffer.from(err.toString(), 'utf8')]));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
res.on('abort', function() {
|
|
112
|
+
delete Pending[id];
|
|
113
|
+
client.send(Buffer.concat([BUFFER_ABORT, req.ID]));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
res.on('end', () => client.send(Buffer.concat([BUFFER_END, req.ID])));
|
|
117
|
+
});
|
|
118
|
+
break;
|
|
119
|
+
|
|
120
|
+
case 1: // data
|
|
121
|
+
req && req.write(data);
|
|
122
|
+
break;
|
|
123
|
+
|
|
124
|
+
case 2: // end
|
|
125
|
+
req && req.end();
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return client;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
exports.client = function (proxyserver, port, logger = log) {
|
|
135
|
+
|
|
136
|
+
let Target = proxyserver;
|
|
137
|
+
let Source = port;
|
|
138
|
+
|
|
139
|
+
if (!Target || !Source) {
|
|
140
|
+
help();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
Source = Source.split(':');
|
|
145
|
+
|
|
146
|
+
if (!Source[1]) {
|
|
147
|
+
Source[1] = +Source[0];
|
|
148
|
+
Source[0] = '127.0.0.1';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return connect(Source, Target, logger);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
})();
|
|
155
|
+
|
|
156
|
+
// Server implementation
|
|
157
|
+
(function() {
|
|
158
|
+
|
|
159
|
+
const BUFFER_INIT = Buffer.alloc(1);
|
|
160
|
+
BUFFER_INIT.writeUInt8(0);
|
|
161
|
+
|
|
162
|
+
const BUFFER_DATA = Buffer.alloc(1);
|
|
163
|
+
BUFFER_DATA.writeUInt8(1);
|
|
164
|
+
|
|
165
|
+
const BUFFER_END = Buffer.alloc(1);
|
|
166
|
+
BUFFER_END.writeUInt8(2);
|
|
167
|
+
|
|
168
|
+
var Messages = 0;
|
|
169
|
+
var Client = null;
|
|
170
|
+
var Pending = {};
|
|
171
|
+
var Logger = null;
|
|
172
|
+
|
|
173
|
+
function log() {
|
|
174
|
+
let arr = [new Date().format('yyyy-MM-dd HH:mm:ss')];
|
|
175
|
+
for (let index in arguments)
|
|
176
|
+
arr.push(arguments[index]);
|
|
177
|
+
console.log.apply(console, arr);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Handles communication between the server and proxy client
|
|
181
|
+
function socket($) {
|
|
182
|
+
|
|
183
|
+
$.autodestroy();
|
|
184
|
+
|
|
185
|
+
$.on('open', function(client) {
|
|
186
|
+
if (Client) {
|
|
187
|
+
let err = 'Proxy client is already in use';
|
|
188
|
+
Logger && Logger('[OK]', err);
|
|
189
|
+
Client.close(4001, err);
|
|
190
|
+
} else {
|
|
191
|
+
Logger && Logger('[OK]', 'Proxy client is connected');
|
|
192
|
+
Client = client;
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
$.on('close', function(client) {
|
|
197
|
+
if (Client == client) {
|
|
198
|
+
Client = null;
|
|
199
|
+
Logger && Logger('[NO]', 'Proxy client is disconnected.');
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
$.on('message', function(client, msg) {
|
|
204
|
+
|
|
205
|
+
let id = msg.readInt32BE(1);
|
|
206
|
+
let type = msg.readInt8(0);
|
|
207
|
+
let data = msg.slice(5);
|
|
208
|
+
let ctrl = Pending[id];
|
|
209
|
+
|
|
210
|
+
switch (type) {
|
|
211
|
+
case 0: // init
|
|
212
|
+
let tmp = JSON.parse(data.toString('utf8'));
|
|
213
|
+
delete tmp.headers.connection;
|
|
214
|
+
delete tmp.headers['content-length'];
|
|
215
|
+
ctrl.res.writeHead(tmp.status, tmp.headers);
|
|
216
|
+
break;
|
|
217
|
+
case 1: // data
|
|
218
|
+
ctrl.res.write(data);
|
|
219
|
+
break;
|
|
220
|
+
case 2: // end
|
|
221
|
+
delete Pending[id];
|
|
222
|
+
ctrl.res.end();
|
|
223
|
+
break;
|
|
224
|
+
case 3: // error
|
|
225
|
+
delete Pending[id];
|
|
226
|
+
ctrl.res.end();
|
|
227
|
+
break;
|
|
228
|
+
case 4: // abort
|
|
229
|
+
delete Pending[id];
|
|
230
|
+
ctrl.res.end();
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Handles communication between the client and server
|
|
238
|
+
function proxy(ctrl) {
|
|
239
|
+
|
|
240
|
+
if (!Client) {
|
|
241
|
+
ctrl.invalid('Not yet available.');
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
var obj = {};
|
|
246
|
+
obj.headers = ctrl.req.headers;
|
|
247
|
+
obj.url = ctrl.req.url;
|
|
248
|
+
obj.method = ctrl.req.method;
|
|
249
|
+
obj.ip = ctrl.ip;
|
|
250
|
+
|
|
251
|
+
let buffer = Buffer.from(JSON.stringify(obj), 'utf8');
|
|
252
|
+
let id = Buffer.alloc(4);
|
|
253
|
+
let index = Messages++;
|
|
254
|
+
|
|
255
|
+
// For answer
|
|
256
|
+
Pending[index] = ctrl;
|
|
257
|
+
|
|
258
|
+
// Write request id
|
|
259
|
+
id.writeUInt32BE(index);
|
|
260
|
+
|
|
261
|
+
// Sent initial data
|
|
262
|
+
Client.send(Buffer.concat([BUFFER_INIT, id, buffer]));
|
|
263
|
+
|
|
264
|
+
// Send data
|
|
265
|
+
ctrl.req.on('data', chunk => Client.send(Buffer.concat([BUFFER_DATA, id, chunk])));
|
|
266
|
+
|
|
267
|
+
// Send empty buffer
|
|
268
|
+
ctrl.req.on('end', () => Client.send(Buffer.concat([BUFFER_END, id])));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
exports.server = function(endpoint = '/', socketendpoint = '/', logger = log) {
|
|
272
|
+
PROXY(endpoint, proxy).check(ctrl => ctrl.method !== 'SOCKET');
|
|
273
|
+
ROUTE('SOCKET {0} @binary <100MB'.format(socketendpoint), socket);
|
|
274
|
+
Logger = logger;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
})();
|
package/websocket.js
CHANGED
|
@@ -605,10 +605,11 @@ Controller.prototype.senddeflate = function() {
|
|
|
605
605
|
};
|
|
606
606
|
|
|
607
607
|
Controller.prototype.ping = function(ts) {
|
|
608
|
-
|
|
608
|
+
let ctrl = this;
|
|
609
|
+
ctrl.$ping = ts || Date.now();
|
|
610
|
+
ctrl.latency = null;
|
|
609
611
|
if (!ctrl.isclosed) {
|
|
610
612
|
try {
|
|
611
|
-
ctrl.$ping = ts || Date.now();
|
|
612
613
|
ctrl.socket.write(getWebSocketFrame(0, 'PING', 0x09, false, ctrl.masking));
|
|
613
614
|
} catch (e) {
|
|
614
615
|
// Socket error
|
|
@@ -1366,10 +1367,10 @@ WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
|
|
|
1366
1367
|
|
|
1367
1368
|
WebSocketClient.prototype.ping = function(timeout) {
|
|
1368
1369
|
var self = this;
|
|
1370
|
+
self.$ping = Date.now();
|
|
1369
1371
|
if (!self.isclosed && !self.timeout) {
|
|
1370
1372
|
self.timeout = setTimeout(wsclient_timeout, timeout || 3000, self);
|
|
1371
1373
|
self.socket.write(getWebSocketFrame(0, 'PING', 0x09, false, self.options.masking));
|
|
1372
|
-
self.$ping = Date.now();
|
|
1373
1374
|
}
|
|
1374
1375
|
return self;
|
|
1375
1376
|
};
|
package/proxyclient.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
// Total.js client for Proxy
|
|
2
|
-
// The MIT License
|
|
3
|
-
// Copyright 2017-2023 (c) Peter Širka <petersirka@gmail.com>
|
|
4
|
-
|
|
5
|
-
const BUFFER_INIT = Buffer.alloc(1);
|
|
6
|
-
BUFFER_INIT.writeUInt8(0);
|
|
7
|
-
|
|
8
|
-
const BUFFER_DATA = Buffer.alloc(1);
|
|
9
|
-
BUFFER_DATA.writeUInt8(1);
|
|
10
|
-
|
|
11
|
-
const BUFFER_END = Buffer.alloc(1);
|
|
12
|
-
BUFFER_END.writeUInt8(2);
|
|
13
|
-
|
|
14
|
-
const BUFFER_ERROR = Buffer.alloc(1);
|
|
15
|
-
BUFFER_END.writeUInt8(3);
|
|
16
|
-
|
|
17
|
-
const BUFFER_ABORT = Buffer.alloc(1);
|
|
18
|
-
BUFFER_END.writeUInt8(4);
|
|
19
|
-
|
|
20
|
-
const Pending = {};
|
|
21
|
-
|
|
22
|
-
function help() {
|
|
23
|
-
console.log('Proxy client arguments:');
|
|
24
|
-
console.log('client [WSS proxy server] [port or ip:port]');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function log() {
|
|
28
|
-
|
|
29
|
-
let arr = [new Date().format('yyyy-MM-dd HH:mm:ss')];
|
|
30
|
-
|
|
31
|
-
for (let index in arguments)
|
|
32
|
-
arr.push(arguments[index]);
|
|
33
|
-
|
|
34
|
-
console.log.apply(console, arr);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function connect(Source, Target) {
|
|
38
|
-
|
|
39
|
-
F.websocketclient(function(client) {
|
|
40
|
-
|
|
41
|
-
client.options.type = 'binary';
|
|
42
|
-
client.connect(Target.replace(/^http/i, 'ws'));
|
|
43
|
-
|
|
44
|
-
client.on('open', function() {
|
|
45
|
-
log('\x1b[42m[OK]\x1b[0m', 'Proxy client successfully connected to \x1b[41m{0}\x1b[0m'.format(Target));
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
client.on('close', function() {
|
|
49
|
-
log('\x1b[41m[NO]\x1b[0m', 'Proxy client is disconnected.');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
client.on('message', function(msg) {
|
|
53
|
-
|
|
54
|
-
let type = msg.readInt8(0);
|
|
55
|
-
let id = msg.readInt32BE(1);
|
|
56
|
-
let data = msg.slice(5);
|
|
57
|
-
let req = Pending[id];
|
|
58
|
-
|
|
59
|
-
switch (type) {
|
|
60
|
-
case 0: // init
|
|
61
|
-
|
|
62
|
-
let tmp = JSON.parse(data.toString('utf8'));
|
|
63
|
-
let opt = {};
|
|
64
|
-
opt.hostname = Source[0];
|
|
65
|
-
opt.port = Source[1];
|
|
66
|
-
opt.headers = tmp.headers;
|
|
67
|
-
opt.headers['x-proxy'] = 'Total.js';
|
|
68
|
-
opt.headers.host = Source[0] + ':' + Source[1];
|
|
69
|
-
opt.method = tmp.method;
|
|
70
|
-
opt.path = tmp.url;
|
|
71
|
-
|
|
72
|
-
log('\x1b[43m[' + opt.method + ']\x1b[0m', '\x1b[34m' + tmp.ip + '\x1b[0m', opt.path);
|
|
73
|
-
|
|
74
|
-
req = Total.Http.request(opt);
|
|
75
|
-
req.ID = Buffer.alloc(4);
|
|
76
|
-
req.ID.writeUInt32BE(id);
|
|
77
|
-
|
|
78
|
-
Pending[id] = req;
|
|
79
|
-
|
|
80
|
-
req.on('error', function(err) {
|
|
81
|
-
delete Pending[id];
|
|
82
|
-
client.send(Buffer.concat([BUFFER_ERROR, req.ID, Buffer.from(err.toString(), 'utf8')]));
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
req.on('abort', function() {
|
|
86
|
-
delete Pending[id];
|
|
87
|
-
client.send(Buffer.concat([BUFFER_ABORT, req.ID]));
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
req.on('response', function(res) {
|
|
91
|
-
|
|
92
|
-
let obj = {};
|
|
93
|
-
obj.status = res.statusCode;
|
|
94
|
-
obj.headers = res.headers;
|
|
95
|
-
|
|
96
|
-
delete Pending[id];
|
|
97
|
-
res.req = req;
|
|
98
|
-
client.send(Buffer.concat([BUFFER_INIT, req.ID, Buffer.from(JSON.stringify(obj))]));
|
|
99
|
-
res.on('data', chunk => client.send(Buffer.concat([BUFFER_DATA, req.ID, chunk])));
|
|
100
|
-
|
|
101
|
-
res.on('error', function(err) {
|
|
102
|
-
delete Pending[id];
|
|
103
|
-
client.send(Buffer.concat([BUFFER_ERROR, req.ID, Buffer.from(err.toString(), 'utf8')]));
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
res.on('abort', function() {
|
|
107
|
-
delete Pending[id];
|
|
108
|
-
client.send(Buffer.concat([BUFFER_ABORT, req.ID]));
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
res.on('end', () => client.send(Buffer.concat([BUFFER_END, req.ID])));
|
|
112
|
-
});
|
|
113
|
-
break;
|
|
114
|
-
|
|
115
|
-
case 1: // data
|
|
116
|
-
req && req.write(data);
|
|
117
|
-
break;
|
|
118
|
-
|
|
119
|
-
case 2: // end
|
|
120
|
-
req && req.end();
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
exports.init = function (endpoint, port) {
|
|
129
|
-
let Target = endpoint;
|
|
130
|
-
let Source = port;
|
|
131
|
-
|
|
132
|
-
if (!Target || !Source) {
|
|
133
|
-
help();
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
Source = Source.split(':');
|
|
138
|
-
|
|
139
|
-
if (!Source[1]) {
|
|
140
|
-
Source[1] = +Source[0];
|
|
141
|
-
Source[0] = '127.0.0.1';
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
connect(Source, Target);
|
|
145
|
-
};
|