--
-- (C) 2013-22 - ntop.org
--
local dirs = ntop.getDirs()
local info = ntop.getInfo()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local script_manager = require("script_manager")
local template_utils = require("template_utils")
local recording_utils = require("recording_utils")
local alert_entities = require("alert_entities")
local page_utils = {}
local is_nedge = ntop.isnEdge()
-- #################################
local active_section = nil
local active_entry = nil
local active_sub_entry = nil
-- #################################
page_utils.menu_sections = {
shortcuts = {key = "shortcut", i18n_title = "shortcuts", icon = "fas fa-fast-forward"},
dashboard = {key = "dashboard", i18n_title = "index_page.dashboard", icon = "fas fa-tachometer-alt"},
alerts = {key = "alerts", i18n_title = "details.alerts", icon = "fas fa-exclamation-triangle"},
flows = {key = "flows", i18n_title = "flows", icon = "fas fa-stream"},
hosts = {key = "hosts", i18n_title = "hosts", icon = "fas fa-laptop"},
maps = {key = "maps", i18n_title = "maps", icon = "fas fa-map"},
collection = {key = "collection", i18n_title = "flow_devices.exporters", icon = "fas fa-file-export", zmq_only = true --[[ only for non-packet ZMQ interfaces --]] },
if_stats = {key = "if_stats", i18n_title = "interface", icon = "fas fa-ethernet"},
system_stats = {key = "system_stats", i18n_title = "system", icon = "fas fa-desktop"},
admin = {key = "admin", i18n_title = "settings", icon = "fas fa-cog"},
alert_exclusions = {key = "alert_exclusions", i18n_title = "edit_check.exclusion_list", icon = "fas fa-bell-slash"},
dev = {key = "dev", i18n_title = "developer", icon = "fas fa-code"},
about = {key = "about", i18n_title = "help", icon = "fas fa-life-ring"},
health = {key = "health", i18n_title = "health", icon = "fas fa-laptop-medical"},
pollers = {key = "pollers", i18n_title = "pollers", icon = "fas fa-heartbeat"},
tools = {key = "tools", i18n_title = "tools", icon = "fas fa-cogs"},
pools = {key = "pools", i18n_title = "pools.pools", icon = "fas fa-users"},
notifications = {key = "notifications", i18n_title = "endpoint_notifications.notifications", icon = "fas fa-bell"},
checks = {key = "checks", i18n_title = "about.checks" },
-- nEdge
views = {key = "hosts", i18n_title = "views", icon = "fas fa-bars"},
}
-- #################################
-- help_link: this variable contains the contextual link to the documentation
page_utils.menu_entries = {
-- Shortcuts
active_monitoring = {key = "active_monitor", i18n_title = "active_monitoring_stats.active_monitoring"},
-- Dashboard
traffic_dashboard = {key = "traffic_dashboard", i18n_title = "dashboard.traffic_dashboard", section = "dashboard", help_link = "https://www.ntop.org/guides/ntopng/web_gui/dashboard.html"},
network_discovery = {key = "network_discovery", i18n_title = "discover.network_discovery", section = "dashboard"},
traffic_report = {key = "traffic_report", i18n_title = "report.traffic_report", section = "dashboard", help_link = "https://www.ntop.org/guides/ntopng/web_gui/report.html"},
traffic_analysis = {key = "traffic_analysis", i18n_title = "db_explorer.traffic_analysis", section = "dashboard", help_link = "https://www.ntop.org/guides/ntopng/clickhouse.html?#historical-flows-explorer"},
-- Alerts
detected_alerts = {key = "detected_alerts", i18n_title = "show_alerts.detected_alerts", section = "alerts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/alerts.html"},
alerts_dashboard = {key = "alerts_dashboard", i18n_title = "alerts_dashboard.alerts_dashboard", section = "alerts"},
flow_alerts_explorer = {key = "flow_alerts_explorer", i18n_title = "flow_alerts_explorer.label", section = "alerts"},
-- Flows
flows = {key = "flows", i18n_title = "flows", section = "flows", help_link = "https://www.ntop.org/guides/ntopng/web_gui/flows.html"},
active_flows = {key = "active_flows", i18n_title = "active_flows", section = "flows" },
flow_details = {key = "flow_details", i18n_title = "flow_details.flow_details", section = "flows"},
db_explorer = {key = "db_explorer", i18n_title = "db_explorer.historical_data_explorer", section = "flows", help_link = "https://www.ntop.org/guides/ntopng/clickhouse.html?#historical-flows-explorer"},
-- Hosts
hosts = {key = "hosts", i18n_title = "hosts", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html#"},
devices = {key = "devices", i18n_title = "layer_2", section = "hosts"},
networks = {key = "networks", i18n_title = "networks", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#networks"},
vlans = {key = "vlans", i18n_title = "vlan_stats.vlans", section = "hosts"},
host_pools = {key = "host_pools", i18n_title = "host_pools.host_pools", section = "hosts"},
autonomous_systems = {key = "autonomous_systems", i18n_title = "as_stats.autonomous_systems", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#autonomous-systems"},
countries = {key = "countries", i18n_title = "countries", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#countries"},
operating_systems = {key = "operating_systems", i18n_title = "operating_systems", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#operating-systems"},
http_servers = {key = "http_servers", i18n_title = "http_servers_stats.http_servers", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#http-servers-local"},
top_hosts = {key = "top_hosts", i18n_title = "processes_stats.top_hosts", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#top-hosts-local"},
hosts_treemap = {key = "hosts_treemap", i18n_title = "tree_map.hosts_treemap", section = "hosts"},
containers = {key = "containers", i18n_title = "containers_stats.containers", section = "hosts"},
pods = {key = "pods", i18n_title = "containers_stats.pods", section = "hosts"},
-- Interface
interface = {key = "interface", i18n_title = "interface_ifname", section = "if_stats"},
-- Pollers
snmp = {key = "snmp", i18n_title = "prefs.snmp", section = "pollers"},
infrastructure_dashboard = {key = "infrastructure_dashboard", i18n_title = "infrastructure_dashboard.infrastructure_dashboard", section = "pollers"},
-- Status (Health)
system_status = {key = "system_status", i18n_title = "system_status", section = "health"},
interfaces_status = {key = "interfaces_status", i18n_title = "system_interfaces_status", section = "health"},
alerts_status = {key = "alerts_status", i18n_title = "system_alerts_status", section = "health"},
influxdb_status = {key = "influxdb", i18n_title = "InfluxDB", section = "health"},
redis_status = {key = "redis_monitor", i18n_title = "Redis", section = "health"},
clickhouse_status = {key = "clickhouse", i18n_title = "ClickHouse", section = "health"},
-- exporters
event_exporters = {key = "event_exporters", i18n_title = "event_exporters.event_exporters", section = "collection"},
sflow_exporters = {key = "sflow_exporters", i18n_title = "flows_page.sflow_devices", section = "collection"},
exporters = {key = "exporters", i18n_title = "flow_devices.flow_exporters", section = "collection", help_link = "https://www.ntop.org"},
observation_points = {key = "observation_points", i18n_title = "flow_devices.observation_points", section = "collection", help_link = "https://www.ntop.org"},
-- Map
service_map = {key = "service_map", i18n_title="service", section = "maps"},
periodicity_map = {key = "periodicity_map", i18n_title="periodicity", section = "maps"},
geo_map = {key = "geo_map", i18n_title = "geo_map.geo_map", section = "maps", help_link = "https://www.ntop.org/guides/ntopng/web_gui/hosts.html?#geo-map"},
hosts_map = {key = "hosts_map", i18n_title = "flows_page.hosts", section = "maps"},
-- Settings
nedge_users = {key = "nedge_users", i18n_title = "manage_users.manage_users", section = "admin", help_link = "https://www.ntop.org/guides/nedge/users.html#"},
manage_users = {key = "manage_users", i18n_title = ternary(is_nedge, "nedge.system_users", "manage_users.manage_users"), section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/settings.html#manage-users"},
preferences = {key = "preferences", i18n_title = "prefs.preferences", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/settings.html#preferences"},
scripts_config = {key = "scripts_config", i18n_title = "about.checks", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_all = {key = "scripts_config", subkey="all", i18n_title = "all", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_hosts = {key = "scripts_config", subkey="hosts", i18n_title = alert_entities.host.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_interfaces = {key = "scripts_config", subkey="interfaces", i18n_title = alert_entities.interface.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_networks = {key = "scripts_config", subkey="networks", i18n_title = alert_entities.network.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_snmp_devices = {key = "scripts_config", subkey="snmp_devices", i18n_title = alert_entities.snmp_device.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_flows = {key = "scripts_config", subkey="flows", i18n_title = alert_entities.flow.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_system = {key = "scripts_config", subkey="system", i18n_title = alert_entities.system.i18n_label, section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
scripts_config_syslog = {key = "scripts_config", subkey="syslog", i18n_title = "syslog.syslog", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
alert_exclusions = {key = "alert_exclusions", i18n_title = "edit_check.exclusion_list", section = "alert_exclusions", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
alert_exclusions_hosts = {key = "alert_exclusions", subkey="hosts", i18n_title = alert_entities.host.i18n_label, section = "alert_exclusions", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
alert_exclusions_flows = {key = "alert_exclusions", subkey="flows", i18n_title = alert_entities.flow.i18n_label, section = "alert_exclusions", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
alert_exclusions_domains = {key = "alert_exclusions", subkey="domains", i18n_title = alert_entities.domain.i18n_label, section = "alert_exclusions", help_link = "https://www.ntop.org/guides/ntopng/web_gui/checks.html"},
license = {key = "license", i18n_title = "license_page.license", section = "admin"},
profiles = {key = "profiles", i18n_title = "traffic_profiles.traffic_profiles", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/advanced_features/traffic_profiles.html"},
categories = {key = "categories", i18n_title = "custom_categories.apps_and_categories", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/categories.html#custom-applications"},
category_lists = {key = "category_lists", i18n_title = "category_lists.category_lists", section = "admin"},
device_protocols = {key = "device_protocols", i18n_title = "device_protocols.device_protocols", section = "admin"},
conf_backup = {key = "conf_backup", i18n_title = "conf_backup.conf_backup", section = "admin"},
conf_restore = {key = "conf_restore", i18n_title = "conf_backup.conf_restore", section = "admin"},
manage_data = {key = "manage_data", i18n_title = "manage_data.manage_data", section = "admin", help_link = "https://www.ntop.org/guides/ntopng/web_gui/settings.html#manage-data"},
manage_configurations = {key = "manage_configurations", i18n_title = "manage_configurations.manage_configurations", section = "admin"},
-- Notifications
endpoint_notifications = {key = "endpoint_notifications", i18n_title = "endpoint_notifications.endpoint_list", section="notifications", help_link = "https://www.ntop.org/guides/ntopng/scripts/alert_endpoints.html"},
endpoint_recipients = {key = "endpoint_recipients", i18n_title = "endpoint_notifications.enpoint_recipients_list", section="notifications", help_link = "https://www.ntop.org/guides/ntopng/scripts/alert_endpoints.html"},
-- Tools
export_data = {key = "export_data", i18n_title = "manage_data.export", section = "tools"},
-- Pools
host_members = {key = "host_members", i18n_title = "host_pools.host_members", section = "pools"},
manage_pools = {key = "manage_pools", i18n_title = "pools.pools", section = "pools"},
-- Home
live_capture = {key = "live_capture", i18n_title = "live_capture.active_live_captures", section = "home"},
-- Developer
directories = {key = "directories", i18n_title = "about.directories", section = "dev", help_link = "https://www.ntop.org/guides/ntopng/scripts/distributing_scripts.html"},
checks_dev = {key = "checks_dev", i18n_title = "about.checks", section = "dev", help_link = "https://www.ntop.org/guides/ntopng/scripts/checks.html"},
alert_definitions = {key = "alert_definitions", i18n_title = "about.alert_defines", section = "dev", help_link = "https://www.ntop.org/guides/ntopng/scripts/alert_definitions.html"},
api = {key = "api", i18n_title = "lua_c_api", section = "dev"},
-- Help
about = {key = "about", i18n_title = "about.about", section = "about", help_link = "https://www.ntop.org/guides/ntopng/web_gui/help_menu.html?#about"},
telemetry = {key = "telemetry", i18n_title = "telemetry", section = "about"},
blog = {key = "blog", i18n_title = "about.ntop_blog", section = "about"},
telegram = {key = "telegram", i18n_title = "about.telegram", section = "about"},
report_issue = {key = "report_issue", i18n_title = "about.report_issue", section = "about"},
manual = {key = "manual", i18n_title = "about.readme_and_manual", section = "about"},
suggest_feature = {key = "suggest_feature", i18n_title = "about.suggest_feature", section = "about"},
-- Just a divider for horizontal rows in the menu
divider = {key = "divider"},
-- nEdge
gateways_users = {key = "gateways_users", i18n_title = "dashboard.gateways_users", section = "dashboard"},
nedge_flows = {key = "nedge_flows", i18n_title = "flows", section = "hosts"},
nedge_interfaces = {key = "nedge_interfaces", i18n_title = "interfaces", section = "dashboard"},
users = {key = "users", i18n_title = "users.users", section = "hosts", help_link = "https://www.ntop.org/guides/ntopng/advanced_features/authentication.html"},
system_setup = {key = "system_setup", i18n_title = "nedge.system_setup", section = "system_stats", help_link = "https://www.ntop.org/guides/nedge/get_started.html"},
dhcp_static_leases = {key = "dhcp_static_leases", i18n_title = "nedge.static_dhcp_leases", section = "system_stats"},
dhcp_active_leases = {key = "dhcp_active_leases", i18n_title = "nedge.active_dhcp_leases", section = "system_stats"},
port_forwarding = {key = "port_forwarding", i18n_title = "nedge.port_forwarding", section = "system_stats"},
}
-- Extend the menu entries with the scripts
local menu, entries_data = script_manager.getMenuEntries()
page_utils.scripts_menu = menu or {}
if entries_data then
for k, v in pairs(entries_data) do
page_utils.menu_entries[k] = v
end
end
-- #################################
-- NOTE: this function must be called after page_utils.set_active_menu_entry
function page_utils.print_page_title(title)
local help_link = page_utils.menu_entries[active_entry].help_link or ""
print("")
print("".. title .."
")
if (not isEmptyString(help_link)) then
print("")
end
print("")
end
-- #################################
-- NOTE: this function is called by the web pages in order to
-- set the active entry and section and highlight it into the menu
function page_utils.set_active_menu_entry(entry, i18n_params, alt_title)
entry = entry or page_utils.menu_entries.traffic_dashboard
active_section = entry.section
active_entry = entry.key
if entry.subkey then
active_sub_entry = entry.subkey
end
page_utils.print_header(alt_title or i18n(entry.i18n_title, i18n_params) or entry.i18n_title)
end
-- #################################
function page_utils.get_active_section()
return(active_section)
end
function page_utils.get_active_entry()
return(active_entry)
end
function page_utils.get_active_sub_entry()
return(active_sub_entry)
end
-- #################################
-- @brief Encode the HTML entities in a string.
-- Useful when printing dissected data which may result in XSS
-- e.g., curl -u admin:admin1 "http://devel:3000/"
-- @param s The string to escape.
-- @return The string with HTML entities properly escaped
function page_utils.safe_html(s)
if not s then
return ''
end
ret = string.gsub(s, "[}{\">/<'&]", {
["&"] = "&",
["<"] = "<",
[">"] = ">",
['"'] = """,
["'"] = "'",
["/"] = "/"
})
return(ret)
end
-- #################################
function page_utils.is_dark_mode_enabled(theme)
local dark_mode
local theme = theme or ntop.getPref("ntopng.user." .. _SESSION["user"] .. ".theme")
if (isEmptyString(theme)) then
dark_mode = false
else
dark_mode = (theme == "dark")
end
return dark_mode
end
-- #################################
function page_utils.print_header(title, addLoginJS)
local http_prefix = ntop.getHttpPrefix()
local static_file_epoch = ntop.getStaticFileEpoch()..""
local dark_mode = page_utils.is_dark_mode_enabled(_POST["toggle_theme"])
local page_title = i18n("welcome_to", { product=info.product })
local favicon_path = nil
local admin_lang = ntop.getPref("ntopng.user.admin.language")
local language = ternary(isEmptyString(admin_lang), "en", admin_lang)
local locale_path = dirs.installdir.."/scripts/locales/"..language..".lua"
local locale_when = ntop.fileLastChange(locale_path)
if title ~= nil then
page_title = info.product .. " - " .. title
end
print [[
]] print(page_title) print[[
]]
if (dark_mode) then
print[[]]
end
if (ntop.isPro() or ntop.isnEdge()) and ntop.exists(dirs.installdir .. "/httpdocs/img/custom_favicon.ico") then
favicon_path = ntop.getHttpPrefix().."/img/custom_favicon.ico"
end
if (favicon_path ~= nil) then
print[[]]
else
print[[]]
end
print[[
]]
if addLoginJS then
print[[]]
end
print[[
]]
print([[
]])
end
-- #################################
function page_utils.print_header_minimal(title, addLoginJS)
local http_prefix = ntop.getHttpPrefix()
local page_title = i18n("welcome_to", { product=info.product })
if title ~= nil then
page_title = info.product .. " - " .. title
end
local admin_lang = ntop.getPref("ntopng.user.admin.language")
local language = ternary(isEmptyString(admin_lang), "en", admin_lang)
local locale_path = dirs.installdir.."/scripts/locales/"..language..".lua"
local locale_when = ntop.fileLastChange(locale_path)
local static_file_epoch = ntop.getStaticFileEpoch()..""
print [[
]] print(page_title) print[[
]]
if (ntop.isPro() or ntop.isnEdge()) and ntop.exists(dirs.installdir .. "/httpdocs/img/custom_favicon.ico") then
favicon_path = ntop.getHttpPrefix().."/img/custom_favicon.ico"
end
if (favicon_path ~= nil) then
print[[]]
else
print[[]]
end
print[[