feat: implement validateSubnet

This commit is contained in:
divocat
2025-10-03 00:59:24 +03:00
parent 77e141b305
commit 547feb0e06
5 changed files with 121 additions and 15 deletions

View File

@@ -508,23 +508,18 @@ function createConfigSection(section, map, network) {
o.rmempty = false;
o.ucisection = s.section;
o.validate = function (section_id, value) {
if (!value || value.length === 0) return true;
const subnetRegex = /^(\d{1,3}\.){3}\d{1,3}(\/\d{1,2})?$/;
if (!subnetRegex.test(value)) return _('Invalid format. Use format: X.X.X.X or X.X.X.X/Y');
const [ip, cidr] = value.split('/');
if (ip === "0.0.0.0") {
return _('IP address 0.0.0.0 is not allowed');
// Optional
if (!value || value.length === 0) {
return true
}
const ipParts = ip.split('.');
for (const part of ipParts) {
const num = parseInt(part);
if (num < 0 || num > 255) return _('IP address parts must be between 0 and 255');
const validation = main.validateSubnet(value);
if (validation.valid) {
return true;
}
if (cidr !== undefined) {
const cidrNum = parseInt(cidr);
if (cidrNum < 0 || cidrNum > 32) return _('CIDR must be between 0 and 32');
}
return true;
return _(validation.message)
};
o = s.taboption('basic', form.TextValue, 'user_subnets_text', _('User Subnets List'), _('Enter subnets in CIDR notation or single IP addresses, separated by comma, space or newline. You can add comments after //'));

View File

@@ -80,6 +80,35 @@ function validatePath(value) {
};
}
// src/validators/validateSubnet.ts
function validateSubnet(value) {
const subnetRegex = /^(\d{1,3}\.){3}\d{1,3}(?:\/\d{1,2})?$/;
if (!subnetRegex.test(value)) {
return {
valid: false,
message: "Invalid format. Use X.X.X.X or X.X.X.X/Y"
};
}
const [ip, cidr] = value.split("/");
if (ip === "0.0.0.0") {
return { valid: false, message: "IP address 0.0.0.0 is not allowed" };
}
const ipCheck = validateIPV4(ip);
if (!ipCheck.valid) {
return ipCheck;
}
if (cidr) {
const cidrNum = parseInt(cidr, 10);
if (cidrNum < 0 || cidrNum > 32) {
return {
valid: false,
message: "CIDR must be between 0 and 32"
};
}
}
return { valid: true, message: "Valid" };
}
// src/constants.ts
var STATUS_COLORS = {
SUCCESS: "#4caf50",
@@ -218,5 +247,6 @@ return baseclass.extend({
validateDomain,
validateIPV4,
validatePath,
validateSubnet,
validateUrl
});