forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add isLinkLocalIp function (libp2p#2863)
To detect when an IP address is link local, add a function,
- Loading branch information
1 parent
771b3fb
commit 143669c
Showing
3 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { isIPv4, isIPv6 } from '@chainsafe/is-ip' | ||
|
||
export function isLinkLocalIp (ip: string): boolean { | ||
if (isIPv4(ip)) { | ||
return ip.startsWith('169.254.') | ||
} | ||
|
||
if (isIPv6(ip)) { | ||
return ip.toLowerCase().startsWith('fe80') | ||
} | ||
|
||
return false | ||
} |
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,58 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { isLinkLocalIp } from '../src/link-local-ip.js' | ||
|
||
describe('isLinkLocalIp', () => { | ||
it('identifies link-local ip4 multiaddrs', () => { | ||
[ | ||
'169.254.35.4', | ||
'169.254.35.4', | ||
'169.254.0.0', | ||
'169.254.255.255' | ||
].forEach(ma => { | ||
expect(isLinkLocalIp(ma)).to.be.true() | ||
}) | ||
}) | ||
|
||
it('identifies non link-local ip4 multiaddrs', () => { | ||
[ | ||
'101.0.26.90', | ||
'10.0.0.1', | ||
'192.168.0.1', | ||
'172.16.0.1' | ||
].forEach(ma => { | ||
expect(isLinkLocalIp(ma)).to.be.false() | ||
}) | ||
}) | ||
|
||
it('identifies link-local ip6 multiaddrs', () => { | ||
[ | ||
'fe80::1%lo0', | ||
'fe80::1%lo0', | ||
'fe80::1893:def4:af04:635a%en', | ||
'fe80::1893:def4:af04:635a', | ||
'fe80::1893:def4:af04:635a' | ||
].forEach(ma => { | ||
expect(isLinkLocalIp(ma)).to.be.true() | ||
}) | ||
}) | ||
|
||
it('identifies non link-local ip6 multiaddrs', () => { | ||
[ | ||
'2001:8a0:7ac5:4201:3ac9:86ff:fe31', | ||
'::' | ||
].forEach(ma => { | ||
expect(isLinkLocalIp(ma)).to.be.false() | ||
}) | ||
}) | ||
|
||
it('identifies other multiaddrs as not link-local addresses', () => { | ||
[ | ||
'wss0.bootstrap.libp2p.io', | ||
'wss0.bootstrap.libp2p.io' | ||
].forEach(ma => { | ||
expect(isLinkLocalIp(ma)).to.be.false() | ||
}) | ||
}) | ||
}) |