diff --git a/README.md b/README.md index f897a24bf..c063fad48 100755 --- a/README.md +++ b/README.md @@ -339,6 +339,7 @@ Remark: ## History +* v2.0, 2021-06-25, For [#2424](https://github.com/ossrs/srs/issues/2424), query the latest available version. 2.0.273 * v2.0, 2020-01-25, [2.0 release8(2.0.272)][r2.0r8] released. 87292 lines. * v2.0, 2020-01-08, Merge [#1554][bug #1554], support logrotate copytruncate. 2.0.272 * v2.0, 2020-01-05, Merge [#1551][bug #1551], fix memory leak in RTSP stack. 2.0.270 diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index a8f12aa10..4c088705e 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -63,6 +63,11 @@ work_dir ./; # default: off asprocess off; +# Query the latest available version of SRS, write a log to notice user to upgrade. +# @see https://github.com/ossrs/srs/issues/2424 +# Default: on +query_latest_version on; + ############################################################################################# # heartbeat/stats sections ############################################################################################# diff --git a/trunk/configure b/trunk/configure index eecba9cab..68fab4ba6 100755 --- a/trunk/configure +++ b/trunk/configure @@ -179,7 +179,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_http_static" "srs_app_recv_thread" "srs_app_security" "srs_app_statistic" "srs_app_hds" "srs_app_mpegts_udp" "srs_app_rtsp" "srs_app_listener" "srs_app_async_call" - "srs_app_caster_flv") + "srs_app_caster_flv" "srs_app_latest_version") DEFINES="" # add each modules for app for SRS_MODULE in ${SRS_MODULES[*]}; do diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 9ee6c668c..1361a27fc 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2212,6 +2212,18 @@ bool SrsConfig::get_asprocess() return SRS_CONF_PERFER_FALSE(conf->arg0()); } +bool SrsConfig::whether_query_latest_version() +{ + static bool DEFAULT = true; + + SrsConfDirective* conf = root->get("query_latest_version"); + if (!conf) { + return DEFAULT; + } + + return SRS_CONF_PERFER_TRUE(conf->arg0()); +} + vector SrsConfig::get_stream_casters() { srs_assert(root); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 89f3922d6..dff6c91e2 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -386,6 +386,8 @@ public: virtual std::string get_work_dir(); // whether use asprocess mode. virtual bool get_asprocess(); + // Whether query the latest available version of SRS. + virtual bool whether_query_latest_version(); // stream_caster section public: /** diff --git a/trunk/src/app/srs_app_latest_version.cpp b/trunk/src/app/srs_app_latest_version.cpp new file mode 100644 index 000000000..f5ccf09fd --- /dev/null +++ b/trunk/src/app/srs_app_latest_version.cpp @@ -0,0 +1,171 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 SRS(ossrs) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +using namespace std; + +SrsLatestVersion::SrsLatestVersion() +{ + trd_ = new SrsEndlessThread("signal", this); +} + +SrsLatestVersion::~SrsLatestVersion() +{ + srs_freep(trd_); +} + +int SrsLatestVersion::start() +{ + if (!_srs_config->whether_query_latest_version()) { + return ERROR_SUCCESS; + } + + char buf[10]; + srs_random_generate(buf, sizeof(buf)); + for (int i = 0; i < (int)sizeof(buf); i++) { + buf[i] = 'a' + uint8_t(buf[i])%25; + } + server_id_ = string(buf, sizeof(buf)); + + return trd_->start(); +} + +int SrsLatestVersion::cycle() +{ + int ret = ERROR_SUCCESS; + + int64_t starttime = srs_update_system_time_ms(); + ret = query_latest_version(); // Ignore any error. + + uint64_t first_random_wait = 0; + srs_random_generate((char*)&first_random_wait, 8); + first_random_wait = (first_random_wait + starttime + getpid()) % (60 * 60); // in s. + + srs_trace("Startup query id=%s, eip=%s, match=%s, stable=%s, wait=%ds, cost=%dms, ret=%d", server_id_.c_str(), srs_get_public_internet_address().c_str(), match_version_.c_str(), stable_version_.c_str(), (int)first_random_wait, (int)(srs_update_system_time_ms() - starttime), ret); + st_usleep(first_random_wait * 1000 * 1000); + + while (true) { + int64_t starttime = srs_update_system_time_ms(); + ret = query_latest_version(); // Ignore any error. + srs_trace("Finish query id=%s, eip=%s, match=%s, stable=%s, cost=%dms, ret=%d", server_id_.c_str(), srs_get_public_internet_address().c_str(), match_version_.c_str(), stable_version_.c_str(), (int)(srs_update_system_time_ms() - starttime), ret); + + st_usleep(3600 * 1000 * 1000LL); // Every an hour. + } + + return ret; +} + +int SrsLatestVersion::query_latest_version() +{ + int ret = ERROR_SUCCESS; + + // Generate uri and parse to object. + stringstream ss; + ss << "http://api.ossrs.net/service/v1/releases?" + << "version=v" << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_REVISION + << "&id=" << server_id_ + << "&eip=" << srs_get_public_internet_address() + << "&ts=" << srs_get_system_time_ms(); + string url = ss.str(); + + SrsHttpUri uri; + if ((ret = uri.initialize(url)) != ERROR_SUCCESS) { + srs_error("http: post failed. url=%s, ret=%d", url.c_str(), ret); + return ret; + } + + // Start HTTP request and read response. + SrsHttpClient http; + if ((ret = http.initialize(uri.get_host(), uri.get_port())) != ERROR_SUCCESS) { + return ret; + } + + ISrsHttpMessage* msg = NULL; + if ((ret = http.get(uri.get_path(), "", &msg)) != ERROR_SUCCESS) { + return ret; + } + SrsAutoFree(ISrsHttpMessage, msg); + + string res; + int code = msg->status_code(); + if ((ret = msg->body_read_all(res)) != ERROR_SUCCESS) { + return ret; + } + + // Check the response code and content. + if (code != SRS_CONSTS_HTTP_OK) { + ret = ERROR_HTTP_STATUS_INVALID; + srs_error("invalid response status=%d. ret=%d", code, ret); + return ret; + } + + if (res.empty()) { + ret = ERROR_HTTP_DATA_INVALID; + srs_error("invalid empty response. ret=%d", ret); + return ret; + } + + // Response in json object. + SrsJsonAny* jres = SrsJsonAny::loads((char*)res.c_str()); + if (!jres || !jres->is_object()) { + ret = ERROR_HTTP_DATA_INVALID; + srs_error("invalid response %s. ret=%d", res.c_str(), ret); + return ret; + } + SrsAutoFree(SrsJsonAny, jres); + + SrsJsonObject* obj = jres->to_object(); + SrsJsonAny* prop = NULL; + + // Parse fields of response. + if ((prop = obj->ensure_property_string("match_version")) == NULL) { + ret = ERROR_RESPONSE_CODE; + srs_error("invalid response without match_version, ret=%d", ret); + return ret; + } + match_version_ = prop->to_str(); + + if ((prop = obj->ensure_property_string("stable_version")) == NULL) { + ret = ERROR_RESPONSE_CODE; + srs_error("invalid response without stable_version, ret=%d", ret); + return ret; + } + stable_version_ = prop->to_str(); + + return ret; +} + diff --git a/trunk/src/app/srs_app_latest_version.hpp b/trunk/src/app/srs_app_latest_version.hpp new file mode 100644 index 000000000..187c3f8f6 --- /dev/null +++ b/trunk/src/app/srs_app_latest_version.hpp @@ -0,0 +1,58 @@ +/* +The MIT License (MIT) + +Copyright (c) 2013-2015 SRS(ossrs) + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#ifndef SRS_APP_LATEST_VERSION_HPP +#define SRS_APP_LATEST_VERSION_HPP + +/* +#include +*/ + +#include + +#include + +#include + +class SrsLatestVersion : public ISrsEndlessThreadHandler +{ +private: + SrsEndlessThread* trd_; + std::string server_id_; +private: + std::string match_version_; + std::string stable_version_; +public: + SrsLatestVersion(); + virtual ~SrsLatestVersion(); +public: + virtual int start(); +// interface ISrsEndlessThreadHandler. +public: + virtual int cycle(); +private: + int query_latest_version(); +}; + +#endif + diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 9efdba41d..da180caeb 100755 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -48,6 +48,7 @@ using namespace std; #include #include #include +#include // signal defines. #define SIGNAL_RELOAD SIGHUP @@ -486,6 +487,7 @@ SrsServer::SrsServer() pid_fd = -1; signal_manager = NULL; + latest_version_ = new SrsLatestVersion(); handler = NULL; ppid = ::getppid(); @@ -540,6 +542,7 @@ void SrsServer::destroy() } srs_freep(signal_manager); + srs_freep(latest_version_); } void SrsServer::dispose() @@ -652,7 +655,18 @@ int SrsServer::initialize_st() int SrsServer::initialize_signal() { - return signal_manager->initialize(); + int ret = ERROR_SUCCESS; + + if ((ret = signal_manager->initialize()) != ERROR_SUCCESS) { + return ret; + } + + // Start the version query coroutine. + if ((ret = latest_version_->start()) != ERROR_SUCCESS) { + return ret; + } + + return ret; } int SrsServer::acquire_pid_file() diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index cd668d5e7..5f230e8aa 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -55,6 +55,7 @@ class SrsTcpListener; #ifdef SRS_AUTO_STREAM_CASTER class SrsAppCasterFlv; #endif +class SrsLatestVersion; // listener type for server to identify the connection, // that is, use different type to process the connection. @@ -267,6 +268,8 @@ private: * signal manager which convert gignal to io message. */ SrsSignalManager* signal_manager; + // To query the latest available version of SRS. + SrsLatestVersion* latest_version_; /** * handle in server cycle. */ diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 84f48766f..0325acf4f 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 272 +#define VERSION_REVISION 273 // generated by configure, only macros. #include