-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·666 lines (635 loc) · 19.8 KB
/
server.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/usr/bin/env node
const flowsFile = __dirname + "/flows.json";
var fs = require("fs");
var http = require("http");
var sshClient = require("ssh2").Client;
var server = http.createServer();
var html = readPlaintextFromFile(__dirname + "/index.html", true);
var client_js = readPlaintextFromFile(__dirname + "/js/client.js", true);
var config = JSON.parse(readPlaintextFromFile(__dirname + "/config.json", true));
var sshPrivateKey = readPlaintextFromFile(config.sshPrivateKey, true);
var { spawn, execSync } = require("child_process");
var uuidv4 = require('uuid/v4');
var readline = require("readline");
var sse;
var state;
function onHttpRequest(request, response) {
switch (request.method) {
case "GET":
console.log("GET " + request.url);
switch (request.url) {
/* Files */
case "/":
case "/index.html":
response.setHeader("Content-Type", "text/html");
response.end(html);
break;
case "/js/client.js":
response.setHeader("Content-Type", "application/javascript");
response.end(client_js);
break;
case "/flows":
response.setHeader("Content-Type", "application/json");
response.end(curateStateForSend(state));
break;
case "/running":
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify({ running: state.running }));
break;
default:
httpLogErr(response, 404, "invalid url " + request.url);
break;
}
break;
case "PUT":
console.log("PUT " + request.url);
request.setEncoding("utf8");
switch (request.url) {
case "/flows":
if (state.running == true) {
httpLogErr(response, 405,
"Flow config changes not allowed while traffic is running");
break;
}
var body = "";
request.on("data", (chunk) => {
body += chunk;
});
request.on("end", () => {
try {
state = createNewState(body);
var flowsString = curateStateForSend(state);
fs.writeFile(flowsFile, flowsString, function onWrite() {
console.log("Successfully written flows to file.");
/* Send flows back to client, as
* part of confirmation */
response.setHeader("Content-Type", "application/json");
response.end(flowsString);
});
} catch (reason) {
httpLogErr(response, 400,
"cannot parse flows from " + body +
", reason: " + reason);
}
});
break;
case "/running":
var body = "";
request.on("data", (chunk) => {
body += chunk;
});
request.on("end", () => {
try {
var msg = JSON.parse(body);
} catch (e) {
httpLogErr(response, 400, e + ": invalid request body " + body);
return;
}
onStartStopTraffic(msg.running);
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify({ running: state.running }));
});
break;
default:
httpLogErr(response, 405, "invalid url for PUT: " + request.url);
break;
}
break;
default:
httpLogErr(response, 405, "Unknown method called: " + request.method);
break;
}
}
function httpLogErr(response, statusCode, text) {
console.log("httpLogErr :: " + text);
response.setHeader("Content-Type", "text/plain");
response.statusCode = statusCode;
response.end(text);
}
/* Method of objects from the state.flows[flowType] arrays */
function onSourceSSHConnReady(flowType) {
var cmd;
if (flowType == "iperf") {
/* Run for 24 hours */
cmd = "iperf3 -t 86400 -p " + this.port +
((this.transport == "udp") ? " -u -b " + this.bandwidth + "M " : " ") +
" -c " + this.destination.hostname;
} else if (flowType == "ping") {
cmd = "ping " + ((this.intervalType == "adaptive") ? "-A " :
"-i " + (this.intervalMS / 1000)) +
" -s " + this.packetSize + " " + this.destination.hostname +
" | prl --count 1 --every 5ms";
} else {
console.log("Destination SSH Client :: invalid flow type %s", flowType);
return;
}
console.log("%s %s Client :: conn ready", this.label, flowType);
this.srcSSHConn.exec(cmd, { pty: true }, (err, stream) => {
if (err) {
console.log(err);
this.srcSSHConn.end();
stopTraffic(err);
return;
}
stream.setEncoding("utf8");
stream.on("close", (code, signal) => {
var msg = 'Command "' + cmd + '" on ' +
this.srcSSHConn.toString() +
' exited with code ' + code + ' and signal ' + signal;
console.log(msg);
this.srcSSHConn.end();
if (code || signal) {
/* Abnormal termination. Notify browser. */
stopTraffic(new Error(msg));
} else {
stopTraffic();
}
});
/* stdout */
readline.createInterface({ input: stream })
.on("line", (line) => {
/* The only reports taken at traffic source are ping RTT values.
* Ping PIT values, as well as iPerf3 reports, are taken at destination.
*/
if (flowType == "ping" && config.ping.measurement == "rtt") {
var time = (Date.now() - state.startTime) / 1000;
if (line.includes("ms")) {
var words = line.trim().split(/\ +/);
var rtt = words[words.indexOf("ms") - 1].split("=")[1];
/* Plot an extra ping point */
state.plotter[flowType].stdin.write(
time + " " + this.id + " " + rtt + "\n");
} else {
console.log("%s %s Source STDOUT: %s",
this.label, flowType, line);
}
} else {
/* If not taking reports, just print out the output as-is. */
//console.log("%s %s Source :: STDOUT: %s",
//this.label, flowType, line);
}
});
/* stderr */
readline.createInterface({ input: stream.stderr })
.on("line", (line) => {
var msg = this.label + " " + flowType + " Source :: STDERR " + line;
console.log(msg);
this.srcSSHConn.end();
stopTraffic(new Error(msg));
});
});
}
/* Method of objects from the state.flows[flowType] arrays */
function onDestinationSSHConnReady(flowType) {
var cmd;
if (flowType == "iperf") {
cmd = "iperf3 -1 -f m -i 0.5 -s -p " + this.port;
} else if (flowType == "ping") {
if (config.ping.measurement == "pit") {
var filter = "src host " + this.source.hostname +
" and icmp[icmptype] == icmp-echo";
cmd = "tcpdump -i " + config.ping.measurementInterface +
" -n -l --buffer-size 10240 -ttt -j adapter_unsynced" +
" --immediate-mode -- " + filter +
" | prl --count 1 --every 5ms";
} else {
/* Ping, but RTT measurement. Nothing to do here. */
return;
}
} else {
console.log("Destination SSH Client :: invalid flow type %s", flowType);
return;
}
console.log("%s %s Destination :: conn ready", this.label, flowType);
this.dstSSHConn.exec(cmd, { pty: true }, (err, stream) => {
if (err) {
console.log(err);
this.dstSSHConn.end();
stopTraffic(err);
return;
}
stream.setEncoding("utf8");
stream.on("close", (code, signal) => {
var msg = 'Command "' + cmd + '" on ' +
this.dstSSHConn.toString() +
' exited with code ' + code + ' and signal ' + signal;
console.log(msg);
if (code || signal) {
stopTraffic(new Error(msg));
} else {
stopTraffic();
}
});
this.lastSeq = 0;
/* stdout */
readline.createInterface({ input: stream })
.on("line", (line) => {
var time = (Date.now() - state.startTime) / 1000;
if (flowType == "iperf") {
if (line.includes("Server listening on " + this.port)) {
/* iPerf Server managed to start up.
* Time to connect to iPerf client and start
* that up as well.
*/
this.srcSSHConn.connect(this.srcSSHConn.config);
} else if (line.includes("Mbits/sec")) {
var arr = line.trim().split(/\ +/);
var bw = arr[arr.indexOf("Mbits/sec") - 1];
/* Plot an extra iperf point */
state.plotter[flowType].stdin.write(
time + " " + this.id + " " + bw + "\n");
} else {
console.log("%s %s Destination STDOUT: %s",
this.label, flowType, line);
}
} else if (flowType == "ping") {
/* PIT measurements taken by tcpdump. */
var words = line.trim().split(/[, ]+/);
try {
if (words.includes("seq")) {
/* This is a line containing a valid tcpdump packet.
* Let's inspect it.
*/
var pit = words[0];
var seq = words[words.indexOf("seq") + 1];
/* Convert seq to numeric value */
seq = +seq;
if (seq == this.lastSeq + 1) {
/* PIT output format: HH:MM:SS.msmsms */
var hms = pit.split(":");
var hours = hms[0];
var minutes = hms[1];
var seconds = hms[2];
var pitMs = ((hours * 24 * 60) + (minutes * 60) + seconds) * 1000;
state.plotter[flowType].stdin.write(
time + " " + this.id + " " + pitMs + "\n");
} else {
console.log("seq %s, lastSeq %s. skipping.",
seq, this.lastSeq);
}
this.lastSeq = seq;
} else {
console.log("%s %s Destination :: STDOUT: %s",
this.label, flowType, line);
}
} catch (e) {
console.log(e);
console.log("%s %s Destination :: invalid PIT %s",
this.label, flowType, pit);
}
}
});
/* stderr */
readline.createInterface({ input: stream.stderr })
.on("line", (line) => {
var msg = this.label + " " + flowType + " Destination :: STDERR " + line;
console.log(msg);
this.dstSSHConn.end();
stopTraffic(new Error(msg));
});
});
}
/* method of state.plotter.iperf and state.plotter.ping */
function onGnuplotData(flowType, data) {
if (data.includes("</svg>")) {
/* New SVG can be reassembled. */
var halves = data.split("</svg>");
this.svg += halves[0] + "</svg>";
/* Send it to the SSE clients */
state.clients.forEach((stream) => {
stream.send(flowType, JSON.stringify({ svg: this.svg }));
});
/* Re-initialize the svg with the remainder */
this.svg = halves[1];
} else {
this.svg += data;
}
}
function sshConnToString() {
return this.config.username + '@' +
this.config.host + ':' +
this.config.port;
}
function startFlows(flows, flowType) {
if (!flows.length) { return; }
var feedgnuplotParams = [
"--stream", "0.5",
"--domain", /* First column (time) is domain */
"--dataid", /* Second column (f.id) is dataid */
"--exit",
"--lines",
"--terminal", "svg"
];
if (config[flowType].xmin) feedgnuplotParams.push("--xmin", config[flowType].xmin);
if (config[flowType].ymin) feedgnuplotParams.push("--ymin", config[flowType].ymin);
if (config[flowType].xmax) feedgnuplotParams.push("--xmax", config[flowType].xmax);
if (config[flowType].ymax) feedgnuplotParams.push("--ymax", config[flowType].ymax);
if (config[flowType].xlen) feedgnuplotParams.push("--xlen", config[flowType].xlen);
if (config[flowType].xlabel) feedgnuplotParams.push("--xlabel", config[flowType].xlabel);
if (config[flowType].ylabel) feedgnuplotParams.push("--ylabel", config[flowType].ylabel);
if (config[flowType].title) feedgnuplotParams.push("--title", config[flowType].title);
if (config[flowType].plotStyle == "histogram" && config[flowType].binwidth) {
feedgnuplotParams.push("--binwidth", config[flowType].binwidth);
}
/* "--timefmt", "%H:%M:%S", "--set", 'format x "%H:%M:%S"', */
state.startTime = Date.now();
flows.forEach((f) => {
if (config[flowType].plotStyle == "histogram") {
feedgnuplotParams.push("--legend", f.id, f.label);
feedgnuplotParams.push("--histogram", f.id);
} else {
feedgnuplotParams.push("--style", f.id, 'linewidth 2');
feedgnuplotParams.push("--legend", f.id, f.label);
}
f.srcSSHConn = new sshClient();
f.srcSSHConn.toString = sshConnToString.bind(f.srcSSHConn);
f.srcSSHConn.on("ready", () => onSourceSSHConnReady.call(f, flowType));
f.srcSSHConn.on("error", (e) => {
var msg = "SSH connection to " +
f.srcSSHConn.toString() + ": " + e;
console.log(msg);
stopTraffic(new Error(msg));
});
f.srcSSHConn.config = {
username: f.source.user,
host: f.source.hostname,
port: f.source.port,
privateKey: sshPrivateKey
};
f.dstSSHConn = new sshClient();
f.dstSSHConn.toString = sshConnToString.bind(f.dstSSHConn);
f.dstSSHConn.on("ready", () => onDestinationSSHConnReady.call(f, flowType));
f.dstSSHConn.on("error", (e) => {
var msg = "SSH connection to " +
f.dstSSHConn.toString() + ": " + e;
console.log(msg);
stopTraffic(new Error(msg));
});
f.dstSSHConn.config = {
username: f.destination.user,
host: f.destination.hostname,
port: f.destination.port,
privateKey: sshPrivateKey
};
if (flowType == "ping" && config.ping.measurement == "rtt") {
/* Ping traffic is initiated through the
* SSH connection to source. RTT measurements are also
* reported by the ping sender, so no connection
* is necessary at the destination.
*/
f.srcSSHConn.connect(f.srcSSHConn.config);
} else if (flowType == "ping" && config.ping.measurement == "pit") {
/* If ping measurement is PIT (packet interrarival time),
* this must be reported by the destination
*/
f.srcSSHConn.connect(f.srcSSHConn.config);
f.dstSSHConn.connect(f.dstSSHConn.config);
} else if (flowType == "iperf") {
/* iPerf traffic is initiated through the
* source SSH connection as well, but an iPerf
* server must first be started on the destination.
*/
f.dstSSHConn.connect(f.dstSSHConn.config);
}
});
var plotter = spawn("feedgnuplot", feedgnuplotParams);
plotter.stdout.setEncoding("utf8");
plotter.stderr.setEncoding("utf8");
/* feedgnuplot stdout is delimited by </svg> ending tag.
* No need to emit line events. */
plotter.stdout.on("data", (data) => onGnuplotData.call(plotter, flowType, data));
/* Parse stderr of feedgnuplot by lines */
readline.createInterface({ input: plotter.stderr })
.on("line", (line) => {
console.log("feedgnuplot stderr: %s", line);
/* Some warning messages are not harmful */
if (!line.includes("adjusting to")) {
stopTraffic();
}
});
plotter.on("exit", (code) => {
console.log("feedgnuplot process exited with code %s", code);
});
plotter.svg = "";
state.plotter[flowType] = plotter;
}
function startTraffic() {
["iperf", "ping"].forEach((flowType) => {
var enabled = state.flows[flowType].filter((e) => { return e.enabled });
startFlows(enabled, flowType);
});
state.running = true;
state.clients = [];
}
function stopTraffic(error) {
["iperf", "ping"].forEach((flowType) => {
var enabled = state.flows[flowType].filter((e) => { return e.enabled });
if (enabled.length) {
enabled.forEach((f) => {
if (typeof(f.srcSSHConn) != "undefined") { f.srcSSHConn.end() };
if (typeof(f.dstSSHConn) != "undefined") { f.dstSSHConn.end() };
});
state.plotter[flowType].stdin.end();
}
});
state.clients.forEach((stream) => {
if (typeof error != "undefined") {
stream.send("server-err", JSON.stringify({
name: error.name,
message: error.message,
stack: error.stack
}));
}
stream.close();
});
state.clients = [];
state.running = false;
}
function onStartStopTraffic(newTrafficState) {
console.log("traffic start/stop: old state " + state.running + ", new state " + newTrafficState);
if (newTrafficState == state.running) {
/* This can happen when server restarted, but client
* has stale information about its state. */
return;
}
switch (newTrafficState) {
case true:
startTraffic();
break;
case false:
stopTraffic();
break;
default:
throw new Error("undefined traffic state");
}
}
function onHttpListen() {
console.log("Server listening for http requests on port %s",
config.listenPort);
/* initialize the /sse route */
SSE = require("sse");
sse = new SSE(server);
sse.on("connection", (stream) => {
console.log("sse :: established new connection to %s",
stream.res.connection.remoteAddress);
state.clients.push(stream);
stream.on("close", () => {
state.clients.splice(state.clients.indexOf(stream), 1);
console.log("sse :: closed connection to %s",
stream.res.connection.remoteAddress);
});
});
}
function onHttpServerError(e) {
console.log(e.name + ": " + e.message);
}
function onExit() {
console.log("Server exiting");
process.exit();
}
function readPlaintextFromFile(filename, exitOnFail) {
var content = "";
try {
content = fs.readFileSync(filename, "utf-8");
} catch (e) {
console.log(e.name + ": " + e.message);
if (exitOnFail) {
console.log("Cannot read file, exiting");
process.exit(1);
}
}
return content;
}
/*
* state = {
* running: boolean,
* clients: [Client],
* plotter: {
* iperf: ChildProcess,
* ping: ChildProcess
* },
* flows: {
* iperf: [
* {
* id: [uuidv4],
* source: "user@host",
* destination: "user@host",
* port: integer,
* transport: "tcp|udp",
* bandwidth: integer,
* enabled: boolean,
* label: string,
* data: [number],
* }, (...)
* ],
* ping: [
* {
* id: [uuidv4],
* source: "user@host",
* destination: "user@host",
* intervalType: "periodic|adaptive",
* intervalMS: integer,
* enabled: boolean,
* label: string,
* data: []
* }, (...)
* ]
* }
* };
*/
/* We create a new state object from the given flowsString
* interpreted as JSON.
* state.running always gets initialized as false, because
* it is not semantically correct anyway to call this function
* while running == true.
* Warning: Throws exception!
*/
function createNewState(flowsString) {
var newFlows = JSON.parse(flowsString);
/* Append unique identifiers to each flow
* (to be distinguished by gnuplot) */
["iperf", "ping"].forEach((type) => {
newFlows.flows[type].forEach((f) => {
f.id = uuidv4();
});
});
console.log(JSON.stringify(newFlows));
return {
running: false,
clients: [],
plotter: { ping: {}, iperf: {} },
flows: newFlows.flows
};
}
/* The reason we start creating this from scratch is that
* we put a lot of extraneous stuff in the state, such as data,
* plotter, srcSSHConn, dstSSHConn, that we don't want to leak
*/
function curateStateForSend(state) {
var newFlows = { iperf: [], ping: [] };
state.flows.iperf.forEach((f) => {
newFlows.iperf.push({
source: f.source,
destination: f.destination,
port: f.port,
transport: f.transport,
bandwidth: f.bandwidth,
enabled: f.enabled,
label: f.label
});
});
state.flows.ping.forEach((f) => {
newFlows.ping.push({
source: f.source,
destination: f.destination,
intervalType: f.intervalType,
intervalMS: f.intervalMS,
packetSize: f.packetSize,
enabled: f.enabled,
label: f.label
});
});
return JSON.stringify({ flows: newFlows }, null, "\t");
}
function checkVersion(cmd, where, requiredMajor, requiredMinor) {
try {
var version = execSync(cmd).toString().split(" ")[where];
var major, minor;
[major, minor] = version.split(".").map(Number);
return (major > requiredMajor ||
(major == requiredMajor && minor >= requiredMinor));
} catch (e) {
console.log(e);
return false;
}
}
if (!checkVersion("gnuplot --version", 1, 5, 2)) {
/* Sample stdout: "gnuplot 5.2 patchlevel 0" */
console.log("Please ensure a minimum version of gnuplot 5.2 is available.");
process.exit();
}
if (!checkVersion("feedgnuplot --version", 2, 1, 45)) {
/* Sample stdout: "feedgnuplot version 1.45" */
console.log("Please ensure a minimum version of feedgnuplot 1.45 is available.");
process.exit();
}
process.on("SIGHUP", onExit);
process.on("SIGINT", onExit);
process.on("SIGTERM", onExit);
process.on("SIGABRT", onExit);
process.on("SIGQUIT", onExit);
try {
state = createNewState(readPlaintextFromFile(flowsFile, false))
} catch (reason) {
console.log(reason);
console.log("initializing with empty iperf and ping flows array");
state = {
running: false,
clients: [],
plotter: { iperf: {}, ping: {} },
flows: { iperf: [], ping: [] }
};
};
server.on("request", onHttpRequest);
server.on("error", onHttpServerError);
server.listen(config.listenPort, onHttpListen);