ICE servers config

Signed-off-by: ale <ale@manalejandro.com>
Este commit está contenido en:
ale
2025-06-15 13:07:16 +02:00
padre 69205d212f
commit 7c3921cdd3
Se han modificado 8 ficheros con 219 adiciones y 20 borrados

Ver fichero

@@ -24,7 +24,9 @@ export class RingNode extends EventEmitter {
}
};
this.webrtc = new WebRTCManager(this.id);
this.webrtc = new WebRTCManager(this.id, {
iceServers: options.iceServers
});
this.discoveryServer = null;
this.knownNodes = new Map();
this.messageHistory = new Set();

Ver fichero

@@ -4,11 +4,26 @@ import { v4 as uuidv4 } from 'uuid';
import wrtc from '@koush/wrtc';
export class WebRTCManager extends EventEmitter {
constructor(nodeId) {
constructor(nodeId, options = {}) {
super();
this.nodeId = nodeId;
this.connections = new Map();
this.pendingConnections = new Map();
// Configure ICE servers with defaults
this.iceServers = options.iceServers || [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:stun1.l.google.com:19302' }
];
// Validate ICE servers configuration
try {
WebRTCManager.validateIceServers(this.iceServers);
console.log(`🌐 Using ${this.iceServers.length} ICE server(s) for WebRTC connections`);
} catch (error) {
console.error('Invalid ICE servers configuration:', error.message);
throw error;
}
}
async createConnection(peerId, isInitiator = false) {
@@ -23,10 +38,7 @@ export class WebRTCManager extends EventEmitter {
trickle: false,
wrtc: wrtc,
config: {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:stun1.l.google.com:19302' }
]
iceServers: this.iceServers
}
});
@@ -159,4 +171,32 @@ export class WebRTCManager extends EventEmitter {
}
this.connections.clear();
}
static validateIceServers(iceServers) {
if (!Array.isArray(iceServers)) {
throw new Error('ICE servers must be an array');
}
for (const server of iceServers) {
if (!server.urls) {
throw new Error('Each ICE server must have a "urls" property');
}
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
for (const url of urls) {
if (!url.startsWith('stun:') && !url.startsWith('turn:') && !url.startsWith('turns:')) {
throw new Error(`Invalid ICE server URL: ${url}. Must start with stun:, turn:, or turns:`);
}
}
// If it's a TURN server, it should have credentials
if (urls.some(url => url.startsWith('turn:') || url.startsWith('turns:'))) {
if (!server.username || !server.credential) {
console.warn(`Warning: TURN server ${server.urls} missing username/credential`);
}
}
}
return true;
}
}