Added country check

This commit is contained in:
Luca Deri 2025-07-14 11:12:36 +02:00
parent 3cfef3b5b8
commit fdc884742c

View file

@ -1,5 +1,5 @@
--
-- (C) 2013-24 - ntop.org
-- (C) 2013-25 - ntop.org
--
local dirs = ntop.getDirs()
@ -20,7 +20,6 @@ end
interface.select(ifid)
local response = {}
local host_key = _GET["host"]
local hosts_category = _GET["hosts_category"] or ""
@ -54,105 +53,105 @@ end
-- ############################################################
local function handlePeer(host_key)
local host_data = interface.getHostInfo(host_key)
local host_data = interface.getHostInfo(host_key)
return host_data
return host_data
end
-- ##############
-- Function to get hosts data based on hosts category filter -> numerical value as describe in the if below
local function show_hosts(hosts_count, host_key, hosts_category)
local hosts = {}
local num_hosts = 0
local data = {}
-- Get single host data (host_key is the requested IP)
if (host_key) then
-- From req create a table
local host_info = url2hostinfo(_GET)
local flows = getTopFlowPeers(hostinfo2hostkey(host_info), MAX_HOSTS - hosts_count, nil, { detailsLevel = "max" })
data.hosts = {}
local hosts = {}
local num_hosts = 0
local data = {}
-- Get single host data (host_key is the requested IP)
if (host_key) then
-- From req create a table
local host_info = url2hostinfo(_GET)
local flows = getTopFlowPeers(hostinfo2hostkey(host_info), MAX_HOSTS - hosts_count, nil, { detailsLevel = "max" })
data.hosts = {}
for key, value in pairs(flows) do
-- create table for client IP
local h = handlePeer(value["cli.ip"])
if h ~= nil then
data.hosts[value["cli.ip"]] = h
end
for key, value in pairs(flows) do
-- create table for client IP
local h = handlePeer(value["cli.ip"])
if h ~= nil then
data.hosts[value["cli.ip"]] = h
end
-- create table for server IP
local h = handlePeer(value["srv.ip"])
if h ~= nil then
data.hosts[value["srv.ip"]] = h
end
-- create table for server IP
local h = handlePeer(value["srv.ip"])
if h ~= nil then
data.hosts[value["srv.ip"]] = h
end
end
-- Active hosts or Alerted hosts
elseif ((hosts_category == 0) or (hosts_category == 1)) then
data = interface.getHostsInfo()
-- Local hosts
elseif hosts_category == 2 then
data = interface.getLocalHostsInfo()
end
-- Active hosts or Alerted hosts
elseif ((hosts_category == 0) or (hosts_category == 1)) then
data = interface.getHostsInfo()
-- Local hosts
elseif hosts_category == 2 then
data = interface.getLocalHostsInfo()
-- Remote hosts
elseif (hosts_category == 3) then
data = interface.getRemoteHostsInfo()
-- Remote hosts
elseif (hosts_category == 3) then
data = interface.getRemoteHostsInfo()
-- Invalid category selected
else
return hosts
end
-- Invalid category selected
else
return hosts
end
if (data ~= nil) and (data["hosts"]) then
for address, value in pairs(data["hosts"]) do
if (data ~= nil) and (data["hosts"]) then
for address, value in pairs(data["hosts"]) do
if value["latitude"] ~= 0 or value["longitude"] ~= 0 then
local country = value["country"]
tprint("Country: " .. country)
local country_info = country_code.get_country_info(country)
tprint(country_info)
tprint("----------")
local iso3_country = country_info[1]
local country_id = country_info[2]
if value["latitude"] ~= 0 or value["longitude"] ~= 0 then
local country = value["country"]
tprint("Country: " .. country)
local country_info = country_code.get_country_info(country)
local host = {
lat = value["latitude"],
lng = value["longitude"],
isRoot = false,
country = iso3_country,
country_id = country_id,
ip = address,
scoreClient = value["score.as_client"],
scoreServer = value["score.as_server"],
numAlerts = value["active_alerted_flows"],
country_code = country,
isAlert = value["num_alerts"] + value["active_alerted_flows"] > 0
}
if(country_info ~= nil) then
local iso3_country = country_info[1]
local country_id = country_info[2]
if not isEmptyString(value["city"]) then
host["city"] = value["city"]
end
local host = {
lat = value["latitude"],
lng = value["longitude"],
isRoot = false,
country = iso3_country,
country_id = country_id,
ip = address,
scoreClient = value["score.as_client"],
scoreServer = value["score.as_server"],
numAlerts = value["active_alerted_flows"],
country_code = country,
isAlert = value["num_alerts"] + value["active_alerted_flows"] > 0
}
table.insert(hosts, host)
num_hosts = num_hosts + 1
if not isEmptyString(value["city"]) then
host["city"] = value["city"]
end
if num_hosts >= MAX_HOSTS then
return hosts
end
end
end
end
table.insert(hosts, host)
num_hosts = num_hosts + 1
return hosts
if num_hosts >= MAX_HOSTS then
return hosts
end
end
end
end
end
return hosts
end
local rsp = show_hosts(table.len(response["hosts"]), host_key, tonumber(hosts_category))
rest_utils.answer(rest_utils.consts.success.ok, rsp)