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: remove browser dial filter #2838

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
36 changes: 1 addition & 35 deletions packages/transport-websockets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,10 @@ const node = await createLibp2p({
})
await node.start()

const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
const ma = multiaddr('/dns4/example.com/tcp/9090/tls/ws')
await node.dial(ma)
```

## Filters

When run in a browser by default this module will only connect to secure web socket addresses.

To change this you should pass a filter to the factory function.

You can create your own address filters for this transports, or rely in the filters [provided](./src/filters.js).

The available filters are:

- `filters.all`
- Returns all TCP and DNS based addresses, both with `ws` or `wss`.
- `filters.dnsWss`
- Returns all DNS based addresses with `wss`.
- `filters.dnsWsOrWss`
- Returns all DNS based addresses, both with `ws` or `wss`.

## Example - Allow dialing insecure WebSockets

```TypeScript
import { createLibp2p } from 'libp2p'
import { webSockets } from '@libp2p/websockets'
import * as filters from '@libp2p/websockets/filters'

const node = await createLibp2p({
transports: [
webSockets({
// connect to all sockets, even insecure ones
filter: filters.all
})
]
})
```

# Install

```console
Expand Down
1 change: 0 additions & 1 deletion packages/transport-websockets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"p-defer": "^4.0.1",
"progress-events": "^1.0.0",
"race-signal": "^1.0.2",
"wherearewe": "^2.0.1",
"ws": "^8.17.0"
},
"devDependencies": {
Expand Down
45 changes: 4 additions & 41 deletions packages/transport-websockets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,9 @@
* })
* await node.start()
*
* const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
* const ma = multiaddr('/dns4/example.com/tcp/9090/tls/ws')
* await node.dial(ma)
* ```
*
* ## Filters
*
* When run in a browser by default this module will only connect to secure web socket addresses.
*
* To change this you should pass a filter to the factory function.
*
* You can create your own address filters for this transports, or rely in the filters [provided](./src/filters.js).
*
* The available filters are:
*
* - `filters.all`
* - Returns all TCP and DNS based addresses, both with `ws` or `wss`.
* - `filters.dnsWss`
* - Returns all DNS based addresses with `wss`.
* - `filters.dnsWsOrWss`
* - Returns all DNS based addresses, both with `ws` or `wss`.
*
* @example Allow dialing insecure WebSockets
*
* ```TypeScript
* import { createLibp2p } from 'libp2p'
* import { webSockets } from '@libp2p/websockets'
* import * as filters from '@libp2p/websockets/filters'
*
* const node = await createLibp2p({
* transports: [
* webSockets({
* // connect to all sockets, even insecure ones
* filter: filters.all
* })
* ]
* })
* ```
*/

import { transportSymbol, serviceCapabilities, ConnectionFailedError } from '@libp2p/interface'
Expand All @@ -63,7 +29,6 @@ import { connect, type WebSocketOptions } from 'it-ws/client'
import pDefer from 'p-defer'
import { CustomProgressEvent } from 'progress-events'
import { raceSignal } from 'race-signal'
import { isBrowser, isWebWorker } from 'wherearewe'
import * as filters from './filters.js'
import { createListener } from './listener.js'
import { socketToMaConn } from './socket-to-conn.js'
Expand All @@ -75,6 +40,9 @@ import type { ProgressEvent } from 'progress-events'
import type { ClientOptions } from 'ws'

export interface WebSocketsInit extends AbortOptions, WebSocketOptions {
/**
* @deprecated Use a ConnectionGater instead
*/
filter?: MultiaddrFilter
websocket?: ClientOptions
server?: Server
Expand Down Expand Up @@ -206,11 +174,6 @@ class WebSockets implements Transport<WebSocketsDialEvents> {
return this.init?.filter(multiaddrs)
}

// Browser
if (isBrowser || isWebWorker) {
return filters.wss(multiaddrs)
}

return filters.all(multiaddrs)
}

Expand Down
27 changes: 0 additions & 27 deletions packages/transport-websockets/test/browser.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
/* eslint-env mocha */

import { defaultLogger } from '@libp2p/logger'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import { isBrowser, isWebWorker } from 'wherearewe'
import { webSockets } from '../src/index.js'
import type { Transport } from '@libp2p/interface'

describe('libp2p-websockets', () => {
let ws: Transport

beforeEach(async () => {
ws = webSockets()({
logger: defaultLogger()
})
})

it('should filter out no wss websocket addresses', function () {
const ma1 = multiaddr('/ip4/127.0.0.1/tcp/80/ws')
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/443/wss')
const ma3 = multiaddr('/ip6/::1/tcp/80/ws')
const ma4 = multiaddr('/ip6/::1/tcp/443/wss')

const valid = ws.dialFilter([ma1, ma2, ma3, ma4])

if (isBrowser || isWebWorker) {
expect(valid.length).to.equal(2)
expect(valid).to.deep.equal([ma2, ma4])
} else {
expect(valid.length).to.equal(4)
}
})

it('.createServer throws in browser', () => {
expect(webSockets()({
logger: defaultLogger()
Expand Down
Loading