Skip to content
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

fix: peers page with /p2p-websocket-star addrs #1306

Merged
merged 6 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 6 additions & 5 deletions src/bundles/peer-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ const parseLatency = (latency) => {
return value
}

const getPublicIP = memoizee((identity) => {
export const getPublicIP = memoizee((identity) => {
if (!identity) return

for (const maddr of identity.addresses) {
try {
const addr = Multiaddr(maddr).nodeAddress()
if (!ip.isPrivate(addr.address)) {

if ((ip.isV4Format(addr.address) || ip.isV6Format(addr.address)) && !ip.isPrivate(addr.address)) {
return addr.address
}
} catch (_) {}
}
})

const isPrivateAndNearby = (maddr, identity) => {
export const isPrivateAndNearby = (maddr, identity) => {
const publicIP = getPublicIP(identity)
let isPrivate = false
let isNearby = false
Expand All @@ -157,9 +158,9 @@ const isPrivateAndNearby = (maddr, identity) => {
// none of the calls bellow for ip library should fail.
isPrivate = ip.isPrivate(addr.address)

if (addr.family === 4) {
if (ip.isV4Format(addr.address)) {
isNearby = ip.cidrSubnet(`${publicIP}/24`).contains(addr.address)
} else if (addr.family === 6) {
} else if (ip.isV6Format(addr.address)) {
isNearby = ip.cidrSubnet(`${publicIP}/48`).contains(addr.address) &&
!ip.cidrSubnet('fc00::/8').contains(addr.address)
// peerIP6 ∉ fc00::/8 to fix case of cjdns where IPs are not spatial allocated.
Expand Down
126 changes: 124 additions & 2 deletions src/bundles/peer-locations.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global it, expect */
/* global it, describe, expect */
import { composeBundlesRaw, createReactorBundle } from 'redux-bundler'
import Multiaddr from 'multiaddr'
import createPeerLocationsBundle from './peer-locations'
import createPeerLocationsBundle, { getPublicIP, isPrivateAndNearby } from './peer-locations'
import { fakeCid } from '../../test/helpers/cid'
import { randomInt, randomNum } from '../../test/helpers/random'
import sleep from '../../test/helpers/sleep'
Expand Down Expand Up @@ -267,3 +267,125 @@ it('should resolve alternative address for failed address lookup', async () => {
expect(Object.keys(peerLocs)).toHaveLength(1)
expectLocation(peerLocs[nextPeers[1].peer.toB58String()])
})

describe('getPublicIP', () => {
it('returns undefined on null identity', async () => {
expect(getPublicIP()).toEqual(undefined)
expect(getPublicIP(null)).toEqual(undefined)
expect(getPublicIP(undefined)).toEqual(undefined)
})

it('returns undefined on local IPs only', async () => {
const res = getPublicIP({
addresses: [
'/ip6/::1/tcp/4003',
'/ip4/127.0.0.1/tcp/4003',
'/ip4/192.168.0.0/tcp/4003',
'/ip4/192.168.255.255/tcp/4003',
'/ip4/10.0.0.0/tcp/4003',
'/ip4/10.255.255.255/tcp/4003',
'/ip4/172.16.0.0/tcp/4003',
'/ip4/172.16.255.255/tcp/4003'
]
})

expect(res).toEqual(undefined)
})

it('returns undefined on discovery.libp2p.io', async () => {
const res = getPublicIP({
addresses: [
'/dnsaddr/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/QmbJbcN3Fvy5bC7Tr95STx5VFiP1G1WLPCHNceh1yShfbb',
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/QmYy3ka6HsQzpdTXY63nUKShsVdgg5zdhMXZXFeNGPyMT4'
]
})

expect(res).toEqual(undefined)
})

it('returns correct IPv4', async () => {
const res = getPublicIP({
addresses: [
'/ip4/127.0.0.1/tcp/4003',
'/ip4/126.1.0.1/tcp/4003'
]
})

expect(res).toEqual('126.1.0.1')
})

it('returns correct IPv6', async () => {
const res = getPublicIP({
addresses: [
'/ip4/127.0.0.1/tcp/4003',
hacdias marked this conversation as resolved.
Show resolved Hide resolved
'/ip6/963c:d4b6:470b:e67b:afc1:c377:213:cad0/tcp/4003'
]
})

expect(res).toEqual('963c:d4b6:470b:e67b:afc1:c377:213:cad0')
})
})

describe('isPrivateAndNearby', () => {
it('is not nearby not private on unexisting identity', async () => {
hacdias marked this conversation as resolved.
Show resolved Hide resolved
const maddr = Multiaddr('/ip4/1.1.1.255/tcp/4003')
const { isNearby, isPrivate } = isPrivateAndNearby(maddr)

expect(isNearby).toEqual(false)
expect(isPrivate).toEqual(false)
})

it('is nearby on ip/24 range', async () => {
const identity = {
addresses: ['/ip4/1.1.1.1/tcp/4003']
}

const maddr = Multiaddr('/ip4/1.1.1.255/tcp/4003')
const { isNearby, isPrivate } = isPrivateAndNearby(maddr, identity)

expect(isNearby).toEqual(true)
expect(isPrivate).toEqual(false)
})

it('is private on local address', async () => {
const identity = {
addresses: ['/ip4/1.1.1.1/tcp/4003']
}

const maddr = Multiaddr('/ip4/192.168.1.0/tcp/4003')
const { isNearby, isPrivate } = isPrivateAndNearby(maddr, identity)

expect(isNearby).toEqual(false)
expect(isPrivate).toEqual(true)
})

it('is not nearby, nor private with IPv4', async () => {
hacdias marked this conversation as resolved.
Show resolved Hide resolved
const identity = {
addresses: ['/ip4/1.1.1.1/tcp/4003']
}

const maddr = Multiaddr('/ip4/2.2.2.2/tcp/4003')
const { isNearby, isPrivate } = isPrivateAndNearby(maddr, identity)

expect(isNearby).toEqual(false)
expect(isPrivate).toEqual(false)
})

it('is not nearby, nor private on discovery.libp2p.io', async () => {
hacdias marked this conversation as resolved.
Show resolved Hide resolved
const identity = {
addresses: ['/ip4/1.1.1.1/tcp/4003']
}

let maddr = Multiaddr('/dnsaddr/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/QmbJbcN3Fvy5bC7Tr95STx5VFiP1G1WLPCHNceh1yShfbb')
let { isNearby, isPrivate } = isPrivateAndNearby(maddr, identity)

expect(isNearby).toEqual(false)
expect(isPrivate).toEqual(false)

maddr = Multiaddr('/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star/ipfs/QmYy3ka6HsQzpdTXY63nUKShsVdgg5zdhMXZXFeNGPyMT4');
({ isNearby, isPrivate } = isPrivateAndNearby(maddr, identity))

expect(isNearby).toEqual(false)
expect(isPrivate).toEqual(false)
})
})