Skip to content

Commit

Permalink
shared-state multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
G10h4ck committed Mar 8, 2019
1 parent b79eb3f commit de02a22
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 46 deletions.
@@ -1,4 +1,10 @@
#!/bin/sh

echo "*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync bat-hosts)&)" >> /etc/crontabs/root
echo "*/30 * * * * ((sleep $(($RANDOM % 1000)); shared-state-publish_bat_hosts)&)" >> /etc/crontabs/root
unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

unique_append \
'*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync bat-hosts)&)'\
/etc/crontabs/root
@@ -1,3 +1,10 @@
#!/bin/sh
echo "*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync dnsmasq-hosts)&)" >> /etc/crontabs/root
echo "*/30 * * * * ((sleep $(($RANDOM % 1000)); shared-state-publish_dnsmasq_hosts)&)" >> /etc/crontabs/root

unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

unique_append \
'*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync dnsmasq-hosts)&)'\
/etc/crontabs/root
2 changes: 1 addition & 1 deletion packages/shared-state-dnsmasq_leases/Makefile
Expand Up @@ -20,7 +20,7 @@ define Package/$(PKG_NAME)
MAINTAINER:=Gioacchino Mazzurco <gio@altermundi.net>
URL:=http://libremesh.org
DEPENDS:=@(!PACKAGE_dnsmasq-lease-share) +libuci-lua +lua \
+luci-lib-jsonc shared-state
+luci-lib-jsonc shared-state +shared-state-dnsmasq_hosts
PKGARCH:=all
endef

Expand Down
@@ -1,9 +1,22 @@
#!/bin/sh

dhcpHostsFile="/tmp/dhcp.hosts_remote"

uci set dhcp.@dnsmasq[0].dhcpscript=/usr/bin/dnsmasq-lease-share.sh
uci set dhcp.@dnsmasq[0].dhcphostsfile=/tmp/dhcp.hosts_remote
uci set dhcp.@dnsmasq[0].dhcphostsfile=$dhcpHostsFile
uci commit dhcp

echo "*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync dnsmasq-leases)&)" >> /etc/crontabs/root
unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

# needed because init script ignore +dhcphostsfile+ if the file doesn't exists
unique_append "dhcp-hostsfile=$dhcpHostsFile"\
"/etc/dnsmasq.d/shared-state-dhcp-leases"

unique_append \
'*/5 * * * * ((sleep $(($RANDOM % 120)); shared-state sync dnsmasq-leases)&)'\
/etc/crontabs/root

exit 0
@@ -1,2 +1,2 @@
#!/bin/sh
((shared-state-publish_dnsmasq_leases $@; shared-state sync dnsmasq-leases)&)
((shared-state-publish_dnsmasq_leases $@; shared-state sync dnsmasq-leases; shared-state sync dnsmasq-hosts)&)
Expand Up @@ -23,12 +23,6 @@ local outputTable = {}
for key,value in pairs(JSON.parse(io.stdin:read("*all"))) do
if value.data then
local hwaddr = value.data.mac

local id = ",*"
if string.len(value.data.id) > 1 then
id = ",id:"..value.data.id
end

local ipaddr = key
if ipaddr:find(":") then ipaddr = "[" .. ipaddr .. "]" end
ipaddr = ","..ipaddr
Expand All @@ -38,7 +32,7 @@ for key,value in pairs(JSON.parse(io.stdin:read("*all"))) do
hostname = ","..value.data.hostname
end

table.insert(outputTable, hwaddr..id..ipaddr..hostname)
table.insert(outputTable, hwaddr..ipaddr..hostname)
end
end

Expand Down
Expand Up @@ -27,13 +27,57 @@ function string:split(sep)
return ret
end

function add_tmp_dhcp_leases()
local leasesTable = {}
local hostTable = {}

for line in io.lines("/tmp/dhcp.leases") do
_, client_mac, client_ip, client_hostname, client_id = unpack(line:split(" "))
leasesTable[client_ip] = { hostname=client_hostname, --id=client_id,
mac=client_mac }
hostTable[client_ip.." "..client_hostname] = true
end

local jsonstring = JSON.stringify(leasesTable)

local cStdin = io.popen("shared-state insert dnsmasq-leases", "w")
cStdin:write(jsonstring)
cStdin:close()

local cStdin = io.popen("shared-state insert dnsmasq-hosts", "w")
cStdin:write(JSON.stringify(hostTable))
cStdin:close()
end

function add_lease(client_mac, client_ip, client_hostname, client_id)
local leaseData = {}
leaseData[client_ip] = { hostname=client_hostname, id=client_id,
leaseData[client_ip] = { hostname=client_hostname, --id=client_id,
mac=client_mac }
local cStdin = io.popen("shared-state insert dnsmasq-leases", "w")
cStdin:write(JSON.stringify(leaseData))
cStdin:close()

local hostTable = {}
hostTable[client_ip.." "..client_hostname] = true
local cStdin = io.popen("shared-state insert dnsmasq-hosts", "w")
cStdin:write(JSON.stringify(hostTable))
cStdin:close()
end

function add_own()
local leasesTable = {}

local uci_conf = uci.cursor()
local own_ipv4 = uci_conf:get("network", "lan", "ipaddr")
local own_ipv6 = uci_conf:get("network", "lan", "ip6addr")
uci_conf = nil
if own_ipv6 then own_ipv6 = own_ipv6:gsub("/.*$", "") end

local own_hostname = io.input("/proc/sys/kernel/hostname"):read("*line")
local own_mac = io.input("/sys/class/net/br-lan/address"):read("*line")

add_lease(own_mac, own_ipv4, own_hostname, "*")
add_lease(own_mac, own_ipv6, own_hostname, "*")
end

function del_lease(client_ip)
Expand All @@ -42,6 +86,12 @@ function del_lease(client_ip)
local cStdin = io.popen("shared-state remove dnsmasq-leases", "w")
cStdin:write(JSON.stringify(keysArr))
cStdin:close()

local hostArr = {}
table.insert(hostArr, client_ip.." "..client_hostname)
local cStdin = io.popen("shared-state remove dnsmasq-hosts", "w")
cStdin:write(JSON.stringify(hostArr))
cStdin:close()
end

local command = arg[1]
Expand All @@ -57,34 +107,13 @@ if ((not client_id) or (client_id:len() <= 0)) then client_id = "*" end

if command == "add" then
add_lease(client_mac, client_ip, client_hostname, client_id)
add_own()

elseif command == "del" then
del_lease(client_ip)
add_own()

elseif command == nil or command:match("^%s*$") then
local leasesTable = {}

local uci_conf = uci.cursor()
local own_ipv4 = uci_conf:get("network", "lan", "ipaddr")
local own_ipv6 = uci_conf:get("network", "lan", "ip6addr")
uci_conf = nil
if own_ipv6 then own_ipv6 = own_ipv6:gsub("/.*$", "") end

local own_hostname = io.input("/proc/sys/kernel/hostname"):read("*line")
local own_mac = io.input("/sys/class/net/br-lan/address"):read("*line")

leasesTable[own_ipv4] = { hostname=own_hostname, id="*", mac=own_mac }
leasesTable[own_ipv6] = { hostname=own_hostname, id="*", mac=own_mac }

for line in io.lines("/tmp/dhcp.leases") do
_, client_mac, client_ip, client_hostname, client_id = unpack(line:split(" "))
leasesTable[client_ip] = { hostname=client_hostname, id=client_id,
mac=client_mac }
end

local jsonstring = JSON.stringify(leasesTable)

local cStdin = io.popen("shared-state insert dnsmasq-leases", "w")
cStdin:write(jsonstring)
cStdin:close()
add_tmp_dhcp_leases()
add_own()
end
@@ -0,0 +1,10 @@
#!/bin/sh

unique_append()
{
grep -qF "$1" "$2" || echo "$1" >> "$2"
}

unique_append \
'*/30 * * * * ((sleep $(($RANDOM % 1000)); for publisher in /etc/shared-state/publishers/* ; do [ -x "$publisher" ] && "$publisher"; done )&)'\
/etc/crontabs/root
10 changes: 6 additions & 4 deletions packages/shared-state/files/usr/bin/shared-state
Expand Up @@ -98,10 +98,13 @@ function sharedState.sync(urls)
options.method = 'POST'
options.body = sharedState.toJsonString()

local response = http.request_to_buffer(url, options)
if type(response) == "string" and response:len() > 1 then
local success, response = pcall(http.request_to_buffer, url, options)

if success and type(response) == "string" and response:len() > 1 then
local parsedJson = JSON.parse(response)
if parsedJson then sharedState.merge(parsedJson) end
else
log( "debug", "httpclient interal error requesting "..url )
end
end
end
Expand Down Expand Up @@ -139,7 +142,6 @@ sharedState.merge(JSON.parse(sharedState.storageFD:readall()), false)

if arg[1] == "insert" then
local inputString = (io.stdin:read("*all"))
log("debug", "handling reqsync >>>"..inputString.."<<<")
local inputTable = JSON.parse(inputString) or {}
for key, lv in pairs(inputTable) do sharedState.insert(key, lv) end
elseif arg[1] == "get" then
Expand All @@ -150,7 +152,7 @@ elseif arg[1] == "sync" then
sharedState.sync(urls)
elseif arg[1] == "reqsync" then
local inputString = (io.stdin:read("*all"))
log("debug", "handling reqsync >>>"..inputString.."<<<")
log("debug", "handling reqsync "..sharedState.dataType.." >>>"..inputString.."<<<")
sharedState.merge(JSON.parse(inputString))
print(sharedState.toJsonString())
elseif arg[1] == "remove" then
Expand Down
Expand Up @@ -20,6 +20,22 @@
COPYRIGHT

get_uptime()
{
awk '{print $1}' /proc/uptime | awk -F. '{print $1}'
}

cacheFile="/tmp/shared-state-get_candidates_neigh.cache"
lastRunFile="/tmp/shared-state-get_candidates_neigh.lastrun"
lastRun=$(cat "$lastRunFile" 2>/dev/null || echo -9999)
currUptime="$(get_uptime)"

[ "$(($currUptime - $lastRun))" -lt "90" ] &&
{
cat "$cacheFile"
exit 0
}

possiblyAvoidIfaces="anygw br-lan lo"

candidateAddresses="$(
Expand Down Expand Up @@ -48,4 +64,5 @@ $(echo "$candidateAddresses" | grep -v "$cIp")"

done

echo "$candidateAddresses"
echo "$candidateAddresses" | tee "$cacheFile"
get_uptime > "$lastRunFile"

0 comments on commit de02a22

Please sign in to comment.