-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function for detecting link-local multiaddrs
- Loading branch information
1 parent
ce2f45e
commit 0366d0a
Showing
3 changed files
with
92 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,29 @@ | ||
import type { Multiaddr } from '@multiformats/multiaddr' | ||
|
||
const CODEC_IP4 = 0x04 | ||
const CODEC_IP6 = 0x29 | ||
|
||
/** | ||
* Check if a given multiaddr is a link-local address | ||
*/ | ||
export function isLinkLocal (ma: Multiaddr): boolean { | ||
try { | ||
const [[codec, value]] = ma.stringTuples() | ||
|
||
if (value == null) { | ||
return false | ||
} | ||
|
||
if (codec === CODEC_IP4) { | ||
return value.startsWith('169.254.') | ||
} | ||
|
||
if (codec === CODEC_IP6) { | ||
return value.toLowerCase().startsWith('fe80') | ||
} | ||
} catch { | ||
|
||
} | ||
|
||
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,59 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import { isLinkLocal } from '../../src/multiaddr/is-link-local.js' | ||
|
||
describe('multiaddr isLinkLocal', () => { | ||
it('identifies link-local ip4 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip4/169.254.35.4'), | ||
multiaddr('/ip4/169.254.35.4/tcp/1000'), | ||
multiaddr('/ip4/169.254.0.0/tcp/1000'), | ||
multiaddr('/ip4/169.254.255.255/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLinkLocal(ma)).to.be.true() | ||
}) | ||
}) | ||
|
||
it('identifies non link-local ip4 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip4/101.0.26.90/tcp/1000'), | ||
multiaddr('/ip4/10.0.0.1/tcp/1000'), | ||
multiaddr('/ip4/192.168.0.1/tcp/1000'), | ||
multiaddr('/ip4/172.16.0.1/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLinkLocal(ma)).to.be.false() | ||
}) | ||
}) | ||
|
||
it('identifies link-local ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/fe80::1%lo0'), | ||
multiaddr('/ip6/fe80::1%lo0/tcp/1000'), | ||
multiaddr('/ip6/fe80::1893:def4:af04:635a%en'), | ||
multiaddr('/ip6/fe80::1893:def4:af04:635a'), | ||
multiaddr('/ip6/fe80::1893:def4:af04:635a/udp/2183') | ||
].forEach(ma => { | ||
expect(isLinkLocal(ma)).to.be.true() | ||
}) | ||
}) | ||
|
||
it('identifies non link-local ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'), | ||
multiaddr('/ip6/::/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLinkLocal(ma)).to.be.false() | ||
}) | ||
}) | ||
|
||
it('identifies other multiaddrs as not link-local addresses', () => { | ||
[ | ||
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'), | ||
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443') | ||
].forEach(ma => { | ||
expect(isLinkLocal(ma)).to.be.false() | ||
}) | ||
}) | ||
}) |