From 329c49f1b5d40076ff7744b26ff37d84d4efaf69 Mon Sep 17 00:00:00 2001 From: Joel Gustafson Date: Tue, 29 Nov 2022 14:08:46 -0500 Subject: [PATCH 1/3] treat /dns, /dns4, and /dns6 addrs as public --- src/utils.ts | 5 +++++ test/kad-utils.spec.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/utils.ts b/src/utils.ts index 403721fb..ddb690d1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,6 +19,11 @@ export function removePrivateAddresses (peer: PeerInfo): PeerInfo { multiaddrs: peer.multiaddrs.filter(multiaddr => { const [[type, addr]] = multiaddr.stringTuples() + // treat /dns, /dns4, and /dns6 addrs as public + if (type === 53 || type === 54 || type === 55) { + return true + } + if (type !== 4 && type !== 6) { return false } diff --git a/test/kad-utils.spec.ts b/test/kad-utils.spec.ts index c8068605..0673ad44 100644 --- a/test/kad-utils.spec.ts +++ b/test/kad-utils.spec.ts @@ -4,6 +4,7 @@ import { expect } from 'aegir/chai' import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import { multiaddr } from '@multiformats/multiaddr' import * as utils from '../src/utils.js' import { createPeerId, createPeerIds } from './utils/create-peer-id.js' @@ -60,4 +61,36 @@ describe('kad utils', () => { }) }) }) + + describe('removePrivateAddresses', () => { + it('filters private multiaddrs', async () => { + const id = await createPeerId() + + const multiaddrs = [ + multiaddr('/dns4/example.com/tcp/4001'), + multiaddr('/ip4/192.168.0.1/tcp/4001'), + multiaddr('/ip4/1.1.1.1/tcp/4001'), + ] + + const peerInfo = utils.removePrivateAddresses({id, multiaddrs, protocols: []}) + expect(peerInfo.multiaddrs.map((ma) => ma.toString())) + .to.eql(['/dns4/example.com/tcp/4001', '/ip4/1.1.1.1/tcp/4001']) + }) + }) + + describe('removePublicAddresses', () => { + it('filters public multiaddrs', async () => { + const id = await createPeerId() + + const multiaddrs = [ + multiaddr('/dns4/example.com/tcp/4001'), + multiaddr('/ip4/192.168.0.1/tcp/4001'), + multiaddr('/ip4/1.1.1.1/tcp/4001'), + ] + + const peerInfo = utils.removePublicAddresses({id, multiaddrs, protocols: []}) + expect(peerInfo.multiaddrs.map((ma) => ma.toString())) + .to.eql(['/ip4/192.168.0.1/tcp/4001']) + }) + }) }) From 7b679437f885fc9a9e6d4a9ea984d8bc99e168a8 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 7 Dec 2022 08:57:12 +0000 Subject: [PATCH 2/3] chore: linting --- test/kad-utils.spec.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/kad-utils.spec.ts b/test/kad-utils.spec.ts index 0673ad44..457b34b1 100644 --- a/test/kad-utils.spec.ts +++ b/test/kad-utils.spec.ts @@ -69,10 +69,10 @@ describe('kad utils', () => { const multiaddrs = [ multiaddr('/dns4/example.com/tcp/4001'), multiaddr('/ip4/192.168.0.1/tcp/4001'), - multiaddr('/ip4/1.1.1.1/tcp/4001'), + multiaddr('/ip4/1.1.1.1/tcp/4001') ] - - const peerInfo = utils.removePrivateAddresses({id, multiaddrs, protocols: []}) + + const peerInfo = utils.removePrivateAddresses({ id, multiaddrs, protocols: [] }) expect(peerInfo.multiaddrs.map((ma) => ma.toString())) .to.eql(['/dns4/example.com/tcp/4001', '/ip4/1.1.1.1/tcp/4001']) }) @@ -85,10 +85,10 @@ describe('kad utils', () => { const multiaddrs = [ multiaddr('/dns4/example.com/tcp/4001'), multiaddr('/ip4/192.168.0.1/tcp/4001'), - multiaddr('/ip4/1.1.1.1/tcp/4001'), + multiaddr('/ip4/1.1.1.1/tcp/4001') ] - - const peerInfo = utils.removePublicAddresses({id, multiaddrs, protocols: []}) + + const peerInfo = utils.removePublicAddresses({ id, multiaddrs, protocols: [] }) expect(peerInfo.multiaddrs.map((ma) => ma.toString())) .to.eql(['/ip4/192.168.0.1/tcp/4001']) }) From 58a376667a7b3879f97dcda4cfa09009ee5e1f8d Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 7 Dec 2022 09:22:14 +0000 Subject: [PATCH 3/3] chore: handle localhost --- src/utils.ts | 18 +++++++++++++++++- test/kad-utils.spec.ts | 8 +++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 5eeda1f3..058d919f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,6 +21,11 @@ export function removePrivateAddresses (peer: PeerInfo): PeerInfo { // treat /dns, /dns4, and /dns6 addrs as public if (type === 53 || type === 54 || type === 55) { + // localhost can be a dns address but it's private + if (addr === 'localhost') { + return false + } + return true } @@ -50,6 +55,10 @@ export function removePublicAddresses (peer: PeerInfo): PeerInfo { multiaddrs: peer.multiaddrs.filter(multiaddr => { const [[type, addr]] = multiaddr.stringTuples() + if (addr === 'localhost') { + return true + } + if (type !== 4 && type !== 6) { return false } @@ -58,7 +67,14 @@ export function removePublicAddresses (peer: PeerInfo): PeerInfo { return false } - return isPrivateIp(addr) + const isPrivate = isPrivateIp(addr) + + if (isPrivate == null) { + // not an ip address + return false + } + + return isPrivate }) } } diff --git a/test/kad-utils.spec.ts b/test/kad-utils.spec.ts index 457b34b1..9a5f91cc 100644 --- a/test/kad-utils.spec.ts +++ b/test/kad-utils.spec.ts @@ -69,7 +69,8 @@ describe('kad utils', () => { const multiaddrs = [ multiaddr('/dns4/example.com/tcp/4001'), multiaddr('/ip4/192.168.0.1/tcp/4001'), - multiaddr('/ip4/1.1.1.1/tcp/4001') + multiaddr('/ip4/1.1.1.1/tcp/4001'), + multiaddr('/dns4/localhost/tcp/4001') ] const peerInfo = utils.removePrivateAddresses({ id, multiaddrs, protocols: [] }) @@ -85,12 +86,13 @@ describe('kad utils', () => { const multiaddrs = [ multiaddr('/dns4/example.com/tcp/4001'), multiaddr('/ip4/192.168.0.1/tcp/4001'), - multiaddr('/ip4/1.1.1.1/tcp/4001') + multiaddr('/ip4/1.1.1.1/tcp/4001'), + multiaddr('/dns4/localhost/tcp/4001') ] const peerInfo = utils.removePublicAddresses({ id, multiaddrs, protocols: [] }) expect(peerInfo.multiaddrs.map((ma) => ma.toString())) - .to.eql(['/ip4/192.168.0.1/tcp/4001']) + .to.eql(['/ip4/192.168.0.1/tcp/4001', '/dns4/localhost/tcp/4001']) }) }) })