Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

feat: custom address filter #116

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,32 @@ const ws = new WS(properties)
| upgrader | [`Upgrader`](https://github.com/libp2p/interface-transport#upgrader) | connection upgrader object with `upgradeOutbound` and `upgradeInbound` | **REQUIRED** |
| filter | `(multiaddrs: Array<Multiaddr>) => Array<Multiaddr>` | override transport addresses filter | **Browser:** DNS+WSS multiaddrs / **Node.js:** DNS+{WS, WSS} multiaddrs |

### Base Example
## Libp2p Usage Example

```js
const Libp2p = require('libp2p')
const Websockets = require('libp2p-websockets')
const filters = require('libp2p-websockets/src/filters')

const transportKey = Websockets.prototype[Symbol.toStringTag]
const node = await Libp2p.create({
modules: {
transport: [WebRTCStar],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
transport: [WebRTCStar],
transport: [Websockets],

// ... other mandatory modules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add NOISE and mplex in here. There aren't other options right now, so having a config people can just use would be helpful imo.

},
config: {
transport: {
[transportKey]: { // Transport properties -- Libp2p upgrader is automatically added
filter: filters.dnsWsOrWss
}
}
}
})
```

For more information see [libp2p/js-libp2p/doc/CONFIGURATION.md#customizing-transports](https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md#customizing-transports).

## Base Example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just delete this whole section, nobody should use it like this.


```js
const WS = require('libp2p-websockets')
Expand Down Expand Up @@ -87,7 +112,7 @@ console.log(`Value: ${values.toString()}`)
await listener.close()
```

### Custom filters Example
## Custom filters Example

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

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"abort-controller": "^3.0.0",
"aegir": "^28.1.0",
"bl": "^4.0.0",
"is-loopback-addr": "^1.0.1",
"it-goodbye": "^2.0.1",
"it-pipe": "^1.0.1",
"libp2p-interfaces": "^0.7.1",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class WebSockets {
return filters.dnsWss(multiaddrs)
}

return filters.dnsWsOrWss(multiaddrs)
return filters.all(multiaddrs)
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe('libp2p-websockets', () => {
expect(results).to.eql([message])
})

it('should filter out no DNS 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.filter([ma1, ma2, ma3, ma4])
expect(valid.length).to.equal(0)
})

describe('stress', () => {
it('one big write', async () => {
const rawMessage = new Uint8Array(1000000).fill('a')
Expand Down
41 changes: 31 additions & 10 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fs = require('fs')
const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const goodbye = require('it-goodbye')
const isLoopbackAddr = require('is-loopback-addr')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const BufferList = require('bl/BufferList')
Expand Down Expand Up @@ -251,6 +252,36 @@ describe('dial', () => {
})
})

describe('ip4 no loopback', () => {
let ws
let listener
const ma = multiaddr('/ip4/0.0.0.0/tcp/0/ws')

beforeEach(() => {
ws = new WS({ upgrader: mockUpgrader })
listener = ws.createListener(conn => pipe(conn, conn))
return listener.listen(ma)
})

afterEach(() => listener.close())

it('dial', async () => {
const addrs = listener.getAddrs().filter((ma) => {
const { address } = ma.nodeAddress()

return !isLoopbackAddr(address)
})

// Dial first no loopback address
const conn = await ws.dial(addrs[0])
const s = goodbye({ source: ['hey'], sink: collect })

const result = await pipe(s, conn, s)

expect(result).to.be.eql([uint8ArrayFromString('hey')])
})
})

describe('ip4 with wss', () => {
let ws
let listener
Expand Down Expand Up @@ -343,16 +374,6 @@ describe('filter addrs', () => {
expect(valid.length).to.equal(0)
})

it('should filter out no DNS 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.filter([ma1, ma2, ma3, ma4])
expect(valid.length).to.equal(0)
})

it('should filter correct dns address', function () {
const ma1 = multiaddr('/dnsaddr/ipfs.io/ws')
const ma2 = multiaddr('/dnsaddr/ipfs.io/tcp/80/ws')
Expand Down