mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-10-20 02:17:34 +00:00
move some stuff around
This commit is contained in:
1 parent
4543cf6d0a
commit
074b7e2aae
7 files changed
+185
-2768
No files matched your search
+8
-41
@@ -29,18 +29,14 @@ for (var subdomain in domains) {
|
|||||||
// Handle A records
|
// Handle A records
|
||||||
if (domainData.record.A) {
|
if (domainData.record.A) {
|
||||||
for (var a in domainData.record.A) {
|
for (var a in domainData.record.A) {
|
||||||
records.push(
|
records.push(A(subdomainName, IP(domainData.record.A[a]), proxyState));
|
||||||
A(subdomainName, IP(domainData.record.A[a]), proxyState)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle AAAA records
|
// Handle AAAA records
|
||||||
if (domainData.record.AAAA) {
|
if (domainData.record.AAAA) {
|
||||||
for (var aaaa in domainData.record.AAAA) {
|
for (var aaaa in domainData.record.AAAA) {
|
||||||
records.push(
|
records.push(AAAA(subdomainName, domainData.record.AAAA[aaaa], proxyState));
|
||||||
AAAA(subdomainName, domainData.record.AAAA[aaaa], proxyState)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,14 +44,7 @@ for (var subdomain in domains) {
|
|||||||
if (domainData.record.CAA) {
|
if (domainData.record.CAA) {
|
||||||
for (var caa in domainData.record.CAA) {
|
for (var caa in domainData.record.CAA) {
|
||||||
var caaRecord = domainData.record.CAA[caa];
|
var caaRecord = domainData.record.CAA[caa];
|
||||||
records.push(
|
records.push(CAA(subdomainName, caaRecord.flags, caaRecord.tag, caaRecord.value));
|
||||||
CAA(
|
|
||||||
subdomainName,
|
|
||||||
caaRecord.flags,
|
|
||||||
caaRecord.tag,
|
|
||||||
caaRecord.value
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,13 +52,9 @@ for (var subdomain in domains) {
|
|||||||
if (domainData.record.CNAME) {
|
if (domainData.record.CNAME) {
|
||||||
// Allow CNAME record on root
|
// Allow CNAME record on root
|
||||||
if (subdomainName === "@") {
|
if (subdomainName === "@") {
|
||||||
records.push(
|
records.push(ALIAS(subdomainName, domainData.record.CNAME + ".", proxyState));
|
||||||
ALIAS(subdomainName, domainData.record.CNAME + ".", proxyState)
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
records.push(
|
records.push(CNAME(subdomainName, domainData.record.CNAME + ".", proxyState));
|
||||||
CNAME(subdomainName, domainData.record.CNAME + ".", proxyState)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,13 +63,7 @@ for (var subdomain in domains) {
|
|||||||
for (var ds in domainData.record.DS) {
|
for (var ds in domainData.record.DS) {
|
||||||
var dsRecord = domainData.record.DS[ds];
|
var dsRecord = domainData.record.DS[ds];
|
||||||
records.push(
|
records.push(
|
||||||
DS(
|
DS(subdomainName, dsRecord.key_tag, dsRecord.algorithm, dsRecord.digest_type, dsRecord.digest)
|
||||||
subdomainName,
|
|
||||||
dsRecord.key_tag,
|
|
||||||
dsRecord.algorithm,
|
|
||||||
dsRecord.digest_type,
|
|
||||||
dsRecord.digest
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,13 +71,7 @@ for (var subdomain in domains) {
|
|||||||
// Handle MX records
|
// Handle MX records
|
||||||
if (domainData.record.MX) {
|
if (domainData.record.MX) {
|
||||||
for (var mx in domainData.record.MX) {
|
for (var mx in domainData.record.MX) {
|
||||||
records.push(
|
records.push(MX(subdomainName, 10 + parseInt(mx), domainData.record.MX[mx] + "."));
|
||||||
MX(
|
|
||||||
subdomainName,
|
|
||||||
10 + parseInt(mx),
|
|
||||||
domainData.record.MX[mx] + "."
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,13 +87,7 @@ for (var subdomain in domains) {
|
|||||||
for (var srv in domainData.record.SRV) {
|
for (var srv in domainData.record.SRV) {
|
||||||
var srvRecord = domainData.record.SRV[srv];
|
var srvRecord = domainData.record.SRV[srv];
|
||||||
records.push(
|
records.push(
|
||||||
SRV(
|
SRV(subdomainName, srvRecord.priority, srvRecord.weight, srvRecord.port, srvRecord.target + ".")
|
||||||
subdomainName,
|
|
||||||
srvRecord.priority,
|
|
||||||
srvRecord.weight,
|
|
||||||
srvRecord.port,
|
|
||||||
srvRecord.target + "."
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
-2437
File diff suppressed because it is too large.
Load diff
@@ -12,10 +12,7 @@ t("Nested subdomains should not exist without a parent subdomain", (t) => {
|
|||||||
if (subdomain.split(".").length > 1) {
|
if (subdomain.split(".").length > 1) {
|
||||||
const parentSubdomain = subdomain.split(".").pop();
|
const parentSubdomain = subdomain.split(".").pop();
|
||||||
|
|
||||||
t.true(
|
t.true(files.includes(`${parentSubdomain}.json`), `${file}: Parent subdomain does not exist`);
|
||||||
files.includes(`${parentSubdomain}.json`),
|
|
||||||
`${file}: Parent subdomain does not exist`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+7
-16
@@ -4,25 +4,24 @@ const path = require("path");
|
|||||||
|
|
||||||
const requiredFields = {
|
const requiredFields = {
|
||||||
owner: "object",
|
owner: "object",
|
||||||
record: "object",
|
record: "object"
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionalFields = {
|
const optionalFields = {
|
||||||
proxied: "boolean",
|
proxied: "boolean",
|
||||||
reserved: "boolean",
|
reserved: "boolean"
|
||||||
};
|
};
|
||||||
|
|
||||||
const requiredOwnerFields = {
|
const requiredOwnerFields = {
|
||||||
username: "string",
|
username: "string"
|
||||||
};
|
};
|
||||||
|
|
||||||
const optionalOwnerFields = {
|
const optionalOwnerFields = {
|
||||||
email: "string",
|
email: "string"
|
||||||
};
|
};
|
||||||
|
|
||||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||||
const hostnameRegex =
|
const hostnameRegex = /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
|
||||||
/^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
|
|
||||||
|
|
||||||
const domainsPath = path.resolve("domains");
|
const domainsPath = path.resolve("domains");
|
||||||
const files = fs.readdirSync(domainsPath);
|
const files = fs.readdirSync(domainsPath);
|
||||||
@@ -30,11 +29,7 @@ const files = fs.readdirSync(domainsPath);
|
|||||||
const validateRequiredFields = (t, obj, requiredFields, file) => {
|
const validateRequiredFields = (t, obj, requiredFields, file) => {
|
||||||
Object.keys(requiredFields).forEach((key) => {
|
Object.keys(requiredFields).forEach((key) => {
|
||||||
t.true(obj.hasOwnProperty(key), `${file}: Missing required field: ${key}`);
|
t.true(obj.hasOwnProperty(key), `${file}: Missing required field: ${key}`);
|
||||||
t.is(
|
t.is(typeof obj[key], requiredFields[key], `${file}: Field ${key} should be of type ${requiredFields[key]}`);
|
||||||
typeof obj[key],
|
|
||||||
requiredFields[key],
|
|
||||||
`${file}: Field ${key} should be of type ${requiredFields[key]}`
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -94,11 +89,7 @@ t("All files should have valid optional fields", (t) => {
|
|||||||
validateOptionalFields(t, data.owner, optionalOwnerFields, file);
|
validateOptionalFields(t, data.owner, optionalOwnerFields, file);
|
||||||
|
|
||||||
if (data.owner.email) {
|
if (data.owner.email) {
|
||||||
t.regex(
|
t.regex(data.owner.email, emailRegex, `${file}: Owner email should be a valid email address`);
|
||||||
data.owner.email,
|
|
||||||
emailRegex,
|
|
||||||
`${file}: Owner email should be a valid email address`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+28
-248
@@ -2,57 +2,39 @@ const t = require("ava");
|
|||||||
const fs = require("fs-extra");
|
const fs = require("fs-extra");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
const { expandIPv6, isPublicIPv4, isPublicIPv6 } = require("../utils/functions");
|
const {
|
||||||
|
checkCNAME,
|
||||||
|
checkNSAndDS,
|
||||||
|
validateHostname,
|
||||||
|
validateIPv4,
|
||||||
|
validateIPv6,
|
||||||
|
validateSpecialRecords,
|
||||||
|
validateTXT,
|
||||||
|
validateURL
|
||||||
|
} = require("../utils/records");
|
||||||
|
|
||||||
const validRecordTypes = ["A", "AAAA", "CAA", "CNAME", "DS", "MX", "NS", "SRV", "TXT", "URL"];
|
const validRecordTypes = ["A", "AAAA", "CAA", "CNAME", "DS", "MX", "NS", "SRV", "TXT", "URL"];
|
||||||
|
|
||||||
const hostnameRegex =
|
|
||||||
/^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
|
|
||||||
const ipv4Regex =
|
|
||||||
/^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/;
|
|
||||||
const ipv6Regex =
|
|
||||||
/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^(?:[0-9a-fA-F]{1,4}:){0,6}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}$/;
|
|
||||||
|
|
||||||
const domainsPath = path.resolve("domains");
|
const domainsPath = path.resolve("domains");
|
||||||
const files = fs.readdirSync(domainsPath);
|
const files = fs.readdirSync(domainsPath);
|
||||||
|
|
||||||
t("All files should have valid record types", (t) => {
|
t("All files should have valid record types", (t) => {
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
const data = fs.readJsonSync(path.join(domainsPath, file));
|
const data = fs.readJsonSync(path.join(domainsPath, file));
|
||||||
|
|
||||||
const recordKeys = Object.keys(data.record);
|
const recordKeys = Object.keys(data.record);
|
||||||
|
|
||||||
recordKeys.forEach((key) => {
|
recordKeys.forEach((key) => {
|
||||||
t.true(validRecordTypes.includes(key), `${file}: Invalid record type: ${key}`);
|
t.true(validRecordTypes.includes(key), `${file}: Invalid record type: ${key}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
// CNAME records cannot be combined with any other record type
|
checkCNAME(t, file, recordKeys);
|
||||||
if (recordKeys.includes("CNAME")) {
|
checkNSAndDS(t, file, recordKeys);
|
||||||
t.is(recordKeys.length, Number(1), `${file}: CNAME records cannot be combined with other records`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NS records cannot be combined with any other record type, except for DS records
|
|
||||||
if (recordKeys.includes("NS")) {
|
|
||||||
t.true(
|
|
||||||
recordKeys.length === 1 || recordKeys.length === 2 && recordKeys.includes("DS"),
|
|
||||||
`${file}: NS records cannot be combined with other records, except for DS records`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// DS records must be combined with NS records
|
|
||||||
if (recordKeys.includes("DS")) {
|
|
||||||
t.true(
|
|
||||||
recordKeys.includes("NS"),
|
|
||||||
`${file}: DS records must be combined with NS records`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
t("All files should not have duplicate record keys", (t) => {
|
t("All files should not have duplicate record keys", (t) => {
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
const data = fs.readJsonSync(path.join(domainsPath, file));
|
const data = fs.readJsonSync(path.join(domainsPath, file));
|
||||||
|
|
||||||
const recordKeys = Object.keys(data.record);
|
const recordKeys = Object.keys(data.record);
|
||||||
const uniqueRecordKeys = new Set(recordKeys);
|
const uniqueRecordKeys = new Set(recordKeys);
|
||||||
|
|
||||||
@@ -63,236 +45,34 @@ t("All files should not have duplicate record keys", (t) => {
|
|||||||
t("All files should have valid record values", (t) => {
|
t("All files should have valid record values", (t) => {
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
const data = fs.readJsonSync(path.join(domainsPath, file));
|
const data = fs.readJsonSync(path.join(domainsPath, file));
|
||||||
|
|
||||||
Object.keys(data.record).forEach((key) => {
|
Object.keys(data.record).forEach((key) => {
|
||||||
const value = data.record[key];
|
const value = data.record[key];
|
||||||
|
|
||||||
// These records must be an array of strings
|
|
||||||
if (["A", "AAAA", "MX", "NS"].includes(key)) {
|
if (["A", "AAAA", "MX", "NS"].includes(key)) {
|
||||||
t.true(Array.isArray(value), `${file}: Record value should be an array for ${key}`);
|
t.true(Array.isArray(value), `${file}: Record value should be an array for ${key}`);
|
||||||
|
|
||||||
value.forEach((record) => {
|
value.forEach((record) =>
|
||||||
t.true(
|
t.true(typeof record === "string", `${file}: Record value should be a string for ${key}`)
|
||||||
typeof record === "string",
|
|
||||||
`${file}: Record value should be a string for ${key}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// A records must be a valid IPv4 address
|
|
||||||
if (key === "A") {
|
|
||||||
value.forEach((record) => {
|
|
||||||
t.regex(
|
|
||||||
record,
|
|
||||||
ipv4Regex,
|
|
||||||
`${file}: Record value should be a valid IPv4 address for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
isPublicIPv4(record, data.proxied),
|
|
||||||
`${file}: Record value should be a public IPv4 address for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// AAAA records must be a valid IPv6 address
|
|
||||||
if (key === "AAAA") {
|
|
||||||
value.forEach((record) => {
|
|
||||||
t.regex(
|
|
||||||
expandIPv6(record),
|
|
||||||
ipv6Regex,
|
|
||||||
`${file}: Record value should be a valid IPv6 address for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
isPublicIPv6(record),
|
|
||||||
`${file}: Record value should be a public IPv6 address for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// MX and NS records must be a valid hostname
|
|
||||||
if (["MX", "NS"].includes(key)) {
|
|
||||||
value.forEach((record) => {
|
|
||||||
t.regex(
|
|
||||||
record,
|
|
||||||
hostnameRegex,
|
|
||||||
`${file}: Record value should be a valid hostname for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// These records must be strings
|
|
||||||
if (["CNAME", "URL"].includes(key)) {
|
|
||||||
t.true(
|
|
||||||
typeof value === "string",
|
|
||||||
`${file}: Record value should be a string for ${key}`
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// CNAME records must be a valid hostname
|
if (key === "A") validateIPv4(t, file, key, value, data);
|
||||||
if (key === "CNAME") {
|
if (key === "AAAA") validateIPv6(t, file, key, value);
|
||||||
t.regex(
|
if (["MX", "NS"].includes(key)) validateHostname(t, file, key, value);
|
||||||
value,
|
} else if (["CNAME", "URL"].includes(key)) {
|
||||||
hostnameRegex,
|
t.true(typeof value === "string", `${file}: Record value should be a string for ${key}`);
|
||||||
`${file}: Record value should be a valid hostname for ${key}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// URL records must be a valid URL
|
if (key === "CNAME") validateHostname(t, file, key, [value]);
|
||||||
if (key === "URL") {
|
if (key === "URL") validateURL(t, file, key, value);
|
||||||
try {
|
} else if (["CAA", "DS", "SRV"].includes(key)) {
|
||||||
new URL(value);
|
|
||||||
} catch (error) {
|
|
||||||
t.fail(`${file}: Record value should be a valid URL for ${key}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// These records must be arrays of objects
|
|
||||||
if (["CAA", "DS", "SRV"].includes(key)) {
|
|
||||||
t.true(Array.isArray(value), `${file}: Record value should be an array for ${key}`);
|
t.true(Array.isArray(value), `${file}: Record value should be an array for ${key}`);
|
||||||
|
|
||||||
value.forEach((record) => {
|
value.forEach((record) =>
|
||||||
t.true(
|
t.true(typeof record === "object", `${file}: Record value should be an object for ${key}`)
|
||||||
typeof record === "object",
|
);
|
||||||
`${file}: Record value should be an object for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (key === "CAA") {
|
validateSpecialRecords(t, file, key, value);
|
||||||
value.forEach((record) => {
|
} else if (key === "TXT") {
|
||||||
// flags must be a number
|
validateTXT(t, file, key, value);
|
||||||
t.true(
|
|
||||||
typeof record.flags === "number",
|
|
||||||
`${file}: CAA record value should have a number for flags at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// tag and value must be strings
|
|
||||||
t.true(
|
|
||||||
typeof record.tag === "string",
|
|
||||||
`${file}: CAA record value should have a string for tag at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
typeof record.value === "string",
|
|
||||||
`${file}: CAA record value should have a string for value at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key === "DS") {
|
|
||||||
value.forEach((record) => {
|
|
||||||
// key_tag, algorithm, and digest_type must be numbers
|
|
||||||
t.true(
|
|
||||||
typeof record.key_tag === "number",
|
|
||||||
`${file}: DS record value should have a number for key_tag at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
typeof record.algorithm === "number",
|
|
||||||
`${file}: DS record value should have a number for algorithm at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
typeof record.digest_type === "number",
|
|
||||||
`${file}: DS record value should have a number for digest_type at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// digest must be a string
|
|
||||||
t.true(
|
|
||||||
typeof record.digest === "string",
|
|
||||||
`${file}: DS record value should have a string for digest at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key === "SRV") {
|
|
||||||
value.forEach((record) => {
|
|
||||||
// priority, weight, and port must be numbers
|
|
||||||
t.true(
|
|
||||||
typeof record.priority === "number",
|
|
||||||
`${file}: SRV record value should have a number for priority at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
typeof record.weight === "number",
|
|
||||||
`${file}: SRV record value should have a number for weight at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
t.true(
|
|
||||||
typeof record.port === "number",
|
|
||||||
`${file}: SRV record value should have a number for port at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// target must be a string
|
|
||||||
t.true(
|
|
||||||
typeof record.target === "string",
|
|
||||||
`${file}: SRV record value should have a string for target at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
|
|
||||||
// target must be a valid hostname
|
|
||||||
t.regex(
|
|
||||||
value.target,
|
|
||||||
hostnameRegex,
|
|
||||||
`${file}: SRV record value should be a valid hostname for target at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TXT records must be either a string or array of strings
|
|
||||||
if (key === "TXT") {
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
value.forEach((record) => {
|
|
||||||
t.true(
|
|
||||||
typeof record === "string",
|
|
||||||
`${file}: Record value should be a string for ${key} at index ${value.indexOf(
|
|
||||||
record
|
|
||||||
)}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
t.true(
|
|
||||||
typeof value === "string",
|
|
||||||
`${file}: Record value should be a string for ${key}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+29
-22
@@ -8,9 +8,13 @@ module.exports.expandIPv6 = function (ip) {
|
|||||||
// Calculate how many "0000" segments are missing
|
// Calculate how many "0000" segments are missing
|
||||||
const nonEmptySegments = segments.filter((seg) => seg !== "");
|
const nonEmptySegments = segments.filter((seg) => seg !== "");
|
||||||
const missingSegments = 8 - nonEmptySegments.length;
|
const missingSegments = 8 - nonEmptySegments.length;
|
||||||
|
|
||||||
// Insert the missing "0000" segments into the position of the empty segment
|
// Insert the missing "0000" segments into the position of the empty segment
|
||||||
segments = [...nonEmptySegments.slice(0, emptyIndex), ...Array(missingSegments).fill("0000"), ...nonEmptySegments.slice(emptyIndex)];
|
segments = [
|
||||||
|
...nonEmptySegments.slice(0, emptyIndex),
|
||||||
|
...Array(missingSegments).fill("0000"),
|
||||||
|
...nonEmptySegments.slice(emptyIndex)
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expand each segment to 4 characters, padding with leading zeros
|
// Expand each segment to 4 characters, padding with leading zeros
|
||||||
@@ -21,10 +25,10 @@ module.exports.expandIPv6 = function (ip) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports.isPublicIPv4 = function (ip, proxied) {
|
module.exports.isPublicIPv4 = function (ip, proxied) {
|
||||||
const parts = ip.split('.').map(Number);
|
const parts = ip.split(".").map(Number);
|
||||||
|
|
||||||
// Validate IPv4 address format
|
// Validate IPv4 address format
|
||||||
if (parts.length !== 4 || parts.some(part => isNaN(part) || part < 0 || part > 255)) {
|
if (parts.length !== 4 || parts.some((part) => isNaN(part) || part < 0 || part > 255)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +40,20 @@ module.exports.isPublicIPv4 = function (ip, proxied) {
|
|||||||
// Check for private and reserved IPv4 ranges
|
// Check for private and reserved IPv4 ranges
|
||||||
return !(
|
return !(
|
||||||
// Private ranges
|
// Private ranges
|
||||||
parts[0] === 10 ||
|
(
|
||||||
(parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
|
parts[0] === 10 ||
|
||||||
(parts[0] === 192 && parts[1] === 168) ||
|
(parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
|
||||||
|
(parts[0] === 192 && parts[1] === 168) ||
|
||||||
// Reserved or special-use ranges
|
// Reserved or special-use ranges
|
||||||
(parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127) || // Carrier-grade NAT
|
(parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127) || // Carrier-grade NAT
|
||||||
(parts[0] === 169 && parts[1] === 254) || // Link-local
|
(parts[0] === 169 && parts[1] === 254) || // Link-local
|
||||||
(parts[0] === 192 && parts[1] === 0 && parts[2] === 0) || // IETF Protocol Assignments
|
(parts[0] === 192 && parts[1] === 0 && parts[2] === 0) || // IETF Protocol Assignments
|
||||||
(parts[0] === 192 && parts[1] === 0 && parts[2] === 2) || // Documentation (TEST-NET-1)
|
(parts[0] === 192 && parts[1] === 0 && parts[2] === 2) || // Documentation (TEST-NET-1)
|
||||||
(parts[0] === 198 && parts[1] === 18) || // Network Interconnect Devices
|
(parts[0] === 198 && parts[1] === 18) || // Network Interconnect Devices
|
||||||
(parts[0] === 198 && parts[1] === 51 && parts[2] === 100) || // Documentation (TEST-NET-2)
|
(parts[0] === 198 && parts[1] === 51 && parts[2] === 100) || // Documentation (TEST-NET-2)
|
||||||
(parts[0] === 203 && parts[1] === 0 && parts[2] === 113) || // Documentation (TEST-NET-3)
|
(parts[0] === 203 && parts[1] === 0 && parts[2] === 113) || // Documentation (TEST-NET-3)
|
||||||
(parts[0] >= 224) // Multicast and reserved ranges
|
parts[0] >= 224
|
||||||
|
) // Multicast and reserved ranges
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,10 +62,12 @@ module.exports.isPublicIPv6 = function (ip) {
|
|||||||
|
|
||||||
// Check for private or special-use IPv6 ranges
|
// Check for private or special-use IPv6 ranges
|
||||||
return !(
|
return !(
|
||||||
normalizedIP.startsWith("fc") || // Unique Local Address (ULA)
|
(
|
||||||
normalizedIP.startsWith("fd") || // Unique Local Address (ULA)
|
normalizedIP.startsWith("fc") || // Unique Local Address (ULA)
|
||||||
normalizedIP.startsWith("fe80") || // Link-local
|
normalizedIP.startsWith("fd") || // Unique Local Address (ULA)
|
||||||
normalizedIP.startsWith("::1") || // Loopback address (::1)
|
normalizedIP.startsWith("fe80") || // Link-local
|
||||||
normalizedIP.startsWith("2001:db8") // Documentation range
|
normalizedIP.startsWith("::1") || // Loopback address (::1)
|
||||||
|
normalizedIP.startsWith("2001:db8")
|
||||||
|
) // Documentation range
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
const { expandIPv6, isPublicIPv4, isPublicIPv6 } = require("./functions");
|
||||||
|
|
||||||
|
const hostnameRegex = /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/;
|
||||||
|
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/;
|
||||||
|
const ipv6Regex =
|
||||||
|
/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^(?:[0-9a-fA-F]{1,4}:){0,6}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}$/;
|
||||||
|
|
||||||
|
// Check CNAME records
|
||||||
|
function checkCNAME(t, file, recordKeys) {
|
||||||
|
if (recordKeys.includes("CNAME")) {
|
||||||
|
t.is(recordKeys.length, 1, `${file}: CNAME records cannot be combined with other records`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check NS and DS records
|
||||||
|
function checkNSAndDS(t, file, recordKeys) {
|
||||||
|
if (recordKeys.includes("NS")) {
|
||||||
|
t.true(
|
||||||
|
recordKeys.length === 1 || (recordKeys.length === 2 && recordKeys.includes("DS")),
|
||||||
|
`${file}: NS records cannot be combined with other records, except for DS records`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recordKeys.includes("DS")) {
|
||||||
|
t.true(recordKeys.includes("NS"), `${file}: DS records must be combined with NS records`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate IPv4 record
|
||||||
|
function validateIPv4(t, file, key, value, data) {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.regex(record, ipv4Regex, `${file}: Record value should be a valid IPv4 address for ${key}`);
|
||||||
|
t.true(isPublicIPv4(record, data.proxied), `${file}: Record value should be a public IPv4 address for ${key}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate IPv6 record
|
||||||
|
function validateIPv6(t, file, key, value) {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.regex(expandIPv6(record), ipv6Regex, `${file}: Record value should be a valid IPv6 address for ${key}`);
|
||||||
|
t.true(isPublicIPv6(record), `${file}: Record value should be a public IPv6 address for ${key}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate hostname record
|
||||||
|
function validateHostname(t, file, key, value) {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.regex(record, hostnameRegex, `${file}: Record value should be a valid hostname for ${key}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate URL record
|
||||||
|
function validateURL(t, file, key, value) {
|
||||||
|
try {
|
||||||
|
new URL(value);
|
||||||
|
} catch (error) {
|
||||||
|
t.fail(`${file}: Record value should be a valid URL for ${key}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate special records (CAA, DS, SRV)
|
||||||
|
function validateSpecialRecords(t, file, key, value) {
|
||||||
|
if (key === "CAA") {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.true(typeof record.flags === "number", `${file}: CAA record value should have a number for flags`);
|
||||||
|
t.true(typeof record.tag === "string", `${file}: CAA record value should have a string for tag`);
|
||||||
|
t.true(typeof record.value === "string", `${file}: CAA record value should have a string for value`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (key === "DS") {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.true(typeof record.key_tag === "number", `${file}: DS record value should have a number for key_tag`);
|
||||||
|
t.true(typeof record.algorithm === "number", `${file}: DS record value should have a number for algorithm`);
|
||||||
|
t.true(
|
||||||
|
typeof record.digest_type === "number",
|
||||||
|
`${file}: DS record value should have a number for digest_type`
|
||||||
|
);
|
||||||
|
t.true(typeof record.digest === "string", `${file}: DS record value should have a string for digest`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (key === "SRV") {
|
||||||
|
value.forEach((record) => {
|
||||||
|
t.true(typeof record.priority === "number", `${file}: SRV record value should have a number for priority`);
|
||||||
|
t.true(typeof record.weight === "number", `${file}: SRV record value should have a number for weight`);
|
||||||
|
t.true(typeof record.port === "number", `${file}: SRV record value should have a number for port`);
|
||||||
|
t.true(typeof record.target === "string", `${file}: SRV record value should have a string for target`);
|
||||||
|
t.regex(record.target, hostnameRegex, `${file}: SRV record value should have a valid hostname for target`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate TXT record
|
||||||
|
function validateTXT(t, file, key, value) {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((record) =>
|
||||||
|
t.true(typeof record === "string", `${file}: Record value should be a string for ${key}`)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
t.true(typeof value === "string", `${file}: Record value should be a string for ${key}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
checkCNAME,
|
||||||
|
checkNSAndDS,
|
||||||
|
validateIPv4,
|
||||||
|
validateIPv6,
|
||||||
|
validateHostname,
|
||||||
|
validateURL,
|
||||||
|
validateSpecialRecords,
|
||||||
|
validateTXT
|
||||||
|
};
|
||||||
Reference in new issue
Block a user