| 51 | } |
| 52 | |
| 53 | function isLoopback(addr: string) { |
| 54 | // If addr is an IPv4 address in long integer form (no dots and no colons), convert it |
| 55 | if (!/\./.test(addr) && !/:/.test(addr)) { |
| 56 | addr = fromLong(Number(addr)); |
| 57 | } |
| 58 | |
| 59 | return ( |
| 60 | /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test(addr) || |
| 61 | /^0177\./.test(addr) || |
| 62 | /^0x7f\./i.test(addr) || |
| 63 | /^fe80::1$/i.test(addr) || |
| 64 | /^::1$/.test(addr) || |
| 65 | /^\[::1\]/.test(addr) || |
| 66 | /^::$/.test(addr) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | function fromLong(ipl: number) { |
| 71 | return `${ipl >>> 24}.${(ipl >> 16) & 255}.${(ipl >> 8) & 255}.${ipl & 255}`; |