Skip to content

Commit ebb0680

Browse files
authoredMar 6, 2025··
Merge pull request #18 from Will-create/main
Added proxy client to cli
2 parents bc42d46 + 057bb32 commit ebb0680

File tree

2 files changed

+157
-6
lines changed

2 files changed

+157
-6
lines changed
 

‎bin/total5

+11-6
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ function load(args) {
3636
setTimeout(load, 10, ARGS);
3737

3838
FUNC.help = function() {
39-
console.log('translate : it makes a resource file with a localized dictionary from the current directory');
40-
console.log('minify <filename> : it minifies .js, .css and .html files');
41-
console.log('bundle <filename> : it makes a bundle from the current directory');
42-
console.log('extract <filename> : it extracts a bundle into the current directory');
43-
console.log('edit <url?id=project> : it opens remote editing of the current directory with the Total.js Code Editor');
44-
console.log('8000 : it starts a web server on port "8000" for the current directory');
39+
console.log('translate : it makes a resource file with a localized dictionary from the current directory');
40+
console.log('minify <filename> : it minifies .js, .css and .html files');
41+
console.log('bundle <filename> : it makes a bundle from the current directory');
42+
console.log('extract <filename> : it extracts a bundle into the current directory');
43+
console.log('edit <url?id=project> : it opens remote editing of the current directory with the Total.js Code Editor');
44+
console.log('proxyclient <server_endpoint> <local_port> : It connects a local port with a remote totaljs proxy server');
45+
console.log('8000 : it starts a web server on port "8000" for the current directory');
4546
done();
4647
};
4748

@@ -241,6 +242,10 @@ FUNC.server = function(port) {
241242
F.http({ load: 'none', port: port || 8000 });
242243
};
243244

245+
FUNC.proxyclient = function(endpoint, port) {
246+
require('../proxyclient').init(endpoint, port);
247+
};
248+
244249
FUNC.edit = function(url) {
245250
if (!url)
246251
throw new Error('Invalid URL address');

‎proxyclient.js

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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(/^http/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

Comments
 (0)
Please sign in to comment.