gluon-hoodselector: drop VPN mode and rename hood to domain. Furthermore implement geolocator mode as neorayder way

Signed-off-by: Jan-Tarek Butt <tarek@ring0.de>
This commit is contained in:
Jan-Tarek Butt 2019-02-25 13:26:42 +01:00
parent 7f24875a26
commit d5ab39189b
2 changed files with 44 additions and 70 deletions

View File

@ -3,18 +3,9 @@ local util = require 'gluon.util'
local json = require 'jsonc' local json = require 'jsonc'
local uci = require('simple-uci').cursor() local uci = require('simple-uci').cursor()
local site = require 'gluon.site' local site = require 'gluon.site'
local vpn_util = require('gluon.mesh-vpn')
local logger = require('posix.syslog') local logger = require('posix.syslog')
local M = {} local M = {}
function M.split(s, delimiter)
local result = {}
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end
function M.log(msg) function M.log(msg)
io.stdout:write(msg..'\n') io.stdout:write(msg..'\n')
logger.openlog(msg, logger.LOG_PID) logger.openlog(msg, logger.LOG_PID)
@ -31,31 +22,23 @@ function M.get_domains()
return list return list
end end
-- Return the default hood in the hood list. -- Return the default domain from the domaon list.
-- This method can return the following data: -- This method can return the following data:
-- * default hood -- * default domain
-- * nil if no default hood has been defined -- * nil if no default domain has been defined
function M.get_default_hood(jhood) function M.get_default_domain(jdomains)
for _, h in pairs(jhood) do for _, domain in pairs(jdomains) do
if h.domain_code == site.default_domain() then if domain.domain_code == site.default_domain() then
return h return domain
end end
end end
return nil return nil
end end
-- Returns true if the node has an established running VPN connection. -- Get Geoposition.
function M.direct_vpn() -- This method can return the following data:
for outgoing_if in io.popen(string.format("batctl o"), 'r'):lines() do -- * table {lat, lon}
-- escape special chars "[]-" -- * nil for no position
if outgoing_if:match(string.gsub("%[ " .. vpn_util.get_mesh_vpn_interface() .. "%]","%-", "%%-")) then
return true
end
end
return false
end
-- Get Geoposition. Return nil for no position
function M.get_geolocation() function M.get_geolocation()
return { return {
lat = tonumber(uci:get('gluon-node-info', uci:get_first('gluon-node-info', 'location'), 'latitude')), lat = tonumber(uci:get('gluon-node-info', uci:get_first('gluon-node-info', 'location'), 'latitude')),
@ -115,21 +98,21 @@ function M.point_in_polygon(poly, point)
return t return t
end end
-- Return hood from the hood file based on geo position or nil if no real hood could be determined -- Return domain from the domain list based on geo position or nil if no geo base domain could be
-- First check if an area has > 2 points and is hence a polygon. Else assume it is a rectangular -- determined. First check if an area has > 2 points and is hence a polygon. Else assume it is a
-- box defined by two points (south-west and north-east) -- rectangular box defined by two points (south-west and north-east)
function M.get_hood_by_geo(jhood,geo) function M.get_domain_by_geo(jdomains,geo)
for _, hood in pairs(jhood) do for _, domain in pairs(jdomains) do
if hood.domain_code ~= site.default_domain() then if domain.domain_code ~= site.default_domain() then
for _, area in pairs(hood.domain.hoodselector.shapes) do for _, area in pairs(domain.domain.hoodselector.shapes) do
if #area > 2 then if #area > 2 then
if (M.point_in_polygon(area,geo) == 1) then if (M.point_in_polygon(area,geo) == 1) then
return hood return domain
end end
else else
if ( geo.lat >= area[1].lat and geo.lat < area[2].lat and geo.lon >= area[1].lon if ( geo.lat >= area[1].lat and geo.lat < area[2].lat and geo.lon >= area[1].lon
and geo.lon < area[2].lon ) then and geo.lon < area[2].lon ) then
return hood return domain
end end
end end
end end
@ -138,12 +121,12 @@ function M.get_hood_by_geo(jhood,geo)
return nil return nil
end end
function M.set_hoodconfig(geo_hood) function M.set_domain_config(domain)
if uci:get('gluon', 'core', 'domain') ~= geo_hood.domain_code then if uci:get('gluon', 'core', 'domain') ~= domain.domain_code then
uci:set('gluon', 'core', 'domain', geo_hood.domain_code) uci:set('gluon', 'core', 'domain', domain.domain_code)
uci:commit('gluon') uci:commit('gluon')
os.execute('gluon-reconfigure') os.execute('gluon-reconfigure')
M.log('Set domain "'..geo_hood.domain.domain_names[geo_hood.domain_code]..'"') M.log('Set domain "'..domain.domain.domain_names[domain.domain_code]..'"')
return true return true
end end
return false return false

View File

@ -29,38 +29,29 @@ if not ok then
os.exit(1) os.exit(1)
end end
-- VPN MODE -- geolocation mode
-- If we have a VPN connection we will try to get the node's location and -- If we have a location we will try to select the domain coresponding to this location.
-- select the hood coresponding to this location. -- If no domain for the location has been defined or if we can't determine the node's location,
-- If no hood for the location has been defined, we will select -- we will select the default domain as last fallback instanc.
-- the default hood. local geo = hoodutil.get_geolocation()
-- If we can't determine the node's location, we will continue to the next mode. if geo.lat ~= nil and geo.lon ~= nil then
if hoodutil.direct_vpn() then io.stdout:write('Position found. Enter "geolocation mode" ...\n')
io.stdout:write('VPN connection found.\n') local jdomains = hoodutil.get_domains()
local geo = hoodutil.get_geolocation() local geo_base_domain = hoodutil.get_domain_by_geo(jdomains, geo)
if geo.lat ~= nil and geo.lon ~= nil then if geo_base_domain ~= nil then
io.stdout:write('Position found.\n') if hoodutil.set_domain_config(geo_base_domain) then
local jhood = hoodutil.get_domains()
local geo_hood = hoodutil.get_hood_by_geo(jhood, geo)
if geo_hood ~= nil then
if hoodutil.set_hoodconfig(geo_hood) then
hoodutil.restart_services() hoodutil.restart_services()
hoodutil.log('Domain set by VPN mode.\n') hoodutil.log('Domain set by geolocation mode.\n')
end end
return return
end end
io.stdout:write('No domain has been defined for the current position.\n') io.stdout:write('No domain has been defined for the current position. Continure with default domain mode\n')
if hoodutil.set_hoodconfig(hoodutil.get_default_hood(jhood)) then
hoodutil.restart_services()
hoodutil.log('Domain set by VPN mode.\n')
end
return
else
-- The hoodselector should continue with the next states because there can be other
-- VPN nodes 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 else
io.stdout:write('No VPN connection found\n') io.stdout:write('No position found. Continure with default domain mode\n')
end
-- default domain mode
if hoodutil.set_domain_config(hoodutil.get_default_domain(hoodutil.get_domains())) then
hoodutil.restart_services()
hoodutil.log('Set default domain.\n')
end end