-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
332 lines (282 loc) · 9.44 KB
/
index.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
const path = require('path');
const os = require('os');
const fs = require('fs');
const chalk = require('chalk');
const shell = require('./lib/shell');
const utils = require('./lib/utils');
const inquirer = require('inquirer');
const program = require('commander');
let nssh = function (constConf) {
let cts = constConf || require('./lib/constant');
let initCheck = function () {
utils.mkdirSync(cts.NSSHPath);
}
return {
init: function () {
try {
// check initialized
initCheck();
// create default symlink
function createDefaultLink(linkPath, filePath) {
let stat = utils.existsFile(linkPath);
if (stat) {
let copyPath = linkPath;
if (stat.isSymbolicLink()) {
copyPath = fs.readlinkSync(linkPath);
}
if (copyPath !== filePath) {
utils.copySync(copyPath, filePath);
utils.createLink(linkPath, filePath);
}
return true
}
return false;
}
// create ssh private key
let isCreate = createDefaultLink(cts.SSHPrivateKeyPath, cts.DefaultPrivateKeyPath)
if (isCreate) {
// create ssh public key
createDefaultLink(cts.SSHPublicKeyPath, cts.DefaultPublicKeyPath)
}
console.log(chalk.green('✔ ssh key store initialized!'));
console.log(chalk.green('ssh key store location is:' + cts.NSSHPath));
} catch (error) {
console.log(chalk.red('ssh key store initialize fail' + error.message));
}
},
list: function () {
initCheck();
// load ssh key store
let keys = utils.loadSSHKeys(cts.NSSHPath);
let currentKeyPath = '';
// get default ssh key's name
let stat = utils.existsFile(cts.SSHPrivateKeyPath);
if (stat && stat.isSymbolicLink()) {
currentKeyPath = fs.readlinkSync(cts.SSHPrivateKeyPath);
}
let content = [];
for (let name in keys) {
// exclude the temp ssh key
if (name !== cts.TmpKey) {
// set style for current ssh key
if (keys[name].PrivateKey === currentKeyPath) {
content.push(chalk.green('-> ' + name + ' (current)'));
} else {
content.push(chalk.cyan(' ' + name));
}
}
}
console.log(chalk.green('✔ Found ' + content.length + ' ssh key(s)!'));
console.log();
if (content.length > 0) {
content.forEach(function (item) {
console.log(item);
})
}
},
create: function (name) {
initCheck();
if (!name || name === '') {
program.help();
return false;
}
let type = program.type;
let password = program.password;
let passphrase = program.new_passphrase;
// check name is exists
let newPath = path.join(cts.NSSHPath, name);
if (fs.existsSync(newPath)) {
console.log(chalk.red('SSH key name already exists'));
return true;
}
let host = utils.getHost(program.host, program)
// check host
if (type && !program.host) {
console.log(chalk.red('remote host is empty'));
return false;
}
let conf = null;
// check config
if (type === 1 || type === 3) {
// load config
conf = utils.loadConf(cts.ConfPath);
// check config is exists
if (conf[name]) {
console.log(chalk.red('SSH key config already exists'));
return true;
}
}
// clear temp floder
utils.rmdirSync(cts.TmpPath);
// create temp floder
utils.mkdirSync(cts.TmpPath);
let newKeyFilePath = path.join(newPath, cts.PrivateKey);
shell.keygen(cts.TmpPrivateKeyPath, passphrase)
.then(function (code) {
console.log(chalk.green('create ssh key success'), code);
// copy ssh key
if (type > 1) {
return shell.copy(host.user + '@' + host.address, cts.TmpPublicKeyPath, host.port);
}
return Promise.resolve();
})
.then(function (code) {
if (type > 1) {
console.log(chalk.green('copy ssh key success'));
}
// add config
if (type === 1 || type === 3) {
conf[name] = {
Host: name,
HostName: host.address,
PreferredAuthentications: 'publickey',
IdentityFile: newKeyFilePath,
User: host.user,
Port: host.port
};
utils.saveConf(conf, cts.ConfPath);
console.log(chalk.green('add ssh key success'));
}
// 拷贝密钥
fs.renameSync(cts.TmpPath, newPath);
console.log(chalk.green('✔ ssh key [' + name + '] created'));
})
.catch(function (error) {
var platform = os.platform();
if (platform === 'win32' && error.message.indexOf('ENOENT') > -1) {
console.log(chalk.red('please use git bash exec nssh'));
} else {
console.log(chalk.red(error.message));
}
})
},
rename: function (name, newName) {
initCheck();
// check arguments
if (!name || name === '' || !newName || newName === '') {
program.help();
return false;
}
// check newName
let keys = utils.loadSSHKeys(cts.NSSHPath);
if (keys[newName]) {
console.log(chalk.red(newName + ' already exists'));
return false;
}
// check name
if (!keys[name]) {
console.log(chalk.red('No found ' + name));
return false;
}
let confs = utils.loadConf(cts.ConfPath);
// delete config of newName
if (confs[newName]) {
delete confs[newName];
}
// update config
if (confs[name]) {
confs[newName] = confs[name];
delete confs[name];
confs[newName].Host = newName;
confs[newName].IdentityFile = path.join(cts.NSSHPath, newName, cts.PrivateKey);
utils.saveConf(confs, cts.ConfPath);
}
// check current symlink
let isDefault = false;
let currentKeyPath = utils.getLinkPath(cts.SSHPrivateKeyPath);
if (path.join(cts.NSSHPath, name, cts.PrivateKey) === currentKeyPath) {
fs.unlinkSync(cts.SSHPrivateKeyPath);
fs.unlinkSync(cts.SSHPublicKeyPath);
isDefault = true;
}
// rename floder
fs.renameSync(path.join(cts.NSSHPath, name), path.join(cts.NSSHPath, newName));
if (isDefault) {
// create symlink
fs.symlinkSync(path.join(cts.NSSHPath, newName, cts.PrivateKey), cts.SSHPrivateKeyPath)
fs.symlinkSync(path.join(cts.NSSHPath, newName, cts.PublicKey), cts.SSHPublicKeyPath)
}
console.log(chalk.green('✔ ssh key [' + name + '] renamed to [' + newName + ']'));
},
remove: function (name) {
initCheck();
// check arguments
if (!name || name === '') {
program.help();
return false;
}
// check config
let keys = utils.loadSSHKeys(cts.NSSHPath);
if (!keys[name]) {
console.log(chalk.red('Not Found ' + name));
return false;
}
inquirer.prompt([{
type: 'confirm',
name: 'yes',
message: 'confirm to remove [' + name + ']?'
}]).then(function (resp) {
if (resp.yes) {
// check current symlink
let currentKeyPath = utils.getLinkPath(cts.SSHPrivateKeyPath);
if (keys[name].PrivateKey === currentKeyPath) {
fs.unlinkSync(cts.SSHPrivateKeyPath);
fs.unlinkSync(cts.SSHPublicKeyPath);
}
// remove floder
utils.rmdirSync(path.join(cts.NSSHPath, name));
// remove config
let confs = utils.loadConf(cts.ConfPath);
if (confs[name]) {
delete confs[name];
utils.saveConf(confs, cts.ConfPath);
}
console.log(chalk.green('✔ ssh key [' + name + '] removed'));
}
});
},
copy: function (name, host) {
initCheck();
// check arguments
if (!name || name === '' || !host || host === '') {
program.help();
return false;
}
// check name is exists
let keys = utils.loadSSHKeys(cts.NSSHPath);
if (!keys[name]) {
console.log(chalk.red('Not Found ' + name));
return false;
}
host = utils.getHost(host, program)
shell.copy(host.user + '@' + host.address, keys[name].PublicKey, host.port)
.then(function () {
console.log(chalk.green('copy ssh key success'));
}).catch(function (error) {
var platform = os.platform();
if (platform === 'win32' && error.message.indexOf('ENOENT') > -1) {
console.log(chalk.red('please use git bash exec nssh'));
} else {
console.log(chalk.red(error.message));
}
});
},
use: function (name) {
if (!name || name === '') {
program.help();
return false;
}
initCheck();
// load ssh key store,check name is exists
let keys = utils.loadSSHKeys(cts.NSSHPath);
if (!keys[name]) {
console.log(chalk.red('Not Found ' + name));
return false;
}
utils.createLink(cts.SSHPrivateKeyPath, keys[name].PrivateKey);
utils.createLink(cts.SSHPublicKeyPath, keys[name].PublicKey);
console.log(chalk.green('✔ Now using SSH key: [' + name + ']'));
}
}
}
module.exports = nssh;