Skip to content

Commit

Permalink
fix: add local definition of isPrivateIp (#2362)
Browse files Browse the repository at this point in the history
Use local defintion of isPrivateIp instead of `private-ip` package

This should reduce bundle size and remove another commonjs sub-dependency (`ipaddr.js`)
  • Loading branch information
wemeetagain authored Jan 18, 2024
1 parent 74477f6 commit f27138c
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/kad-dht/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"p-defer": "^4.0.0",
"p-event": "^6.0.0",
"p-queue": "^8.0.0",
"private-ip": "^3.0.1",
"progress-events": "^1.0.0",
"protons-runtime": "^5.0.0",
"race-signal": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/kad-dht/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { peerIdFromBytes } from '@libp2p/peer-id'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { Key } from 'interface-datastore/key'
import { sha256 } from 'multiformats/hashes/sha2'
import isPrivateIp from 'private-ip'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
Expand Down
1 change: 0 additions & 1 deletion packages/libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
"it-parallel": "^3.0.6",
"merge-options": "^3.0.4",
"multiformats": "^13.0.0",
"private-ip": "^3.0.1",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/libp2p/src/config/connection-gater.browser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isPrivate from 'private-ip'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import type { ConnectionGater } from '@libp2p/interface'
import type { Multiaddr } from '@multiformats/multiaddr'

Expand All @@ -14,7 +14,7 @@ export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
const tuples = multiaddr.stringTuples()

if (tuples[0][0] === 4 || tuples[0][0] === 41) {
return Boolean(isPrivate(`${tuples[0][1]}`))
return Boolean(isPrivateIp(`${tuples[0][1]}`))
}

return false
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-autonat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
"@libp2p/interface-internal": "^1.0.7",
"@libp2p/peer-id": "^4.0.5",
"@libp2p/peer-id-factory": "^4.0.5",
"@libp2p/utils": "^5.2.2",
"@multiformats/multiaddr": "^12.1.10",
"it-first": "^3.0.3",
"it-length-prefixed": "^9.0.3",
"it-map": "^3.0.4",
"it-parallel": "^3.0.6",
"it-pipe": "^3.0.1",
"private-ip": "^3.0.1",
"protons-runtime": "^5.0.0",
"uint8arraylist": "^2.4.7"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-autonat/src/autonat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { CodeError, ERR_TIMEOUT, setMaxListeners } from '@libp2p/interface'
import { peerIdFromBytes } from '@libp2p/peer-id'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { multiaddr, protocols } from '@multiformats/multiaddr'
import first from 'it-first'
import * as lp from 'it-length-prefixed'
import map from 'it-map'
import parallel from 'it-parallel'
import { pipe } from 'it-pipe'
import isPrivateIp from 'private-ip'
import {
MAX_INBOUND_STREAMS,
MAX_OUTBOUND_STREAMS,
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol-dcutr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
"dependencies": {
"@libp2p/interface": "^1.1.2",
"@libp2p/interface-internal": "^1.0.7",
"@libp2p/utils": "^5.2.2",
"@multiformats/multiaddr": "^12.1.10",
"@multiformats/multiaddr-matcher": "^1.1.0",
"delay": "^6.0.0",
"it-protobuf-stream": "^1.1.1",
"private-ip": "^3.0.1",
"protons-runtime": "^5.0.0",
"uint8arraylist": "^2.4.7"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol-dcutr/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { type Multiaddr } from '@multiformats/multiaddr'
import { Circuit, IP, DNS } from '@multiformats/multiaddr-matcher'
import isPrivate from 'private-ip'
import type { TransportManager } from '@libp2p/interface-internal'

/**
Expand Down Expand Up @@ -29,5 +29,5 @@ export function isPublicAndDialable (ma: Multiaddr, transportManager: TransportM
return false
}

return isPrivate(ma.toOptions().host) === false
return isPrivateIp(ma.toOptions().host) === false
}
1 change: 0 additions & 1 deletion packages/upnp-nat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"@libp2p/interface-internal": "^1.0.7",
"@libp2p/utils": "^5.2.2",
"@multiformats/multiaddr": "^12.1.10",
"private-ip": "^3.0.1",
"wherearewe": "^2.0.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/upnp-nat/src/upnp-nat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { upnpNat, type NatAPI } from '@achingbrain/nat-port-mapper'
import { CodeError, ERR_INVALID_PARAMETERS } from '@libp2p/interface'
import { isLoopback } from '@libp2p/utils/multiaddr/is-loopback'
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { fromNodeAddress } from '@multiformats/multiaddr'
import isPrivateIp from 'private-ip'
import { isBrowser } from 'wherearewe'
import type { UPnPNATComponents, UPnPNATInit } from './index.js'
import type { Logger, Startable } from '@libp2p/interface'
Expand Down
7 changes: 6 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"types": "./dist/src/peer-queue.d.ts",
"import": "./dist/src/peer-queue.js"
},
"./private-ip": {
"types": "./dist/src/private-ip.d.ts",
"import": "./dist/src/private-ip.js"
},
"./queue": {
"types": "./dist/src/queue/index.d.ts",
"import": "./dist/src/queue/index.js"
Expand Down Expand Up @@ -132,14 +136,15 @@
"is-loopback-addr": "^2.0.1",
"it-pushable": "^3.2.3",
"it-stream-types": "^2.0.1",
"netmask": "^2.0.2",
"p-defer": "^4.0.0",
"private-ip": "^3.0.1",
"race-event": "^1.1.0",
"race-signal": "^1.0.2",
"uint8arraylist": "^2.4.7"
},
"devDependencies": {
"@libp2p/peer-id-factory": "^4.0.5",
"@types/netmask": "^2.0.5",
"aegir": "^42.0.0",
"delay": "^6.0.0",
"it-all": "^3.0.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/multiaddr/is-private.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isIpPrivate from 'private-ip'
import { isPrivateIp } from '../private-ip.js'
import type { Multiaddr } from '@multiformats/multiaddr'

/**
Expand All @@ -8,7 +8,7 @@ export function isPrivate (ma: Multiaddr): boolean {
try {
const { address } = ma.nodeAddress()

return Boolean(isIpPrivate(address))
return Boolean(isPrivateIp(address))
} catch {
return true
}
Expand Down
61 changes: 61 additions & 0 deletions packages/utils/src/private-ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { isIPv4, isIPv6 } from '@chainsafe/is-ip'
import { Netmask } from 'netmask'

const PRIVATE_IP_RANGES = [
'0.0.0.0/8',
'10.0.0.0/8',
'100.64.0.0/10',
'127.0.0.0/8',
'169.254.0.0/16',
'172.16.0.0/12',
'192.0.0.0/24',
'192.0.0.0/29',
'192.0.0.8/32',
'192.0.0.9/32',
'192.0.0.10/32',
'192.0.0.170/32',
'192.0.0.171/32',
'192.0.2.0/24',
'192.31.196.0/24',
'192.52.193.0/24',
'192.88.99.0/24',
'192.168.0.0/16',
'192.175.48.0/24',
'198.18.0.0/15',
'198.51.100.0/24',
'203.0.113.0/24',
'240.0.0.0/4',
'255.255.255.255/32'
]

const NETMASK_RANGES = PRIVATE_IP_RANGES.map(ipRange => new Netmask(ipRange))

function ipv4Check (ipAddr: string): boolean {
for (const r of NETMASK_RANGES) {
if (r.contains(ipAddr)) return true
}

return false
}

function ipv6Check (ipAddr: string): boolean {
return /^::$/.test(ipAddr) ||
/^::1$/.test(ipAddr) ||
/^::f{4}:([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
/^::f{4}:0.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
/^64:ff9b::([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/.test(ipAddr) ||
/^100::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
/^2001::([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
/^2001:2[0-9a-fA-F]:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
/^2001:db8:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
/^2002:([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4}):?([0-9a-fA-F]{0,4})$/.test(ipAddr) ||
/^f[c-d]([0-9a-fA-F]{2,2}):/i.test(ipAddr) ||
/^fe[8-9a-bA-B][0-9a-fA-F]:/i.test(ipAddr) ||
/^ff([0-9a-fA-F]{2,2}):/i.test(ipAddr)
}

export function isPrivateIp (ip: string): boolean | undefined {
if (isIPv4(ip)) return ipv4Check(ip)
else if (isIPv6(ip)) return ipv6Check(ip)
else return undefined
}
Loading

0 comments on commit f27138c

Please sign in to comment.