mirror of
https://github.com/skidoodle/hostinfo
synced 2025-03-16 13:59:38 +01:00
almost there
This commit is contained in:
parent
38e6f714cb
commit
5eb326de05
273 changed files with 230 additions and 261 deletions
|
@ -1,26 +1,36 @@
|
|||
export function isPrivateIP(host: string): boolean {
|
||||
try {
|
||||
const ip = host.startsWith('[') ? host.slice(1, -1) : host;
|
||||
if (ip === 'localhost') return true;
|
||||
try {
|
||||
const rawIp = host.startsWith('[') ? host.slice(1, -1) : host
|
||||
|
||||
// IPv4 private ranges
|
||||
if (ip.includes('.')) {
|
||||
const parts = ip.split('.').map(Number);
|
||||
return parts[0] === 10 ||
|
||||
(parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
|
||||
(parts[0] === 192 && parts[1] === 168) ||
|
||||
(parts[0] === 169 && parts[1] === 254);
|
||||
}
|
||||
if (rawIp === 'localhost') return true
|
||||
|
||||
// IPv6 private ranges
|
||||
if (ip.includes(':')) {
|
||||
return ip.startsWith('fc00::/7') ||
|
||||
ip.startsWith('fe80::/10') ||
|
||||
ip.startsWith('::1');
|
||||
}
|
||||
if (rawIp.includes('.')) {
|
||||
const parts = rawIp.split('.').map(Number)
|
||||
if (parts.length !== 4 || parts.some(isNaN)) return false
|
||||
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
return (
|
||||
parts[0] === 10 ||
|
||||
(parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
|
||||
(parts[0] === 192 && parts[1] === 168) ||
|
||||
(parts[0] === 169 && parts[1] === 254)
|
||||
)
|
||||
}
|
||||
|
||||
if (rawIp.includes(':')) {
|
||||
const ip = rawIp.split('%')[0]
|
||||
|
||||
if (ip === '::1') return true
|
||||
|
||||
const firstHextet = parseInt(ip.split(':')[0], 16)
|
||||
if ((firstHextet & 0xfe00) === 0xfc00) return true
|
||||
|
||||
if ((firstHextet & 0xffc0) === 0xfe80) return true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return false
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue