-- -- (C) 2013-21 - ntop.org -- local dirs = ntop.getDirs() package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path require "lua_utils" local template = require "template_utils" local json = require "dkjson" local page_utils = require("page_utils") local is_nedge = ntop.isnEdge() local is_appliance = ntop.isAppliance() local is_iot_bridge = ntop.isIoTBridge() if not (is_nedge or is_appliance) then return end local subpages = { { name = "mode", nedge = true, appliance = true, url = "mode.lua", label = i18n("nedge.setup_mode") }, { name = "wifi", nedge = false, appliance = true, url = "wifi.lua", label = i18n("prefs.wifi") }, { name = "network_interfaces", nedge = true, appliance = true, url = "interfaces.lua", label = i18n("prefs.network_interfaces") }, { name = "network_setup", nedge = true, appliance = true, url = "network.lua", label = i18n("nedge.interfaces_configuration") }, { name = "dhcp", nedge = true, appliance = false, routing_only = true, url = "dhcp.lua", label = i18n("nedge.dhcp_server") }, { name = "dns", nedge = true, appliance = true, vlan_trunk = false, url = "dns.lua", label = i18n("nedge.dns_configuration") }, { name = "captive_portal", nedge = true, appliance = false, vlan_trunk = false, url = "captive_portal.lua", label = i18n("prefs.toggle_captive_portal_title") }, { name = "shapers", nedge = true, appliance = false, url = "shapers.lua", label = i18n("nedge.shapers") }, { name = "gateways", nedge = true, appliance = false, routing_only = true, url = "gateways.lua", label = i18n("nedge.gateways") }, { name = "static_routes", nedge = true, appliance = false, routing_only = true, url = "static_routes.lua", label = i18n("nedge.static_routes") }, { name = "routing", nedge = true, appliance = false, routing_only = true, url = "routing.lua", label = i18n("nedge.routing_policies") }, { name = "date_time", nedge = true, appliance = true, url = "date_time.lua", label = i18n("nedge.date_time") }, { name = "security", nedge = true, appliance = false, vlan_trunk = false, url = "security.lua", label = i18n("nedge.security") }, { name = "misc", nedge = true, appliance = true, url = "misc.lua", label = i18n("prefs.misc") }, } local system_setup_ui_utils = {} function system_setup_ui_utils.process_apply_discard_config(sys_config) if table.len(_POST) > 0 and sys_config then if _POST["nedge_config_action"] == "discard" then sys_config:discard() elseif _POST["nedge_config_action"] == "make_permanent" then sys_config:applyChanges() end end end function system_setup_ui_utils.print_page_before() sendHTTPContentTypeHeader('text/html') if not haveAdminPrivileges() then return false end page_utils.set_active_menu_entry(page_utils.menu_entries.system_setup) active_page = "admin" dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua") page_utils.print_page_title(i18n("nedge.system_setup")) return true end function system_setup_ui_utils.print_setup_page(print_page_body_callback, sys_config, warnings) warnings = warnings or {} if not system_setup_ui_utils.print_page_before() then return false end system_setup_ui_utils.print_page_after(print_page_body_callback, sys_config, warnings) return true end function system_setup_ui_utils.print_page_after(print_page_body_callback, sys_config, warnings) system_setup_ui_utils.printConfigChange(sys_config, warnings) system_setup_ui_utils.printPageBody(sys_config, print_page_body_callback) end function system_setup_ui_utils.printConfigChange(sys_config, warnings) local first_start = sys_config.isFirstStart() local config_changed = sys_config.configChanged() if config_changed or first_start then if is_nedge then local dhcp_config = sys_config:getDhcpServerConfig() if dhcp_config.enabled then if not sys_config:hasValidDhcpRange(dhcp_config.subnet.first_ip, dhcp_config.subnet.last_ip) then warnings[#warnings + 1] = i18n("nedge.invalid_dhcp_range") end end end print( template.gen("modal_confirm_dialog.html", { dialog={ id = "config_apply_dialog_reboot", action = "$('#applyNedgeConfig').submit()", title = i18n("nedge.apply_configuration"), message = i18n("nedge.apply_configuration_and_reboot"), custom_alert_class = "alert alert-danger", confirm = i18n("nedge.apply_and_reboot"), confirm_button = "btn-primary", } }) ) print( template.gen("modal_confirm_dialog.html", { dialog={ id = "config_apply_dialog_restart_self", action = "$('#applyNedgeConfig').submit()", title = i18n("nedge.apply_configuration"), message = i18n("nedge.apply_configuration_and_restart_self", {product = ntop.getInfo()["product"]}), custom_alert_class = "alert alert-danger", confirm = i18n("nedge.apply_and_restart_self"), confirm_button = "btn-primary", } }) ) print[[ ]] end for _, warning in ipairs(warnings) do if not isEmptyString(warning) then printWarningAlert(warning) end end end function system_setup_ui_utils.printPageBody(sys_config, print_page_body_callback) print[[
]] local mode = sys_config:getOperatingMode() for _, subpage in ipairs(subpages) do if is_appliance and not subpage.appliance then goto continue elseif (not is_iot_bridge or mode == "passive") and subpage.name == "wifi" then goto continue elseif (is_iot_bridge and mode == "bridging") and subpage.name == "network_interfaces" then goto continue elseif is_nedge and not subpage.nedge then goto continue elseif is_nedge then if (subpage.routing_only == true) and (not sys_config:isMultipathRoutingEnabled()) then goto continue elseif (subpage.vlan_trunk == false) and (sys_config:isBridgeOverVLANTrunkEnabled()) then goto continue end end print("]] print(subpage.label) print[[]] ::continue:: end print[[
]] if print_page_body_callback then print_page_body_callback() end print[[
]] dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua") end function system_setup_ui_utils.printPrivateAddressSelector(label, comment, ip_key, netmask_key, value, showEnabled, extra) local field_id = ip_key .. "__id" local networks_presets = { {prefix="192.168", netmask="255.255.255.0"}, {prefix="172.16", netmask="255.255.0.0"}, {prefix="10.0", netmask="255.255.0.0"}, } extra = extra or {} local initial_preset = "" local initial_quad3 = "" local initial_quad4 = "" local netmask_value = "255.255.255.0" if value ~= nil then local parts = string.split(value, "%.") local prefix = table.concat({parts[1], parts[2]}, ".") for _, preset in pairs(networks_presets) do if preset.prefix == prefix then initial_preset = preset.prefix initial_quad3 = parts[3] initial_quad4 = parts[4] break end end end if ((showEnabled == nil) or (showEnabled == true)) then showEnabled = "table-row" else showEnabled = "none" end print(''..label..'

'..comment..'') print [[ ]] print[[
]] -- Find initial netmask for _, preset in pairs(networks_presets) do if preset.prefix == initial_preset then netmask_value = preset.netmask end end if extra.net_select ~= false then print[[ .]] else print[[]] print([[]] .. initial_preset .. [[]]) print(ternary(extra.quad3_select == false, ".", " . ")) end local input_type = "number" if extra.quad3_select == false then input_type = "hidden" print(initial_quad3) end print[[]] print(ternary(extra.quad4_select == false, ".", " . ")) local input_type = "number" if extra.quad4_select == false then input_type = "hidden" print(initial_quad4) end print[[ ]] if netmask_key ~= nil then print[[]] end print[[
]] end function system_setup_ui_utils.prefsDateTimeFieldPrefs(label, comment, key, default_value, showEnabled, extra) extra = extra or {} if ((showEnabled == nil) or (showEnabled == true)) then showEnabled = "table-row" else showEnabled = "none" end local attributes = {} if extra.disabled == true then attributes["disabled"] = "disabled" end if extra.required == true then attributes["required"] = "" end local input_type = "text" print(''..label..'

'..comment..'') local style = {} style["text-align"] = "right" style["margin-bottom"] = "0.5em" print [[ ]] 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) local orig_name = key.."_orig" local picker_name = key.."_picker" print[[
]] end return system_setup_ui_utils