This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search by CIDR address with ipv4 and ipv6
- Loading branch information
1 parent
b2e2d6d
commit fbd0a24
Showing
3 changed files
with
63 additions
and
11 deletions.
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
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,59 @@ | ||
import { NodeNetworkConfigurationInterface } from '@types'; | ||
import { getIPV4Address, getIPV6Address } from '@utils/interfaces/getters'; | ||
|
||
const decimalToBinary = (decimalNumber: number) => (decimalNumber >>> 0).toString(2); | ||
|
||
const ipv4StringToBinary = (ip: string) => | ||
ip | ||
.split('.') | ||
.map((classes) => decimalToBinary(parseInt(classes)).padStart(8, '0')) | ||
.join(''); | ||
|
||
const convertIPv6ToBinary = (ip: string) => { | ||
const ipBinary = ip | ||
.toLowerCase() | ||
.replaceAll('::', ':0000:') | ||
.split(':') | ||
.map((ip) => parseInt(ip, 16).toString(2).padStart(16, '0')); | ||
return ipBinary.join(''); | ||
}; | ||
|
||
export const compareCIDR = (ipSearch: string, ip: string, isIPv4 = true) => { | ||
const [baseIp, range] = ipSearch.split('/'); | ||
|
||
const baseIpBinary = isIPv4 ? ipv4StringToBinary(baseIp) : convertIPv6ToBinary(baseIp); | ||
|
||
const ipBinary = isIPv4 ? ipv4StringToBinary(ip) : convertIPv6ToBinary(baseIp); | ||
|
||
const rangeNumber = parseInt(range); | ||
|
||
const baseIpBinarySlice = baseIpBinary.slice(0, rangeNumber); | ||
const ipBinarySlice = ipBinary.slice(0, rangeNumber); | ||
|
||
return baseIpBinarySlice === ipBinarySlice; | ||
}; | ||
|
||
export const searchInterfaceByIP = ( | ||
searchIPAddress: string, | ||
iface: NodeNetworkConfigurationInterface, | ||
) => { | ||
if (!searchIPAddress) return true; | ||
|
||
const isSearchByIpv4 = searchIPAddress.includes('.'); | ||
|
||
if (searchIPAddress.includes('/')) { | ||
const ipv4Address = getIPV4Address(iface); | ||
const ipv6Address = getIPV6Address(iface); | ||
|
||
if (ipv4Address && isSearchByIpv4 && compareCIDR(searchIPAddress, ipv4Address)) { | ||
return true; | ||
} | ||
if (ipv6Address && !isSearchByIpv4 && compareCIDR(searchIPAddress, ipv6Address, false)) { | ||
return true; | ||
} | ||
} | ||
|
||
const addresses = [getIPV4Address(iface), getIPV6Address(iface)].filter(Boolean); | ||
|
||
return addresses?.some((address) => address?.toLowerCase().includes(searchIPAddress)); | ||
}; |