can not -> can't routers -> router's continure -> continue to next -> to the next TMP -> temporary for current -> for the current continure -> continue with next -> with the next thier -> there provides -> provide possition -> position therfore -> therefore
96 lines
2.8 KiB
Lua
Executable File
96 lines
2.8 KiB
Lua
Executable File
#!/usr/bin/lua
|
|
|
|
-- PID file to ensure the hoodselector isn't running parallel
|
|
local pidPath="/var/run/hoodselector.pid"
|
|
|
|
local uci = require('simple-uci').cursor()
|
|
local hoodutil = require("hoodselector.util")
|
|
|
|
if io.open(pidPath, "r") ~=nil then
|
|
hoodutil.log("The hoodselector is still running.")
|
|
os.exit(1)
|
|
else
|
|
if io.open(pidPath, "w") ==nil then
|
|
hoodutil.log("Can`t create pid file on "..pidPath)
|
|
os.exit(1)
|
|
end
|
|
end
|
|
|
|
-- Program terminating function including removing of PID file
|
|
local function exit(exc)
|
|
if io.open(pidPath, "r") ~=nil then
|
|
os.remove(pidPath)
|
|
end
|
|
os.exit(tonumber(exc))
|
|
end
|
|
|
|
-- initialization done
|
|
--
|
|
local function get_mesh_vpn_interface()
|
|
local ret = {}
|
|
if hoodutil.fastd_installed() then
|
|
local vpnifac = uci:get('fastd', 'mesh_vpn_backbone', 'net')
|
|
if vpnifac ~= nil then
|
|
vpnifac = vpnifac:gsub("%_",'-')
|
|
table.insert(ret,vpnifac)
|
|
else
|
|
hoodutil.log("fastd uci config broken! abort...")
|
|
exit(1)
|
|
end
|
|
end
|
|
if hoodutil.tunneldigger_installed() then
|
|
local vpnifac = uci:get('tunneldigger', 'mesh_vpn', 'interface')
|
|
if vpnifac ~= nil then
|
|
table.insert(ret,vpnifac)
|
|
else
|
|
hoodutil.log("tunneldigger uci config broken! abort...")
|
|
exit(1)
|
|
end
|
|
end
|
|
return ret
|
|
end
|
|
|
|
-- INITIALIZE AND PREPARE DATA --
|
|
-- read hoodfile...
|
|
local jhood = hoodutil.get_domains()
|
|
|
|
-- get defaul hood
|
|
local defaultHood = hoodutil.getDefaultHood(jhood)
|
|
|
|
-- VPN MODE
|
|
-- If we have a VPN connection then we will try to get the routers location and
|
|
-- select the hood coresponding to our location.
|
|
-- If no hood for the location has been defined, we will select
|
|
-- the default hood.
|
|
-- If we can't get our router's location, we will continue to the next mode.
|
|
if hoodutil.directVPN(get_mesh_vpn_interface()) then
|
|
io.stdout:write('VPN connection found.\n')
|
|
local geo = hoodutil.getGeolocation()
|
|
if geo.lat ~= nil and geo.lon ~= nil then
|
|
io.stdout:write('Position found.\n')
|
|
local geoHood = hoodutil.getHoodByGeo(jhood, geo)
|
|
if geoHood ~= nil then
|
|
if hoodutil.set_hoodconfig(geoHood) then
|
|
hoodutil.restart_services() -- temporary solution
|
|
io.stdout:write('Hood set by VPN mode.\n')
|
|
end
|
|
exit(0)
|
|
end
|
|
io.stdout:write('No hood has been defined for the current position.\n')
|
|
if hoodutil.set_hoodconfig(defaultHood) then
|
|
hoodutil.restart_services() -- TMP solution
|
|
io.stdout:write('Hood set by VPN mode.\n')
|
|
end
|
|
exit(0)
|
|
else
|
|
-- The hoodselector should continue with the next states because there can be other
|
|
-- VPN routers in the local mesh network which provide a position and therefore
|
|
-- have set a geo base hood.
|
|
io.stdout:write('No position found\n')
|
|
end
|
|
else
|
|
io.stdout:write('No VPN connection found\n')
|
|
end
|
|
|
|
exit(0)
|