Complete migration of random /etc/config.mesh files into UCI (#1743)

This commit is contained in:
Tim Wilkinson 2024-12-03 23:46:32 -08:00 committed by GitHub
parent fb6af52305
commit e824a6cb0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 159 additions and 152 deletions

View File

@ -132,7 +132,7 @@ for (let i = 0; i < length(res); i++) {
}
}
}
const f = fs.open(dhcp.leases);
const f = fs.open("/tmp/dhcp.leases");
if (f) {
for (let l = f.read("line"); length(l); l = f.read("line")) {
// ?, mac, ip, name, ?

View File

@ -50,25 +50,29 @@ if (request.env.REQUEST_METHOD === "PUT") {
}
if ("aliases" in request.args) {
const aliases = json(request.args.aliases);
const dhcp = configuration.getDHCP();
let f = fs.open(dhcp.aliases, "w");
if (f) {
for (let i = 0; i < length(aliases); i++) {
f.write(`${aliases[i]}\n`);
if (!uciMesh.get("setup", "aliases")) {
uciMesh.set("setup", "aliases", "aliases");
}
f.close();
if (length(aliases)) {
uciMesh.set("setup", "aliases", "alias", aliases);
}
else {
uciMesh.delete("setup", "aliases", "alias");
}
uciMesh.commit("setup");
}
if ("ports" in request.args) {
const ports = json(request.args.ports);
const dhcp = configuration.getDHCP();
let f = fs.open(dhcp.ports, "w");
if (f) {
for (let i = 0; i < length(ports); i++) {
f.write(`${ports[i]}\n`);
if (!uciMesh.get("setup", "ports")) {
uciMesh.set("setup", "ports", "ports");
}
f.close();
if (length(ports)) {
uciMesh.set("setup", "ports", "port", ports);
}
else {
uciMesh.delete("setup", "ports", "port");
}
uciMesh.commit("setup");
}
print(_R("changes"));
return;
@ -158,27 +162,21 @@ for (let i = 0; i < length(res); i++) {
}
}
}
let f = fs.open(dhcp.aliases);
if (f) {
for (let l = f.read("line"); length(l); l = f.read("line")) {
const v = match(trim(l), /^(.+) (.+)$/);
const als = uciMesh.get("setup", "aliases", "alias") || [];
for (let i = 0; i < length(als); i++) {
const v = match(trim(als[i]), /^(.+) (.+)$/);
if (v) {
push(aliases.map, { hostname: v[2], address: v[1] });
}
}
f.close();
}
const ports = [];
f = fs.open(dhcp.ports);
if (f) {
for (let l = f.read("line"); length(l); l = f.read("line")) {
const m = match(trim(l), /^(wan|wifi|both):(tcp|udp|both):([0-9\-]+):([.0-9]+):([0-9]+):([01])$/);
const pts = uciMesh.get("setup", "ports", "port") || [];
for (let l = 0; l < length(pts); l++) {
const m = match(trim(pts[l]), /^(wan|wifi|both):(tcp|udp|both):([0-9\-]+):([.0-9]+):([0-9]+):([01])$/);
if (m) {
push(ports, { src: m[1], type: m[2], sports: m[3], dst: m[4], dport: m[5], enabled: m[6] === "1" });
}
}
f.close();
}
%}
<div class="dialog">
{{_R("dialog-header", "Local Services")}}

View File

@ -62,27 +62,20 @@ if (request.env.REQUEST_METHOD === "PUT") {
configuration.setSetting("dmz_dhcp_start", 2);
configuration.setSetting("dmz_dhcp_end", (2 << (mode - 1)) - 2);
const dhcp = configuration.getDHCP();
let f = fs.open(dhcp.aliases);
if (f) {
const als = uciMesh.get("setup", "aliases", "alias");
if (als) {
const aliases = [];
const dmz_dhcp_start = (wifi_shift + 2) & 0xff;
const dmz_dhcp_end = dmz_dhcp_start + (2 << (mode - 1)) - 4;
const n_lan_ip = iptoarr(dmz_lan_ip);
for (let l = f.read("line"); length(l); l = f.read("line")) {
const v = match(trim(l), /^(.+) (.+)$/);
for (let l = 0; l < length(als); l++) {
const v = match(trim(als[l]), /^(.+) (.+)$/);
if (v) {
const octet = max(dmz_dhcp_start, min(dmz_dhcp_end, n_lan_ip[3] + iptoarr(v[1])[3] - o_lan_ip[3]));
push(aliases, `${arrtoip([ n_lan_ip[0], n_lan_ip[1], n_lan_ip[2], octet ])} ${v[2]}\n`);
}
}
f.close();
f = fs.open(dhcp.aliases, "w");
if (f) {
for (let i = 0; i < length(aliases); i++) {
f.write(aliases[i]);
}
f.close();
}
uciMesh.set("setup", "aliases", "alias", aliases);
}
}
}

View File

@ -39,7 +39,7 @@
let at = 0;
let ao = 0;
if (dhcp.enabled) {
const f = fs.open(dhcp.leases);
const f = fs.open("/tmp/dhcp.leases");
if (f) {
while (length(f.read("line"))) {
da++;

View File

@ -106,7 +106,7 @@
if (dhcp.enabled) {
const leased = {};
f = fs.open(dhcp.leases);
f = fs.open("/tmp/dhcp.leases");
if (f) {
for (let l = f.read("line"); length(l); l = f.read("line")) {
const m = split(trim(l), " ");

View File

@ -53,7 +53,6 @@ if not c:get("setup", "globals") and nixio.fs.stat("/etc/config.mesh/_setup") th
end
end
end
c:commit("setup")
end
-- Migrate the old school _setup.services.{nat,dmz} files
@ -66,15 +65,16 @@ if not c:get("setup", "services") then
else
file = nil
end
c:set("setup", "services", "services")
if file then
local services = {}
for line in io.lines(file)
do
services[#services + 1] = line
end
c:set("setup", "services", "services")
if #services > 0 then
c:set("setup", "services", "service", services)
c:commit("setup")
end
end
end
@ -88,15 +88,16 @@ if not c:get("setup", "dhcpreservations") then
else
file = nil
end
c:set("setup", "dhcpreservations", "dhcpreservations")
if file then
local dhcp = {}
for line in io.lines(file)
do
dhcp[#dhcp + 1] = line
end
c:set("setup", "dhcpreservations", "dhcpreservations")
if #dhcp > 0 then
c:set("setup", "dhcpreservations", "reservation", dhcp)
c:commit("setup")
end
end
end
@ -110,15 +111,16 @@ if not c:get("setup", "dhcptags") then
else
file = nil
end
c:set("setup", "dhcptags", "dhcptags")
if file then
local dhcp = {}
for line in io.lines(file)
do
dhcp[#dhcp + 1] = line
end
c:set("setup", "dhcptags", "dhcptags")
if #dhcp > 0 then
c:set("setup", "dhcptags", "tag", dhcp)
c:commit("setup")
end
end
end
@ -132,17 +134,66 @@ if not c:get("setup", "dhcpoptions") then
else
file = nil
end
c:set("setup", "dhcpoptions", "dhcpoptions")
if file then
local dhcp = {}
for line in io.lines(file)
do
dhcp[#dhcp + 1] = line
end
c:set("setup", "dhcpoptions", "dhcpoptions")
if #dhcp > 0 then
c:set("setup", "dhcpoptions", "option", dhcp)
end
end
end
-- Migrate the old school _setup.ports.{nat,dmz} files
if not c:get("setup", "ports") then
local file = "/etc/config.mesh/ports."
if (nixio.fs.stat(file .. "nat", "size") or -1) >= 0 then
file = file .. "nat"
elseif (nixio.fs.stat(file .. "dmz", "size") or -1) >= 0 then
file = file .. "dmz"
else
file = nil
end
c:set("setup", "ports", "ports")
if file then
local ports = {}
for line in io.lines(file)
do
ports[#ports + 1] = line
end
if #ports > 0 then
c:set("setup", "ports", "port", ports)
end
end
end
-- Migrate the old school _setup.aliases.{nat,dmz} files
if not c:get("setup", "aliases") then
local file = "/etc/config.mesh/aliases."
if (nixio.fs.stat(file .. "nat", "size") or -1) >= 0 then
file = file .. "nat"
elseif (nixio.fs.stat(file .. "dmz", "size") or -1) >= 0 then
file = file .. "dmz"
else
file = nil
end
c:set("setup", "aliases", "aliases")
if file then
local aliases = {}
for line in io.lines(file)
do
aliases[#aliases + 1] = line
end
if #aliases > 0 then
c:set("setup", "aliases", "alias", aliases)
end
end
end
c:commit("setup")
end
end
-- Dont remove this yet otherwise we cannot revert this node
-- os.remove("/etc/config.mesh/_setup")
@ -154,6 +205,11 @@ end
-- os.remove("/etc/config.mesh/_setup.dhcptags.nat")
-- os.remove("/etc/config.mesh/_setup.dhcpoptions.dmz")
-- os.remove("/etc/config.mesh/_setup.dhcpoptions.nat")
-- os.remove("/etc/config.mesh/_setup.ports.dmz")
-- os.remove("/etc/config.mesh/_setup.ports.nat")
-- os.remove("/etc/config.mesh/aliases.dmz")
-- os.remove("/etc/config.mesh/aliases.nat")
-- os.remove("/etc/config.mesh/aliases")
__EOF__
/usr/bin/lua /tmp/setup_migrate

View File

@ -183,11 +183,11 @@ local function get(validate)
-- Load NAT
local nat = nil
if dmz_mode == "0" then
local portfile = "/etc/config.mesh/_setup.ports.nat"
if nixio.fs.access(portfile) then
local ports = uci.cursor("/etc/config.mesh"):get_all("setup", "ports", "port")
if ports then
nat = {}
local lname = name:lower() .. ".local.mesh"
for line in io.lines(portfile)
for _, line in ipairs(ports)
do
local _, type, sport, addr, dport, enable = line:match("^(.+):(.+):(.+):(.+):(%d+):(%d)$")
if enable == "1" then

View File

@ -307,29 +307,6 @@ do
end
end
-- select ports and dhcp files based on mode
local portfile = "/etc/config.mesh/_setup.ports"
local aliasfile = "/etc/config.mesh/aliases"
if is_nat_mode() then
portfile = portfile .. ".nat"
aliasfile = aliasfile .. ".nat"
else
portfile = portfile .. ".dmz"
aliasfile = aliasfile .. ".dmz"
end
-- check for old aliases file, copy it to .dmz and create symlink
-- just in case anyone is already using the file for some script or something
if not nixio.fs.readlink("/etc/config.mesh/aliases") then
if nixio.fs.stat("/etc/config.mesh/aliases") then
filecopy("/etc/config.mesh/aliases", "/etc/config.mesh/aliases.dmz")
os.remove("/etc/config.mesh/aliases")
else
io.open("/etc/config.mesh/aliases.dmz", "a"):close()
end
nixio.fs.symlink("aliases.dmz", "/etc/config.mesh/aliases")
end
-- generate the new school bridge configuration
if nixio.fs.stat("/etc/aredn_include/bridge.network.user") then
cfg.bridge_network_config = expand_vars(read_all("/etc/aredn_include/bridge.network.user"))
@ -703,10 +680,9 @@ if fw then
end
end
if nixio.fs.access(portfile) then
for line in io.lines(portfile)
local ports = cm:get_all("setup", "ports", "port") or {}
for _, line in ipairs(ports)
do
if not (line:match("^%s*#") or line:match("^%s*$")) then
local dip = line:match("dmz_ip = (%w+)")
if dip and is_dmz_mode() then
fw:write("\nconfig redirect\n option src wifi\n option proto tcp\n option src_dip " .. cfg.wifi_ip .. "\n option dest_ip " .. dip .. "\n")
@ -749,8 +725,6 @@ if fw then
end
end
end
end
end
fw:close();
end
@ -1081,11 +1055,9 @@ if h and e then
-- aliases need to ba added to /etc/hosts or they will now show up on the localnode
-- nor will the services thehy offer
-- also add a comment to the hosts file so we can display the aliases differently if needed
local f = io.open(aliasfile, "r")
if f then
for line in f:lines()
local aliases = cm:get_all("setup", "aliases", "alias") or {}
for _, line in ipairs(aliases)
do
if not (line:match("^%s*#") or line:match("^%s*$")) then
local ip, host = line:match("(%S+)%s+(%S+)")
if ip then
if host:match("%.") and not host:match("%.local%.mesh$") then
@ -1094,9 +1066,6 @@ if h and e then
h:write(ip .. "\t" .. host .. " #ALIAS\n")
end
end
end
f:close()
end
h:write("\n")

View File

@ -219,10 +219,7 @@ export function getDHCP(mode)
end: `${i[0]}.${i[1]}.${(e >> 8) & 255}.${e & 255}`,
gateway: setup.lan_ip,
mask: setup.lan_mask,
cidr: network.netmaskToCIDR(setup.lan_mask),
leases: "/tmp/dhcp.leases",
ports: "/etc/config.mesh/_setup.ports.nat",
aliases: "/etc/config.mesh/aliases.nat"
cidr: network.netmaskToCIDR(setup.lan_mask)
};
}
else if (setup.dmz_mode === "1") {
@ -239,10 +236,7 @@ export function getDHCP(mode)
end: `${i[0]}.${i[1]}.${(e >> 8) & 255}.${e & 255}`,
gateway: setup.lan_ip,
mask: setup.lan_mask,
cidr: network.netmaskToCIDR(setup.lan_mask),
leases: "/tmp/dhcp.leases",
ports: "/etc/config.mesh/_setup.ports.dmz",
aliases: "/etc/config.mesh/aliases.dmz"
cidr: network.netmaskToCIDR(setup.lan_mask)
};
}
else {
@ -259,10 +253,7 @@ export function getDHCP(mode)
end: `${i[0]}.${i[1]}.${(e >> 8) & 255}.${e & 255}`,
gateway: setup.dmz_lan_ip,
mask: setup.dmz_lan_mask,
cidr: network.netmaskToCIDR(setup.dmz_lan_mask),
leases: "/tmp/dhcp.leases",
ports: "/etc/config.mesh/_setup.ports.dmz",
aliases: "/etc/config.mesh/aliases.dmz"
cidr: network.netmaskToCIDR(setup.dmz_lan_mask)
};
}
};