forked from monteslu/hsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·129 lines (116 loc) · 3.68 KB
/
cli.js
File metadata and controls
executable file
·129 lines (116 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
import { program, Option } from 'commander';
import { createRequire } from 'module';
import config from './config.js';
import { createConnection } from './hsync.js';
import shell from './shell.js';
// For JSON imports (package.json)
const require = createRequire(import.meta.url);
const pack = require('./package.json');
program
.name(pack.name)
.description(pack.description)
.version(pack.version)
.addOption(new Option('-p, --port <number>', 'port for local webserver', 3000).env('PORT'))
.addOption(
new Option('-d, --dynamic-host <url>', 'host to get a dynamic connection from').env(
'HSYNC_DYNAMIC_HOST'
)
)
.addOption(
new Option('-s, --hsync-server <url>', 'hsync-server location ex: wss://sub.mydomain.com').env(
'HSYNC_SERVER'
)
)
.addOption(
new Option('-S, --hsync-secret <string>', 'password to connect to hsync-server').env(
'HSYNC_SECRET'
)
)
.addOption(
new Option('-L, --listener-local-port <number>', 'local port to open for listener').env(
'HSYNC_LLP'
)
)
.addOption(
new Option('-H, --listener-target-host <url>', 'target host for listener').env('HSYNC_LTH')
)
.addOption(
new Option('-T, --listener-target-port <number>', 'target port for listener').env('HSYNC_LTP')
)
.addOption(
new Option('-R, --relay-inbound-port <number>', 'inbound port for remote relay requests').env(
'HSYNC_RIP'
)
)
.addOption(
new Option(
'-t, --relay-target-host <url>',
'target host for relay to open tcp connection on'
).env('HSYNC_RTH')
)
.addOption(
new Option(
'-P, --relay-target-port <number>',
'target port for relay to open tcp connection on'
).env('HSYNC_RTP')
)
.addOption(
new Option(
'-w, --relay-whitelist <string>',
'whitelist of domains that can access this relay'
).env('HSYNC_RWL')
)
.addOption(
new Option(
'-b, --relay-blacklist <string>',
'blacklist of domains that should be blocked from this relay'
).env('HSYNC_RBL')
)
.addOption(
new Option('-x, --shell', 'shell to localhost and --port for piping data to a listener')
);
program.parse();
const options = program.opts();
if (options.port) {
options.port = Number(options.port);
}
if (options.shell) {
shell(options.port);
} else {
if (options.listenerLocalPort) {
options.listenerLocalPort = options.listenerLocalPort.split(',').map((p) => Number(p));
}
if (options.listenerTargetHost) {
options.listenerTargetHost = options.listenerTargetHost.split(',');
}
if (options.listenerTargetPort) {
options.listenerTargetPort = options.listenerTargetPort.split(',').map((p) => Number(p));
}
if (options.relayInboundPort) {
options.relayInboundPort = options.relayInboundPort.split(',').map((p) => Number(p));
}
if (options.relayTargetHost) {
options.relayTargetHost = options.relayTargetHost.split(',');
}
if (options.relayTargetPort) {
options.relayTargetPort = options.relayTargetPort.split(',').map((p) => Number(p));
}
// console.log('options', options);
let [defaultCon] = config.connections;
defaultCon = { ...defaultCon, ...options };
if (!defaultCon.hsyncServer && !defaultCon.dynamicHost) {
defaultCon.dynamicHost = config.defaultDynamicHost;
}
config.connections[0] = defaultCon;
config.connections.forEach(async (conConfig) => {
const con = await createConnection(conConfig);
console.log();
console.log('Listening for requests on: ', con.webUrl);
console.log('And forwarding to: ', 'http://localhost:' + con.port);
console.log();
console.log('Admin ui at: ', con.webAdmin);
console.log('Secret: ', con.hsyncSecret);
console.log();
});
}