-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multiaddr filter function (#305)
related to libp2p/js-libp2p#1510 --------- Co-authored-by: Alex Potsides <[email protected]>
- Loading branch information
1 parent
b083ac9
commit bcd3cb5
Showing
6 changed files
with
117 additions
and
2 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
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,46 @@ | ||
import type { IpNet } from '@chainsafe/netmask' | ||
import { convertToIpNet } from '../convert.js' | ||
import { multiaddr, Multiaddr, MultiaddrInput } from '../index.js' | ||
|
||
/** | ||
* A utility class to determine if a Multiaddr contains another | ||
* multiaddr. | ||
* | ||
* This can be used with ipcidr ranges to determine if a given | ||
* multiaddr is in a ipcidr range. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { multiaddr, MultiaddrFilter } from '@multiformats/multiaddr' | ||
* | ||
* const range = multiaddr('/ip4/192.168.10.10/ipcidr/24') | ||
* const filter = new MultiaddrFilter(range) | ||
* | ||
* const input = multiaddr('/ip4/192.168.10.2/udp/60') | ||
* console.info(filter.contains(input)) // true | ||
* ``` | ||
*/ | ||
export class MultiaddrFilter { | ||
private readonly multiaddr: Multiaddr | ||
private readonly netmask: IpNet | ||
|
||
public constructor (input: MultiaddrInput) { | ||
this.multiaddr = multiaddr(input) | ||
this.netmask = convertToIpNet(this.multiaddr) | ||
} | ||
|
||
public contains (input: MultiaddrInput): boolean { | ||
if (input == null) return false | ||
const m = multiaddr(input) | ||
let ip | ||
for (const [code, value] of m.stringTuples()) { | ||
if (code === 4 || code === 41) { | ||
ip = value | ||
break | ||
} | ||
} | ||
if (ip === undefined) return false | ||
return this.netmask.contains(ip) | ||
} | ||
} |
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
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,25 @@ | ||
/* eslint-env mocha */ | ||
import { expect } from 'aegir/chai' | ||
import { MultiaddrFilter, multiaddr, MultiaddrInput } from '../../src/index.js' | ||
|
||
describe('MultiaddrFilter', () => { | ||
const cases: Array<[MultiaddrInput, MultiaddrInput, boolean]> = [ | ||
['/ip4/192.168.10.10/ipcidr/24', '/ip4/192.168.10.2/tcp/60', true], | ||
[multiaddr('/ip4/192.168.10.10/ipcidr/24'), '/ip4/192.168.10.2/tcp/60', true], | ||
[multiaddr('/ip4/192.168.10.10/ipcidr/24').bytes, '/ip4/192.168.10.2/tcp/60', true], | ||
['/ip4/192.168.10.10/ipcidr/24', '/ip4/192.168.10.2/udp/60', true], | ||
['/ip4/192.168.10.10/ipcidr/24', multiaddr('/ip4/192.168.11.2/tcp/60'), false], | ||
['/ip4/192.168.10.10/ipcidr/24', null, false], | ||
['/ip4/192.168.10.10/ipcidr/24', multiaddr('/ip4/192.168.11.2/udp/60').bytes, false], | ||
['/ip4/192.168.10.10/ipcidr/24', '/ip4/192.168.11.2/udp/60', false], | ||
['/ip4/192.168.10.10/ipcidr/24', '/ip6/2001:db8:3333:4444:5555:6666:7777:8888/tcp/60', false], | ||
['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/60', '/ip6/2001:0db8:3333:4440:0000:0000:0000:0000/tcp/60', true], | ||
['/ip6/2001:db8:3333:4444:5555:6666:7777:8888/ipcidr/60', '/ip6/2001:0db8:3333:4450:0000:0000:0000:0000/tcp/60', false] | ||
] | ||
|
||
cases.forEach(([cidr, ip, result]) => { | ||
it(`multiaddr filter cidr=${cidr} ip=${ip} result=${String(result)}`, function () { | ||
expect(new MultiaddrFilter(cidr).contains(ip)).to.be.equal(result) | ||
}) | ||
}) | ||
}) |