From 4d16ffcc05be1566b0cb1e3280c8df501e247821 Mon Sep 17 00:00:00 2001 From: Klabukov Erik Date: Sun, 14 Jun 2026 19:30:26 +0300 Subject: [PATCH] Fix P2P connection: initialize ICE servers and add fallback public STUN servers - Always initialize iceServers array even if turnSettings are missing - Add array of reliable public STUN servers as fallback: - Google STUN servers (stun1-4.l.google.com) - Mozilla STUN server - stunprotocol.org server - If no TURN config exists, use only public STUN servers - If TURN config exists, use public STUN servers first, then TURN servers - Add console logging for debugging ICE server configuration - This fixes P2P connection establishment without requiring 'force turn' workaround --- .../services/PeerMeetingRtcMulticonnection.js | 71 +++++++++++++++---- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/src/PeerMeeting.Host/ClientApp/src/services/PeerMeetingRtcMulticonnection.js b/src/PeerMeeting.Host/ClientApp/src/services/PeerMeetingRtcMulticonnection.js index 08d6343..a953e7b 100644 --- a/src/PeerMeeting.Host/ClientApp/src/services/PeerMeetingRtcMulticonnection.js +++ b/src/PeerMeeting.Host/ClientApp/src/services/PeerMeetingRtcMulticonnection.js @@ -12,6 +12,17 @@ export default class PeerMeetingRtcMulticonnection { participantsCardFixTimer = null triedTurnOnly = false + // Public STUN servers + defaultStunServers = [ + '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:stun.services.mozilla.com:3478', + 'stun:stun.stunprotocol.org:3478', + ] + constructor(store, router, participants) { this.store = store this.router = router @@ -109,28 +120,64 @@ export default class PeerMeetingRtcMulticonnection { } configureIceServers() { - if (!this.store.state.application.turnSettings) + // Инициализируем iceServers если их нет + if (!this.connection.iceServers) { + this.connection.iceServers = [] + } + + // Если нет turnSettings, добавляем только публичные STUN серверы + if (!this.store.state.application.turnSettings) { + this.connection.iceServers = [ + { + urls: this.defaultStunServers, + }, + ] return + } + + // Если TURN режим включен, очищаем iceServers и используем только TURN if (this.store.state.application.turnOnly || this.triedTurnOnly) { this.connection.iceServers = [] this.connection.candidates.host = false this.connection.iceTransportPolicy = 'relay' } - const turnUri = this.store.state.application.turnSettings.uris[0] - const stunUri = turnUri.replace(/^turn:/, 'stun:').replace(/\?transport=tcp$/, '').replace(/\?transport=udp$/, '') + + // Добавляем публичные STUN серверы в начало списка this.connection.iceServers.unshift({ - urls: stunUri, - }) - this.connection.iceServers.push({ - urls: this.store.state.application.turnSettings.uris[0], - credential: this.store.state.application.turnSettings.password, - username: this.store.state.application.turnSettings.username, + urls: this.defaultStunServers, }) + + // Извлекаем STUN из TURN URI + const turnUri = this.store.state.application.turnSettings.uris[0] + const stunUri = turnUri + .replace(/^turn:/, 'stun:') + .replace(/\?transport=tcp$/, '') + .replace(/\?transport=udp$/, '') + + // Добавляем STUN сервер из конфига this.connection.iceServers.push({ - urls: this.store.state.application.turnSettings.uris[1], - credential: this.store.state.application.turnSettings.password, - username: this.store.state.application.turnSettings.username, + urls: stunUri, }) + + // Добавляем первый TURN сервер + if (this.store.state.application.turnSettings.uris.length > 0) { + this.connection.iceServers.push({ + urls: this.store.state.application.turnSettings.uris[0], + credential: this.store.state.application.turnSettings.password, + username: this.store.state.application.turnSettings.username, + }) + } + + // Добавляем второй TURN сервер (если доступен) + if (this.store.state.application.turnSettings.uris.length > 1) { + this.connection.iceServers.push({ + urls: this.store.state.application.turnSettings.uris[1], + credential: this.store.state.application.turnSettings.password, + username: this.store.state.application.turnSettings.username, + }) + } + + console.log('ICE Servers configured:', this.connection.iceServers) } configureKicked() {