Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: treat /dns, /dns4, and /dns6 addrs as public #406

Merged
merged 4 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ 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) {
// localhost can be a dns address but it's private
if (addr === 'localhost') {
return false
}

return true
}

if (type !== 4 && type !== 6) {
return false
}
Expand All @@ -45,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
}
Expand All @@ -53,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
})
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/kad-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -60,4 +61,38 @@ 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'),
multiaddr('/dns4/localhost/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'),
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', '/dns4/localhost/tcp/4001'])
})
})
})