-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathares-novacom.js
executable file
·223 lines (206 loc) · 6.33 KB
/
ares-novacom.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env node
/*
* Copyright (c) 2020-2024 LG Electronics Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
const path = require('path'),
log = require('npmlog'),
nopt = require('nopt'),
async = require('async'),
novacom = require('./../lib/base/novacom'),
commonTools = require('./../lib/base/common-tools');
const version = commonTools.version,
cliControl = commonTools.cliControl,
help = commonTools.help,
setupDevice = commonTools.setupDevice,
appdata = commonTools.appdata,
errHndl = commonTools.errMsg;
const processName = path.basename(process.argv[1], '.js');
process.on('uncaughtException', function(err) {
log.error('uncaughtException', err.toString());
log.verbose('uncaughtException', err.stack);
cliControl.end(-1);
});
const knownOpts = {
// generic options
"help": Boolean,
"hidden-help": Boolean,
"level": ['silly', 'verbose', 'info', 'http', 'warn', 'error'],
"version": Boolean,
// command-specific options
"device-list": Boolean,
"forward": Boolean,
"port": [String, Array],
"getkey": Boolean,
"passphrase": [String, null],
"device": [String, null],
"run": [String, null],
// no shortHands
"put": [String, null],
"get": [String, null]
};
const shortHands = {
// generic aliases
"h": ["--help"],
"hh": ["--hidden-help"],
"v": ["--level", "verbose"],
"V": ["--version"],
// command-specific aliases
"D": ["--device-list"],
"f": ["--forward"],
"p": ["--port"],
"k": ["--getkey"],
"pass": ["--passphrase"],
"d": ["--device"],
"r": ["--run"]
};
const argv = nopt(knownOpts, shortHands, process.argv, 2 /* drop 'node' & 'ares-*.js' */);
log.heading = processName;
log.level = argv.level || 'warn';
log.verbose("argv", argv);
const curConfigData = appdata.getConfig(true);
if (curConfigData.profile !== "tv") {
return finish(errHndl.getErrMsg("NOT_SUPPORT_COMMOND", curConfigData.profile));
}
const options = {
name: argv.device
};
let op;
if (argv['device-list']) {
setupDevice.showDeviceList(finish);
} else if (argv.getkey) {
op = getkey;
} else if (argv.run) {
op = run;
} else if (argv.forward) {
op = forward;
} else if (argv.version) {
version.showVersionAndExit();
} else {
if (argv['hidden-help']) {
help.display(processName, appdata.getConfig(true).profile, true);
} else {
help.display(processName, appdata.getConfig(true).profile, false);
}
cliControl.end();
}
if (op) {
version.checkNodeVersion(function() {
async.series([
op.bind(this)
], finish);
});
}
function getkey(next) {
const resolver = new novacom.Resolver();
async.waterfall([
resolver.load.bind(resolver),
resolver.getSshPrvKey.bind(resolver, options),
function(keyFileName, next) {
if (keyFileName) {
if (argv.passphrase) {
return next(null, keyFileName, argv.passphrase);
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdout.write('input passphrase [default: webos]:');
process.stdin.on('data', function(text) {
let passphrase = text.toString().trim();
if (passphrase === '') {
passphrase = 'webos';
}
log.info('registered passphrase is ', passphrase);
next(null, keyFileName, passphrase);
});
} else {
return next(new Error("Error getting key file from the device"));
}
},
function(keyFileName, passphrase, next) {
const target = {};
target.name = options.name;
target.privateKey = {
"openSsh": keyFileName
};
target.passphrase = passphrase;
target.files = 'sftp';
target.port = '9922';
target.username = 'prisoner';
target.password = '@DELETE@';
next(null, target);
},
resolver.modifyDeviceFile.bind(resolver, 'modify')
], function(err) {
if (err)
return next(err);
next(null, {
"msg": "Success"
});
});
}
function run(next) {
if (argv.run === 'true') {
finish(errHndl.getErrMsg("EMPTY_VALUE", "DEVICE_COMMAND"));
} else {
const printTarget = true;
options.session = new novacom.Session(options, printTarget, function(err) {
if (err) {
next(err);
return;
}
options.session.run(argv.run, process.stdin, process.stdout, process.stderr, next);
});
}
}
function forward(next) {
if (!argv.port || argv.port.toString() === 'true') {
finish(errHndl.getErrMsg("EMPTY_VALUE", "DEVICE_PORT:HOST_PORT"));
} else {
const tasks = [
function(next) {
const printTarget = true;
options.session = new novacom.Session(options, printTarget, next);
}
];
try {
argv.port.forEach(function(portStr) {
const portArr = portStr.split(':');
const devicePort = parseInt(portArr[0], 10);
const localPort = parseInt(portArr[1], 10) || devicePort;
tasks.push(function(next) {
options.session.forward(devicePort, localPort, next);
});
tasks.push(function() {
console.log('forward', 'running...');
});
});
} catch (err) {
next(err);
return;
}
async.series(tasks, next);
}
}
function finish(err, value) {
if (err) {
// handle err from getErrMsg()
if (Array.isArray(err) && err.length > 0) {
for (const index in err) {
log.error(err[index].heading, err[index].message);
}
log.verbose(err[0].stack);
} else {
// handle general err (string & object)
log.error(err.toString());
log.verbose(err.stack);
}
cliControl.end(-1);
} else {
log.info('finish():', value);
if (value && value.msg) {
console.log(value.msg);
}
cliControl.end();
}
}