mirror of
https://github.com/lush2020/edgetunnel.git
synced 2026-03-23 16:38:34 +08:00
delete some unused code
This commit is contained in:
57
test/cidr.js
57
test/cidr.js
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user