-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (161 loc) · 4.76 KB
/
main.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const process = require('process');
const fs = require('fs');
const { SSHTunnelProxy, KeypairStorage } = require('./lib/index.js');
const keypairStorage = new KeypairStorage();
const homedir = require('os').homedir();
var debug = false;
var exec = [];
var remote_host = null;
var proxies = [];
var configs = null;
var host = null;
var port = null;
var username = null;
var password = null;
var forwardOut = [];
var forwardIn = [];
var ngrokAPIKey = null;
var privateKey = null;
var service = null;
var account = null;
var shell = null;
function get_key_from_file(filename) {
try {
return fs.readFileSync(filename);
} catch (err) {
console.log('Error loading key:', err);
}
}
function get_config(filename) {
try {
configs = JSON.parse(fs.readFileSync(filename), 'utf8');
} catch (err) {
console.log('Error loading config file:', err);
}
}
function get_default_config() {
try {
configs = JSON.parse(fs.readFileSync(homedir + '/.config/ssh_tunnel_proxy/config.json'), 'utf8');
} catch (err) {
console.log('Error loading config file:', err);
}
}
function main(args) {
// parse command line args
for (let i = 0; i < args.length; i++) {
const arg = args[i].split('=');
const cmd = arg[0];
const value = arg[1];
switch (cmd) {
case '--a':
account = value;
break;
case '-c':
case '--config':
if (value) get_config(value);
else get_default_config();
break;
case '-d':
case '--debug':
debug = true;
break;
case '-e':
case '--exec':
exec.push(value);
break;
case '-h':
case '--host':
host = value;
break;
case '-p':
case '--port':
port = value;
break;
case '-P':
case '--password':
password = value;
break;
case '-k':
case '--key':
privateKey = value;
break;
case '-L':
case '--LocalForward':
forwardOut.push(value);
break;
case '-n':
case '--ngrok':
ngrokAPIKey = value;
break;
case '-r':
case '--remote':
remote_host = value;
break;
case '-R':
case '--RemoteForward':
forwardIn.push(value);
break;
case '-s':
case '--service':
service = value;
break;
case '-S':
case '--shell':
shell = true;
break;
case '-u':
case '--username':
username = value;
break;
default:
if (i > 1) console.log('Invalid argument:', arg);
}
}
// prevent process from exiting
process.stdin.resume();
// if no config file specified, build config from args
if (!configs) {
var config = {
enabled: true,
username: username,
password: password,
host: host,
port: port,
proxy_ports: forwardOut,
remote_ports: forwardIn,
service_name: service,
server_name: account,
ngrok_api: ngrokAPIKey,
private_key: privateKey
};
configs = [config];
}
// process each tunnel config in ssh proxy tunnel list
configs.forEach(async config => {
// skip tunnel if not enabled
if (!config.enabled) return;
// if remote host specified, only select tunnel for remote host
if (remote_host && remote_host !== config.alias) return;
// if system key storage name specified get key from system keychain
if (config.server_name) {
const service_name = config.service_name || 'ssh_tunnel_proxy';
config.private_key = await keypairStorage.get_keypair(service_name, config.server_name);
}
// if private key filename specified, read private key
else if (config.private_key) {
config.private_key = get_key_from_file(config.private_key);
}
if (shell) {
config.shell = true;
}
if (exec.length > 0) {
config.exec = exec;
}
// create new tunnel and start connection
var sshTunnelProxy = new SSHTunnelProxy();
proxies.push(sshTunnelProxy);
sshTunnelProxy.debug_en = debug;
sshTunnelProxy.connectSSH(config);
});
}
main(process.argv);