-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (58 loc) · 1.8 KB
/
server.js
File metadata and controls
65 lines (58 loc) · 1.8 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
const config = require('./config.json');
const chalk = require('chalk');
const exec = require('child_process').exec;
const fastify = require('fastify')({ logger: false });
const ipRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const win32 = 'win32';
const linux = 'linux';
fastify.post('/api/ping/:ip', function (request, reply) {
authAction(request.ip);
let n = 3;
if (request.query.n) {
n = checkEchoRequestsCount(request.query.n, reply);
}
const ip = request.params.ip;
validateIp(ip, reply);
cliMsg(`${ip} ping`);
const platform = process.platform;
if (platform === win32) {
exec(`ping -n ${n} -w 1000 ${ip}`, (error, stdout, stderr) => { reply.send(stdout) });
} else if (platform === linux) {
exec(`/bin/ping -O ${ip} -W 1 -c ${n}`, (error, stdout, stderr) => { reply.send(stdout) });
}
});
checkEchoRequestsCount = (n, reply) => {
if (n < 1 || n > 100) {
reply.code(400);
throw new Error();
}
return n;
}
validateIp = (ip, reply) => {
var ipformat = ipRegex;
if (!ip.match(ipformat)) {
reply.code(400);
throw new Error();
}
}
cliMsg = (msg, type) => {
if (type !== 1) {
console.log(chalk.black.bgWhite("server-api") + " " + msg)
} else {
console.log(chalk.black.bgWhite("server-api") + " " + chalk.white.bgRed.bold(msg))
}
}
authAction = (ip) => {
if (config.allowedHosts.includes(ip)) {
return true;
}
if (config.allowedHosts.includes("*")) {
return true;
}
cliMsg(`${ip} denied access, please note this activity.`);
return false;
}
fastify.listen(config.listen.port, config.listen.host, err => {
if (err) throw err;
cliMsg(`Now listening on ${config.listen.host}:${fastify.server.address().port}`)
});