From 47518dd9e3821fb3ceffdf595d97a12a9d00593b Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Thu, 21 Mar 2024 19:57:11 +0100 Subject: [PATCH] [mirotalksfu] - refactoring config --- app/src/Server.js | 7 ++++++- app/src/config.template.js | 42 +++++++++++++++++--------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/src/Server.js b/app/src/Server.js index 2aab9bd8..01ab7a7d 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -202,6 +202,11 @@ const presenters = {}; // collect presenters grp by roomId const webRtcServerActive = config.mediasoup.webRtcServerActive; +// ip (server local IPv4) +const IPv4 = webRtcServerActive + ? config.mediasoup.webRtcServerOptions.listenInfos[0].ip + : config.mediasoup.webRtcTransport.listenInfos[0].ip; + // announcedAddress (server public IPv4) let announcedAddress = webRtcServerActive ? config.mediasoup.webRtcServerOptions.listenInfos[0].announcedAddress @@ -212,7 +217,7 @@ const workers = []; let nextMediasoupWorkerIdx = 0; // Autodetect announcedAddress (https://www.ipify.org) -if (!announcedAddress) { +if (!announcedAddress && IPv4 === '0.0.0.0') { http.get( { host: 'api.ipify.org', diff --git a/app/src/config.template.js b/app/src/config.template.js index 64798587..5d8d8a14 100644 --- a/app/src/config.template.js +++ b/app/src/config.template.js @@ -1,28 +1,24 @@ 'use strict'; const os = require('os'); -const ifaces = os.networkInterfaces(); - -const getLocalIp = () => { - let localIp = '127.0.0.1'; - let checkIp = true; - Object.keys(ifaces).forEach((ifname) => { - for (const iface of ifaces[ifname]) { - // Ignore IPv6 and 127.0.0.1 - if (iface.family !== 'IPv4' || iface.internal !== false || checkIp === false) { - continue; - } - // Set the local ip to the first IPv4 address found and exit the loop - localIp = iface.address; - checkIp = false; - return; - } - }); - return localIp; -}; // https://api.ipify.org +function getIPv4() { + const ifaces = os.networkInterfaces(); + for (const interfaceName in ifaces) { + const iface = ifaces[interfaceName]; + for (const { address, family, internal } of iface) { + if (family === 'IPv4' && !internal) { + return address; + } + } + } + return '0.0.0.0'; // Default to 0.0.0.0 if no external IPv4 address found +} + +const IPv4 = getIPv4(); + module.exports = { server: { listen: { @@ -402,15 +398,15 @@ module.exports = { webRtcServerActive: false, webRtcServerOptions: { listenInfos: [ - { protocol: 'udp', ip: '0.0.0.0', announcedAddress: getLocalIp(), port: 44444 }, - { protocol: 'tcp', ip: '0.0.0.0', announcedAddress: getLocalIp(), port: 44444 }, + { protocol: 'udp', ip: '0.0.0.0', announcedAddress: IPv4, port: 44444 }, + { protocol: 'tcp', ip: '0.0.0.0', announcedAddress: IPv4, port: 44444 }, ], }, // WebRtcTransportOptions webRtcTransport: { listenInfos: [ - { protocol: 'udp', ip: '0.0.0.0', announcedAddress: getLocalIp() }, - { protocol: 'tcp', ip: '0.0.0.0', announcedAddress: getLocalIp() }, + { protocol: 'udp', ip: '0.0.0.0', announcedAddress: IPv4 }, + { protocol: 'tcp', ip: '0.0.0.0', announcedAddress: IPv4 }, ], initialAvailableOutgoingBitrate: 1000000, minimumAvailableOutgoingBitrate: 600000,