-- -- (C) 2014-24 - ntop.org -- -- This file contains the description of all functions -- used to trigger host alerts local verbose = false local prefs = ntop.getPrefs() local info = ntop.getInfo() local menu_subpages = require "prefs_menu" local have_nedge = ntop.isnEdge() local skip_redis = false -- ############################################ -- -- A menu is a list of menu entries -- A menu entry is composed by the following fields: -- - id: the subpage id -- - label: a label to be shown into the menu -- - entries: a list subpages (see belo) -- -- The following optional fields can be specified -- -- - advanced: if true, it should be only shown when advanced settings are enabled -- - pro_only: if true, it should be only shown in ntopng pro version -- - enterprise_only: if true, it should be only shown in ntopng enterprise version -- - disabled: if true, the entry is hidden -- -- A subpage is composed by the following fields: -- - title: the subpage title -- - description: the subpage description -- - hidden: (optional) if true, this subpage is hidden -- -- ############################################ DNS_PRESETS = { {id="comodo_secure", label="Comodo Secure DNS", url="https://www.comodo.com/secure-dns/", primary_dns="8.26.56.26", secondary_dns="8.20.247.20"}, {id="dyn_internet_guide", label="Dyn Internet Guide", url="http://dyn.com/labs/dyn-internet-guide/", primary_dns="216.146.35.35", secondary_dns="216.146.36.36"}, --{id="fool_dns", label="FoolDNS", url="http://www.fooldns.com/fooldns-community/english-version/", primary_dns="87.118.111.215", secondary_dns="213.187.11.62"}, --{id="greenteam_internet", label="GreenTeam Internet", url="http://members.greentm.co.uk/", primary_dns="81.218.119.11", secondary_dns="209.88.198.133"}, {id="opendns", label="OpenDNS", url="https://www.opendns.com/", primary_dns="208.67.222.222", secondary_dns="208.67.220.220"}, {id="opendns_familyshield", label="OpenDNS - FamilyShield", url="https://www.opendns.com/setupguide/?url=familyshield", primary_dns="208.67.222.123", secondary_dns="208.67.220.123", child_safe=true}, {id="quad9_security", label="Quad 9: Security", url="https://quad9.net", primary_dns="9.9.9.9", secondary_dns=""}, {id="google", label="Google DNS", url="https://developers.google.com/speed/public-dns", primary_dns="8.8.8.8", secondary_dns="8.8.4.4"}, {id="cloudflare", label="Cloudflare DNS", url="https://www.cloudflare.com/learning/dns/what-is-1.1.1.1", primary_dns="1.1.1.1", secondary_dns="1.0.0.1"}, {id="cleanbrowsing_security", label="CleanBrowsing - Security", url="https://cleanbrowsing.org", primary_dns="185.228.168.9", secondary_dns="185.228.169.9"}, {id="cleanbrwosing_adult", label="CleanBrowsing - Adult Filter", url="https://cleanbrowsing.org", primary_dns="185.228.168.168", secondary_dns="185.228.169.168", child_safe=true}, } function isSubpageAvailable(subpage, show_advanced_prefs) if show_advanced_prefs == nil then local show_advanced_prefs_key = "ntopng.prefs.show_advanced_prefs" show_advanced_prefs = toboolean(ntop.getPref(show_advanced_prefs_key)) end if (subpage.hidden) or ((subpage.advanced) and (not show_advanced_prefs)) or ((subpage.pro_only) and (not ntop.isPro())) or (subpage.enterprise_l_only and (not info["version.enterprise_l_edition"]) and (not info["version.nedge_enterprise_edition"])) or ((subpage.enterprise_only) and (not info["version.enterprise_edition"]) and (not have_nedge)) or (subpage.nedge_hidden) and (have_nedge) then return false end return true end function prefsGetActiveSubpage(show_advanced_prefs, tab) local subpage_active = nil for _, subpage in ipairs(menu_subpages) do if not isSubpageAvailable(subpage, show_advanced_prefs) then subpage.hidden = true if subpage.id == tab then -- will set to default tab = nil end elseif subpage.id == tab then subpage_active = subpage end end -- default subpage if isEmptyString(tab) then -- Pick the first available subpage for _, subpage in ipairs(menu_subpages) do if isSubpageAvailable(subpage, show_advanced_prefs) then subpage_active = subpage tab = subpage.id break end end end return subpage_active, tab end function printMenuSubpages(tab) for _, subpage in ipairs(menu_subpages) do if not subpage.hidden then local url = ternary(subpage.disabled, "#", ntop.getHttpPrefix() .. [[/lua/admin/prefs.lua?tab=]] .. (subpage.id)) print[[]] print(subpage.label) print[[]] end end end -- ############################################ --- Notify ntopng upon preference changes function notifyNtopng(key, value) if key == nil then return end -- notify runtime ntopng configuration changes if string.starts(key, 'toggle_logging_level') then if verbose then io.write('notifying ntopng upon logging level pref change\n') end ntop.setLoggingLevel(value) end end -- ############################################ local options_script_loaded = false local options_ctr = 0 function prefsResolutionButtons(fmt, value, fixed_id, format_spec, max_val) local ctrl_id if fixed_id ~= nil then ctrl_id = fixed_id else ctrl_id = "options_group_" .. options_ctr options_ctr = options_ctr + 1 end local res = makeResolutionButtons(format_spec or FMT_TO_DATA_TIME, ctrl_id, fmt, value, {classes={"float-right"}}, max_val) res.value = truncate(res.value or 1) print(res.html) print("") return res.value end -- ############################################ -- Runtime preference function prefsInputFieldPrefs(label, comment, prekey, key, default_value, _input_type, showEnabled, disableAutocomplete, allowURLs, extra) extra = extra or {} if(string.ends(prekey, ".")) then k = prekey..key else k = prekey.."."..key end skip_redis = extra.skip_redis or false if not skip_redis then if(_POST[key] ~= nil) then v_s = _POST[key] v = tonumber(v_s) v_cache = ntop.getPref(k) value = v_cache if ((v_cache == nil) or (v_s ~= v_cache)) then if(v ~= nil and (v > 0) and (v <= 86400)) then ntop.setPref(k, tostring(v)) value = v elseif (v_s ~= nil) then if(allowURLs or (extra.pattern == getURLPattern())) then v_s = string.gsub(v_s, "ldaps:__", "ldaps://") v_s = string.gsub(v_s, "ldap:__", "ldap://") v_s = string.gsub(v_s, "http:__", "http://") v_s = string.gsub(v_s, "https:__", "https://") v_s = string.gsub(v_s, "smtp:__", "smtp://") v_s = string.gsub(v_s, "smtps:__", "smtps://") end ntop.setPref(k, v_s) value = v_s end -- least but not last we asynchronously notify the runtime ntopng instance for changes notifyNtopng(key) end else local v_s = nil if not isEmptyString(prekey) then v_s = ntop.getPref(k) end value = v_s if((v_s==nil) or (v_s=="") or (v_s=="nil")) then value = default_value if not isEmptyString(prekey) and (ntop.getPref(k) ~= tostring(default_value)) then ntop.setPref(k, tostring(default_value)) notifyNtopng(key) end end end else value = default_value end -- skip_redis if ((showEnabled == nil) or (showEnabled == true)) then showEnabled = "table-row" else showEnabled = "none" end local attributes = {} if extra.min ~= nil then if extra.tformat ~= nil then attributes["data-min"] = extra.min else attributes["min"] = extra.min end end if extra.max ~= nil then if extra.tformat ~= nil then attributes["data-max"] = extra.max else attributes["max"] = extra.max end end if extra.minlength ~= nil then if extra.tformat ~= nil then attributes["data-minlength"] = extra.minlength else attributes["minlength"] = extra.minlength end end if extra.maxlength ~= nil then if extra.tformat ~= nil then attributes["data-maxlength"] = extra.maxlength else attributes["maxlength"] = extra.maxlength end end if extra.step ~= nil then if extra.tformat ~= nil then attributes["data-step"] = extra.step else attributes["step"] = extra.step end end if extra.disabled == true then attributes["disabled"] = "disabled" end if extra.required == true then attributes["required"] = "" end if extra.pattern ~= nil then attributes["pattern"] = extra.pattern end if (_input_type == "number") then attributes["required"] = "required" elseif (_input_type == "password") then -- disable chrome autocomplete attributes["autocomplete"] = "new-password" end local input_type = "text" if _input_type ~= nil then input_type = _input_type end print('
'..comment..'
| ]] if extra.tformat ~= nil then value = prefsResolutionButtons(extra.tformat, value, nil, extra.format_spec, extra.max) end if extra.width == nil then if _input_type == "number" then style["width"] = "8em" else style["width"] = "20em" end style["margin-left"] = "auto" else style["width"] = "15em" end style["margin-left"] = "auto" style["display"] = "inline" style = table.merge(style, extra.style) attributes = table.merge(attributes, extra.attributes) print[[ | ]] print(extra.append or "") print[[ ! | |
'..comment..'
| ]] if extra.width == nil then style["width"] = "20em" style["margin-left"] = "auto" else style["width"] = "15em" end style["margin-left"] = "auto" style = table.merge(style, extra.style) attributes = table.merge(attributes, extra.attributes) print[[ | ||
'..comment..'
'..comment..'
'..comment..'