libp2p 2.10.0-a02cb0461 → 2.10.0-e8398d97e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +39 -37
  2. package/dist/index.min.js +13 -12
  3. package/dist/index.min.js.map +4 -4
  4. package/dist/src/address-manager/dns-mappings.d.ts +1 -2
  5. package/dist/src/address-manager/dns-mappings.d.ts.map +1 -1
  6. package/dist/src/address-manager/dns-mappings.js +39 -42
  7. package/dist/src/address-manager/dns-mappings.js.map +1 -1
  8. package/dist/src/address-manager/index.d.ts.map +1 -1
  9. package/dist/src/address-manager/index.js +31 -12
  10. package/dist/src/address-manager/index.js.map +1 -1
  11. package/dist/src/address-manager/ip-mappings.d.ts +1 -0
  12. package/dist/src/address-manager/ip-mappings.d.ts.map +1 -1
  13. package/dist/src/address-manager/ip-mappings.js +51 -40
  14. package/dist/src/address-manager/ip-mappings.js.map +1 -1
  15. package/dist/src/address-manager/transport-addresses.js +6 -6
  16. package/dist/src/address-manager/transport-addresses.js.map +1 -1
  17. package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
  18. package/dist/src/config/connection-gater.browser.js +8 -22
  19. package/dist/src/config/connection-gater.browser.js.map +1 -1
  20. package/dist/src/config/connection-gater.d.ts.map +1 -1
  21. package/dist/src/config/connection-gater.js +1 -12
  22. package/dist/src/config/connection-gater.js.map +1 -1
  23. package/dist/src/config.d.ts.map +1 -1
  24. package/dist/src/config.js +3 -22
  25. package/dist/src/config.js.map +1 -1
  26. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  27. package/dist/src/connection-manager/connection-pruner.js +6 -1
  28. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  29. package/dist/src/connection-manager/dial-queue.js +2 -2
  30. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  31. package/dist/src/connection-manager/index.d.ts +0 -16
  32. package/dist/src/connection-manager/index.d.ts.map +1 -1
  33. package/dist/src/connection-manager/index.js +18 -10
  34. package/dist/src/connection-manager/index.js.map +1 -1
  35. package/dist/src/connection-manager/utils.d.ts +3 -3
  36. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  37. package/dist/src/connection-manager/utils.js +24 -21
  38. package/dist/src/connection-manager/utils.js.map +1 -1
  39. package/dist/src/connection.d.ts +1 -0
  40. package/dist/src/connection.d.ts.map +1 -1
  41. package/dist/src/connection.js +32 -2
  42. package/dist/src/connection.js.map +1 -1
  43. package/dist/src/get-peer.js +3 -3
  44. package/dist/src/get-peer.js.map +1 -1
  45. package/dist/src/libp2p.d.ts +3 -1
  46. package/dist/src/libp2p.d.ts.map +1 -1
  47. package/dist/src/libp2p.js +11 -5
  48. package/dist/src/libp2p.js.map +1 -1
  49. package/dist/src/registrar.d.ts +5 -1
  50. package/dist/src/registrar.d.ts.map +1 -1
  51. package/dist/src/registrar.js +17 -6
  52. package/dist/src/registrar.js.map +1 -1
  53. package/dist/src/upgrader.d.ts.map +1 -1
  54. package/dist/src/upgrader.js +3 -2
  55. package/dist/src/upgrader.js.map +1 -1
  56. package/dist/src/version.d.ts +1 -1
  57. package/dist/src/version.js +1 -1
  58. package/package.json +13 -13
  59. package/src/address-manager/dns-mappings.ts +50 -49
  60. package/src/address-manager/index.ts +36 -14
  61. package/src/address-manager/ip-mappings.ts +64 -44
  62. package/src/address-manager/transport-addresses.ts +7 -7
  63. package/src/config/connection-gater.browser.ts +8 -24
  64. package/src/config/connection-gater.ts +1 -12
  65. package/src/config.ts +3 -25
  66. package/src/connection-manager/connection-pruner.ts +7 -1
  67. package/src/connection-manager/dial-queue.ts +2 -2
  68. package/src/connection-manager/index.ts +20 -28
  69. package/src/connection-manager/utils.ts +26 -22
  70. package/src/connection.ts +40 -3
  71. package/src/get-peer.ts +3 -3
  72. package/src/libp2p.ts +14 -6
  73. package/src/registrar.ts +21 -8
  74. package/src/upgrader.ts +3 -2
  75. package/src/version.ts +1 -1
@@ -1,9 +1,9 @@
1
- import { isPrivateIp, trackedMap } from '@libp2p/utils'
2
- import { multiaddr, protocols } from '@multiformats/multiaddr'
1
+ import { getNetConfig, isNetworkAddress, isPrivateIp, trackedMap } from '@libp2p/utils'
2
+ import { CODE_SNI, CODE_TLS, multiaddr } from '@multiformats/multiaddr'
3
3
  import type { AddressManagerComponents, AddressManagerInit } from './index.js'
4
4
  import type { Logger } from '@libp2p/interface'
5
5
  import type { NodeAddress } from '@libp2p/interface-internal'
6
- import type { Multiaddr, StringTuple } from '@multiformats/multiaddr'
6
+ import type { Multiaddr } from '@multiformats/multiaddr'
7
7
 
8
8
  const MAX_DATE = 8_640_000_000_000_000
9
9
 
@@ -18,13 +18,6 @@ interface DNSMapping {
18
18
  lastVerified?: number
19
19
  }
20
20
 
21
- const CODEC_TLS = 0x01c0
22
- const CODEC_SNI = 0x01c1
23
- const CODEC_DNS = 0x35
24
- const CODEC_DNS4 = 0x36
25
- const CODEC_DNS6 = 0x37
26
- const CODEC_DNSADDR = 0x38
27
-
28
21
  export class DNSMappings {
29
22
  private readonly log: Logger
30
23
  private readonly mappings: Map<string, DNSMapping>
@@ -38,7 +31,12 @@ export class DNSMappings {
38
31
  }
39
32
 
40
33
  has (ma: Multiaddr): boolean {
41
- const host = this.findHost(ma)
34
+ const config = getNetConfig(ma)
35
+ let host = config.host
36
+
37
+ if ((config.type === 'ip4' || config.type === 'ip6') && config.sni != null) {
38
+ host = config.sni
39
+ }
42
40
 
43
41
  for (const mapping of this.mappings.values()) {
44
42
  if (mapping.domain === host) {
@@ -66,11 +64,16 @@ export class DNSMappings {
66
64
  }
67
65
 
68
66
  remove (ma: Multiaddr): boolean {
69
- const host = this.findHost(ma)
67
+ const config = getNetConfig(ma)
68
+
69
+ if (config.type !== 'ip4' && config.type !== 'ip6') {
70
+ return false
71
+ }
72
+
70
73
  let wasConfident = false
71
74
 
72
75
  for (const [ip, mapping] of this.mappings.entries()) {
73
- if (mapping.domain === host) {
76
+ if (mapping.domain === config.sni) {
74
77
  this.log('removing %s to %s DNS mapping %e', ip, mapping.domain)
75
78
  this.mappings.delete(ip)
76
79
  wasConfident = wasConfident || mapping.verified
@@ -84,37 +87,30 @@ export class DNSMappings {
84
87
  const dnsMappedAddresses: NodeAddress[] = []
85
88
 
86
89
  for (let i = 0; i < addresses.length; i++) {
87
- const address = addresses[i]
88
- const tuples = address.multiaddr.stringTuples()
89
- const host = tuples[0][1]
90
+ const address = addresses[i].multiaddr
90
91
 
91
- if (host == null) {
92
+ if (!isNetworkAddress(address)) {
92
93
  continue
93
94
  }
94
95
 
96
+ const config = getNetConfig(address)
97
+
95
98
  for (const [ip, mapping] of this.mappings.entries()) {
96
- if (host !== ip) {
99
+ if (config.host !== ip) {
97
100
  continue
98
101
  }
99
102
 
100
103
  // insert SNI tuple after TLS tuple, if one is present
101
- const mappedIp = this.maybeAddSNITuple(tuples, mapping.domain)
104
+ const maWithSni = this.maybeAddSNIComponent(address, mapping.domain)
102
105
 
103
- if (mappedIp) {
106
+ if (maWithSni != null) {
104
107
  // remove the address and replace it with the version that includes
105
108
  // the SNI tuple
106
109
  addresses.splice(i, 1)
107
110
  i--
108
111
 
109
112
  dnsMappedAddresses.push({
110
- multiaddr: multiaddr(`/${
111
- tuples.map(tuple => {
112
- return [
113
- protocols(tuple[0]).name,
114
- tuple[1]
115
- ].join('/')
116
- }).join('/')
117
- }`),
113
+ multiaddr: maWithSni,
118
114
  verified: mapping.verified,
119
115
  type: 'dns-mapping',
120
116
  expires: mapping.expires,
@@ -127,19 +123,30 @@ export class DNSMappings {
127
123
  return dnsMappedAddresses
128
124
  }
129
125
 
130
- private maybeAddSNITuple (tuples: StringTuple[], domain: string): boolean {
131
- for (let j = 0; j < tuples.length; j++) {
132
- if (tuples[j][0] === CODEC_TLS && tuples[j + 1]?.[0] !== CODEC_SNI) {
133
- tuples.splice(j + 1, 0, [CODEC_SNI, domain])
134
- return true
126
+ private maybeAddSNIComponent (ma: Multiaddr, domain: string): Multiaddr | undefined {
127
+ const components = ma.getComponents()
128
+
129
+ for (let j = 0; j < components.length; j++) {
130
+ if (components[j].code === CODE_TLS && components[j + 1]?.code !== CODE_SNI) {
131
+ components.splice(j + 1, 0, {
132
+ name: 'sni',
133
+ code: CODE_SNI,
134
+ value: domain
135
+ })
136
+
137
+ return multiaddr(components)
135
138
  }
136
139
  }
137
-
138
- return false
139
140
  }
140
141
 
141
142
  confirm (ma: Multiaddr, ttl: number): boolean {
142
- const host = this.findHost(ma)
143
+ const config = getNetConfig(ma)
144
+ let host = config.host
145
+
146
+ if ((config.type === 'ip4' || config.type === 'ip6') && config.sni != null) {
147
+ host = config.sni
148
+ }
149
+
143
150
  let startingConfidence = false
144
151
 
145
152
  for (const [ip, mapping] of this.mappings.entries()) {
@@ -156,7 +163,13 @@ export class DNSMappings {
156
163
  }
157
164
 
158
165
  unconfirm (ma: Multiaddr, ttl: number): boolean {
159
- const host = this.findHost(ma)
166
+ const config = getNetConfig(ma)
167
+
168
+ if (config.type !== 'ip4' && config.type !== 'ip6') {
169
+ return false
170
+ }
171
+
172
+ const host = config.sni ?? config.host
160
173
  let wasConfident = false
161
174
 
162
175
  for (const [ip, mapping] of this.mappings.entries()) {
@@ -170,16 +183,4 @@ export class DNSMappings {
170
183
 
171
184
  return wasConfident
172
185
  }
173
-
174
- private findHost (ma: Multiaddr): string | undefined {
175
- for (const tuple of ma.stringTuples()) {
176
- if (tuple[0] === CODEC_SNI) {
177
- return tuple[1]
178
- }
179
-
180
- if (tuple[0] === CODEC_DNS || tuple[0] === CODEC_DNS4 || tuple[0] === CODEC_DNS6 || tuple[0] === CODEC_DNSADDR) {
181
- return tuple[1]
182
- }
183
- }
184
- }
185
186
  }
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable complexity */
2
2
  import { isIPv4 } from '@chainsafe/is-ip'
3
3
  import { peerIdFromString } from '@libp2p/peer-id'
4
- import { debounce, createScalableCuckooFilter, isPrivateIp } from '@libp2p/utils'
5
- import { multiaddr } from '@multiformats/multiaddr'
4
+ import { debounce, createScalableCuckooFilter, isPrivateIp, getNetConfig, isNetworkAddress, isLoopback } from '@libp2p/utils'
5
+ import { CODE_P2P, multiaddr } from '@multiformats/multiaddr'
6
6
  import { QUIC_V1, TCP, WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
7
7
  import { DNSMappings } from './dns-mappings.js'
8
8
  import { IPMappings } from './ip-mappings.js'
@@ -99,7 +99,7 @@ const defaultAddressFilter = (addrs: Multiaddr[]): Multiaddr[] => addrs
99
99
  * If the passed multiaddr contains the passed peer id, remove it
100
100
  */
101
101
  function stripPeerId (ma: Multiaddr, peerId: PeerId): Multiaddr {
102
- const observedPeerIdStr = ma.getPeerId()
102
+ const observedPeerIdStr = ma.getComponents().findLast(c => c.code === CODE_P2P)?.value
103
103
 
104
104
  // strip our peer id if it has been passed
105
105
  if (observedPeerIdStr != null) {
@@ -175,7 +175,7 @@ export class AddressManager implements AddressManagerInterface {
175
175
  const addrs = this.getAddresses()
176
176
  .map(ma => {
177
177
  // strip our peer id if it is present
178
- if (ma.getPeerId() === this.components.peerId.toString()) {
178
+ if (ma.getComponents().findLast(c => c.code === CODE_P2P)?.value === this.components.peerId.toString()) {
179
179
  return ma.decapsulate(`/p2p/${this.components.peerId.toString()}`)
180
180
  }
181
181
 
@@ -222,8 +222,22 @@ export class AddressManager implements AddressManagerInterface {
222
222
  * Add peer observed addresses
223
223
  */
224
224
  addObservedAddr (addr: Multiaddr): void {
225
- const tuples = addr.stringTuples()
226
- const socketAddress = `${tuples[0][1]}:${tuples[1][1]}`
225
+ const config = getNetConfig(addr)
226
+ let socketAddress: string
227
+
228
+ switch (config.type) {
229
+ case 'ip4': {
230
+ socketAddress = `${config.host}:${config.port}`
231
+ break
232
+ }
233
+ case 'ip6': {
234
+ socketAddress = `[${config.host}]:${config.port}`
235
+ break
236
+ }
237
+ default: {
238
+ return
239
+ }
240
+ }
227
241
 
228
242
  // ignore if this address if it's been observed before
229
243
  if (this.observedAddressFilter.has(socketAddress)) {
@@ -475,10 +489,14 @@ export class AddressManager implements AddressManagerInterface {
475
489
  return false
476
490
  }
477
491
 
478
- const maOptions = ma.toOptions()
492
+ if (!isNetworkAddress(ma)) {
493
+ return false
494
+ }
495
+
496
+ const config = getNetConfig(ma)
479
497
 
480
498
  // only public IPv4 addresses
481
- if (maOptions.family === 6 || maOptions.host === '127.0.0.1' || isPrivateIp(maOptions.host) === true) {
499
+ if (config.type !== 'ip4' || isPrivateIp(config.host) === true) {
482
500
  return false
483
501
  }
484
502
 
@@ -500,7 +518,7 @@ export class AddressManager implements AddressManagerInterface {
500
518
  const transportListeners = listeners.filter(listener => {
501
519
  return listener.getAddrs().filter(ma => {
502
520
  // only IPv4 addresses of the matcher type
503
- return ma.toOptions().family === 4 && matcher(ma)
521
+ return getNetConfig(ma).type === 'ip4' && matcher(ma)
504
522
  }).length > 0
505
523
  })
506
524
 
@@ -514,23 +532,27 @@ export class AddressManager implements AddressManagerInterface {
514
532
  // we have one listener which listens on one port so whatever the external
515
533
  // NAT port mapping is, it should be for this listener
516
534
  const linkLocalAddr = transportListeners[0].getAddrs().filter(ma => {
517
- return ma.toOptions().host !== '127.0.0.1'
535
+ return !isLoopback(ma)
518
536
  }).pop()
519
537
 
520
538
  if (linkLocalAddr == null) {
521
539
  continue
522
540
  }
523
541
 
524
- const linkLocalOptions = linkLocalAddr.toOptions()
542
+ const linkLocalOptions = getNetConfig(linkLocalAddr)
543
+
544
+ if (linkLocalOptions.port == null) {
545
+ return false
546
+ }
525
547
 
526
548
  // upgrade observed address to IP mapping
527
549
  this.observed.remove(ma)
528
550
  this.ipMappings.add(
529
551
  linkLocalOptions.host,
530
552
  linkLocalOptions.port,
531
- maOptions.host,
532
- maOptions.port,
533
- maOptions.transport
553
+ config.host,
554
+ config.port,
555
+ config.protocol
534
556
  )
535
557
 
536
558
  return true
@@ -1,6 +1,6 @@
1
1
  import { isIPv4 } from '@chainsafe/is-ip'
2
- import { trackedMap } from '@libp2p/utils'
3
- import { multiaddr, protocols } from '@multiformats/multiaddr'
2
+ import { getNetConfig, isNetworkAddress, trackedMap } from '@libp2p/utils'
3
+ import { CODE_IP4, CODE_IP6, multiaddr } from '@multiformats/multiaddr'
4
4
  import type { AddressManagerComponents, AddressManagerInit } from './index.js'
5
5
  import type { Logger } from '@libp2p/interface'
6
6
  import type { NodeAddress } from '@libp2p/interface-internal'
@@ -22,11 +22,6 @@ interface PublicAddressMapping {
22
22
  lastVerified?: number
23
23
  }
24
24
 
25
- const CODEC_IP4 = 0x04
26
- const CODEC_IP6 = 0x29
27
- const CODEC_TCP = 0x06
28
- const CODEC_UDP = 0x0111
29
-
30
25
  export class IPMappings {
31
26
  private readonly log: Logger
32
27
  private readonly mappings: Map<string, PublicAddressMapping[]>
@@ -40,11 +35,15 @@ export class IPMappings {
40
35
  }
41
36
 
42
37
  has (ma: Multiaddr): boolean {
43
- const tuples = ma.stringTuples()
38
+ const config = getNetConfig(ma)
39
+
40
+ if (config.type !== 'ip4' && config.type !== 'ip6') {
41
+ return false
42
+ }
44
43
 
45
44
  for (const mappings of this.mappings.values()) {
46
45
  for (const mapping of mappings) {
47
- if (mapping.externalIp === tuples[0][1]) {
46
+ if (mapping.externalIp === config.host) {
48
47
  return true
49
48
  }
50
49
  }
@@ -72,18 +71,20 @@ export class IPMappings {
72
71
  }
73
72
 
74
73
  remove (ma: Multiaddr): boolean {
75
- const tuples = ma.stringTuples()
76
- const host = tuples[0][1] ?? ''
77
- const protocol = tuples[1][0] === CODEC_TCP ? 'tcp' : 'udp'
78
- const port = parseInt(tuples[1][1] ?? '0')
74
+ const config = getNetConfig(ma)
75
+
76
+ if (config.type !== 'ip4' && config.type !== 'ip6') {
77
+ return false
78
+ }
79
+
79
80
  let wasConfident = false
80
81
 
81
82
  for (const [key, mappings] of this.mappings.entries()) {
82
83
  for (let i = 0; i < mappings.length; i++) {
83
84
  const mapping = mappings[i]
84
85
 
85
- if (mapping.externalIp === host && mapping.externalPort === port && mapping.protocol === protocol) {
86
- this.log('removing %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, host, port, protocol)
86
+ if (mapping.externalIp === config.host && mapping.externalPort === config.port && mapping.protocol === config.protocol) {
87
+ this.log('removing %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, config.host, config.port, config.protocol)
87
88
 
88
89
  wasConfident = wasConfident || mapping.verified
89
90
  mappings.splice(i, 1)
@@ -103,40 +104,38 @@ export class IPMappings {
103
104
  const ipMappedAddresses: NodeAddress[] = []
104
105
 
105
106
  for (const { multiaddr: ma } of addresses) {
106
- const tuples = ma.stringTuples()
107
- let tuple: string | undefined
107
+ if (!isNetworkAddress(ma)) {
108
+ continue
109
+ }
110
+
111
+ const config = getNetConfig(ma)
112
+
113
+ if (config.type !== 'ip4' && config.type !== 'ip6') {
114
+ continue
115
+ }
116
+
117
+ let key: string | undefined
108
118
 
109
119
  // see if the internal host/port/protocol tuple has been mapped externally
110
- if ((tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) && tuples[1][0] === CODEC_TCP) {
111
- tuple = `${tuples[0][1]}-${tuples[1][1]}-tcp`
112
- } else if ((tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) && tuples[1][0] === CODEC_UDP) {
113
- tuple = `${tuples[0][1]}-${tuples[1][1]}-udp`
120
+ if (config.protocol === 'tcp') {
121
+ key = `${config.host}-${config.port}-tcp`
122
+ } else if (config.protocol === 'udp') {
123
+ key = `${config.host}-${config.port}-udp`
114
124
  }
115
125
 
116
- if (tuple == null) {
126
+ if (key == null) {
117
127
  continue
118
128
  }
119
129
 
120
- const mappings = this.mappings.get(tuple)
130
+ const mappings = this.mappings.get(key)
121
131
 
122
132
  if (mappings == null) {
123
133
  continue
124
134
  }
125
135
 
126
136
  for (const mapping of mappings) {
127
- tuples[0][0] = mapping.externalFamily === 4 ? CODEC_IP4 : CODEC_IP6
128
- tuples[0][1] = mapping.externalIp
129
- tuples[1][1] = `${mapping.externalPort}`
130
-
131
137
  ipMappedAddresses.push({
132
- multiaddr: multiaddr(`/${
133
- tuples.map(tuple => {
134
- return [
135
- protocols(tuple[0]).name,
136
- tuple[1]
137
- ].join('/')
138
- }).join('/')
139
- }`),
138
+ multiaddr: this.maybeOverrideIp(ma, mapping.externalIp, mapping.externalFamily, mapping.protocol, mapping.externalPort),
140
139
  verified: mapping.verified,
141
140
  type: 'ip-mapping',
142
141
  expires: mapping.expires,
@@ -148,14 +147,34 @@ export class IPMappings {
148
147
  return ipMappedAddresses
149
148
  }
150
149
 
150
+ private maybeOverrideIp (ma: Multiaddr, externalIp: string, externalFamily: number, protocol: 'tcp' | 'udp', externalPort: number): Multiaddr {
151
+ const components = ma.getComponents()
152
+
153
+ const ipIndex = components.findIndex(c => c.code === CODE_IP4 || c.code === CODE_IP6)
154
+ const portIndex = components.findIndex(c => c.name === protocol)
155
+
156
+ if (ipIndex > -1 && portIndex > -1) {
157
+ components[ipIndex].value = externalIp
158
+ components[ipIndex].code = externalFamily === 4 ? CODE_IP4 : CODE_IP6
159
+ components[portIndex].value = `${externalPort}`
160
+
161
+ return multiaddr(components)
162
+ }
163
+
164
+ return ma
165
+ }
166
+
151
167
  confirm (ma: Multiaddr, ttl: number): boolean {
152
- const tuples = ma.stringTuples()
153
- const host = tuples[0][1]
168
+ if (!isNetworkAddress(ma)) {
169
+ return false
170
+ }
171
+
172
+ const config = getNetConfig(ma)
154
173
  let startingConfidence = false
155
174
 
156
175
  for (const mappings of this.mappings.values()) {
157
176
  for (const mapping of mappings) {
158
- if (mapping.externalIp === host) {
177
+ if (mapping.externalIp === config.host) {
159
178
  this.log('marking %s to %s IP mapping as verified', mapping.internalIp, mapping.externalIp)
160
179
  startingConfidence = mapping.verified
161
180
  mapping.verified = true
@@ -169,18 +188,19 @@ export class IPMappings {
169
188
  }
170
189
 
171
190
  unconfirm (ma: Multiaddr, ttl: number): boolean {
172
- const tuples = ma.stringTuples()
173
- const host = tuples[0][1] ?? ''
174
- const protocol = tuples[1][0] === CODEC_TCP ? 'tcp' : 'udp'
175
- const port = parseInt(tuples[1][1] ?? '0')
191
+ if (!isNetworkAddress(ma)) {
192
+ return false
193
+ }
194
+
195
+ const config = getNetConfig(ma)
176
196
  let wasConfident = false
177
197
 
178
198
  for (const mappings of this.mappings.values()) {
179
199
  for (let i = 0; i < mappings.length; i++) {
180
200
  const mapping = mappings[i]
181
201
 
182
- if (mapping.externalIp === host && mapping.externalPort === port && mapping.protocol === protocol) {
183
- this.log('removing verification of %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, host, port, protocol)
202
+ if (mapping.externalIp === config.host && mapping.externalPort === config.port && mapping.protocol === config.protocol) {
203
+ this.log('removing verification of %s:%s to %s:%s %s IP mapping', mapping.externalIp, mapping.externalPort, config.host, config.port, config.protocol)
184
204
 
185
205
  wasConfident = wasConfident || mapping.verified
186
206
  mapping.verified = false
@@ -1,4 +1,4 @@
1
- import { isNetworkAddress, isPrivate, trackedMap } from '@libp2p/utils'
1
+ import { getNetConfig, isNetworkAddress, isPrivate, trackedMap } from '@libp2p/utils'
2
2
  import type { AddressManagerComponents, AddressManagerInit } from './index.js'
3
3
  import type { Logger } from '@libp2p/interface'
4
4
  import type { NodeAddress } from '@libp2p/interface-internal'
@@ -112,13 +112,13 @@ export class TransportAddresses {
112
112
  }
113
113
 
114
114
  private toKey (ma: Multiaddr): string {
115
- if (isNetworkAddress(ma)) {
116
- // only works for dns/ip based addresses
117
- const options = ma.toOptions()
118
-
119
- return `${options.host}-${options.port}-${options.transport}`
115
+ if (!isNetworkAddress(ma)) {
116
+ return ma.toString()
120
117
  }
121
118
 
122
- return ma.toString()
119
+ // only works for dns/ip based addresses
120
+ const config = getNetConfig(ma)
121
+
122
+ return `${config.host}-${config.port}-${config.protocol}`
123
123
  }
124
124
  }
@@ -1,11 +1,8 @@
1
- import { isPrivateIp } from '@libp2p/utils'
1
+ import { isPrivate } from '@libp2p/utils'
2
2
  import { WebSockets } from '@multiformats/multiaddr-matcher'
3
3
  import type { ConnectionGater } from '@libp2p/interface'
4
4
  import type { Multiaddr } from '@multiformats/multiaddr'
5
5
 
6
- const CODEC_IP4 = 0x04
7
- const CODEC_IP6 = 0x29
8
-
9
6
  /**
10
7
  * Returns a connection gater that disallows dialling private addresses or
11
8
  * insecure websockets by default.
@@ -16,30 +13,17 @@ const CODEC_IP6 = 0x29
16
13
  * confusion.
17
14
  */
18
15
  export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
19
- return {
20
- denyDialPeer: async () => false,
21
- denyDialMultiaddr: async (multiaddr: Multiaddr) => {
16
+ if (gater.denyDialMultiaddr == null) {
17
+ gater.denyDialMultiaddr = (multiaddr: Multiaddr) => {
22
18
  // do not connect to insecure websockets by default
23
19
  if (WebSockets.matches(multiaddr)) {
24
- return false
20
+ return true
25
21
  }
26
22
 
27
- const tuples = multiaddr.stringTuples()
28
-
29
23
  // do not connect to private addresses by default
30
- if (tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) {
31
- return Boolean(isPrivateIp(`${tuples[0][1]}`))
32
- }
33
-
34
- return false
35
- },
36
- denyInboundConnection: async () => false,
37
- denyOutboundConnection: async () => false,
38
- denyInboundEncryptedConnection: async () => false,
39
- denyOutboundEncryptedConnection: async () => false,
40
- denyInboundUpgradedConnection: async () => false,
41
- denyOutboundUpgradedConnection: async () => false,
42
- filterMultiaddrForPeer: async () => true,
43
- ...gater
24
+ return isPrivate(multiaddr)
25
+ }
44
26
  }
27
+
28
+ return gater
45
29
  }
@@ -4,16 +4,5 @@ import type { ConnectionGater } from '@libp2p/interface'
4
4
  * Returns a default connection gater implementation that allows everything
5
5
  */
6
6
  export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
7
- return {
8
- denyDialPeer: async () => false,
9
- denyDialMultiaddr: async () => false,
10
- denyInboundConnection: async () => false,
11
- denyOutboundConnection: async () => false,
12
- denyInboundEncryptedConnection: async () => false,
13
- denyOutboundEncryptedConnection: async () => false,
14
- denyInboundUpgradedConnection: async () => false,
15
- denyOutboundUpgradedConnection: async () => false,
16
- filterMultiaddrForPeer: async () => true,
17
- ...gater
18
- }
7
+ return gater
19
8
  }
package/src/config.ts CHANGED
@@ -1,33 +1,11 @@
1
- import { FaultTolerance, InvalidParametersError } from '@libp2p/interface'
2
- import { mergeOptions } from '@libp2p/utils'
3
- import { dnsaddrResolver } from './connection-manager/resolvers/dnsaddr.ts'
1
+ import { InvalidParametersError } from '@libp2p/interface'
4
2
  import type { Libp2pInit } from './index.js'
5
3
  import type { ServiceMap } from '@libp2p/interface'
6
- import type { Multiaddr } from '@multiformats/multiaddr'
7
-
8
- const DefaultConfig: Libp2pInit = {
9
- addresses: {
10
- listen: [],
11
- announce: [],
12
- noAnnounce: [],
13
- announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
14
- },
15
- connectionManager: {
16
- resolvers: {
17
- dnsaddr: dnsaddrResolver
18
- }
19
- },
20
- transportManager: {
21
- faultTolerance: FaultTolerance.FATAL_ALL
22
- }
23
- }
24
4
 
25
5
  export async function validateConfig <T extends ServiceMap = Record<string, unknown>> (opts: Libp2pInit<T>): Promise<Libp2pInit<T>> {
26
- const resultingOptions: Libp2pInit<T> = mergeOptions(DefaultConfig, opts)
27
-
28
- if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) {
6
+ if (opts.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) {
29
7
  throw new InvalidParametersError('Private network is enforced, but no protector was provided')
30
8
  }
31
9
 
32
- return resultingOptions
10
+ return opts
33
11
  }
@@ -1,4 +1,5 @@
1
1
  import { PeerMap } from '@libp2p/peer-collections'
2
+ import { getNetConfig, isNetworkAddress } from '@libp2p/utils'
2
3
  import { multiaddrToIpNet, safelyCloseConnectionIfUnused } from './utils.js'
3
4
  import type { IpNet } from '@chainsafe/netmask'
4
5
  import type { Libp2pEvents, Logger, ComponentLogger, PeerStore, Connection } from '@libp2p/interface'
@@ -102,7 +103,12 @@ export class ConnectionPruner {
102
103
  this.log('too many connections open - closing a connection to %p', connection.remotePeer)
103
104
  // check allow list
104
105
  const connectionInAllowList = this.allow.some((ipNet) => {
105
- return ipNet.contains(connection.remoteAddr.nodeAddress().address)
106
+ if (isNetworkAddress(connection.remoteAddr)) {
107
+ const config = getNetConfig(connection.remoteAddr)
108
+ return ipNet.contains(config.host)
109
+ }
110
+
111
+ return true
106
112
  })
107
113
 
108
114
  // Connections in the allow list should be excluded from pruning
@@ -2,7 +2,7 @@
2
2
  import { TimeoutError, DialError, AbortError } from '@libp2p/interface'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
4
  import { PriorityQueue } from '@libp2p/utils'
5
- import { multiaddr } from '@multiformats/multiaddr'
5
+ import { CODE_P2P, multiaddr } from '@multiformats/multiaddr'
6
6
  import { Circuit } from '@multiformats/multiaddr-matcher'
7
7
  import { anySignal } from 'any-signal'
8
8
  import { setMaxListeners } from 'main-event'
@@ -446,7 +446,7 @@ export class DialQueue {
446
446
  // if the resolved multiaddr has a PeerID but it's the wrong one, ignore it
447
447
  // - this can happen with addresses like bootstrap.libp2p.io that resolve
448
448
  // to multiple different peers
449
- const addrPeerId = addr.multiaddr.getPeerId()
449
+ const addrPeerId = addr.multiaddr.getComponents().findLast(c => c.code === CODE_P2P)?.value
450
450
  if (peerId != null && addrPeerId != null) {
451
451
  return peerId.equals(addrPeerId)
452
452
  }