From ff4f69ff523252a0ef0a2a6af2abd7ba20667e47 Mon Sep 17 00:00:00 2001 From: Chen Mingliang Date: Fri, 6 Dec 2024 16:05:23 +0800 Subject: [PATCH] Adds a check on whether the configuration is enabled --- bin/app.js | 8 ++++---- src/index.js | 9 ++++++--- src/protocol/amf.js | 1 - src/protocol/flv.js | 2 +- src/protocol/rtmp.js | 4 ++-- src/server/broadcast_server.js | 2 +- src/server/http_server.js | 27 ++++++++++++++++----------- src/server/rtmp_server.js | 21 ++++++++++++--------- src/session/flv_session.js | 4 ++-- src/session/rtmp_session.js | 8 ++++---- 10 files changed, 48 insertions(+), 38 deletions(-) diff --git a/bin/app.js b/bin/app.js index ceb60200..4783390a 100755 --- a/bin/app.js +++ b/bin/app.js @@ -5,19 +5,19 @@ const path = require("path"); const config = require("./config.json"); const NodeMediaServer = require(".."); -if (!fs.existsSync(config.rtmps.key)) { +if (config.rtmps?.key && !fs.existsSync(config.rtmps.key)) { config.rtmps.key = path.join(__dirname, config.rtmps.key); } -if (!fs.existsSync(config.rtmps.cert)) { +if (config.rtmps?.cert && !fs.existsSync(config.rtmps.cert)) { config.rtmps.cert = path.join(__dirname, config.rtmps.cert); } -if (!fs.existsSync(config.https.key)) { +if (config.https?.key && !fs.existsSync(config.https.key)) { config.https.key = path.join(__dirname, config.https.key); } -if (!fs.existsSync(config.https.cert)) { +if (config.https?.cert && !fs.existsSync(config.https.cert)) { config.https.cert = path.join(__dirname, config.https.cert); } diff --git a/src/index.js b/src/index.js index e84a8a0a..97d050bb 100644 --- a/src/index.js +++ b/src/index.js @@ -5,11 +5,11 @@ // Copyright (c) 2024 NodeMedia. All rights reserved. // -const NodeHttpServer = require("./server/http_server.js"); -const NodeRtmpServer = require("./server/rtmp_server.js"); const logger = require("./core/logger.js"); -const Context = require("./core/context.js"); const Package = require("../package.json"); +const Context = require("./core/context.js"); +const NodeHttpServer = require("./server/http_server.js"); +const NodeRtmpServer = require("./server/rtmp_server.js"); class NodeMediaServer { constructor(config) { @@ -18,14 +18,17 @@ class NodeMediaServer { logger.info(`Homepage: ${Package.homepage}`); logger.info(`License: ${Package.license}`); logger.info(`Author: ${Package.author}`); + this.ctx = new Context(config); this.httpServer = new NodeHttpServer(this.ctx); this.rtmpServer = new NodeRtmpServer(this.ctx); } run() { + this.httpServer.run(); this.rtmpServer.run(); + } } diff --git a/src/protocol/amf.js b/src/protocol/amf.js index 97501045..310e8557 100644 --- a/src/protocol/amf.js +++ b/src/protocol/amf.js @@ -5,7 +5,6 @@ const logger = require("../core/logger.js"); - const amf0dRules = { 0x00: amf0decNumber, 0x01: amf0decBool, diff --git a/src/protocol/flv.js b/src/protocol/flv.js index 08a8e2bc..14433090 100644 --- a/src/protocol/flv.js +++ b/src/protocol/flv.js @@ -5,9 +5,9 @@ // Copyright (c) 2023 Nodemedia. All rights reserved. // +const AMF = require("./amf.js"); const logger = require("../core/logger.js"); const AVPacket = require("../core/avpacket.js"); -const AMF = require("./amf.js"); const FLV_MEDIA_TYPE_AUDIO = 8; const FLV_MEDIA_TYPE_VIDEO = 9; diff --git a/src/protocol/rtmp.js b/src/protocol/rtmp.js index dd912ffb..03109a74 100644 --- a/src/protocol/rtmp.js +++ b/src/protocol/rtmp.js @@ -5,11 +5,11 @@ // Copyright (c) 2024 Nodemedia. All rights reserved. // +const AMF = require("./amf.js"); +const Flv = require("./flv.js"); const crypto = require("node:crypto"); const logger = require("../core/logger.js"); const AVPacket = require("../core/avpacket.js"); -const Flv = require("./flv.js"); -const AMF = require("./amf.js"); const N_CHUNK_STREAM = 8; const RTMP_VERSION = 3; diff --git a/src/server/broadcast_server.js b/src/server/broadcast_server.js index e6a2fd4c..944600d3 100644 --- a/src/server/broadcast_server.js +++ b/src/server/broadcast_server.js @@ -5,9 +5,9 @@ // Copyright (c) 2023 NodeMedia. All rights reserved. // -const AVPacket = require("../core/avpacket.js"); const Flv = require("../protocol/flv.js"); const Rtmp = require("../protocol/rtmp.js"); +const AVPacket = require("../core/avpacket.js"); const BaseSession = require("../session/base_session.js"); class BroadcastServer { diff --git a/src/server/http_server.js b/src/server/http_server.js index 8bfd0c09..a46be9a5 100644 --- a/src/server/http_server.js +++ b/src/server/http_server.js @@ -9,10 +9,10 @@ const fs = require("fs"); const http = require("http"); const http2 = require("http2"); const express = require("express"); -const http2Express = require("http2-express-bridge"); -const FlvSession = require("../session/flv_session.js"); const logger = require("../core/logger.js"); const Context = require("../core/context.js"); +const http2Express = require("http2-express-bridge"); +const FlvSession = require("../session/flv_session.js"); class NodeHttpServer { /** @@ -21,11 +21,6 @@ class NodeHttpServer { constructor(ctx) { this.ctx = ctx; const app = http2Express(express); - const opt = { - key: fs.readFileSync(ctx.config.https.key), - cert: fs.readFileSync(ctx.config.https.cert), - allowHTTP1: true - }; app.all("*", (req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); @@ -34,15 +29,25 @@ class NodeHttpServer { app.all("/:app/:name.flv", this.handleFlv); - this.server1 = http.createServer(app); - this.server2 = http2.createSecureServer(opt, app); + if (ctx.config.http?.port) { + this.httpServer = http.createServer(app); + } + if (ctx.config.https?.port) { + const opt = { + key: fs.readFileSync(ctx.config.https.key), + cert: fs.readFileSync(ctx.config.https.cert), + allowHTTP1: true + }; + this.httpsServer = http2.createSecureServer(opt, app); + } + } run = () => { - this.server1.listen(this.ctx.config.http.port, this.ctx.config.http.bind, () => { + this.httpServer?.listen(this.ctx.config.http.port, this.ctx.config.http.bind, () => { logger.info(`HTTP server listening on port ${this.ctx.config.http.bind}:${this.ctx.config.http.port}`); }); - this.server2.listen(this.ctx.config.https.port, this.ctx.config.https.bind, () => { + this.httpsServer?.listen(this.ctx.config.https.port, this.ctx.config.https.bind, () => { logger.info(`HTTPS server listening on port ${this.ctx.config.https.bind}:${this.ctx.config.https.port}`); }); }; diff --git a/src/server/rtmp_server.js b/src/server/rtmp_server.js index d61e27ad..6c8f0772 100644 --- a/src/server/rtmp_server.js +++ b/src/server/rtmp_server.js @@ -18,20 +18,23 @@ class NodeRtmpServer { */ constructor(ctx) { this.ctx = ctx; - const opt = { - key: fs.readFileSync(ctx.config.rtmps.key), - cert: fs.readFileSync(ctx.config.rtmps.cert), - }; - - this.tcpServer = net.createServer(this.handleRequest); - this.tlsServer = tls.createServer(opt, this.handleRequest); + if (ctx.config.rtmp?.port) { + this.tcpServer = net.createServer(this.handleRequest); + } + if (ctx.config.rtmps?.port) { + const opt = { + key: fs.readFileSync(ctx.config.rtmps.key), + cert: fs.readFileSync(ctx.config.rtmps.cert), + }; + this.tlsServer = tls.createServer(opt, this.handleRequest); + } } run = () => { - this.tcpServer.listen(this.ctx.config.rtmp.port, this.ctx.config.rtmp.bind, () => { + this.tcpServer?.listen(this.ctx.config.rtmp.port, this.ctx.config.rtmp.bind, () => { logger.log(`Rtmp Server listening on port ${this.ctx.config.rtmp.bind}:${this.ctx.config.rtmp.port}`); }); - this.tlsServer.listen(this.ctx.config.rtmps.port, this.ctx.config.rtmps.bind, () => { + this.tlsServer?.listen(this.ctx.config.rtmps.port, this.ctx.config.rtmps.bind, () => { logger.log(`Rtmps Server listening on port ${this.ctx.config.rtmps.bind}:${this.ctx.config.rtmps.port}`); }); }; diff --git a/src/session/flv_session.js b/src/session/flv_session.js index c0b22ec1..9be8ca1f 100644 --- a/src/session/flv_session.js +++ b/src/session/flv_session.js @@ -6,12 +6,12 @@ // const express = require( "express"); -const BaseSession = require( "./base_session.js"); -const BroadcastServer = require( "../server/broadcast_server.js"); const Flv = require( "../protocol/flv.js"); const logger = require( "../core/logger.js"); const Context = require( "../core/context.js"); const AVPacket = require( "../core/avpacket.js"); +const BaseSession = require( "./base_session.js"); +const BroadcastServer = require( "../server/broadcast_server.js"); /** * @class diff --git a/src/session/rtmp_session.js b/src/session/rtmp_session.js index 8a392bd9..c5fc0342 100644 --- a/src/session/rtmp_session.js +++ b/src/session/rtmp_session.js @@ -5,13 +5,13 @@ // Copyright (c) 2024 Nodemedia. All rights reserved. // -const net = require( "node:net"); -const Context = require( "../core/context.js"); -const BaseSession = require( "./base_session.js"); -const BroadcastServer = require( "../server/broadcast_server.js"); +const net = require( "net"); const Rtmp = require( "../protocol/rtmp.js"); const logger = require( "../core/logger.js"); +const Context = require( "../core/context.js"); const AVPacket = require( "../core/avpacket.js"); +const BaseSession = require( "./base_session.js"); +const BroadcastServer = require( "../server/broadcast_server.js"); /** * @class