From bc24d1669bff482edf73836a8fa6ede1d01f02e2 Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Mon, 25 Mar 2024 21:23:49 +0100 Subject: [PATCH] [mirotalksfu] - add scripts --- app/src/Room.js | 2 + app/src/scripts/bindable.js | 158 ++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 app/src/scripts/bindable.js diff --git a/app/src/Room.js b/app/src/Room.js index 2d4cfba5..8b2c3499 100644 --- a/app/src/Room.js +++ b/app/src/Room.js @@ -275,6 +275,8 @@ module.exports = class Room { initialAvailableOutgoingBitrate, }; + log.debug('webRtcTransportOptions ----->', webRtcTransportOptions); + const transport = await this.router.createWebRtcTransport(webRtcTransportOptions); if (!transport) { diff --git a/app/src/scripts/bindable.js b/app/src/scripts/bindable.js new file mode 100644 index 00000000..a34bb02d --- /dev/null +++ b/app/src/scripts/bindable.js @@ -0,0 +1,158 @@ +'use strict'; + +const config = require('../config'); + +const net = require('net'); + +/* + Run: node bindable.js + + In networking, "bindable" refers to the ability to assign or allocate a specific IP address and port combination + to a network service or application. Binding an IP address and port allows the service or application to listen for + incoming network connections on that particular address and port. + + When we say an IP address and port are "bindable," it means that there are no conflicts or issues preventing the service + or application from using that specific combination. In other words, the IP address is available, and the port is not already + in use by another process or service on the same machine. + + If an IP address and port are bindable, it indicates that the network service or application can successfully bind to that + combination, allowing it to accept incoming connections and communicate over the network. On the other hand, if the IP address + and port are not bindable, it suggests that there may be conflicts or restrictions preventing the service or application + from using them, such as another process already listening on the same IP address and port. +*/ + +async function main() { + // Server listen + const serverListenIp = config.server.listen.ip; + const serverListenPort = config.server.listen.port; + + // WebRtcServerActive + const webRtcServerActive = config.mediasoup.webRtcServerActive; + + // WebRtcTransportOptions + const webRtcTransportIpInfo = config.mediasoup.webRtcTransport.listenInfos[0]; + const webRtcTransportIpAddress = + webRtcTransportIpInfo.ip !== '0.0.0.0' ? webRtcTransportIpInfo.ip : webRtcTransportIpInfo.announcedAddress; + + // WorkersOptions + const workers = config.mediasoup.numWorkers; + const rtcMinPort = config.mediasoup.worker.rtcMinPort; + const rtcMaxPort = config.mediasoup.worker.rtcMaxPort; + + console.log('=================================='); + console.log('checkServerIsBindable'); + console.log('=================================='); + + await checkServerIsBindable(serverListenIp, serverListenPort); + + console.log('=================================='); + console.log('checkWebRtcTransportPorts'); + console.log('=================================='); + + await checkWebRtcTransportPorts(webRtcTransportIpAddress, rtcMinPort, rtcMaxPort); + + if (webRtcServerActive) { + console.log('=================================='); + console.log('checkWebRtcServerPorts'); + console.log('=================================='); + + // WebRtcServerOptions + const webRtcServerIpInfo = config.mediasoup.webRtcServerOptions.listenInfos[0]; + const webRtcServerIpAddress = + webRtcServerIpInfo.ip !== '0.0.0.0' ? webRtcServerIpInfo.ip : webRtcServerIpInfo.announcedAddress; + const webRtcServerStartPort = webRtcServerIpInfo.port; + + await checkWebRtcServerPorts(webRtcServerIpAddress, webRtcServerStartPort, workers); + } +} + +/** + * Check if Server port is bindable + * @param {string} ipAddress + * @param {integer} port + */ +async function checkServerIsBindable(ipAddress, port){ + const bindable = await isBindable(ipAddress, port); + if (bindable) { + console.log(`${ipAddress}:${port} is bindable 🟢`); + } else { + console.log(`${ipAddress}:${port} is not bindable 🔴`); + } +} + +/** + * Check if WebRtcServer ports are bindable + * @param {string} ipAddress + * @param {integer} startPort + * @param {integer} workers + */ +async function checkWebRtcServerPorts(ipAddress, startPort, workers) { + let port = startPort; + for (let i = 0; i < workers; i++) { + try { + const bindable = await isBindable(ipAddress, port); + if (bindable) { + console.log(`${ipAddress}:${port} is bindable 🟢`); + } else { + console.log(`${ipAddress}:${port} is not bindable 🔴`); + } + port++; + } catch (err) { + console.error('Error occurred:', err); + } + } +} + +/** + * Check if WebRtcTransport Worker ports are bindable + * @param {string} ipAddress + * @param {integer} minPort + * @param {integer} maxPort + */ +async function checkWebRtcTransportPorts(ipAddress, minPort, maxPort) { + let port = minPort; + for (let i = 0; i <= maxPort - minPort; i++) { + try { + const bindable = await isBindable(ipAddress, port); + if (bindable) { + console.log(`${ipAddress}:${port} is bindable 🟢`); + } else { + console.log(`${ipAddress}:${port} is not bindable 🔴`); + } + port++; + } catch (err) { + console.error('Error occurred:', err); + } + } +} + +/** + * Check if ipAddress:port are bindable + * @param {string} ipAddress + * @param {integer} port + * @returns {Promise} A promise that resolves to true if the address is bindable, false otherwise. + */ +async function isBindable(ipAddress, port) { + return new Promise((resolve, reject) => { + const server = net.createServer(); + + server.once('error', (err) => { + if (err.code === 'EADDRINUSE') { + resolve(false); // Address is already in use + } else { + reject(err); // Other error occurred + } + }); + + server.once('listening', () => { + server.close(); + resolve(true); // Address is bindable + }); + + server.listen(port, ipAddress); + }); +} + +main().catch((err) => { + console.error('Error occurred in main function:', err.message); +});