From 00a415c755c741f1b3ab95eb7a6daa5d0dea5374 Mon Sep 17 00:00:00 2001 From: logicog Date: Tue, 18 Nov 2025 08:14:28 +0100 Subject: [PATCH] Add filtering logic for configuration commands --- html/config.js | 70 +++++++++++++++++++++++++++++------------------- html/system.html | 4 +-- html/system.js | 29 ++++++++++++++------ 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/html/config.js b/html/config.js index 92a07e5..2d48021 100644 --- a/html/config.js +++ b/html/config.js @@ -1,41 +1,55 @@ var configInterval = Number(); -var configuration = {}; +var configuration = []; +const conf_cmds = [ + /ip\s+(\d{1,3}\.){3}\d{1,3}/, /gw\s+(\d{1,3}\.){3}\d{1,3}/, /netmask\s+(\d{1,3}\.){3}\d{1,3}/, + /eee(\s+\d)?\s+(on|off)/, /mirror(\s+(\d|10))(\s+(\d|10)(t|r)?)+/, /vlan\s+(\d{1,4})(\s+(\d|10)(t|u)?)+/ +]; +const conf_overwrite = [ + /ip/, /gw/, /netmask/, /eee\s+\w+/, /eee(\s+\w)/, /mirror/, /vlan\s+(\d{1,4})/ +]; -function parseConf(c){ +function parseConf(s){ var a = s.split(/\r\n|\n/); for (var l = 0; l < a.length; l++) { + if (!a[l].length || a[l] == "\n" || a[l] == "\r\n") + continue; console.log(l + ' --> ' + a[l]); + var ignore = true; + for (const x of conf_cmds) + if (x.test(a[l])) ignore = false; + if (ignore) continue; + for (const x of conf_overwrite) { + if (x.test(a[l])) { + console.log("Match ", x, " to ", a[l]); + m = a[l].match(x); + console.log("Starts with ", m[0]); + configuration = configuration.filter(item => !(item.startsWith(m[0]))); + } + } + configuration.push(a[l]); } + console.log("Configuration now:"); + for (const x of configuration) { console.log(x); } } -function fetchConfig() { - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - s = xhttp.responseText; - console.log("CONFIG: ", s); - parseConf(s); - clearInterval(configInterval); - } +async function fetchConfig() { + try { + const response = await fetch('/config'); + console.log("CONFIG: ", response); + const t = await response.text(); + return t; + } catch(err) { + console.error("Error: ", err); } - xhttp.open("GET", `/config`, true); - xhttp.send(); } -function fetchCmdLog() { - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - s = xhttp.responseText; - console.log("CMD-Log: ", s); - parseConf(s); - clearInterval(configInterval); - } +async function fetchCmdLog() { + try { + const response = await fetch('/cmd_log'); + console.log("CMD-Log: ", response); + const t = await response.text(); + return t; + } catch(error) { + console.error("Error: ", err); } - xhttp.open("GET", `/cmd_log`, true); - xhttp.send(); } - -window.addEventListener("load", function() { - configInterval = setInterval(fetchConfig, 1000); -}); diff --git a/html/system.html b/html/system.html index 728b300..57e6a74 100644 --- a/html/system.html +++ b/html/system.html @@ -14,8 +14,8 @@
-
-
+
+
diff --git a/html/system.js b/html/system.js index a368f9f..50beec5 100644 --- a/html/system.js +++ b/html/system.js @@ -1,5 +1,5 @@ var systemInterval = Number(); -const ips = ["ip", "nm", "gw"]; +const ips = ["ip", "netmask", "gw"]; function checkIp(ip) { const ipv4 = /^(\d{1,3}\.){3}\d{1,3}$/; @@ -27,14 +27,14 @@ async function ipSub() { } } -async function flashSave() { - fetchConfig(); - fetchCmdLog(); - return; +async function sendConfig(c) { + const form = new FormData(); + form.append("MAX_FILE_SIZE", "4096"); + form.append("configuration", new Blob([c], {type: "application/octet-stream"})); try { - const response = await fetch('/save', { + const response = await fetch('/config', { method: 'POST', - body: cmd + body: form }); console.log('Completed!', response); } catch(err) { @@ -42,6 +42,19 @@ async function flashSave() { } } +async function flashSave() { + fetchConfig().then((s) => { + parseConf(s); + fetchCmdLog().then((s) => { + parseConf(s); + var body = ""; + for (const x of configuration) { body = body + x + "\n"; } + console.log("CONFIGURATION to save: ", body); + sendConfig(body); + }); + }); +} + function fetchIP() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { @@ -49,7 +62,7 @@ function fetchIP() { const s = JSON.parse(xhttp.responseText); console.log("IP: ", s); document.getElementById("ip").value=s.ip_address; - document.getElementById("nm").value=s.ip_netmask; + document.getElementById("netmask").value=s.ip_netmask; document.getElementById("gw").value=s.ip_gateway; clearInterval(systemInterval); }