-- -- (C) 2013-20 - 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() if not (is_nedge or is_appliance) then return end local subpages = { {name = "mode", nedge_only = false, label = i18n("nedge.setup_mode"), url = "mode.lua"}, {name = "network_interfaces", nedge_only = false, label = i18n("prefs.network_interfaces"), url = "interfaces.lua" }, {name = "network_setup", nedge_only = false, label = i18n("nedge.interfaces_configuration"), url = "network.lua"}, {name = "dhcp", nedge_only = false, label = i18n("nedge.dhcp_server"), url = "dhcp.lua", routing_only = true}, {name = "dns", nedge_only = false, label = i18n("nedge.dns_configuration"), url = "dns.lua", vlan_trunk = false}, {name = "captive_portal", nedge_only = true, label = i18n("prefs.toggle_captive_portal_title"), url = "captive_portal.lua", vlan_trunk = false}, {name = "shapers", nedge_only = true, label = i18n("nedge.shapers"), url="shapers.lua"}, {name = "gateways", nedge_only = true, label = i18n("nedge.gateways"), url = "gateways.lua", routing_only = true}, {name = "static_routes", nedge_only = true, label = i18n("nedge.static_routes"), url = "static_routes.lua", routing_only = true}, {name = "routing", nedge_only = true, label = i18n("nedge.routing_policies"), url = "routing.lua", routing_only = true}, {name = "date_time", nedge_only = false, label = i18n("nedge.date_time"), url = "date_time.lua"}, {name = "security", nedge_only = true, label = i18n("nedge.security"), url = "security.lua", vlan_trunk = false}, {name = "misc", nedge_only = false, label = i18n("prefs.misc"), url = "misc.lua"}, } 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 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 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 not is_nedge and subpage.nedge_only then goto continue end if (subpage.routing_only == true) and (not sys_config:isMultipathRoutingEnabled()) then goto continue end if (subpage.vlan_trunk == false) and (sys_config:isBridgeOverVLANTrunkEnabled()) then goto continue 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