gluon/package/gluon-hoodselector/luasrc/usr/sbin/hoodselector

88 lines
2.6 KiB
Lua
Executable File

#!/usr/bin/lua
local nixio = require('nixio')
local uci = require('simple-uci').cursor()
local hoodutil = require("hoodselector.util")
-- PID file to ensure the hoodselector isn't running parallel
local lockfile = '/var/lock/hoodselector.lock'
local lockfd = nixio.open(lockfile, 'w', 'rw-------')
if not lockfd:lock('tlock') then
io.stderr:write(string.format(
"Unable to lock file %s. Make sure there is no other instance of the hoodselector running.\n",
lockfile
))
os.exit(1)
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...")
os.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...")
os.exit(1)
end
end
return ret
end
-- INITIALIZE AND PREPARE DATA --
-- read hoodfile...
local jhood = hoodutil.get_domains()
-- get default hood
local defaultHood = hoodutil.getDefaultHood(jhood)
-- VPN MODE
-- If we have a VPN connection we will try to get the router's 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
os.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
os.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
os.exit(0)