Skip to content

Commit

Permalink
Added network config page
Browse files Browse the repository at this point in the history
  • Loading branch information
DGabri committed Sep 3, 2024
1 parent c92d250 commit b5697e4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 209 deletions.
54 changes: 28 additions & 26 deletions http_src/vue/page-network-configuration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ const props = defineProps({
const ipAddresses = reactive({});
const validationErrors = reactive({});
const update_config_url = `${http_prefix}/lua/rest/v2/get/network_config/network_config.lua`
//const get_config_url = `${http_prefix}/lua/rest/v2/get/network_config/network_config.lua?action=get`
const get_config_url = `${http_prefix}/lua/rest/v2/get/checks/get_checks.lua?check_subdir=flow&ifid=${props.context.ifid}`
const set_config_url = `${http_prefix}/lua/rest/v2/set/network/config.lua`
const get_config_url = `${http_prefix}/lua/rest/v2/get/network/config.lua?ifid=${props.context.ifid}`
const modifiedInputs = ref([]);
const isSaving = ref(false);
Expand All @@ -54,11 +53,11 @@ const saveButtonClass = computed(() => {
});
const check_name = {
"unexpected_dns": { "i18n_title": "flow_checks.dns_servers_title", "device_type": "DNS Server", "reques_param": "unexpected_dns" },
"unexpected_ntp": { "i18n_title": "flow_checks.ntp_servers_title", "device_type": "NTP Server", "reques_param": "unexpected_ntp" },
"unexpected_dhcp": { "i18n_title": "flow_checks.dhcp_servers_title", "device_type": "DHCP Server", "reques_param": "unexpected_dhcp" },
"unexpected_smtp": { "i18n_title": "flow_checks.smtp_servers_title", "device_type": "SMTP Server", "reques_param": "unexpected_smtp" },
"gateway": { "i18n_title": "flow_checks.gateway", "device_type": "Gateway", "reques_param": "gateway" },
"dns_list": { "i18n_title": "flow_checks.dns_servers_title", "device_type": "DNS Server", "reques_param": "dns_list" },
"ntp_list": { "i18n_title": "flow_checks.ntp_servers_title", "device_type": "NTP Server", "reques_param": "ntp_list" },
"dhcp_list": { "i18n_title": "flow_checks.dhcp_servers_title", "device_type": "DHCP Server", "reques_param": "dhcp_list" },
"smtp_list": { "i18n_title": "flow_checks.smtp_servers_title", "device_type": "SMTP Server", "reques_param": "smtp_list" },
"gateway": { "i18n_title": "flow_checks.gateway", "device_type": "Gateway", "reques_param": "gateway" },
}
Object.keys(check_name).forEach(key => {
Expand All @@ -69,10 +68,10 @@ onMounted(() => {
getConfig();
});
// Function used to populate text area with data received from the backend at page initialization
const getConfig = async () => {
const data = await ntopng_utility.http_request(get_config_url)
console.log(data)
data.forEach(item => {
const key = Object.keys(check_name).find(k => k === item.key);
if (key && item.is_enabled === true) {
Expand All @@ -83,13 +82,14 @@ const getConfig = async () => {
})
};
// Used to mark a text area as modified so that only modified text areas are sent to the backend to be stored in redis
const markAsModified = (key) => {
if (!modifiedInputs.value.includes(key)) {
modifiedInputs.value.push(key);
}
};
// Function to validate IP addresses inserted in text area
const validateIpAddresses = () => {
let isValid = true;
Object.keys(ipAddresses).forEach(key => {
Expand All @@ -106,32 +106,34 @@ const validateIpAddresses = () => {
return isValid;
};
// Function used to post data to the backend and save the values in
const saveConfig = async () => {
if (validateIpAddresses()) {
isSaving.value = true;
let data = { csrf: props.context.csrf, config: []};
let headers = {
'Content-Type': 'application/json'
};
try {
for (const key of modifiedInputs.value) {
const value = ipAddresses[key];
const ips = value.split(',').map(ip => ip.trim());
let enabled_value = ips.length > 0;
let requestData = {
check_subdir: 'flow',
script_key: check_name[key].reques_param,
csrf: props.context.csrf,
JSON: JSON.stringify({
all: {
enabled: enabled_value,
script_conf: {
items: ips
}
}
})
asset_key: check_name[key].reques_param,
item: ips
};
console.log(requestData)
await ntopng_utility.http_post_request(update_config_url, requestData);
data.config.push(requestData)
}
console.log(data)
//await ntopng_utility.http_post_request(set_config_url, data);
await ntopng_utility.http_request(set_config_url, { method: 'post', headers, body: JSON.stringify(data) })
modifiedInputs.value = [];
// Show success when saved
Expand Down
10 changes: 5 additions & 5 deletions scripts/lua/modules/http_lint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1968,14 +1968,17 @@ local known_parameters = {
["flow_hash_id"] = validateNumber, -- The ID uniquely identifying the flow in the hash table
["user"] = validateSingleWord, -- The user ID
["snapshot_name"] = validateSingleWord, -- The user ID
["command"] = validateSingleWord, -- The user ID
["report_name"] = validateSingleWord, -- The report name
["report_template"] = validateSingleWord, -- The report template
["report_title"] = validateSingleWord, -- The report title (used in vs reports)
["pool"] = validateNumber, -- A pool ID
["pool_id"] = validateNumber, -- A pool ID
["direction"] = validateDirection, -- Sent or Received direction
["download"] = validateBool,
["item"] = validateSingleWord, -- Used by the Import/Export page to select the item to import/export
["item"] = validateJSON, -- Used by the Import/Export page to select the item to import/export
["config"] = validateUnquoted, -- Used by the Import/Export page to select the item to import/export
["asset_key"] = validateSingleWord, -- Used by network configuration
["stats_type"] = validateStatsType, -- A mode for historical stats queries
["alertstats_type"] = validateAlertStatsType, -- A mode for alerts stats queries
["flowhosts_type"] = validateFlowHostsTypeOrIpVersionOrIp, -- A filter for local/remote hosts in each of the two directions
Expand Down Expand Up @@ -2827,7 +2830,6 @@ local function validateParameter(k, v)
if (type(v) == "table") then
for table_key, table_value in pairs(v) do
local success, message, purified = validateParameter(table_key, table_value)

-- Stop, if any of the table value fails the validation against
-- the expected table key
if not success then
Expand Down Expand Up @@ -2925,10 +2927,9 @@ local function lintParams()

-- Extend the parameters with validators from the scripts
local additional_params = script_manager.extendLintParams(http_lint, known_parameters)

for _, id in pairs(params_to_validate) do
for k, v in pairs(id) do
if (debug) then
if (false) then
io.write("[LINT] Validating [" .. k .. "]\n")
end

Expand All @@ -2939,7 +2940,6 @@ local function lintParams()
end
else
local success, message, purified = validateSpecialParameter(k, v)

if success then
id[k] = purified
else
Expand Down
130 changes: 0 additions & 130 deletions scripts/lua/rest/v2/get/checks/get_checks.lua

This file was deleted.

27 changes: 27 additions & 0 deletions scripts/lua/rest/v2/get/network/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

--
-- (C) 2021 - ntop.org
--

local dirs = ntop.getDirs()
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path

require "lua_utils"
local rest_utils = require "rest_utils"
local json = require "dkjson"

local ifid = tonumber(_GET["ifid"])


local res = {}

-- Get data from redis: expected format, array of objects with keys:
res = {{key= "unexpected_dhcp", value_description="192.168.2.85, 192.168.2.45" or "", is_enabled=true or false}}


if isEmptyString(ifid) then
rest_utils.answer(rest_utils.consts.err.invalid_interface)
return
end

rest_utils.answer(rest_utils.consts.success.ok, res)
48 changes: 0 additions & 48 deletions scripts/lua/rest/v2/get/network_config/network_config.lua

This file was deleted.

Loading

0 comments on commit b5697e4

Please sign in to comment.