#!/usr/bin/lua
--[[
Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks
Copyright (C) 2021 Tim Wilkinson
Original Perl Copyright (c) 2015 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.http")
require("aredn.utils")
require("aredn.html")
require("aredn.hardware")
aredn.info = require("aredn.info")
require("uci")
require("luci.sys")
local html = aredn.html
local cursor = uci.cursor();
local node = aredn.info.get_nvram("node")
if node == "" then
node = "NOCALL"
end
local config = aredn.info.get_nvram("config");
local VPNVER = "1.1"
-- post_data
local parms = {}
if os.getenv("REQUEST_METHOD") == "POST" then
require('luci.http')
local request = luci.http.Request(luci.sys.getenv(),
function()
local v = io.read(1024)
if not v then
io.close()
end
return v
end
)
parms = request:formvalue()
end
-- helpers start
local cli_err = {}
function err(msg)
cli_err[#cli_err + 1] = msg
end
local errors = {}
function err2(msg)
errors[#errors + 1] = msg
end
local hidden = {}
function hide(inp)
hidden[#hidden + 1] = inp
end
function navbar()
html.print("
")
html.print("
")
end
function get_active_tun()
local tuns = {}
local f = io.popen("ps -w | grep vtun | grep ' tun '")
if f then
for line in f:lines()
do
local m = line:match(".*:.*-(172%-31%-.*)%stun%stun.*")
if m then
tuns[#tuns + 1] = m:gsub("-", ".")
end
end
f:close()
end
return tuns
end
function is_tunnel_active(ip, tunnels)
for _, aip in ipairs(tunnels)
do
if ip == aip then
return true
end
end
return false
end
function get_server_network_address()
local server_net = cursor:get("vtun", "@network[0]", "start")
if not server_net then
local mac = aredn.hardware.get_interface_mac("eth0")
local a, b = mac:match("^..:..:..:..:(..):(..)$")
server_net = "172.31." .. tonumber(b, 16) .. "." .. ((tonumber(a, 16) * 4) % 256)
cursor:set("vtun", "@network[0]", "start", server_net)
cursor:commit("vtun")
end
local a, b, c, d = server_net:match("^(%d+).(%d+).(%d+).(%d+)$")
return { a, b, c, d }
end
function get_server_dns()
local dns = cursor:get("vtun", "@network[0]", "dns")
return dns and dns or ""
end
function install_vtun()
local vfs = nixio.fs.statvfs("/overlay")
local fspace = vfs.bfree * vfs.bsize / 1024
if fspace < 600 then
err("Insufficient free disk space")
return
end
if os.execute("opkg update > /dev/null 2>&1") ~= 0 then
err("Package update failed!")
return
end
if os.execute("opkg install kmod-tun zlib liblzo vtun > /dev/null 2>&1") ~= 0 then
err("Package installation failed!")
return
end
if not cursor:get("aredn", "@tunnel[0]") then
cursor:add("aredn", "tunnel")
end
cursor:set("aredn", "@tunnel[0]", "maxclients", "10")
cursor:set("aredn", "@tunnel[0]", "maxservers", "10")
cursor:commit("aredn")
write_all("/etc/config.mesh/aredn", read_all("/etc/config/aredn"))
io.open("/etc/config/network_tun", "w"):close()
for tunnum = 50,69
do
cursor:set("network_tun", "tun" .. tunnum, "interface")
cursor:set("network_tun", "tun" .. tunnum, "ifname", "tun" .. tunnum)
cursor:set("network_tun", "tun" .. tunnum, "proto", "none")
end
cursor:commit("network_tun")
write_all("/etc/config.mesh/network_tun", read_all("/etc/config/network_tun"))
os.execute("cat /etc/config.mesh/network_tun >> /etc/config.mesh/network")
os.execute("cat /etc/config.mesh/network_tun >> /etc/config/network")
io.open("/etc/config/vtun", "a"):close()
cursor:add("vtun", "options")
cursor:add("vtun", "network")
cursor:commit("vtun")
http_header()
html.header("TUNNEL INSTALLATION IN PROGRESS", true)
html.print("")
html.print("Installing tunnel software...")
html.print("DO NOT REMOVE POWER UNTIL THE INSTALLATION IS FINISHED")
html.print(" ")
html.print([[
The node is rebooting
]])
html.footer()
html.print(" |