From: Bas Schouten Date: Sun, 21 Apr 2024 10:14:43 +0000 (+0000) Subject: Initial commit. X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=9b7278839c10cfb7178ff67dd15776e70cf59f97;p=pithrottler.git Initial commit. --- 9b7278839c10cfb7178ff67dd15776e70cf59f97 diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..d82b11d --- /dev/null +++ b/html/index.html @@ -0,0 +1,80 @@ + + + + + PiThrottler Configuration + + + + +
+ Latency
+ 0 ms +
+
+ Packet Loss
+ 0 % +
+
+ Throughput Limitation (0 for unlimited)
+ 0 kb/s +
+ + + \ No newline at end of file diff --git a/pithrottler.js b/pithrottler.js new file mode 100644 index 0000000..c8a01ac --- /dev/null +++ b/pithrottler.js @@ -0,0 +1,73 @@ +const http = require('http'); +const serveStatic = require('serve-static'); +const finalhandler = require('finalhandler'); +const { exec } = require("node:child_process"); + +var serve = serveStatic("./html"); + +const app = (req, res) => { + console.log(new Date(), req.url); + + if (req.url.startsWith("/set")) { + let searchParams = new URL("http://localhost:2121" + req.url).searchParams; + + var latency = searchParams.get("latency"); + var ploss = searchParams.get("ploss"); + var throughput = searchParams.get("throughput"); + + if (!latency) { + latency = 0; + } + if (!ploss) { + ploss = 0; + } + if (!throughput) { + throughput = 0; + } + + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.end("Setting parameters, latency: " + latency + " ms, packet loss: " + ploss + "%, Throughput: " + throughput + " kbit/s"); + + exec("sudo tc qdisc del dev wlan0 root"); + exec("sudo tc qdisc del dev wlan0 root"); + + if (latency > 0 || ploss > 0) { + let command = "sudo tc qdisc add dev wlan0 root handle 30: netem"; + + if (latency > 0) { + command += " delay " + latency + "ms"; + } + if (ploss > 0) { + command += " loss " + ploss + "%"; + } + + exec(command); + console.log("Setting lattency and packet loss"); + } + + if (throughput > 0) { + let command = "sudo tc qdisc add dev wlan0 "; + + if (latency > 0 || ploss > 0) { + command += "parent 30:1"; + } else { + command += "root"; + } + + command += " tbf rate " + throughput + "kbit burst 32kbit latency 500ms"; + exec(command); + console.log("Setting bandwidth limitation"); + } + + return; + } + + var done = finalhandler(req, res); + serve(req, res, done); +}; + +const server = http.createServer(app) +server.listen(2121, "0.0.0.0", () => { +});