mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
added ip parser helper
This commit is contained in:
35
client/components/utils/ip.jsx
Normal file
35
client/components/utils/ip.jsx
Normal file
@ -0,0 +1,35 @@
|
||||
|
||||
import bigInt from 'big-integer'
|
||||
|
||||
|
||||
export function IPv6ToNumeric(addr) {
|
||||
const parts = addr.split(":"); // let's se what we can do about the :: expansion
|
||||
let expanded = [];
|
||||
|
||||
for (let p of parts) {
|
||||
let binary = parseInt(p, 16).toString(2); // Convert to binary
|
||||
while (binary.length < 16) {
|
||||
// Leftpad
|
||||
binary = "0" + binary;
|
||||
}
|
||||
expanded.push(binary);
|
||||
}
|
||||
|
||||
return bigInt(expanded.join(""), 2);
|
||||
}
|
||||
|
||||
export function IPv4ToNumeric(addr) {
|
||||
const octets = addr.split('.');
|
||||
return parseInt(octets[0], 10) * 16777216 + // 256^3
|
||||
parseInt(octets[1], 10) * 65536 + // 256^2
|
||||
parseInt(octets[2], 10) * 256 + // 256^1
|
||||
parseInt(octets[3], 10); // 256^0
|
||||
}
|
||||
|
||||
export function ipToNumeric(addr) {
|
||||
if (addr.includes(":")) {
|
||||
return IPv6ToNumeric(addr);
|
||||
}
|
||||
return IPv4ToNumeric(addr);
|
||||
}
|
||||
|
Reference in New Issue
Block a user