-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
82 lines (72 loc) · 2.16 KB
/
app.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
const http = require('http');
const https = require('https');
const fs = require("fs")
const Koa = require('koa');
const koaBody = require('koa-body');
const cors = require('@koa/cors')
const app = module.exports = new Koa();
app.use(cors())
const { exec } = require('child_process');
const pwd = fs.readFileSync("./.env",'utf-8')
app.use(koaBody({
jsonLimit: '1kb'
}));
// response
app.use(async function(ctx) {
let cmd = ''
let message = ''
const body = ctx.request.body;
if (!body.rate && !body.delay && !body.loss) ctx.throw(400, 'rate, delay and loss required');
const rate = parseFloat(body.rate)
const delay = parseFloat(body.delay)
const loss = parseFloat(body.loss)
if ( rate === -1 && delay === -1 && loss === -1 ) {
message = `tc qdisc del dev enp4s0 root`
} else {
if (!isNaN(loss) && loss !== 0){
message = `loss ${parseFloat(body.loss)}%`
}
if(!isNaN(delay) && delay !== 0){
message = `delay ${parseFloat(body.delay)}ms ${message}`
}
if (!isNaN(rate) && rate !== 0){
message = `rate ${parseFloat(body.rate)}mbit ${message}`
}
if (message!==''){
message = `tc qdisc add dev enp4s0 root netem ${message}`
}
}
// const message =`sudo tc qdisc add dev enp4s0 root delay ${parseFloat(body.delay)}ms loss ${parseFloat(body.loss)}%`
if (message){
cmd = `echo ${pwd} | sudo -S ${message}`
} else {
cmd = `echo 'invalid parameters'`
}
console.log(message)
// `echo ${pwd} | sudo -S ${message}`
exec(cmd , (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.error(`an error occur.\n${err.message}`)
cmd = ''
message = ''
return;
}
// the *entire* and stderr (buffered)
console.log(message)
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`);
});
ctx.body = { message: message };
cmd = ''
message = ''
});
const options = {
key: fs.readFileSync('./signed/serverkey.pem'),
cert: fs.readFileSync('./signed/servercert.pem')
};
if (!module.parent) {
// app.listen(54321);
http.createServer(app.callback()).listen(54321);
https.createServer(options,app.callback()).listen(54322);
}