gluon-geolocator: update to master package configuration

* drop bssid blacklist
* only allow Master mode scanned wifis for osition request.
This commit is contained in:
Jan-Tarek Butt 2018-07-16 01:33:28 +02:00
parent f6a533327a
commit 5ffb482705
5 changed files with 69 additions and 107 deletions

View File

@ -4,33 +4,11 @@ PKG_NAME:=gluon-geolocator
PKG_VERSION:=1 PKG_VERSION:=1
PKG_RELEASE:=1 PKG_RELEASE:=1
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include ../gluon.mk include ../gluon.mk
define Package/gluon-geolocator define Package/gluon-geolocator
SECTION:=gluon
CATEGORY:=Gluon
TITLE:=Provide the geolocator to receive positions over wifi TITLE:=Provide the geolocator to receive positions over wifi
DEPENDS:=+gluon-node-info +micrond DEPENDS:=+gluon-node-info +micrond
endef endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
endef
define Build/Compile
$(call GluonSrcDiet,./luasrc,$(PKG_BUILD_DIR)/luadest/)
endef
define Package/gluon-geolocator/install
$(CP) ./files/* $(1)/
$(CP) $(PKG_BUILD_DIR)/luadest/* $(1)/
endef
define Package/gluon-geolocator/postinst
#!/bin/sh
$(call GluonCheckSite,check_site.lua)
endef
$(eval $(call BuildPackage,gluon-geolocator)) $(eval $(call BuildPackage,gluon-geolocator))

View File

@ -1,3 +1,2 @@
need_boolean(in_site({'geolocator', 'autolocation'}), false) need_boolean(in_site({'geolocator', 'autolocation'}), false)
need_number(in_site({'geolocator', 'interval'}), false) need_number(in_site({'geolocator', 'interval'}), false)
need_string_array_match(in_site({'geolocator', 'blacklist'}), '^%w+:%w+:%w+:%w+:%w+:%w+$')

View File

@ -2,4 +2,3 @@ config geolocator 'settings'
option static_location '0' option static_location '0'
option auto_location '1' option auto_location '1'
option refresh_interval '1000' option refresh_interval '1000'

View File

@ -8,7 +8,7 @@ local LOC="gluon-node-info"
local GLC="geolocator" local GLC="geolocator"
if not uci:get_bool(GLC, "settings", "auto_location") then if not uci:get_bool(GLC, "settings", "auto_location") then
os.exit(0) os.exit(0)
end end
-- PID file to ensure the geolocator isn't running parallel -- PID file to ensure the geolocator isn't running parallel
@ -16,120 +16,107 @@ local PID_PART="/var/run/geolocator.pid"
local TIME_STAMP="/tmp/geolocator_timestamp" local TIME_STAMP="/tmp/geolocator_timestamp"
local function file_exsist(file) local function file_exsist(file)
return io.open(file, "r") ~= nil return io.open(file, "r") ~= nil
end end
if file_exsist(PID_PART) then if file_exsist(PID_PART) then
io.stdout:write("The geolocator is still running.\n") io.stdout:write("The geolocator is still running.\n")
os.exit(0) os.exit(0)
end end
if io.open(PID_PART, "w") == nil then if io.open(PID_PART, "w") == nil then
io.stdout:write("Can`t create pid file on " .. PID_PART .. "\n") io.stdout:write("Can`t create pid file on " .. PID_PART .. "\n")
os.exit(1) os.exit(1)
end end
-- Program terminating function including removing of PID file -- Program terminating function including removing of PID file
local function exit(exc) local function exit(exc)
if file_exsist(PID_PART) then if file_exsist(PID_PART) then
os.remove(PID_PART) os.remove(PID_PART)
end end
os.exit(exc) os.exit(exc)
end
local function table_contains(tbl, prefix)
for _, entry in ipairs(tbl) do
if entry:match(prefix) then
return true
end
end
return false
end end
local function getWifiifnames() local function getWifiifnames()
local ifnames = {} local ifnames = {}
uci:foreach('wireless', 'wifi-iface', uci:foreach('wireless', 'wifi-iface',
function(s) function(s)
local ifname = uci:get('wireless', s['.name'], 'ifname') local ifname = uci:get('wireless', s['.name'], 'ifname')
if (ifname ~= nil) then if (ifname ~= nil) then
table.insert(ifnames, ifname) table.insert(ifnames, ifname)
end end
end end
) )
return ifnames return ifnames
end end
-- Get position -- Get position
local function Get_geolocation_info() local function Get_geolocation_info()
-- Get list of BSSID there should ignored local scaned_bssid = ""
local blacklist_bssid = { } local uniq = { }
for _, bl_bssid in ipairs(uci:get(GLC, "settings", "blacklist")) do for _, ifname in ipairs(getWifiifnames()) do
-- Ensure rm colons and uppercase local api = iwinfo.type(ifname)
table.insert(blacklist_bssid,(string.upper(bl_bssid):gsub(":", ""))) if api then
end local iw = iwinfo[api]
local scaned_bssid = "" -- Get list of BSSID without redundancy entrys.
local uniq = { } for _, net in ipairs(iw.scanlist(ifname) or { }) do
for _, ifname in ipairs(getWifiifnames()) do --only alowe Master mode driven wifis
local api = iwinfo.type(ifname) if net.mode:match("Master") then
if api then -- Ensure rm colons and uppercase
local iw = iwinfo[api] net.bssid = string.upper(net.bssid:gsub(":", ""))
if not uniq[net.bssid] then
scaned_bssid = scaned_bssid .. "," .. net.bssid
uniq[net.bssid] = true
end
end
end
if #scaned_bssid < 12 then -- because one BSSID contains 12 characters
io.stdout:write("No surrounded BSSIDs found.\n")
return { }
end
scaned_bssid = scaned_bssid:gsub("^,(.-),*", "%1")
end
end
-- Request position
local req = io.popen("wget -T 15 -q -O - http://openwifi.su/api/v1/bssids/" .. scaned_bssid)
if not req then
io.stdout:write("connection failed.\n")
return { }
end
-- Get list of BSSID without blacklisted and redundancy entrys. local jreq, _, err = json.parse(req:read("*a"), 1, nil)
for _, net in ipairs(iw.scanlist(ifname) or { }) do req:close()
-- Ensure rm colons and uppercase if err or jreq == nil or jreq.lon == nil or jreq.lat == nil then
net.bssid = string.upper(net.bssid:gsub(":", "")) io.stdout:write("openwifi.su doesn't gif a location.\n")
if not uniq[net.bssid] and not table_contains(blacklist_bssid, net.bssid) then return { }
scaned_bssid = scaned_bssid .. "," .. net.bssid end
uniq[net.bssid] = true
end
end
if #scaned_bssid < 12 then
io.stdout:write("No surrounded BSSIDs found.\n")
return { }
end
scaned_bssid = scaned_bssid:gsub("^,(.-),*", "%1")
end
end
-- Request position
local req = io.popen("wget -T 15 -q -O - http://openwifi.su/api/v1/bssids/" .. scaned_bssid)
if not req then
io.stdout:write("connection failed.\n")
return { }
end
local jreq, _, err = json.parse(req:read("*a"), 1, nil) return jreq
req:close()
if err or jreq == nil or jreq.lon == nil or jreq.lat == nil then
io.stdout:write("openwifi.su doesn't gif a location.\n")
return { }
end
return jreq
end -- end Get_geolocation_info() end -- end Get_geolocation_info()
-- Check if interval over or not exist -- Check if interval over or not exist
if file_exsist(TIME_STAMP) then if file_exsist(TIME_STAMP) then
if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then if os.time() - tonumber(io.open(TIME_STAMP):read("*a")) < uci:get(GLC, "settings", "refresh_interval") * 60 then
exit(0) exit(0)
end end
end end
local pos = Get_geolocation_info() local pos = Get_geolocation_info()
if not next(pos) then if not next(pos) then
exit(1) exit(1)
end end
if not uci:get_bool(GLC, "settings", "static_location") then if not uci:get_bool(GLC, "settings", "static_location") then
uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat) uci:set(LOC, uci:get_first(LOC, 'location'), 'latitude', pos.lat)
uci:set(LOC, uci:get_first(LOC, 'location'), 'longitude', pos.lon) uci:set(LOC, uci:get_first(LOC, 'location'), 'longitude', pos.lon)
uci:save(LOC) uci:save(LOC)
uci:commit(LOC) uci:commit(LOC)
end end
local timestap = io.open(TIME_STAMP, "w") local timestap = io.open(TIME_STAMP, "w")
if timestap ~= nil then if timestap ~= nil then
timestap:write(os.time()) timestap:write(os.time())
timestap:close() timestap:close()
exit(0) exit(0)
end end
io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n") io.stdout:write("Can`t create file on " .. TIME_STAMP .. "\n")
exit(1) exit(1)

View File

@ -22,6 +22,5 @@ uci:section(config, config, 'settings', {
static_location = static_location, static_location = static_location,
refresh_interval = refresh_interval, refresh_interval = refresh_interval,
auto_location = auto_location, auto_location = auto_location,
blacklist = site.geolocator.blacklist({}),
}) })
uci:save(config) uci:save(config)