-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
222 lines (175 loc) · 4.81 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
const async = require('async');
const colors = require('colors');
const path = require('path');
const Table = require('cli-table');
const ut = require('utjs');
const yargs = require('yargs');
const ServerReader = require('./lib/ServerReader');
const Connection = require('./lib/Connection');
// Variables
const STATUS_COLUMN_INDEX = 2;
let servers = [];
const headers = {
name: 'name',
id: 'id',
status: 'status',
user: 'user',
host: 'host',
port: 'port',
auth: 'auth',
};
const table = new Table({
head: [
colors.cyan.bold(headers.name), colors.cyan.bold(headers.id),
colors.cyan.bold(headers.status), colors.cyan.bold(headers.user),
colors.cyan.bold(headers.host), colors.cyan.bold(headers.port),
colors.cyan.bold(headers.auth),
],
style: { compact: true, 'padding-left': 1 },
});
// Arguments config
const { argv } = yargs.usage('Usage: $0 [options]')
.alias('f', 'file')
.nargs('f', 1)
.describe('f', 'Provide a json or csv file with servers')
.string('f')
.alias('o', 'order')
.nargs('o', 1)
.string('o')
.choices('o', Object.keys(headers).filter((item) => item !== headers.status))
.describe('o', 'Order the table by the given column')
.alias('s', 'server')
.nargs('s', 1)
.string('s')
.describe('s', 'Specify the server name or id to connect')
.alias('n', 'nocheck')
.describe('n', 'Disable the server connection checks')
.nargs('n', 0)
.boolean('n')
.alias('t', 'timeout')
.nargs('t', 1)
.default('t', 500)
.describe('t', 'Timeout for server checks in milliseconds')
.help('h')
.alias('h', 'help')
.epilog('https://github.com/alemures')
.strict();
// Main code
main();
// Functions
function main() {
process.stdin.resume();
const file = isValidString(argv.file)
? path.resolve(argv.file)
: path.join(__dirname, 'servers/servers.json');
ServerReader.read(file, (err, servs) => {
if (err) {
console.error(err);
process.exit(-1);
}
servers = servs;
if (isValidString(argv.order)) {
servers.sort(comparator(argv.order));
}
populateTable();
showMenu(`Welcome to SSH Manager, type the name or id of a server\nUsing file: ${file}`);
});
}
function populateTable() {
const portHeaderLength = headers.port.length;
servers.forEach((server) => {
table.push([
colors.cyan.bold(server.name), server.id, reachableToString(server.reachable),
server.user, server.host, ut.paddingBoth(String(server.port), ' ', portHeaderLength), server.auth.type,
]);
});
}
function reachableToString(reachable) {
const headerLength = headers.status.length;
if (argv.nocheck) {
return ut.paddingBoth('-', ' ', headerLength);
}
return reachable ? colors.yellow.bold(ut.paddingBoth('up', ' ', headerLength))
: colors.red.bold(ut.paddingBoth('down', ' ', headerLength));
}
function updateTable() {
servers.forEach((server, i) => {
const row = table[i];
if (server.reachable !== row[STATUS_COLUMN_INDEX]) {
row[STATUS_COLUMN_INDEX] = reachableToString(server.reachable);
}
});
}
function showMenu(message) {
if (argv.nocheck) {
showServers(message);
readLine();
} else {
checkServerConnections(() => {
updateTable();
showServers(message);
readLine();
});
}
}
function checkServerConnections(cb) {
async.each(servers, (server, internalCb) => {
server.checkConnection(argv.timeout, internalCb);
}, cb);
}
function showServers(message) {
// Clear screen
process.stdout.write('\u001b[2J\u001b[0;0H');
console.log(table.toString());
console.log(`LOG -> ${colors.gray(message || '')}`);
console.log('');
process.stdout.write('Choose a server: ');
}
function readLine() {
if (isValidString(argv.server)) {
processLine(argv.server);
argv.server = null;
} else {
process.stdin.once('data', processLine);
}
}
function processLine(data) {
const option = data.toString().trim();
if (option === 'quit' || option === 'exit') {
console.log('Bye!');
process.exit(0);
}
const server = servers.find((serv) => serv.id === parseInt(option, 10)
|| serv.name.toLowerCase() === option.toLowerCase());
if (server) {
connect(server);
} else {
showMenu(`The server "${option}" doesn't exist`);
}
}
function connect(server) {
const connection = new Connection(server);
connection.connect((code, signal) => {
let message = `Connection to "${server.name}" closed with code ${code}`;
if (signal) {
message += ` and signal ${signal}`;
}
showMenu(message);
});
}
function comparator(col) {
return (a, b) => {
const val1 = a[col].toString();
const val2 = b[col].toString();
if (val1 > val2) {
return 1;
}
if (val1 < val2) {
return -1;
}
return 0;
};
}
function isValidString(value) {
return typeof value === 'string' && value.trim().length > 0;
}