-
Notifications
You must be signed in to change notification settings - Fork 649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Created network configuration page #8692
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c1dcec9
Fixed remediation links not working
DGabri 47b9383
Merge branch 'ntop:dev' into dev
DGabri 3215758
Merge branch 'ntop:dev' into dev
DGabri 59b37f3
Added network configuration page
DGabri f8c2cf4
Removed commend and updated component name
DGabri 06cdbdc
Merge branch 'ntop:dev' into dev
DGabri c92d250
Merge branch 'ntop:dev' into dev
DGabri b5697e4
Added network config page
DGabri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<template> | ||
<div class="container mt-4"> | ||
<div v-for="(value, key) in check_name" :key="key" class="mb-4"> | ||
<div class="mb-2"> | ||
<strong>{{ _i18n(value.i18n_title) }}</strong> | ||
</div> | ||
<div> | ||
<textarea v-model="ipAddresses[key]" class="form-control rounded" | ||
:placeholder="`Enter ${value.device_type} IPs (Comma Separated)`" @input="markAsModified(key)" | ||
rows="3"></textarea> | ||
<div v-if="validationErrors[key]" class="text-danger mt-1"> | ||
{{ validationErrors[key] }} | ||
</div> | ||
</div> | ||
</div> | ||
<div class="d-flex justify-content-end mt-4"> | ||
<button @click="saveConfig" :class="saveButtonClass" :disabled="isSaving"> {{ saveButtonText }} | ||
</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref, reactive, onMounted, computed } from 'vue' | ||
import { ntopng_utility } from "../services/context/ntopng_globals_services.js"; | ||
import regexValidation from "../utilities/regex-validation.js"; | ||
|
||
const _i18n = (t) => i18n(t); | ||
|
||
const props = defineProps({ | ||
context: Object | ||
}); | ||
|
||
|
||
const ipAddresses = reactive({}); | ||
const validationErrors = reactive({}); | ||
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); | ||
const saveSuccess = ref(false); | ||
|
||
const saveButtonText = computed(() => { | ||
if (isSaving.value) return 'Saving...'; | ||
if (saveSuccess.value) return 'Saved!'; | ||
return _i18n("flow_checks.save_configuration"); | ||
}); | ||
|
||
const saveButtonClass = computed(() => { | ||
if (saveSuccess.value) return 'btn btn-success'; | ||
return 'btn btn-primary'; | ||
}); | ||
|
||
const check_name = { | ||
"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 => { | ||
ipAddresses[key] = ''; | ||
}); | ||
|
||
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) | ||
|
||
data.forEach(item => { | ||
const key = Object.keys(check_name).find(k => k === item.key); | ||
if (key && item.is_enabled === true) { | ||
ipAddresses[key] = Array.isArray(item.value_description) | ||
? item.value_description.join(', ') | ||
: item.value_description; | ||
} | ||
}) | ||
}; | ||
|
||
// 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 => { | ||
const ips = ipAddresses[key].split(',').map(ip => ip.trim()).filter(ip => ip !== ''); | ||
if (ips.length === 0) { | ||
validationErrors[key] = ''; | ||
} else if (!ips.every(regexValidation.validateIP)) { | ||
validationErrors[key] = 'Invalid IP address format'; | ||
isValid = false; | ||
} else { | ||
validationErrors[key] = ''; | ||
} | ||
}); | ||
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 requestData = { | ||
asset_key: check_name[key].reques_param, | ||
item: ips | ||
}; | ||
|
||
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 | ||
saveSuccess.value = true; | ||
setTimeout(() => { | ||
saveSuccess.value = false; | ||
}, 1500); | ||
|
||
} catch (error) { | ||
console.error('Save failed:', error); | ||
|
||
} finally { | ||
isSaving.value = false; | ||
} | ||
} | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-- | ||
-- (C) 2020 - ntop.org | ||
-- | ||
local dirs = ntop.getDirs() | ||
package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path | ||
|
||
require "lua_utils" | ||
require "ntop_utils" | ||
|
||
local page_utils = require "page_utils" | ||
local json = require "dkjson" | ||
local template_utils = require("template_utils") | ||
local ifid = interface.getId() | ||
|
||
sendHTTPContentTypeHeader('text/html') | ||
|
||
page_utils.print_header_and_set_active_menu_entry(page_utils.menu_entries.network_config) | ||
|
||
dofile(dirs.installdir .. "/scripts/lua/inc/menu.lua") | ||
|
||
local ifstats = interface.getStats() | ||
local probes = ifstats.probes | ||
|
||
page_utils.print_navbar(i18n("checks.network_configuration"), ntop.getHttpPrefix() .. "/lua/admin/network_configuration.lua", {{ | ||
url = ntop.getHttpPrefix() .. "/lua/admin/network_configuration.lua", | ||
active = true, | ||
page_name = "assets_inventory", | ||
label = "<i class=\"fas fa-lg fa-home\" data-bs-toggle=\"tooltip\" data-bs-placement=\"top\" title=\"" .. | ||
i18n("checks.network_configuration") .. "\"></i>" | ||
}}) | ||
|
||
local context = { | ||
ifid = interface.getId(), | ||
csrf = ntop.getRandomCSRFValue() | ||
} | ||
|
||
local json_context = json.encode(context) | ||
|
||
template_utils.render("pages/vue_page.template", { | ||
vue_page_name = "PageNetworkConfiguration", | ||
page_context = json_context | ||
}) | ||
|
||
dofile(dirs.installdir .. "/scripts/lua/inc/footer.lua") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is_enabled is not necessary |
||
|
||
|
||
if isEmptyString(ifid) then | ||
rest_utils.answer(rest_utils.consts.err.invalid_interface) | ||
return | ||
end | ||
|
||
rest_utils.answer(rest_utils.consts.success.ok, res) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
-- | ||
-- (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 action = _GET["action"] | ||
local post_data = _POST["payload"] | ||
|
||
local res = {} | ||
|
||
local config = _POST["config"] | ||
tprint(_POST) | ||
|
||
local data = json.decode(config) | ||
|
||
-- data is: | ||
--[[ | ||
asset_key = [ "gateway", "unexpected_dhcp", "unexpected_dns", "unexpected_ntp", "unexpected_smtp"] | ||
|
||
{ "csrf":..., "config": [ {"asset_key": asset_key, "item": [ip1, ip2, ip3...]}, {"asset_key": asset_key_1, "item": [ip1, ip2, ip3...]}]} | ||
]] | ||
|
||
-- local script_key = post_data["asset_key"] -- asset_key | ||
-- local redis_key = "ntopng.prefs." .. script_key .. "_ip_list" | ||
|
||
-- for each element in respone: ntop.getCache(redis_key) | ||
|
||
if isEmptyString(ifid) then | ||
rest_utils.answer(rest_utils.consts.err.invalid_interface) | ||
return | ||
end | ||
|
||
|
||
rest_utils.answer(rest_utils.consts.success.ok, res) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary