biz-a-cli 2.3.0 → 2.3.2
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 +33 -33
- package/bin/index.js +69 -49
- package/bin/proxy.js +14 -11
- package/package.json +8 -7
- package/proxyController.js +83 -0
package/bin/hub.js
CHANGED
|
@@ -1,50 +1,65 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import yargs from 'yargs';
|
|
4
|
+
import net from 'node:net';
|
|
5
|
+
import io from "socket.io-client";
|
|
6
|
+
|
|
7
|
+
import { createRequire } from "module";
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const ss = require('socket.io-stream'); //SCY: Temporary, next will be replaced with import
|
|
10
|
+
|
|
4
11
|
const IDLE_SOCKET_TIMEOUT_MILLISECONDS = 1000 * 30;
|
|
5
12
|
|
|
6
|
-
|
|
7
|
-
.usage('Usage: $0
|
|
13
|
+
const argv = yargs(process.argv.slice(2))
|
|
14
|
+
.usage('Usage: $0 [options]')
|
|
8
15
|
.options('s', {
|
|
9
16
|
alias: 'server',
|
|
10
|
-
|
|
17
|
+
default: 'https://biz-a.herokuapp.com',
|
|
18
|
+
describe: '(Required) Tunnel server endpoint',
|
|
19
|
+
type: 'string',
|
|
20
|
+
demandOption: true
|
|
11
21
|
})
|
|
12
22
|
.options('sub', {
|
|
13
23
|
alias: 'subdomain',
|
|
14
|
-
describe: '(Required) Public URL the tunnel server is forwarding to us'
|
|
24
|
+
describe: '(Required) Public URL the tunnel server is forwarding to us',
|
|
25
|
+
type: 'string',
|
|
26
|
+
demandOption: true
|
|
15
27
|
})
|
|
16
28
|
.options('h', {
|
|
17
29
|
alias: 'hostname',
|
|
18
30
|
default: '127.0.0.1',
|
|
19
|
-
describe: 'Address of local server for forwarding over socket-tunnel'
|
|
31
|
+
describe: 'Address of local server for forwarding over socket-tunnel',
|
|
32
|
+
type: 'string',
|
|
33
|
+
demandOption: true
|
|
20
34
|
})
|
|
21
35
|
.options('p', {
|
|
22
36
|
alias: 'port',
|
|
23
|
-
|
|
37
|
+
default: 212,
|
|
38
|
+
describe: '(Required) Port of local server for forwarding over socket-tunnel',
|
|
39
|
+
type: 'number',
|
|
40
|
+
demandOption: true
|
|
24
41
|
})
|
|
25
42
|
.argv;
|
|
26
43
|
|
|
27
44
|
if (argv.help) {
|
|
28
|
-
yargs.showHelp();
|
|
45
|
+
yargs().showHelp();
|
|
29
46
|
process.exit();
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
if (!argv['server'] || !argv['subdomain'] || !argv['port']) {
|
|
33
|
-
for (
|
|
50
|
+
for (let key in ['server', 'subdomain', 'port']) {
|
|
34
51
|
if (argv[key]) continue;
|
|
35
52
|
|
|
36
53
|
console.log('Error: Required option, but nothing found');
|
|
37
54
|
|
|
38
|
-
yargs.showHelp();
|
|
39
|
-
|
|
55
|
+
yargs().showHelp();
|
|
40
56
|
process.exit();
|
|
41
57
|
}
|
|
42
58
|
}
|
|
43
59
|
|
|
44
|
-
return new Promise((resolve, reject) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let socket = require('socket.io-client')(argv['server']);
|
|
60
|
+
// return new Promise((resolve, reject) => {
|
|
61
|
+
(() => new Promise((resolve, reject) => {
|
|
62
|
+
let socket = io(argv['server']);
|
|
48
63
|
|
|
49
64
|
socket.on('connect', () => {
|
|
50
65
|
console.log(new Date() + ': connected to socket server');
|
|
@@ -59,16 +74,9 @@ return new Promise((resolve, reject) => {
|
|
|
59
74
|
|
|
60
75
|
// clean and concat requested url
|
|
61
76
|
let url;
|
|
62
|
-
let subdomain = argv['subdomain'].toString();
|
|
77
|
+
// let subdomain = argv['subdomain'].toString();
|
|
63
78
|
let server = argv['server'].toString();
|
|
64
79
|
|
|
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
80
|
url = server;
|
|
73
81
|
|
|
74
82
|
// resolve promise with requested URL
|
|
@@ -78,25 +86,17 @@ return new Promise((resolve, reject) => {
|
|
|
78
86
|
});
|
|
79
87
|
socket.on('incomingClient', (clientId) => {
|
|
80
88
|
console.log(clientId, 'incoming clientId')
|
|
81
|
-
// console.log(`connecting to ${argv['hostname']}:${argv['port']}`)
|
|
82
89
|
let client = net.connect(argv['port'], argv['hostname']);
|
|
83
|
-
// let client = net.createConnection(argv['port'], argv['hostname']);
|
|
84
90
|
|
|
85
91
|
client.on('connect', () => {
|
|
86
92
|
console.log(`client connected to ${argv['hostname']}:${argv['port']}`)
|
|
87
|
-
// console.log('client:', client)
|
|
88
93
|
let s = ss.createStream();
|
|
89
94
|
s.pipe(client).pipe(s);
|
|
90
95
|
|
|
91
96
|
s.on('end', () => {
|
|
92
97
|
client.destroy();
|
|
93
98
|
});
|
|
94
|
-
// console.log(client, '<===client')
|
|
95
99
|
ss(socket).emit(clientId, s);
|
|
96
|
-
|
|
97
|
-
// s.on('data', (chunk)=>{
|
|
98
|
-
// console.log(chunk.toString(), 'chunk')
|
|
99
|
-
// })
|
|
100
100
|
})
|
|
101
101
|
|
|
102
102
|
client.setTimeout(IDLE_SOCKET_TIMEOUT_MILLISECONDS);
|
|
@@ -111,5 +111,5 @@ return new Promise((resolve, reject) => {
|
|
|
111
111
|
s.end();
|
|
112
112
|
});
|
|
113
113
|
});
|
|
114
|
-
|
|
115
|
-
})
|
|
114
|
+
// })
|
|
115
|
+
}))()
|
package/bin/index.js
CHANGED
|
@@ -1,94 +1,114 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import yargs from "yargs";
|
|
4
|
+
import axios from "axios";
|
|
5
|
+
import { promises as fs } from "fs";
|
|
6
|
+
import { runInThisContext } from "vm";
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
const options = yargs(process.argv.slice(2))
|
|
9
|
+
.option("s", {
|
|
10
|
+
alias: "server",
|
|
11
|
+
describe: "Server URL",
|
|
12
|
+
type: "string",
|
|
13
|
+
demandOption: true
|
|
14
|
+
})
|
|
15
|
+
.option("i", {
|
|
16
|
+
alias: "dbindex",
|
|
17
|
+
describe: "database index",
|
|
18
|
+
type: "number",
|
|
19
|
+
demandOption: true
|
|
20
|
+
})
|
|
21
|
+
// .option("d", {
|
|
22
|
+
// alias: "dir",
|
|
23
|
+
// describe: "Directory",
|
|
24
|
+
// type: "string",
|
|
25
|
+
// demandOption: false
|
|
26
|
+
// })
|
|
27
|
+
.option("sub", {
|
|
28
|
+
alias: "subdomain",
|
|
29
|
+
describe: "Subdomain",
|
|
30
|
+
type: "string",
|
|
31
|
+
demandOption: false
|
|
32
|
+
})
|
|
14
33
|
.argv;
|
|
15
34
|
|
|
16
|
-
const sendFile = async (filename, data)=>{
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
const sendFile = async (filename, data) => {
|
|
36
|
+
const baseUrl = 'fina/rest/TOrmMethod/%22setTemplate%22'
|
|
37
|
+
const url = options.sub ?
|
|
38
|
+
`${options.server}/hub/${baseUrl}?subdomain=${options.sub}` :
|
|
39
|
+
`http://${options.server}:212/${baseUrl}`
|
|
40
|
+
|
|
41
|
+
const headers = {
|
|
42
|
+
'Content-Type': 'text/plain'
|
|
43
|
+
}
|
|
44
|
+
const param = { _parameters: [options.dbindex, filename, data] };
|
|
45
|
+
return await axios.post(url, param, { headers: headers });
|
|
21
46
|
}
|
|
22
47
|
|
|
23
|
-
function getParam(funcName){
|
|
24
|
-
|
|
48
|
+
function getParam(funcName) {
|
|
49
|
+
let params = funcName.split('(');
|
|
25
50
|
return params[1].split(')')[0].split(',');
|
|
26
51
|
}
|
|
27
52
|
|
|
28
|
-
function replacer(key,value){
|
|
29
|
-
if (typeof value == 'function'){
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
53
|
+
function replacer(key, value) {
|
|
54
|
+
if (typeof value == 'function') {
|
|
55
|
+
let arr = value.toString().replace(/(\r\n|\n|\r)/gm, "°").split("°");
|
|
56
|
+
if (arr.length < 3) throw 'Function must be minimal 3 lines';
|
|
57
|
+
return [
|
|
58
|
+
'window.Function',
|
|
59
|
+
getParam(arr[0]),
|
|
60
|
+
arr.slice(1, arr.length - 1)
|
|
61
|
+
];
|
|
37
62
|
} else {
|
|
38
|
-
|
|
39
|
-
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
40
65
|
}
|
|
41
66
|
|
|
42
|
-
const readTemplateFile= async (filename)=>{
|
|
67
|
+
const readTemplateFile = async (filename) => {
|
|
43
68
|
const data = await fs.readFile(filename);
|
|
44
69
|
const ext = filename.split('.');
|
|
45
70
|
|
|
46
|
-
if (ext[ext.length-1] == 'js'){
|
|
71
|
+
if (ext[ext.length - 1] == 'js') {
|
|
47
72
|
let result;
|
|
48
|
-
try{
|
|
49
|
-
const vmResult =
|
|
73
|
+
try {
|
|
74
|
+
const vmResult = runInThisContext(data);
|
|
50
75
|
result = JSON.stringify(vmResult(), replacer);
|
|
51
|
-
} catch(
|
|
76
|
+
} catch (error) {
|
|
52
77
|
const fn = `file://${process.cwd()}\\${filename}`;
|
|
53
|
-
const lib = await import(fn);
|
|
78
|
+
const lib = await import(fn);
|
|
54
79
|
result = JSON.stringify(lib.default(), replacer);
|
|
55
80
|
}
|
|
56
|
-
|
|
57
|
-
//console.log(result);
|
|
81
|
+
|
|
58
82
|
return Buffer.from(result).toString('hex');
|
|
59
|
-
|
|
60
|
-
|
|
83
|
+
|
|
61
84
|
} else {
|
|
62
85
|
return Buffer.from(data).toString('hex');
|
|
63
86
|
//return JSON.stringify(JSON.parse(data));
|
|
64
87
|
}
|
|
65
88
|
}
|
|
66
89
|
|
|
67
|
-
const loopFiles = async (dir) =>{
|
|
68
|
-
try{
|
|
90
|
+
const loopFiles = async (dir) => {
|
|
91
|
+
try {
|
|
69
92
|
const files = await fs.readdir(dir);
|
|
70
|
-
for (const file of files){
|
|
93
|
+
for (const file of files) {
|
|
71
94
|
//const p = path.join(dir, file);
|
|
72
95
|
if (file == 'package.json' || file == 'package-lock.json') continue;
|
|
73
96
|
|
|
74
97
|
const stat = await fs.stat(file);
|
|
75
98
|
|
|
76
99
|
if (stat.isFile()) {
|
|
77
|
-
|
|
78
100
|
const data = await readTemplateFile(file);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//console.log(data.toString('hex'));
|
|
82
101
|
const res = await sendFile(file, data.toString('hex'));
|
|
83
|
-
if (res.data.success){
|
|
102
|
+
if (res.data.success) {
|
|
84
103
|
console.log(`file: ${file} uploaded`);
|
|
85
104
|
} else {
|
|
86
105
|
console.error(res.data.error);
|
|
87
106
|
}
|
|
88
|
-
}
|
|
107
|
+
}
|
|
89
108
|
}
|
|
90
|
-
} catch (e){
|
|
91
|
-
console.error(e.response && e.response.data ? e.response.data: e);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
// console.error(e.response && e.response.data ? e.response.data : e);
|
|
111
|
+
console.error(e.response?.data ? e.response.data : e);
|
|
92
112
|
}
|
|
93
113
|
}
|
|
94
114
|
|
package/bin/proxy.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const app
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const yargs = require('yargs');
|
|
3
|
+
import express from 'express';
|
|
4
|
+
import cors from 'cors';
|
|
5
|
+
const app = express();
|
|
6
|
+
import { proxy } from '../proxyController.js';
|
|
7
|
+
import yargs from 'yargs';
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
const options = yargs(process.argv.slice(2))
|
|
10
|
+
.option('p', {
|
|
11
|
+
alias: 'port',
|
|
12
|
+
describe: 'Port to use',
|
|
13
|
+
type: 'string',
|
|
14
|
+
demandOption: false
|
|
15
|
+
})
|
|
13
16
|
.argv;
|
|
14
17
|
|
|
15
18
|
app.use(cors());
|
|
16
19
|
|
|
17
|
-
app.use('/proxy',
|
|
20
|
+
app.use('/proxy', proxy);
|
|
18
21
|
|
|
19
22
|
const port = 3000;
|
|
20
23
|
|
package/package.json
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "biz-a-cli",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "bin/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
9
|
},
|
|
9
10
|
"author": "Imamatek",
|
|
10
11
|
"license": "ISC",
|
|
11
12
|
"bin": {
|
|
12
|
-
"uploadbiza": "
|
|
13
|
-
"proxy": "
|
|
14
|
-
"hub": "
|
|
13
|
+
"uploadbiza": "bin/index.js",
|
|
14
|
+
"proxy": "bin/proxy.js",
|
|
15
|
+
"hub": "bin/hub.js"
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
17
|
-
"axios": "^
|
|
18
|
+
"axios": "^1.6.2",
|
|
18
19
|
"cors": "^2.8.5",
|
|
19
20
|
"express": "^4.18.2",
|
|
20
21
|
"net": "^1.0.2",
|
|
21
|
-
"socket.io-client": "^4.7.
|
|
22
|
+
"socket.io-client": "^4.7.2",
|
|
22
23
|
"socket.io-stream": "^0.9.1",
|
|
23
|
-
"yargs": "^17.
|
|
24
|
+
"yargs": "^17.7.2"
|
|
24
25
|
}
|
|
25
26
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as https from 'https';
|
|
2
|
+
import * as http from 'http';
|
|
3
|
+
|
|
4
|
+
export function proxy(clientRequest, clientResponse) {
|
|
5
|
+
try {
|
|
6
|
+
const isHttps = url => url.startsWith('https://');
|
|
7
|
+
const hasPort = url => url.indexOf(':') > -1;
|
|
8
|
+
|
|
9
|
+
const targetUrl = clientRequest.query.target ?
|
|
10
|
+
clientRequest.query.target
|
|
11
|
+
: clientRequest.get('target');
|
|
12
|
+
const target = targetUrl.split('/')[2];
|
|
13
|
+
|
|
14
|
+
let parsedHost = targetUrl.split('/').splice(2).splice(0, 1).join('/');
|
|
15
|
+
if (hasPort(target)) parsedHost = parsedHost.split(':')[0];
|
|
16
|
+
let targetPort = hasPort(target) ? target.split(':')[1] : null;
|
|
17
|
+
|
|
18
|
+
const parsedSSL = isHttps(targetUrl) ? https : http;
|
|
19
|
+
|
|
20
|
+
// console.log(clientRequest.headers)
|
|
21
|
+
let svrReqHeader = {};
|
|
22
|
+
if (clientRequest.headers["content-type"]) {
|
|
23
|
+
svrReqHeader["content-type"] = clientRequest.headers["content-type"];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const options = {
|
|
27
|
+
hostname: parsedHost,
|
|
28
|
+
port: isHttps(targetUrl) ?
|
|
29
|
+
// (targetPort ? targetPort : 443) :
|
|
30
|
+
// (targetPort ? targetPort : 80),
|
|
31
|
+
(targetPort || 443) :
|
|
32
|
+
(targetPort || 80),
|
|
33
|
+
path: clientRequest.url,
|
|
34
|
+
method: clientRequest.method,
|
|
35
|
+
headers: svrReqHeader
|
|
36
|
+
};
|
|
37
|
+
if (clientRequest.headers['user-agent']) {
|
|
38
|
+
options['User-Agent'] = clientRequest.headers['user-agent'];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const serverRequest = parsedSSL.request(options, function (serverResponse) {
|
|
42
|
+
try {
|
|
43
|
+
let body = '';
|
|
44
|
+
if (serverResponse.headers['content-type'] && String(serverResponse.headers['content-type']).indexOf('text/html') !== -1) {
|
|
45
|
+
serverResponse.on('data', function (chunk) {
|
|
46
|
+
body += chunk;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
serverResponse.on('end', function () {
|
|
50
|
+
clientResponse.writeHead(serverResponse.statusCode, serverResponse.headers);
|
|
51
|
+
clientResponse.end(body);
|
|
52
|
+
});
|
|
53
|
+
} else {
|
|
54
|
+
serverResponse.pipe(clientResponse, {
|
|
55
|
+
end: true,
|
|
56
|
+
});
|
|
57
|
+
clientResponse.contentType(serverResponse.headers['content-type']);
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.log('exception')
|
|
61
|
+
console.log(error);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
serverRequest.on('error', function (err) {
|
|
66
|
+
// clientResponse.writeHead(503, {Error: err.message});
|
|
67
|
+
console.log('On Error serverRequest Proxy: ', err);
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// console.log(clientRequest.body)
|
|
71
|
+
if (clientRequest.body) {
|
|
72
|
+
serverRequest.write(JSON.stringify(clientRequest.body));
|
|
73
|
+
}
|
|
74
|
+
serverRequest.end();
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.log(err);
|
|
77
|
+
|
|
78
|
+
// serverRequest.end();
|
|
79
|
+
// clientResponse.end();
|
|
80
|
+
serverRequest.destroy();
|
|
81
|
+
// clientResponse.destroy();
|
|
82
|
+
}
|
|
83
|
+
};
|