total5 0.0.17-2 → 0.0.17-4
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 +3 -3
- package/changelog.txt +3 -0
- package/global.js +2 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/proxy.js +277 -0
- 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
|
@@ -41,7 +41,7 @@ FUNC.help = function() {
|
|
|
41
41
|
console.log('bundle <filename> : it makes a bundle from the current directory');
|
|
42
42
|
console.log('extract <filename> : it extracts a bundle into the current directory');
|
|
43
43
|
console.log('edit <url?id=project> : it opens remote editing of the current directory with the Total.js Code Editor');
|
|
44
|
-
console.log('
|
|
44
|
+
console.log('proxy <server_endpoint> <local_port> : It connects a local port with a remote totaljs proxy server');
|
|
45
45
|
console.log('8000 : it starts a web server on port "8000" for the current directory');
|
|
46
46
|
done();
|
|
47
47
|
};
|
|
@@ -242,8 +242,8 @@ FUNC.server = function(port) {
|
|
|
242
242
|
F.http({ load: 'none', port: port || 8000 });
|
|
243
243
|
};
|
|
244
244
|
|
|
245
|
-
FUNC.
|
|
246
|
-
require('../
|
|
245
|
+
FUNC.proxy = function(endpoint, port) {
|
|
246
|
+
require('../proxy').client(endpoint, port);
|
|
247
247
|
};
|
|
248
248
|
|
|
249
249
|
FUNC.edit = function(url) {
|
package/changelog.txt
CHANGED
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
- fixed server pinging of all WebSocket connections
|
|
13
13
|
- improved code
|
|
14
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.
|
|
15
18
|
|
|
16
19
|
========================
|
|
17
20
|
0.0.16
|
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
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/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
|
-
};
|