biz-a-cli 2.3.1 → 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 +32 -17
- package/bin/index.js +67 -55
- 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');
|
|
@@ -96,5 +111,5 @@ return new Promise((resolve, reject) => {
|
|
|
96
111
|
s.end();
|
|
97
112
|
});
|
|
98
113
|
});
|
|
99
|
-
|
|
100
|
-
})
|
|
114
|
+
// })
|
|
115
|
+
}))()
|
package/bin/index.js
CHANGED
|
@@ -1,102 +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
|
-
|
|
14
|
-
.option("
|
|
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
|
+
})
|
|
15
33
|
.argv;
|
|
16
34
|
|
|
17
|
-
const sendFile = async (filename, data)=>{
|
|
35
|
+
const sendFile = async (filename, data) => {
|
|
18
36
|
const baseUrl = 'fina/rest/TOrmMethod/%22setTemplate%22'
|
|
19
|
-
const url = options.sub ?
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
'Content-Type':'text/plain'
|
|
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'
|
|
26
43
|
}
|
|
27
|
-
const param = {_parameters: [options.dbindex, filename, data]};
|
|
28
|
-
return await axios.post(url, param, {headers:headers});
|
|
44
|
+
const param = { _parameters: [options.dbindex, filename, data] };
|
|
45
|
+
return await axios.post(url, param, { headers: headers });
|
|
29
46
|
}
|
|
30
47
|
|
|
31
|
-
function getParam(funcName){
|
|
32
|
-
|
|
48
|
+
function getParam(funcName) {
|
|
49
|
+
let params = funcName.split('(');
|
|
33
50
|
return params[1].split(')')[0].split(',');
|
|
34
51
|
}
|
|
35
52
|
|
|
36
|
-
function replacer(key,value){
|
|
37
|
-
if (typeof value == 'function'){
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
+
];
|
|
45
62
|
} else {
|
|
46
|
-
|
|
47
|
-
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
48
65
|
}
|
|
49
66
|
|
|
50
|
-
const readTemplateFile= async (filename)=>{
|
|
67
|
+
const readTemplateFile = async (filename) => {
|
|
51
68
|
const data = await fs.readFile(filename);
|
|
52
69
|
const ext = filename.split('.');
|
|
53
70
|
|
|
54
|
-
if (ext[ext.length-1] == 'js'){
|
|
71
|
+
if (ext[ext.length - 1] == 'js') {
|
|
55
72
|
let result;
|
|
56
|
-
try{
|
|
57
|
-
const vmResult =
|
|
73
|
+
try {
|
|
74
|
+
const vmResult = runInThisContext(data);
|
|
58
75
|
result = JSON.stringify(vmResult(), replacer);
|
|
59
|
-
} catch(
|
|
76
|
+
} catch (error) {
|
|
60
77
|
const fn = `file://${process.cwd()}\\${filename}`;
|
|
61
|
-
const lib = await import(fn);
|
|
78
|
+
const lib = await import(fn);
|
|
62
79
|
result = JSON.stringify(lib.default(), replacer);
|
|
63
80
|
}
|
|
64
|
-
|
|
65
|
-
//console.log(result);
|
|
81
|
+
|
|
66
82
|
return Buffer.from(result).toString('hex');
|
|
67
|
-
|
|
68
|
-
|
|
83
|
+
|
|
69
84
|
} else {
|
|
70
85
|
return Buffer.from(data).toString('hex');
|
|
71
86
|
//return JSON.stringify(JSON.parse(data));
|
|
72
87
|
}
|
|
73
88
|
}
|
|
74
89
|
|
|
75
|
-
const loopFiles = async (dir) =>{
|
|
76
|
-
try{
|
|
90
|
+
const loopFiles = async (dir) => {
|
|
91
|
+
try {
|
|
77
92
|
const files = await fs.readdir(dir);
|
|
78
|
-
for (const file of files){
|
|
93
|
+
for (const file of files) {
|
|
79
94
|
//const p = path.join(dir, file);
|
|
80
95
|
if (file == 'package.json' || file == 'package-lock.json') continue;
|
|
81
96
|
|
|
82
97
|
const stat = await fs.stat(file);
|
|
83
98
|
|
|
84
99
|
if (stat.isFile()) {
|
|
85
|
-
|
|
86
100
|
const data = await readTemplateFile(file);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
//console.log(data.toString('hex'));
|
|
90
101
|
const res = await sendFile(file, data.toString('hex'));
|
|
91
|
-
if (res.data.success){
|
|
102
|
+
if (res.data.success) {
|
|
92
103
|
console.log(`file: ${file} uploaded`);
|
|
93
104
|
} else {
|
|
94
105
|
console.error(res.data.error);
|
|
95
106
|
}
|
|
96
|
-
}
|
|
107
|
+
}
|
|
97
108
|
}
|
|
98
|
-
} catch (e){
|
|
99
|
-
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);
|
|
100
112
|
}
|
|
101
113
|
}
|
|
102
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
|
+
};
|