]]
end
-- #################################
local function print_section(section, list_name)
local active_page = page_utils.get_active_section()
local show_section = false
local section_has_submenu = (section.entries and #section.entries > 0)
if(not section.hidden) then
if not section_has_submenu then
show_section = true
else
-- The section is shown only if at least one of its sub entries
-- is shown
for _, section_entry in pairs(section.entries or {}) do
if not section_entry.hidden then
show_section = true
break
end
end
end
end
if show_section then
local section_key = section.section.key
local section_id = section_key.."-submenu"
print[[
]] print(i18n(section.section.i18n_title)) print[[
]]
if section_has_submenu then
print_submenu(section, list_name)
end
end
end
-- #################################
function page_utils.print_menubar()
local is_system_interface = page_utils.is_system_view()
local logo_path = nil
local list_name = "sidebar"
if (ntop.isPro() or ntop.isnEdge()) and ntop.exists(dirs.installdir .. "/httpdocs/img/custom_logo.png") then
logo_path = ntop.getHttpPrefix().."/img/custom_logo.png"
end
local navbar_style = _POST["toggle_theme"] or ntop.getPref("ntopng.user." .. _SESSION["user"] .. ".theme")
if ((navbar_style == nil) or (navbar_style == "white") or (navbar_style == "")) then
navbar_style = "default"
end
if (navbar_style == "default") then
navbar_style = "dark"
end
print('
]])
end
-- ##############################################
function page_utils.get_interface_list()
local interfaces = {}
local iface_names = interface.getIfNames()
local is_system_interface = page_utils.is_system_view()
local num_ifaces = 0
for k,v in pairs(iface_names) do
num_ifaces = num_ifaces+1
end
local ifs = interface.getStats()
local is_pcap_dump = interface.isPcapDumpInterface()
local is_packet_interface = interface.isPacketInterface()
local views = {}
local drops = {}
local recording = {}
local packetinterfaces = {}
local ifnames = {}
local ifdescr = {}
local ifHdescr = {}
local ifCustom = {}
local dynamic = {}
for v,k in pairs(iface_names) do
interface.select(k)
local _ifstats = interface.getStats()
ifnames[_ifstats.id] = k
ifdescr[_ifstats.id] = _ifstats.description
--io.write("["..k.."/"..v.."][".._ifstats.id.."] "..ifnames[_ifstats.id].."=".._ifstats.id.."\n")
if(_ifstats.isView == true) then views[k] = true end
if(_ifstats.isDynamic == true) then dynamic[k] = true end
if(recording_utils.isEnabled(_ifstats.id)) then recording[k] = true end
if(interface.isPacketInterface()) then packetinterfaces[k] = true end
if(_ifstats.stats_since_reset.drops * 100 > _ifstats.stats_since_reset.packets) then
drops[k] = true
end
ifHdescr[_ifstats.id] = getHumanReadableInterfaceName(_ifstats.description.."")
ifCustom[_ifstats.id] = _ifstats.customIftype
end
-- First round: only physical interfaces
-- Second round: only virtual interfaces
for round = 1, 2 do
for k,_ in pairsByValues(ifHdescr, asc) do
if ((round == 1) and (ifCustom[k] ~= nil)) or (((round == 2) and (ifCustom[k] == nil))) then
goto continue
end
local v = ifnames[k]
-- table.clone needed to modify some parameters while keeping the original unchanged
local page_params = table.clone(_GET)
page_params.ifid = k
local url_query = getPageUrl("", page_params)
-- NOTE: the actual interface switching is performed in C in LuaEngine::handle_script_request
local action_url = ""
if(is_system_interface) then
action_url = ntop.getHttpPrefix() .. '/' .. url_query
else
action_url = url_query
end
local descr = getHumanReadableInterfaceName(v.."")
if(string.contains(descr, "{")) then -- Windows
descr = ifdescr[k]
else
if(descr ~= ifdescr[k]) and (not views[v]) then
if(descr == shortenCollapse(ifdescr[k])) then
descr = ifdescr[k]
else
descr = descr .. " (".. ifdescr[k] ..")" -- Add description
end
end
end
interfaces[#interfaces+1] = {
is_virtual = (round == 2), -- Is the interface virtual or physical?
is_paused = isPausedInterface(v), -- Is the interfaces paused?
is_selected = (v == ifname) and not is_system_interface, -- Is the in
is_dynamic = (dynamic[v]),
is_recording = (recording[v]),
has_drops = (drops[v]),
has_views = (views[v]),
ifid = k,
action_url = action_url,
human_name = descr
}
::continue::
end
end
interface.select(ifs.id.."")
return interfaces
end
function page_utils.generate_info_stats()
local _ifstats = interface.getStats()
if _ifstats.has_traffic_directions then
return ([[
]])
else
return ([[
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
]])
end
end
-- ##############################################
function page_utils.is_system_view()
return interface.getId() == tonumber(getSystemInterfaceId()) and isAdministrator()
end
-- ##############################################
--- Check if the current page is valid
--- @return boolean True if the current page is in available_pages, false otherwise
function page_utils.is_valid_page(selected_page, available_pages)
if selected_page == nil then
traceError(TRACE_WARNING, TRACE_DEBUG, "selected_pages is nil!")
return false
end
if available_pages == nil then
traceError(TRACE_WARNING, TRACE_DEBUG, "available_pages is nil!")
return false
end
if type(available_pages) ~= 'table' then
traceError(TRACE_WARNING, TRACE_DEBUG, "available_pages is not a table!")
return false
end
-- do a linear search
for _, page in ipairs(available_pages) do
if (page == selected_page) then return true end
end
return false
end
-- ##############################################
-- @brief Prepares the URL which will be used for the redirect after interface switch
-- @param ifid The interface identifier of the interface that will be switched
-- @param if_type The type of the interface that will be switched
-- @return The URL
function page_utils.switch_interface_form_action_url(active_ifid, switch_ifid, switch_if_type)
local action_url = ""
local page_params = table.clone(_GET)
-- Read the currently active page
local active_page = page_utils.get_active_section()
if active_ifid == tonumber(getSystemInterfaceId()) then
-- If the switch is between the system interface and a non-system interface
-- the URL is just the home
action_url = ntop.getHttpPrefix() .. '/'
elseif page_utils.menu_sections[active_page] and page_utils.menu_sections[active_page]["zmq_only"] and switch_if_type ~= "zmq" and switch_if_type ~= "custom" then
-- If the interface that will be switched is non-ZMQ, and the currently
-- selected page is for ZMQ-only interfaces, then a redirection to root
-- is performed, rather than preserving the current page
action_url = ntop.getHttpPrefix() .. '/'
end
-- Attach the interface id of the interface that will be switched
page_params.ifid = switch_ifid
-- Return the url, preserving all existing page parameters (e.g., host=xx)
local res = getPageUrl(action_url, page_params)
return res
end
-- ##############################################
return page_utils