-
Notifications
You must be signed in to change notification settings - Fork 67
feat: add multiaddr filter function #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebc985e
feat: add multiaddr filter function
mpetrunic d04216c
chore: update test/filter/multiaddr-filter.spec.ts
achingbrain 5b1840d
Merge remote-tracking branch 'origin/master' into feat/multiaddr-filter
achingbrain 43844cd
fix: export MultiaddrFilter class from index.js
achingbrain 24f10cd
chore: add docs
achingbrain 0c62f49
chore: add docs
achingbrain e9f609d
chore: linting and deps
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
import type { IpNet } from '@chainsafe/netmask' | ||
import { convertToIpNet } from '../convert.js' | ||
import { multiaddr, Multiaddr, MultiaddrInput } from '../index.js' | ||
|
||
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 hidden or 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 hidden or 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,28 @@ | ||
/* eslint-env mocha */ | ||
import { expect } from 'aegir/chai' | ||
import { MultiaddrFilter } from '../../src/filter/multiaddr-filter.js' | ||
import { 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) | ||
}) | ||
}) | ||
}) | ||
|
||
export {} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.