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

fix: use transport manager getListeners to get listen addresses #166

Merged
merged 3 commits into from
May 15, 2023
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@
"dependencies": {
"@chainsafe/libp2p-noise": "^12.0.0",
"@libp2p/interface-connection": "^5.0.2",
"@libp2p/interface-libp2p": "^3.1.0",
"@libp2p/interface-metrics": "^4.0.8",
"@libp2p/interface-peer-id": "^2.0.2",
"@libp2p/interface-registrar": "^2.0.12",
"@libp2p/interface-stream-muxer": "^4.0.1",
"@libp2p/interface-transport": "^4.0.2",
"@libp2p/interface-transport": "^4.0.3",
"@libp2p/interfaces": "^3.3.2",
"@libp2p/logger": "^2.0.7",
"@libp2p/peer-id": "^2.0.3",
"@multiformats/mafmt": "^12.1.2",
"@multiformats/multiaddr": "^12.1.2",
"abortable-iterator": "^5.0.1",
"detect-browser": "^5.3.0",
Expand Down
32 changes: 10 additions & 22 deletions src/private-to-private/listener.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
import { EventEmitter } from '@libp2p/interfaces/events'
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
import { Circuit } from '@multiformats/mafmt'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { ListenerEvents, Listener } from '@libp2p/interface-transport'
import type { ListenerEvents, Listener, TransportManager } from '@libp2p/interface-transport'
import type { Multiaddr } from '@multiformats/multiaddr'

export interface ListenerOptions {
peerId: PeerId
events: EventEmitter<Libp2pEvents>
transportManager: TransportManager
}

export class WebRTCPeerListener extends EventEmitter<ListenerEvents> implements Listener {
private readonly peerId: PeerId
private listeners: Listener[] = []
private readonly transportManager: TransportManager

constructor (opts: ListenerOptions) {
super()

this.peerId = opts.peerId

opts.events.addEventListener('transport:listening', (event) => {
const listener = event.detail

if (listener === this || this.listeners.includes(listener)) {
return
}

this.listeners.push(listener)
})

opts.events.addEventListener('transport:close', (event) => {
const listener = event.detail

this.listeners = this.listeners.filter(l => l !== listener)
})
this.transportManager = opts.transportManager
}

async listen (ma: Multiaddr): Promise<void> {
async listen (): Promise<void> {
this.safeDispatchEvent('listening', {})
}

getAddrs (): Multiaddr[] {
return this.listeners
return this.transportManager
.getListeners()
.filter(l => l !== this)
.map(l => l.getAddrs()
.filter(ma => Circuit.matches(ma))
.map(ma => {
return ma.encapsulate(`/webrtc/p2p/${this.peerId}`)
})
Expand Down
3 changes: 0 additions & 3 deletions src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { WebRTCMultiaddrConnection } from '../maconn.js'
import { initiateConnection, handleIncomingStream } from './handler.js'
import { WebRTCPeerListener } from './listener.js'
import type { Connection } from '@libp2p/interface-connection'
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
import type { PeerId } from '@libp2p/interface-peer-id'
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
import type { EventEmitter } from '@libp2p/interfaces/events'
import type { Startable } from '@libp2p/interfaces/startable'

const log = logger('libp2p:webrtc:peer')
Expand All @@ -30,7 +28,6 @@ export interface WebRTCTransportComponents {
registrar: Registrar
upgrader: Upgrader
transportManager: TransportManager
events: EventEmitter<Libp2pEvents>
}

export class WebRTCTransport implements Transport, Startable {
Expand Down
40 changes: 40 additions & 0 deletions test/listener.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import { stubInterface } from 'sinon-ts'
import { WebRTCPeerListener } from '../src/private-to-private/listener'
import type { Listener, TransportManager } from '@libp2p/interface-transport'

describe('webrtc private-to-private listener', () => {
it('should only return relay addresses as webrtc listen addresses', async () => {
const relayedAddress = '/ip4/127.0.0.1/tcp/4034/ws/p2p-circuit'
const otherListenAddress = '/ip4/127.0.0.1/tcp/4001'
const peerId = await createEd25519PeerId()
const transportManager = stubInterface<TransportManager>()

const listener = new WebRTCPeerListener({
peerId,
transportManager
})

const otherListener = stubInterface<Listener>({
getAddrs: [multiaddr(otherListenAddress)]
})

const relayListener = stubInterface<Listener>({
getAddrs: [multiaddr(relayedAddress)]
})

transportManager.getListeners.returns([
listener,
otherListener,
relayListener
])

const addresses = listener.getAddrs()

expect(addresses.map(ma => ma.toString())).to.deep.equal([
`${relayedAddress}/webrtc/p2p/${peerId}`
])
})
})
4 changes: 1 addition & 3 deletions test/peer.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { mockConnection, mockMultiaddrConnection, mockRegistrar, mockStream, mockUpgrader } from '@libp2p/interface-mocks'
import { EventEmitter } from '@libp2p/interfaces/events'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
Expand Down Expand Up @@ -99,8 +98,7 @@ describe('webrtc filter', () => {
transportManager: Sinon.stub() as any,
peerId: Sinon.stub() as any,
registrar: mockRegistrar(),
upgrader: mockUpgrader({}),
events: new EventEmitter()
upgrader: mockUpgrader({})
}, {})

const valid = [
Expand Down