Skip to content

Commit

Permalink
fix: add default STUN servers to WebRTC transport (#2606)
Browse files Browse the repository at this point in the history
To make it easier to connect to remote nodes, add default STUN
servers to the WebRTC transport.
  • Loading branch information
achingbrain committed Jul 2, 2024
1 parent 21cf7bc commit af85a7c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
16 changes: 16 additions & 0 deletions packages/transport-webrtc/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* STUN servers help clients discover their own public IPs
*
* @see https://gist.github.com/mondain/b0ec1cf5f60ae726202e
*/
export const DEFAULT_ICE_SERVERS = [
'stun:stun.l.google.com:19302',
'stun:stun1.l.google.com:19302',
'stun:stun2.l.google.com:19302',
'stun:stun3.l.google.com:19302',
'stun:stun4.l.google.com:19302',
'stun:global.stun.twilio.com:3478',
'stun:stun.cloudflare.com:3478',
'stun:stun.services.mozilla.com:3478',
'stun:stun.1und1.de:3478'
]
5 changes: 3 additions & 2 deletions packages/transport-webrtc/src/private-to-private/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { WebRTC } from '@multiformats/multiaddr-matcher'
import { codes } from '../error.js'
import { WebRTCMultiaddrConnection } from '../maconn.js'
import { DataChannelMuxerFactory } from '../muxer.js'
import { getRtcConfiguration } from '../util.js'
import { RTCPeerConnection } from '../webrtc/index.js'
import { initiateConnection } from './initiate-connection.js'
import { WebRTCPeerListener } from './listener.js'
Expand Down Expand Up @@ -134,7 +135,7 @@ export class WebRTCTransport implements Transport, Startable {
this.log.trace('dialing address: %a', ma)

const { remoteAddress, peerConnection, muxerFactory } = await initiateConnection({
rtcConfiguration: typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration,
rtcConfiguration: await getRtcConfiguration(this.init.rtcConfiguration),
dataChannel: this.init.dataChannel,
multiaddr: ma,
dataChannelOptions: this.init.dataChannel,
Expand Down Expand Up @@ -166,7 +167,7 @@ export class WebRTCTransport implements Transport, Startable {

async _onProtocol ({ connection, stream }: IncomingStreamData): Promise<void> {
const signal = AbortSignal.timeout(this.init.inboundConnectionTimeout ?? INBOUND_CONNECTION_TIMEOUT)
const peerConnection = new RTCPeerConnection(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration)
const peerConnection = new RTCPeerConnection(await getRtcConfiguration(this.init.rtcConfiguration))
const muxerFactory = new DataChannelMuxerFactory(this.components, {
peerConnection,
dataChannelOptions: this.init.dataChannel
Expand Down
4 changes: 2 additions & 2 deletions packages/transport-webrtc/src/private-to-public/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgumen
import { WebRTCMultiaddrConnection } from '../maconn.js'
import { DataChannelMuxerFactory } from '../muxer.js'
import { createStream } from '../stream.js'
import { isFirefox } from '../util.js'
import { getRtcConfiguration, isFirefox } from '../util.js'
import { RTCPeerConnection } from '../webrtc/index.js'
import * as sdp from './sdp.js'
import { genUfrag } from './util.js'
Expand Down Expand Up @@ -139,7 +139,7 @@ export class WebRTCDirectTransport implements Transport {
} as any)

const peerConnection = new RTCPeerConnection({
...(typeof this.init.rtcConfiguration === 'function' ? await this.init.rtcConfiguration() : this.init.rtcConfiguration ?? {}),
...(await getRtcConfiguration(this.init.rtcConfiguration)),
certificates: [certificate]
})

Expand Down
17 changes: 17 additions & 0 deletions packages/transport-webrtc/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { detect } from 'detect-browser'
import pDefer from 'p-defer'
import pTimeout from 'p-timeout'
import { DEFAULT_ICE_SERVERS } from './constants.js'
import type { LoggerOptions } from '@libp2p/interface'

const browser = detect()
Expand Down Expand Up @@ -64,3 +65,19 @@ export interface AbortPromiseOptions {
signal?: AbortSignal
message?: string
}

export async function getRtcConfiguration (config?: RTCConfiguration | (() => RTCConfiguration | Promise<RTCConfiguration>)): Promise<RTCConfiguration> {
config = config ?? {}

if (typeof config === 'function') {
config = await config()
}

config.iceServers = config.iceServers ?? DEFAULT_ICE_SERVERS.map(url => ({
urls: [
url
]
}))

return config
}

0 comments on commit af85a7c

Please sign in to comment.