mirror of
https://github.com/logicog/RTLPlayground.git
synced 2026-08-30 14:52:51 +08:00
Add filtering logic for configuration commands
This commit is contained in:
+45
-31
@@ -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); }
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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 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", `/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);
|
||||
}
|
||||
}
|
||||
xhttp.open("GET", `/cmd_log`, true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
window.addEventListener("load", function() {
|
||||
configInterval = setInterval(fetchConfig, 1000);
|
||||
});
|
||||
|
||||
+2
-2
@@ -14,8 +14,8 @@
|
||||
<div class="rcol"> <input id="ip" class="ip" type="text" minlength="7" maxlength="15" size="15"/></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="lcol"> <label for="nm">Netmask:</label></div>
|
||||
<div class="rcol"><input id="nm" class="ip" type="text" minlength="7" maxlength="15" size="15"/></div>
|
||||
<div class="lcol"> <label for="netmask">Netmask:</label></div>
|
||||
<div class="rcol"><input id="netmask" class="ip" type="text" minlength="7" maxlength="15" size="15"/></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="lcol"> <label for="gw">Gateway:</label></div>
|
||||
|
||||
+21
-8
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user