From 98e38589a9a35cf129c5fd0b8e1f546b03175a69 Mon Sep 17 00:00:00 2001 From: Bas Schouten Date: Mon, 22 Apr 2024 10:27:54 +0100 Subject: [PATCH] Add update code to the Web GUI. --- html/index.html | 47 +++++++++++++++++++++++++++++++++-------------- pithrottler.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/html/index.html b/html/index.html index d82b11d..3566a34 100644 --- a/html/index.html +++ b/html/index.html @@ -40,6 +40,23 @@ fetch("/set?latency=" + document.getElementById("latencyRange").value + "&ploss=" + document.getElementById("plossRange").value + "&throughput=" + document.getElementById("tpRange").value); } + async function checkForUpdates() { + const updateAvailable = await fetch("/checkupdate"); + if (await updateAvailable.text() == "0") { + document.getElementById("updateStatus").innerHTML = "Up to date."; + } else { + document.getElementById("updateStatus").innerHTML = "An update is available."; + document.getElementById("updateButton").style.display = ""; + } + } + + async function runUpdate() { + document.getElementById("updateStatus").innerHTML = "Updating..."; + document.getElementById("updateButton").style.display = "none"; + const updateAvailable = await fetch("/runupdate"); + location.reload(); + } + function loadPresets() { let innerhtml = ""; for (type in presets) { @@ -61,20 +78,22 @@ -
- Latency
- 0 ms -
-
- Packet Loss
- 0 % -
-
- Throughput Limitation (0 for unlimited)
- 0 kb/s -
- +
+ 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 index c8a01ac..bef8773 100644 --- a/pithrottler.js +++ b/pithrottler.js @@ -8,6 +8,37 @@ var serve = serveStatic("./html"); const app = (req, res) => { console.log(new Date(), req.url); + if (req.url.startsWith("/checkupdate")) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Access-Control-Allow-Origin', '*'); + + exec('git ls-remote -q origin master', function (error, stdout, stderr) { + let remoteRevision = stdout.substring(0, 40); + exec('git rev-parse HEAD', function (error, stdout, stderr) { + console.log(remoteRevision); + console.log(stdout); + if (remoteRevision != stdout) { + res.end("1"); + } else { + res.end("0"); + } + }); + }); + return; + } + + if (req.url.startsWith("/runupdate")) { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.setHeader('Access-Control-Allow-Origin', '*'); + + exec('./install.sh', function (error, stdout, stderr) { + res.end("Done"); + }); + return; + } + if (req.url.startsWith("/set")) { let searchParams = new URL("http://localhost:2121" + req.url).searchParams; -- 2.47.3