Skip to content

Commit

Permalink
gluon-hoodselector: backport tested comunity version:
Browse files Browse the repository at this point in the history
* Hoodselector: is now able to handle Polygon hoods. !60

* Hoodselector: does not use `scan dump` anymore which saved airtime
                this functionality is not avalable in LEDE annymore.

* Hoodselector: L2TP tunneldigger support #47

* Hoodselector: the upgrade script got a refactoring. Dead code is
                removed. desing failuer is fixed, the script use the
                hood BSSID instead the possible redundant hood name
                for hood identification. #107

* Hoodselector: New state, a router from hood A which is connected to an
                another router from hood B is now able to switch back in
                its own hood if a router from hood A viseble. #108

* Hoodselector: in state "radio less" is now ensured that mesh on LAN /
                WAN is enabled befor entering mode. #109

* Hoodselector: A bug in the function get_mesh_if() is fixed now. This
                function returns now a list of all mesh interfaces
                exzept the VPN interface #116

* Hoodselector: Old VPN configurations will be deleted now. If a hood
                without VPN peers got configured the old peers from the
                old hood was still presend. #117

* Hoodselector: many functions of the hoodselector are now placed in a
                lua libary. #118
  • Loading branch information
2tata committed Apr 23, 2018
1 parent f9be1b0 commit 25c3b18
Show file tree
Hide file tree
Showing 3 changed files with 1,239 additions and 1,003 deletions.
181 changes: 50 additions & 131 deletions package/gluon-hoodselector/luasrc/lib/gluon/upgrade/540-hoodselector
@@ -1,87 +1,14 @@
#!/usr/bin/lua

local json = require ("luci.jsonc")
local uci = require('simple-uci').cursor()

-- Read the full hoodfile. Return nil for wrong format or no such file
local function readHoodfile(file)
local jhood = io.open(file, 'r')
if not jhood then return nil end
local obj, _, err = json.parse (jhood:read('*a'), 1, nil)
if err then
return nil
else
return obj
end
end

local function split(s, delimiter)
local result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end

local function vpn_reconfigure(hood_serverlist,local_serverlist)
-- remove all servers
for config_index, _ in pairs(local_serverlist) do
uci:delete('fastd',config_index)
end

-- add servers from hoodfile
local group = 'mesh_vpn_backbone'
for _,hood_server in pairs(hood_serverlist) do
uci:section('fastd', 'peer', group .. '_peer_' .. split(hood_server.host, '.')[1]:gsub("%-", "%_") .. "_" .. hood_server.port,
{
enabled = 1,
net = 'mesh_vpn',
group = group,
key = hood_server.publickey,
remote = {'\"'..hood_server.host..'\"'..' port '..hood_server.port}
}
)
end

uci:save('fastd')
end

-- Reconfigure wireless
local function wireless_reconfigure(radios, hood_bssid)
for _, radio in ipairs(radios) do
if not ( uci:get('wireless', 'ibss_' .. radio, 'bssid') == hood_bssid ) then
uci:section('wireless', 'wifi-iface', 'ibss_' .. radio, {
bssid = hood_bssid
})
end
end
uci:save('wireless')
end
local hoodutil = require("hoodselector.util")

-- Retun a table of current peers from /etc/config/fastd
local function getCurrentPeers()
local configPeers = {}
local err = uci:foreach('fastd', 'peer',
function(s)
if s['.name'] then
for prefix,peer in pairs(s) do
local tmpPeer = {}
if prefix:match(".name") then
if peer:match("mesh_vpn_backbone_peer_") then
-- val tmpRemote does not need uci exception check because its already include by "uci:foreach"
local tmpRemote = uci:get('fastd', peer, 'remote')
tmpRemote = split(tmpRemote[1], " ")
local remote = {}
remote['host'] = tmpRemote[1]
remote[tmpRemote[2]] = tmpRemote[3]
-- uci:get does not need uci exception check because its already include by "uci:foreach"
tmpPeer['key'] = tostring(uci:get('fastd', peer, 'key'))
tmpPeer['remote'] = remote
configPeers[peer] = tmpPeer
end
end
end
end
hoodutil.extrac_fastd_peer(s,configPeers)
end
)
if not err then
Expand All @@ -90,80 +17,72 @@ local function getCurrentPeers()
return configPeers
end

local function getHoodByName(jhood,hoodname)
for _, h in pairs(jhood) do
if h.name == hoodname then
return h
end
end
return nil
end

-- Get a list of wifi devices return an emty table for no divices
local function getWifiDevices()
local radios = {}
uci:foreach('wireless', 'wifi-device',
function(s)
table.insert(radios, s['.name'])
end
)
return radios
end

local function getDefaultHood(jhood)
for _, h in pairs(jhood) do
if h.defaulthood then
return h
end
end
return nil
end

-- START HERE

local hoodfile = uci:get('hoodselector', 'hoodselector', 'hoodfile')
local hoodname = uci:get('hoodselector', 'hoodselector', 'hood')
local static = uci:get('hoodselector', 'hoodselector', 'static')
local enabled = uci:get('hoodselector', 'hoodselector', 'enabled')
local hoodbssid = uci:get('hoodselector', 'hoodselector', 'hood')

if hoodfile == nil then
os.exit(1)
end

if static == nil then
static = '0'
end

if enabled == nil then
enabled = '1'
end

local jhood = readHoodfile(hoodfile)
local jhood = hoodutil.readHoodfile(hoodfile)
if jhood == nil then
enabled = 0
uci:set('hoodselector', 'hoodselector', 'enabled', false)
uci:save('hoodselector')
os.exit(1)
end

if hoodname == nil then
if jhood ~= nil then
hoodname = (getDefaultHood(jhood)).name
if hoodbssid == nil then
local defaulthood = hoodutil.getDefaultHood(jhood)
if defaulthood == nil then
os.exit(1)
end
if defaulthood.bssid == nil then
os.exit(1)
end
hoodbssid = defaulthood.bssid
end

local hood = hoodutil.gethoodByBssid(jhood,hoodbssid)
if hood == nil then
os.exit(1)
end

uci:section('hoodselector', 'settings', 'hoodselector', {
hood = hoodname,
hood = hoodbssid,
hoodfile = hoodfile,
static = static,
enabled = enabled
enabled = true
})
uci:save('hoodselector')

local hood = getHoodByName(jhood,hoodname)
if hood == nil then
os.exit(1)
local radios = hoodutil.getWifiDevices()
-- pre check if fastd conf exsist
if uci:get('fastd', 'mesh_vpn_backbone', 'net') ~= nil then
hoodutil.fastd_reconfigure(hood["servers"],getCurrentPeers())
end
-- per check if tunneldigger conf exsist
if uci:get('tunneldigger', 'mesh_vpn', 'interface') ~= nil then
hoodutil.tunneldigger_reconfigure(hood["servers"])
end

local radios = getWifiDevices()
vpn_reconfigure(hood["servers"],getCurrentPeers())
if radios ~= nil then
wireless_reconfigure(radios, hood["bssid"])
if next(radios) then
local ibss_exists = false
local mesh_exists = false
for _,radio in ipairs(radios) do
local ifname = uci:get('wireless', 'ibss_' .. radio, 'ifname')
if (ifname ~= nil) then
ibss_exists = true
end
ifname = uci:get('wireless', 'mesh_' .. radio, 'ifname')
if (ifname ~= nil) then
mesh_exists = true
end
end
if ibss_exists then
hoodutil.ibss_reconfigure(radios, hood["bssid"])
end
if mesh_exists then
local mesh_prefix = hoodutil.get_mesh_prefix(radios)
hoodutil.mesh_reconfigure(radios, mesh_prefix..hood["bssid"]:lower())
end
end

0 comments on commit 25c3b18

Please sign in to comment.