From cf13689e7d6192c825a7b33e9f297e9f401f2e58 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 28 Aug 2024 17:24:47 +0800 Subject: [PATCH] Proxy: Support heartbeat with ports --- .run/private.run.xml | 1 - trunk/conf/full.conf | 8 +++++ trunk/src/app/srs_app_config.cpp | 27 +++++++++++++--- trunk/src/app/srs_app_config.hpp | 1 + trunk/src/app/srs_app_heartbeat.cpp | 50 +++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/.run/private.run.xml b/.run/private.run.xml index 458dabc613..5f46184622 100644 --- a/.run/private.run.xml +++ b/.run/private.run.xml @@ -1,7 +1,6 @@ - diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 0579788fac..a84309c226 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -907,6 +907,14 @@ heartbeat { # Overwrite by env SRS_HEARTBEAT_SUMMARIES # default: off summaries off; + # Whether report with listen ports. + # if on, request with the ports of SRS: + # { + # "rtmp": ["1935"], "http": ["8080"], "api": ["1985"], "srt": ["10080"], "rtc": ["8000"] + # } + # Overwrite by env SRS_HEARTBEAT_PORTS + # default: off + ports off; } # system statistics section. diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 0731f3cd2b..eeae92ef16 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2409,7 +2409,7 @@ srs_error_t SrsConfig::check_normal_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "enabled" && n != "interval" && n != "url" - && n != "device_id" && n != "summaries") { + && n != "device_id" && n != "summaries" && n != "ports") { return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal heartbeat.%s", n.c_str()); } } @@ -8794,17 +8794,36 @@ bool SrsConfig::get_heartbeat_summaries() SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.summaries"); // SRS_HEARTBEAT_SUMMARIES static bool DEFAULT = false; - + SrsConfDirective* conf = get_heartbeart(); if (!conf) { return DEFAULT; } - + conf = conf->get("summaries"); if (!conf || conf->arg0().empty()) { return DEFAULT; } - + + return SRS_CONF_PREFER_FALSE(conf->arg0()); +} + +bool SrsConfig::get_heartbeat_ports() +{ + SRS_OVERWRITE_BY_ENV_BOOL("srs.heartbeat.ports"); // SRS_HEARTBEAT_PORTS + + static bool DEFAULT = false; + + SrsConfDirective* conf = get_heartbeart(); + if (!conf) { + return DEFAULT; + } + + conf = conf->get("ports"); + if (!conf || conf->arg0().empty()) { + return DEFAULT; + } + return SRS_CONF_PREFER_FALSE(conf->arg0()); } diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 28aec179db..e7432d14cf 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -1119,6 +1119,7 @@ class SrsConfig virtual std::string get_heartbeat_device_id(); // Whether report with summaries of http api: /api/v1/summaries. virtual bool get_heartbeat_summaries(); + bool get_heartbeat_ports(); // stats section private: // Get the stats directive. diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index 819075a687..3878bcab06 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -18,6 +18,7 @@ using namespace std; #include #include #include +#include SrsHttpHeartbeat::SrsHttpHeartbeat() { @@ -67,6 +68,55 @@ srs_error_t SrsHttpHeartbeat::do_heartbeat() srs_api_dump_summaries(summaries); } + + if (_srs_config->get_heartbeat_ports()) { + // For RTMP listen endpoints. + if (true) { + SrsJsonArray* o = SrsJsonAny::array(); + obj->set("rtmp", o); + + vector endpoints = _srs_config->get_listens(); + for (int i = 0; i < (int) endpoints.size(); i++) { + o->append(SrsJsonAny::str(endpoints.at(i).c_str())); + } + } + + // For HTTP Stream listen endpoints. + if (_srs_config->get_http_stream_enabled()) { + SrsJsonArray* o = SrsJsonAny::array(); + obj->set("http", o); + + string endpoint = _srs_config->get_http_stream_listen(); + o->append(SrsJsonAny::str(endpoint.c_str())); + } + + // For HTTP API listen endpoints. + if (_srs_config->get_http_api_enabled()) { + SrsJsonArray* o = SrsJsonAny::array(); + obj->set("api", o); + + string endpoint = _srs_config->get_http_api_listen(); + o->append(SrsJsonAny::str(endpoint.c_str())); + } + + // For SRT listen endpoints. + if (_srs_config->get_srt_enabled()) { + SrsJsonArray* o = SrsJsonAny::array(); + obj->set("srt", o); + + uint16_t endpoint = _srs_config->get_srt_listen_port(); + o->append(SrsJsonAny::str(srs_int2str(endpoint).c_str())); + } + + // For WebRTC listen endpoints. + if (_srs_config->get_rtc_server_enabled()) { + SrsJsonArray* o = SrsJsonAny::array(); + obj->set("rtc", o); + + int endpoint = _srs_config->get_rtc_server_listen(); + o->append(SrsJsonAny::str(srs_int2str(endpoint).c_str())); + } + } SrsHttpClient http; if ((err = http.initialize(uri.get_schema(), uri.get_host(), uri.get_port())) != srs_success) {