delete some unused code

This commit is contained in:
Emo-Damage
2023-05-28 01:39:09 +08:00
parent 13fb9eb86d
commit 990f001d1b
3 changed files with 61 additions and 69 deletions

View File

@@ -6546,3 +6546,60 @@ const cidrList = [
const ipInt = "142.250.204.68".split('.').reduce((acc, val) => (acc << 8) + parseInt(val, 10), 0) >>> 0;
console.log(ipInt);
/**
* Checks if an IPv4 address is within a CIDR range.
*
* @param {string} address The IPv4 address to check.
* @param {string} cidr The CIDR range to check against.
* @returns {boolean} `true` if the address is within the CIDR range, `false` otherwise.
*/
function isIPv4InRange(address, cidr) {
// Parse the address and CIDR range
const addressParts = address.split('.').map(part => parseInt(part, 10));
const [rangeAddress, rangePrefix] = cidr.split('/');
const rangeParts = rangeAddress.split('.').map(part => parseInt(part, 10));
const prefix = parseInt(rangePrefix, 10);
// Convert the address and range to binary format
const addressBinary = addressParts.reduce((acc, part) => acc + part.toString(2).padStart(8, '0'), '');
const rangeBinary = rangeParts.reduce((acc, part) => acc + part.toString(2).padStart(8, '0'), '');
// Compare the bits up to the prefix length
for (let i = 0; i < prefix; i++) {
if (addressBinary[i] !== rangeBinary[i]) {
return false;
}
}
return true;
}
/**
* Checks if an IPv6 address is within a CIDR range.
*
* @param {string} address The IPv6 address to check.
* @param {string} cidr The CIDR range to check against.
* @returns {boolean} `true` if the address is within the CIDR range, `false` otherwise.
*/
function isIPv6InRange(address, cidr) {
// Parse the address and CIDR range
const addressParts = address.split(':').map(part => parseInt(part, 16));
const [rangeAddress, rangePrefix] = cidr.split('/');
const rangeParts = rangeAddress.split(':').map(part => parseInt(part, 16));
const prefix = parseInt(rangePrefix, 10);
// Convert the address and range to binary format
const addressBinary = addressParts.reduce((acc, part) => acc + part.toString(2).padStart(16, '0'), '');
const rangeBinary = rangeParts.reduce((acc, part) => acc + part.toString(2).padStart(16, '0'), '');
// Compare the bits up to the prefix length
for (let i = 0; i < prefix; i++) {
if (addressBinary[i] !== rangeBinary[i]) {
return false;
}
}
return true;
}