Skip to content

Commit

Permalink
fix: Only filter by wss not dns (libp2p#218)
Browse files Browse the repository at this point in the history
Browsers can dial IP addresses just fine. There's no need for this to be
only DNS addresses.
  • Loading branch information
MarcoPolo committed Mar 1, 2023
1 parent e54590f commit 434d44c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export function all (multiaddrs: Multiaddr[]) {
})
}

export function wss (multiaddrs: Multiaddr[]) {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
return false
}

const testMa = ma.decapsulateCode(CODE_P2P)

return mafmt.WebSocketsSecure.matches(testMa)
})
}

export function dnsWss (multiaddrs: Multiaddr[]) {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class WebSockets implements Transport {

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

return filters.all(multiaddrs)
Expand Down
5 changes: 3 additions & 2 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('libp2p-websockets', () => {
expect(res[0].subarray()).to.equalBytes(data)
})

it('should filter out no DNS websocket addresses', function () {
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')
Expand All @@ -49,7 +49,8 @@ describe('libp2p-websockets', () => {
const valid = ws.filter([ma1, ma2, ma3, ma4])

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

0 comments on commit 434d44c

Please sign in to comment.