This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: is multiaddr private and loopback (#10)
- Loading branch information
1 parent
03e272c
commit d7fa562
Showing
7 changed files
with
210 additions
and
4 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
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,17 @@ | ||
'use strict' | ||
|
||
const isLoopbackAddr = require('is-loopback-addr') | ||
|
||
/** | ||
* Check if a given multiaddr is a loopback address. | ||
* | ||
* @param {Multiaddr} ma | ||
* @returns {boolean} | ||
*/ | ||
function isLoopback (ma) { | ||
const { address } = ma.nodeAddress() | ||
|
||
return isLoopbackAddr(address) | ||
} | ||
|
||
module.exports = isLoopback |
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,17 @@ | ||
'use strict' | ||
|
||
const isIpPrivate = require('private-ip') | ||
|
||
/** | ||
* Check if a given multiaddr has a private address. | ||
* | ||
* @param {Multiaddr} ma | ||
* @returns {boolean} | ||
*/ | ||
function isPrivate (ma) { | ||
const { address } = ma.nodeAddress() | ||
|
||
return isIpPrivate(address) | ||
} | ||
|
||
module.exports = isPrivate |
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,57 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const { expect } = require('aegir/utils/chai') | ||
const multiaddr = require('multiaddr') | ||
|
||
const isLoopback = require('../../src/multiaddr/is-loopback') | ||
|
||
describe('multiaddr isLoopback', () => { | ||
it('identifies loopback ip4 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip4/127.0.0.1/tcp/1000'), | ||
multiaddr('/ip4/127.0.1.1/tcp/1000'), | ||
multiaddr('/ip4/127.1.1.1/tcp/1000'), | ||
multiaddr('/ip4/127.255.255.255/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLoopback(ma)).to.eql(true) | ||
}) | ||
}) | ||
|
||
it('identifies non loopback 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(isLoopback(ma)).to.eql(false) | ||
}) | ||
}) | ||
|
||
it('identifies loopback ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/::1/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLoopback(ma)).to.eql(true) | ||
}) | ||
}) | ||
|
||
it('identifies non loopback ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'), | ||
multiaddr('/ip6/::/tcp/1000') | ||
].forEach(ma => { | ||
expect(isLoopback(ma)).to.eql(false) | ||
}) | ||
}) | ||
|
||
it('identifies other multiaddrs as not loopback addresses', () => { | ||
[ | ||
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'), | ||
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443') | ||
].forEach(ma => { | ||
expect(isLoopback(ma)).to.eql(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 */ | ||
'use strict' | ||
|
||
const { expect } = require('aegir/utils/chai') | ||
const multiaddr = require('multiaddr') | ||
|
||
const isPrivate = require('../../src/multiaddr/is-private') | ||
|
||
describe('multiaddr isPrivate', () => { | ||
it('identifies private ip4 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip4/127.0.0.1/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(isPrivate(ma)).to.eql(true) | ||
}) | ||
}) | ||
|
||
it('identifies public ip4 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip4/101.0.26.90/tcp/1000'), | ||
multiaddr('/ip4/40.1.20.9/tcp/1000'), | ||
multiaddr('/ip4/92.168.0.1/tcp/1000'), | ||
multiaddr('/ip4/2.16.0.1/tcp/1000') | ||
].forEach(ma => { | ||
expect(isPrivate(ma)).to.eql(false) | ||
}) | ||
}) | ||
|
||
it('identifies private ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:7095/tcp/1000'), | ||
multiaddr('/ip6/fd52:8342:fc46:6c91:3ac9:86ff:fe31:1/tcp/1000') | ||
].forEach(ma => { | ||
expect(isPrivate(ma)).to.eql(true) | ||
}) | ||
}) | ||
|
||
it('identifies public ip6 multiaddrs', () => { | ||
[ | ||
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'), | ||
multiaddr('/ip6/2000:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000') | ||
].forEach(ma => { | ||
expect(isPrivate(ma)).to.eql(false) | ||
}) | ||
}) | ||
|
||
it('identifies other multiaddrs as not private addresses', () => { | ||
[ | ||
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'), | ||
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443') | ||
].forEach(ma => { | ||
expect(isPrivate(ma)).to.eql(false) | ||
}) | ||
}) | ||
}) |