biz-a-cli 2.1.0 → 2.2.0
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/bin/hub.js +114 -0
- package/package.json +6 -2
package/bin/hub.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const yargs = require('yargs');
|
|
4
|
+
const IDLE_SOCKET_TIMEOUT_MILLISECONDS = 1000 * 30;
|
|
5
|
+
|
|
6
|
+
let argv = yargs
|
|
7
|
+
.usage('Usage: $0 --server [string] --subdomain [string] --hostname [string] --port [number]')
|
|
8
|
+
.options('s', {
|
|
9
|
+
alias: 'server',
|
|
10
|
+
describe: '(Required) Tunnel server endpoint'
|
|
11
|
+
})
|
|
12
|
+
.options('sub', {
|
|
13
|
+
alias: 'subdomain',
|
|
14
|
+
describe: '(Required) Public URL the tunnel server is forwarding to us'
|
|
15
|
+
})
|
|
16
|
+
.options('h', {
|
|
17
|
+
alias: 'hostname',
|
|
18
|
+
default: '127.0.0.1',
|
|
19
|
+
describe: 'Address of local server for forwarding over socket-tunnel'
|
|
20
|
+
})
|
|
21
|
+
.options('p', {
|
|
22
|
+
alias: 'port',
|
|
23
|
+
describe: '(Required) Port of local server for forwarding over socket-tunnel'
|
|
24
|
+
})
|
|
25
|
+
.argv;
|
|
26
|
+
|
|
27
|
+
if (argv.help) {
|
|
28
|
+
yargs.showHelp();
|
|
29
|
+
process.exit();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!argv['server'] || !argv['subdomain'] || !argv['port']) {
|
|
33
|
+
for (var key in ['server', 'subdomain', 'port']) {
|
|
34
|
+
if (argv[key]) continue;
|
|
35
|
+
|
|
36
|
+
console.log('Error: Required option, but nothing found');
|
|
37
|
+
|
|
38
|
+
yargs.showHelp();
|
|
39
|
+
|
|
40
|
+
process.exit();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const net = require('node:net');
|
|
46
|
+
const ss = require('socket.io-stream');
|
|
47
|
+
let socket = require('socket.io-client')(argv['server']);
|
|
48
|
+
|
|
49
|
+
socket.on('connect', () => {
|
|
50
|
+
console.log(new Date() + ': connected to socket server');
|
|
51
|
+
console.log(new Date() + ': requesting subdomain ' + argv['subdomain'] + ' via ' + argv['server']);
|
|
52
|
+
|
|
53
|
+
socket.emit('createTunnel', argv['subdomain'], (err) => {
|
|
54
|
+
if (err) {
|
|
55
|
+
console.log(new Date() + ': [error] ' + err);
|
|
56
|
+
reject(err);
|
|
57
|
+
} else {
|
|
58
|
+
console.log(new Date() + ': registered with server successfully');
|
|
59
|
+
|
|
60
|
+
// clean and concat requested url
|
|
61
|
+
let url;
|
|
62
|
+
let subdomain = argv['subdomain'].toString();
|
|
63
|
+
let server = argv['server'].toString();
|
|
64
|
+
|
|
65
|
+
if (server.includes('https://')) {
|
|
66
|
+
url = `https://${subdomain}.${server.slice(8)}`;
|
|
67
|
+
} else if (server.includes('http://')) {
|
|
68
|
+
url = `http://${subdomain}.${server.slice(7)}`;
|
|
69
|
+
} else {
|
|
70
|
+
url = `https://${subdomain}.${server}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// resolve promise with requested URL
|
|
74
|
+
resolve(url);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
socket.on('incomingClient', (clientId) => {
|
|
79
|
+
// console.log(clientId, 'incoming clientId')
|
|
80
|
+
// console.log(`connecting to ${argv['hostname']}:${argv['port']}`)
|
|
81
|
+
let client = net.connect(argv['port'], argv['hostname']);
|
|
82
|
+
// let client = net.createConnection(argv['port'], argv['hostname']);
|
|
83
|
+
|
|
84
|
+
client.on('connect', () => {
|
|
85
|
+
console.log(`client connected to ${argv['hostname']}:${argv['port']}`)
|
|
86
|
+
// console.log('client:', client)
|
|
87
|
+
let s = ss.createStream();
|
|
88
|
+
s.pipe(client).pipe(s);
|
|
89
|
+
|
|
90
|
+
s.on('end', () => {
|
|
91
|
+
client.destroy();
|
|
92
|
+
});
|
|
93
|
+
// console.log(client, '<===client')
|
|
94
|
+
ss(socket).emit(clientId, s);
|
|
95
|
+
|
|
96
|
+
// s.on('data', (chunk)=>{
|
|
97
|
+
// console.log(chunk.toString(), 'chunk')
|
|
98
|
+
// })
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
client.setTimeout(IDLE_SOCKET_TIMEOUT_MILLISECONDS);
|
|
102
|
+
client.on('timeout', () => {
|
|
103
|
+
client.end();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
client.on('error', () => {
|
|
107
|
+
// handle connection refusal (create a stream and immediately close it)
|
|
108
|
+
let s = ss.createStream();
|
|
109
|
+
ss(socket).emit(clientId, s);
|
|
110
|
+
s.end();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "biz-a-cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "bin/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,12 +10,16 @@
|
|
|
10
10
|
"license": "ISC",
|
|
11
11
|
"bin": {
|
|
12
12
|
"uploadbiza": "./bin/index.js",
|
|
13
|
-
"proxy": "./bin/proxy.js"
|
|
13
|
+
"proxy": "./bin/proxy.js",
|
|
14
|
+
"hub": "./bin/hub.js"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"axios": "^0.27.2",
|
|
17
18
|
"cors": "^2.8.5",
|
|
18
19
|
"express": "^4.18.2",
|
|
20
|
+
"net": "^1.0.2",
|
|
21
|
+
"socket.io-client": "^4.7.1",
|
|
22
|
+
"socket.io-stream": "^0.9.1",
|
|
19
23
|
"yargs": "^17.4.1"
|
|
20
24
|
}
|
|
21
25
|
}
|