Skip to content

Commit

Permalink
fix: Add fallback stun
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Apr 26, 2024
1 parent e29ce75 commit 064521e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 48 deletions.
56 changes: 32 additions & 24 deletions xmcl-runtime/iceServers/pluginIceServers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,44 @@ export const pluginIceServers: LauncherAppPlugin = async (app) => {
: undefined,
})
if (response.statusCode === 200) {
const credential: {
password: string
username: string
uris: string[]
stuns: string[]
} = await response.body.json() as any
iceServers = [
...credential.uris
.filter(u => u.startsWith('turn:'))
.map(u => u.substring('turn:'.length))
.map(u => {
const [hostname, port] = u.split(':')
try {
const credential: {
password: string
username: string
uris: string[]
stuns: string[]
} = await response.body.json() as any
iceServers = [
...credential.uris
.filter(u => u.startsWith('turn:'))
.map(u => u.substring('turn:'.length))
.map(u => {
const [hostname, port] = u.split(':')
return {
username: credential.username,
password: credential.password,
hostname,
port: port ? Number.parseInt(port) : 3478,
relayType: 'TurnUdp' as any,
}
}),
...credential.stuns.map((s) => {
const [hostname, port] = s.split(':')
return {
username: credential.username,
password: credential.password,
hostname,
port: port ? Number.parseInt(port) : 3478,
relayType: 'TurnUdp' as any,
}
}),
...credential.stuns.map((s) => {
const [hostname, port] = s.split(':')
return {
hostname,
port: port ? Number.parseInt(port) : 3478,
}
}),
]
await writeJson(cachePath, iceServers)
]
} catch (e) {
logger.error(e as any)
iceServers = [{
hostname: 'stun.l.google.com',
port: 19302,
}]
}

await writeJson(cachePath, iceServers)
logger.log(`Updated the rtc credential by xbox ${username}.`)
} else {
logger.error(new Error(`Fail to fetch the rtc credential by xbox ${username}. Status ${response.statusCode}.`))
Expand Down
2 changes: 1 addition & 1 deletion xmcl-runtime/nat/NatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class NatService extends StatefulService<NatState> implements INatService

// randomly pick one
const stun = servers[Math.floor(Math.random() * servers.length)]
const info = await getNatInfoUDP({ stun })
const info = await getNatInfoUDP({ stun: { ip: stun.hostname, port: stun.port } })
if (info.type !== 'Blocked') {
this.state.natInfoSet(info.externalIp, info.externalPort)
}
Expand Down
23 changes: 0 additions & 23 deletions xmcl-runtime/peer/stun.ts

This file was deleted.

0 comments on commit 064521e

Please sign in to comment.