Skip to content

Commit

Permalink
chore: skip ws addresses in browser gater
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Nov 25, 2024
1 parent e76328d commit 78f76a1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions packages/libp2p/src/config/connection-gater.browser.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { isPrivateIp } from '@libp2p/utils/private-ip'
import { WebSockets } from '@multiformats/multiaddr-matcher'
import type { ConnectionGater } from '@libp2p/interface'
import type { Multiaddr } from '@multiformats/multiaddr'

const CODEC_IP4 = 0x04
const CODEC_IP6 = 0x29

/**
* Returns a connection gater that disallows dialling private addresses by
* default. Browsers are severely limited in their resource usage so don't
* waste time trying to dial undiallable addresses.
* Returns a connection gater that disallows dialling private addresses or
* insecure websockets by default.
*
* Browsers are severely limited in their resource usage so don't waste time
* trying to dial undiallable addresses, and they also print verbose error
* messages when making connections over insecure transports which causes
* confusion.
*/
export function connectionGater (gater: ConnectionGater = {}): ConnectionGater {
return {
denyDialPeer: async () => false,
denyDialMultiaddr: async (multiaddr: Multiaddr) => {
// do not connect to insecure websockets by default
if (WebSockets.matches(multiaddr)) {
return false
}

Check warning on line 25 in packages/libp2p/src/config/connection-gater.browser.ts

View check run for this annotation

Codecov / codecov/patch

packages/libp2p/src/config/connection-gater.browser.ts#L24-L25

Added lines #L24 - L25 were not covered by tests

const tuples = multiaddr.stringTuples()

if (tuples[0][0] === 4 || tuples[0][0] === 41) {
// do not connect to private addresses by default
if (tuples[0][0] === CODEC_IP4 || tuples[0][0] === CODEC_IP6) {
return Boolean(isPrivateIp(`${tuples[0][1]}`))
}

Expand Down

0 comments on commit 78f76a1

Please sign in to comment.