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: add default STUN servers to WebRTC transport #2606

Merged
merged 2 commits into from
Jul 2, 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
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'
]
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
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 @@
signal?: AbortSignal
message?: string
}

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

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

Check warning on line 74 in packages/transport-webrtc/src/util.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webrtc/src/util.ts#L73-L74

Added lines #L73 - L74 were not covered by tests

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

return config
}
Loading