#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (C) 2020 - Darryl Quinn
See Contributors file for additional contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Additional Terms:
Additional use restrictions exist on the AREDN(TM) trademark and logo.
See AREDNLicense.txt for more info.
Attributions to the AREDN Project must be retained in the source code.
If importing this code into a new or existing project attribution
to the AREDN project must be added to the source code.
You must not misrepresent the origin of the material contained within.
Modified versions must be modified to attribute to the original source
and be marked in reasonable ways as differentiate it from the original
version
--]]
require("nixio")
require("aredn.hardware")
require("aredn.http")
require("aredn.utils")
aredn.html = require("aredn.html")
require("uci")
aredn.info = require("aredn.info")
require("luci.sys")
local html = aredn.html
local urlprefix
local target = "unknown"
function defaultPackageRepos(repo)
if not urlprefix then
urlprefix = "http://downloads.arednmesh.org"
local release = "unknown"
for line in io.lines("/etc/openwrt_release")
do
local m = line:match("DISTRIB_RELEASE='(.*)'")
if m then
release = m
end
m = line:match("DISTRIB_TARGET='(.*)'")
if m then
target = m
end
end
if release:match("%.") then
local a, b = release:match("^(%d+)%.(%d+)%.")
urlprefix = urlprefix .. "/releases/" .. a .. "/" .. b .. "/" .. release
else
-- nightly
urlprefix = urlprefix .. "/snapshots"
end
end
if repo:match("aredn_core") then
return urlprefix .. "/targets/" .. target .. "/packages"
else
return urlprefix .. "/packages/mips_24kc/" .. repo
end
end
local settings = {
{
category = "Map Paths",
key = "aredn.@map[0].maptiles",
type = "string",
desc = "Specifies the URL of the location to access map tiles",
default = "http://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.jpg"
},
{
category = "Map Paths",
key = "aredn.@map[0].leafletcss",
type = "string",
desc = "Specifies the URL of the leaflet.css file",
default = "http://unpkg.com/leaflet@0.7.7/dist/leaflet.css"
},
{
category = "Map Paths",
key = "aredn.@map[0].leafletjs",
type = "string",
desc = "Specifies the URL of the leaflet.js file",
default = "http://unpkg.com/leaflet@0.7.7/dist/leaflet.js"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].firmwarepath",
type = "string",
desc = "Specifies the URL of the location from which firmware files will be downloaded.",
default = "http://downloads.arednmesh.org/firmware"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_core",
type = "string",
desc = "Specifies the URL for the 'core' packages: kernel modules and the like",
default = defaultPackageRepos('aredn_core'),
postcallback = "writePackageRepo('core')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_base",
type = "string",
desc = "Specifies the URL for the 'base' packages: libraries, shells, etc.",
default = defaultPackageRepos('base'),
postcallback = "writePackageRepo('base')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_arednpackages",
type = "string",
desc = "Specifies the URL for the 'arednpackages' packages: vtun, etc.",
default = defaultPackageRepos('arednpackages'),
postcallback = "writePackageRepo('arednpackages')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_luci",
type = "string",
desc = "Specifies the URL for the 'luci' packages: luci and things needed for luci.",
default = defaultPackageRepos('luci'),
postcallback = "writePackageRepo('luci')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_packages",
type = "string",
desc = "Specifies the URL for the 'packages' packages: everything not included in the other dirs.",
default = defaultPackageRepos('packages'),
postcallback = "writePackageRepo('packages')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_routing",
type = "string",
desc = "Specifies the URL for the 'routing' packages: olsr, etc.",
default = defaultPackageRepos('routing'),
postcallback = "writePackageRepo('routing')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_telephony",
type = "string",
desc = "Specifies the URL for the 'telephony' packages.",
default = defaultPackageRepos('telephony'),
postcallback = "writePackageRepo('telephony')"
},
{
category = "Firmware Paths",
key = "aredn.@downloads[0].pkgs_freifunk",
type = "string",
desc = "Specifies the URL for the 'freifunk' packages.",
default = defaultPackageRepos('freifunk'),
postcallback = "writePackageRepo('freifunk')"
},
{
category = "Power Options",
key = "aredn.@poe[0].passthrough",
type = "boolean",
desc = "Specifies whether a PoE passthrough port should be on or off. (Not all devices have PoE passthrough ports).",
default = "0",
condition = "hasPOE()",
postcallback = "setPOEOutput()"
},
{
category = "Power Options",
key = "aredn.@usb[0].passthrough",
type = "boolean",
desc = "Specifies whether the USB port should be on or off. (Not all devices have USB powered ports).",
default = "1",
postcallback = "setUSBOutput()",
condition = "hasUSB()"
},
{
category = "Tunnel Options",
key = "aredn.@tunnel[0].maxclients",
type = "string",
desc = "Specifies the maximum number of tunnel clients this node can serve; must be an integer in the range [0,100].",
default = "10",
precallback = "restrictTunnelLimitToValidRange()",
postcallback = "adjustTunnelInterfaceCount()"
},
{
category = "Tunnel Options",
key = "aredn.@tunnel[0].maxservers",
type = "string",
desc = "Specifies the maximum number of tunnel servers to which this node can connect; must be an integer in the range [0,100].",
default = "10",
precallback = "restrictTunnelLimitToValidRange()",
postcallback = "adjustTunnelInterfaceCount()"
},
{
category = "Tunnel Options",
key = "aredn.@tunnel[0].wanonly",
type = "boolean",
desc = "Prevents tunnel traffic from being routed over the mesh network itself.",
default = "1",
needreboot= true
},
{
category = "Memory",
key = "aredn.@meshstatus[0].lowmem",
type = "string",
desc = "Specifies the low memory threshold (in KB) when we will truncate the mesh status page",
default = "10000"
},
{
category = "Memory",
key = "aredn.@meshstatus[0].lowroutes",
type = "string",
desc = "When low memory is detected, limit the number of routes shown on the mesh status page",
default = "1000"
},
{
category = "WAN",
key = "aredn.wan.vlanid",
type = "string",
desc = "Specify WAN VLAN #",
default = "",
condition = "supportsVLANChange()",
current = "currentWANVLAN()",
postcallback = "changeWANVLAN()",
needreboot = true
},
{
category = "OLSR",
key = "aredn.olsr.restart",
type = "none",
desc = "Will restart OLSR when saving setting -- wait up to 2 or 3 minutes to receive response.",
default = "0",
postcallback = "olsr_restart()"
},
{
category = "AREDN Alerts",
key = "aredn.aam.refresh",
type = "none",
desc = "Attempt to pull any AREDN Alert messages.",
default = "0",
postcallback = "aam_refresh()"
},
{
category = "AREDN Alerts",
key = "aredn.@alerts[0].localpath",
type = "string",
desc = "Specifies the URL of the location from which local AREDN Alerts can be downloaded.",
default = ""
},
{
category = "AREDN Alerts",
key = "aredn.@alerts[0].pollrate",
type = "string",
desc = "Specifies how many hours to wait between polling for new AREDN Alerts.",
default = "12",
needreboot = true
},
{
category = "AREDN Alerts",
key = "aredn.aam.purge",
type = "none",
desc = "Immediately purge/delete all AREDN (and local) Alerts from this node.",
default = "",
postcallback = "alert_purge()"
},
{
category = "iPerf",
key = "aredn.@iperf[0].enable",
type = "boolean",
desc = "Enable the included iperf3 client/server support",
default = "1"
}
}
local msgs = {}
--
-- helpers
--
function msg(m)
msgs[#msgs + 1] = m
end
-- uci cursor
local cursora = uci.cursor()
local cursorb = uci.cursor("/etc/config.mesh")
function cursor_set(a, b, c, d)
if not cursora:get(a, b) and b:match("@(.+)%[0%]") then
cursora:add(a, b:match("@(.+)%[0%]"))
end
cursora:set(a, b, c, d)
if not cursorb:get(a, b) and b:match("@(.+)%[0%]") then
cursorb:add(a, b:match("@(.+)%[0%]"))
end
cursorb:set(a, b, c, d)
cursora:commit(a)
cursorb:commit(a)
end
function cursor_add(a, b, c)
cursora:set(a, b, c)
cursorb:set(a, b, c)
cursora:commit(a)
cursorb:commit(a)
end
function cursor_delete(a, b)
cursora:delete(a, b)
cursorb:delete(a, b)
cursora:commit(a)
cursorb:commit(a)
end
function cursor_get(a, b, c)
return cursora:get(a, b, c)
end
function reboot()
local node = aredn.info.get_nvram("node")
if node == "" then
node = "Node"
end
local lanip, _, lanmask = aredn.hardware.get_interface_ip4(aredn.hardware.get_iface_name("lan"))
local browser = os.getenv("REMOTE_ADDR")
local browser6 = browser:match("::ffff:([%d%.]+)")
if browser6 then
browser = browser6
end
local fromlan = false
local subnet_change = false
if lanip then
fromlan = validate_same_subnet(browser, lanip, lanmask)
if fromlan then
lanmask = ip_to_decimal(lanmask)
local cfgip = cursor_get("network", "lan", "ipaddr")
local cfgmask = ip_to_decimal(cursor_get("network", "lan", "netmask"))
if lanmask ~= cfgmask or nixio.bit.band(ip_to_decimal(lanip), lanmask) ~= nixio.bit.band(ip_to_decimal(cfgip), cfgmask) then
subnet_change = true
end
end
end
http_header()
if fromlan and subnet_change then
html.header(node .. " rebooting", true)
html.print("
")
html.print("
" .. node .. " is rebooting
")
html.print("
The LAN subnet has changed. You will need to acquire a new DHCP lease ")
html.print("and reset any name service caches you may be using.