libp2p 2.3.1-b248eefc0 → 2.3.1-bc90b4fd5

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.
@@ -1,19 +1,33 @@
1
1
  import { isPrivateIp } from '@libp2p/utils/private-ip'
2
+ import { WebSockets } from '@multiformats/multiaddr-matcher'
2
3
  import type { ConnectionGater } from '@libp2p/interface'
3
4
  import type { Multiaddr } from '@multiformats/multiaddr'
4
5
 
6
+ const CODEC_IP4 = 0x04
7
+ const CODEC_IP6 = 0x29
8
+
5
9
  /**
6
- * Returns a connection gater that disallows dialling private addresses by
7
- * default. Browsers are severely limited in their resource usage so don't
8
- * waste time trying to dial undiallable addresses.
10
+ * Returns a connection gater that disallows dialling private addresses or
11
+ * insecure websockets by default.
12
+ *
13
+ * Browsers are severely limited in their resource usage so don't waste time
14
+ * trying to dial undiallable addresses, and they also print verbose error
15
+ * messages when making connections over insecure transports which causes
16
+ * confusion.
9
17
  */
10
18
  export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
11
19
  return {
12
20
  denyDialPeer: async () => false,
13
21
  denyDialMultiaddr: async (multiaddr: Multiaddr) => {
22
+ // do not connect to insecure websockets by default
23
+ if (WebSockets.matches(multiaddr)) {
24
+ return false
25
+ }
26
+
14
27
  const tuples = multiaddr.stringTuples()
15
28
 
16
- if (tuples[0][0] === 4 || tuples[0][0] === 41) {
29
+ // do not connect to private addresses by default
30
+ if (tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) {
17
31
  return Boolean(isPrivateIp(`${tuples[0][1]}`))
18
32
  }
19
33
 
@@ -1,11 +1,11 @@
1
1
  /* eslint-disable max-depth */
2
2
  import { TimeoutError, DialError, setMaxListeners, AbortError } from '@libp2p/interface'
3
3
  import { PeerMap } from '@libp2p/peer-collections'
4
- import { PriorityQueue, type PriorityQueueJobOptions } from '@libp2p/utils/priority-queue'
5
- import { type Multiaddr, type Resolver, resolvers, multiaddr } from '@multiformats/multiaddr'
4
+ import { PriorityQueue } from '@libp2p/utils/priority-queue'
5
+ import { resolvers, multiaddr } from '@multiformats/multiaddr'
6
6
  import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
7
7
  import { Circuit } from '@multiformats/multiaddr-matcher'
8
- import { type ClearableSignal, anySignal } from 'any-signal'
8
+ import { anySignal } from 'any-signal'
9
9
  import { CustomProgressEvent } from 'progress-events'
10
10
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
11
11
  import { DialDeniedError, NoValidAddressesError } from '../errors.js'
@@ -23,7 +23,9 @@ import { resolveMultiaddrs } from './utils.js'
23
23
  import { DEFAULT_DIAL_PRIORITY } from './index.js'
24
24
  import type { AddressSorter, ComponentLogger, Logger, Connection, ConnectionGater, Metrics, PeerId, Address, PeerStore, PeerRouting, IsDialableOptions, OpenConnectionProgressEvents } from '@libp2p/interface'
25
25
  import type { OpenConnectionOptions, TransportManager } from '@libp2p/interface-internal'
26
+ import type { PriorityQueueJobOptions } from '@libp2p/utils/priority-queue'
26
27
  import type { DNS } from '@multiformats/dns'
28
+ import type { Multiaddr, Resolver } from '@multiformats/multiaddr'
27
29
  import type { ProgressOptions } from 'progress-events'
28
30
 
29
31
  export interface PendingDialTarget {
@@ -204,7 +206,12 @@ export class DialQueue {
204
206
  options?.onProgress?.(new CustomProgressEvent('dial-queue:start-dial'))
205
207
  // create abort conditions - need to do this before `calculateMultiaddrs` as
206
208
  // we may be about to resolve a dns addr which can time out
207
- const signal = this.createDialAbortController(options?.signal)
209
+ const signal = anySignal([
210
+ this.shutDownController.signal,
211
+ options.signal
212
+ ])
213
+ setMaxListeners(Infinity, signal)
214
+
208
215
  let addrsToDial: Address[]
209
216
 
210
217
  try {
@@ -299,25 +306,11 @@ export class DialQueue {
299
306
  peerId,
300
307
  priority: options.priority ?? DEFAULT_DIAL_PRIORITY,
301
308
  multiaddrs: new Set(multiaddrs.map(ma => ma.toString())),
302
- signal: options.signal,
309
+ signal: options.signal ?? AbortSignal.timeout(this.dialTimeout),
303
310
  onProgress: options.onProgress
304
311
  })
305
312
  }
306
313
 
307
- private createDialAbortController (userSignal?: AbortSignal): ClearableSignal {
308
- // let any signal abort the dial
309
- const signal = anySignal([
310
- AbortSignal.timeout(this.dialTimeout),
311
- this.shutDownController.signal,
312
- userSignal
313
- ])
314
-
315
- // This emitter gets listened to a lot
316
- setMaxListeners(Infinity, signal)
317
-
318
- return signal
319
- }
320
-
321
314
  // eslint-disable-next-line complexity
322
315
  private async calculateMultiaddrs (peerId?: PeerId, multiaddrs: Set<string> = new Set<string>(), options: OpenConnectionOptions = {}): Promise<Address[]> {
323
316
  const addrs: Address[] = [...multiaddrs].map(ma => ({
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '2.3.1-b248eefc0'
1
+ export const version = '2.3.1-bc90b4fd5'
2
2
  export const name = 'libp2p'