| export const isLanAddress = (url: string) => { |
| try { |
| const urlObj = new URL(url); |
| const hostname = urlObj.hostname; |
|
|
| if (hostname === 'localhost' || hostname === '127.0.0.1') { |
| return true; |
| } |
|
|
| |
| const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; |
| const match = hostname.match(ipv4Regex); |
|
|
| if (match) { |
| const [, a, b, c, d] = match.map(Number); |
| if (a === undefined || b === undefined || c === undefined || d === undefined) { |
| return false; |
| } |
|
|
| |
| if (a > 255 || b > 255 || c > 255 || d > 255) { |
| return false; |
| } |
|
|
| |
| |
| if (a === 10) return true; |
|
|
| |
| if (a === 172 && b >= 16 && b <= 31) return true; |
|
|
| |
| if (a === 192 && b === 168) return true; |
|
|
| |
| if (a === 169 && b === 254) return true; |
|
|
| |
| if (a === 100 && b >= 64 && b <= 127) return true; |
| } |
|
|
| |
| if (hostname.includes(':')) { |
| if ( |
| hostname.startsWith('::1') || |
| hostname.startsWith('fe80:') || |
| hostname.startsWith('fc00:') || |
| hostname.startsWith('fd00:') |
| ) { |
| return true; |
| } |
| } |
|
|
| return false; |
| } catch { |
| return false; |
| } |
| }; |
|
|