diff --git a/README.md b/README.md
index f897a24bf..caa3d0267 100755
--- a/README.md
+++ b/README.md
@@ -295,6 +295,7 @@ Remark:
## Releases
+* 2021-06-26, [Release v2.0-r9](https://github.com/ossrs/srs/releases/tag/v2.0-r9), 2.0 release9, 2.0.273, 87552 lines.
* 2020-01-25, [Release v2.0-r8][r2.0r8], 2.0 release8, 2.0.272, 87292 lines.
* 2018-11-29, [Release v2.0-r7][r2.0r7], 2.0 release7, 2.0.265, 86994 lines.
* 2018-10-28, [Release v2.0-r6][r2.0r6], 2.0 release6, 2.0.263, 86994 lines.
@@ -339,6 +340,10 @@ Remark:
## History
+* v2.0, 2021-06-26, [2.0 release9(2.0.274)](https://github.com/ossrs/srs/releases/tag/v2.0-r10) released. 87575 lines.
+* v2.0, 2021-07-04, For [#2424](https://github.com/ossrs/srs/issues/2424), use srandom/random to generate. 2.0.274
+* v2.0, 2021-06-26, [2.0 release9(2.0.273)](https://github.com/ossrs/srs/releases/tag/v2.0-r9) released. 87552 lines.
+* 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_ingest.cpp b/trunk/src/app/srs_app_ingest.cpp
index a30069ba3..a46615595 100644
--- a/trunk/src/app/srs_app_ingest.cpp
+++ b/trunk/src/app/srs_app_ingest.cpp
@@ -35,6 +35,7 @@ using namespace std;
#include
#include
#include
+#include
// when error, ingester sleep for a while and retry.
// ingest never sleep a long time, for we must start the stream ASAP.
@@ -436,7 +437,7 @@ void SrsIngester::show_ingest_log_message()
}
// random choose one ingester to report.
- int index = rand() % (int)ingesters.size();
+ int index = srs_random() % (int)ingesters.size();
SrsIngesterFFMPEG* ingester = ingesters.at(index);
// reportable
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..86438a6b8
--- /dev/null
+++ b/trunk/src/app/srs_app_latest_version.cpp
@@ -0,0 +1,176 @@
+/*
+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[16];
+ 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;
+ }
+
+ // Path with query.
+ string path = uri.get_path();
+ path += "?";
+ path += uri.get_query();
+
+ ISrsHttpMessage* msg = NULL;
+ if ((ret = http.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..9b39b5597 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 274
// generated by configure, only macros.
#include
diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp
index ba663604e..95b24a181 100644
--- a/trunk/src/kernel/srs_kernel_utility.cpp
+++ b/trunk/src/kernel/srs_kernel_utility.cpp
@@ -110,9 +110,17 @@ int64_t srs_get_system_startup_time_ms()
if (_srs_system_time_startup_time <= 0) {
srs_update_system_time_ms();
}
-
+
return _srs_system_time_startup_time / 1000;
}
+int64_t srs_get_system_startup_time_us()
+{
+ if (_srs_system_time_startup_time <= 0) {
+ srs_update_system_time_ms();
+ }
+
+ return _srs_system_time_startup_time;
+}
int64_t srs_update_system_time_ms()
{
timeval now;
diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp
index 085074848..de4023513 100644
--- a/trunk/src/kernel/srs_kernel_utility.hpp
+++ b/trunk/src/kernel/srs_kernel_utility.hpp
@@ -46,6 +46,7 @@ extern int srs_avc_nalu_read_bit(SrsBitStream* stream, int8_t& v);
// get current system time in ms, use cache to avoid performance problem
extern int64_t srs_get_system_time_ms();
extern int64_t srs_get_system_startup_time_ms();
+extern int64_t srs_get_system_startup_time_us();
// the deamon st-thread will update it.
extern int64_t srs_update_system_time_ms();
diff --git a/trunk/src/protocol/srs_rtmp_handshake.cpp b/trunk/src/protocol/srs_rtmp_handshake.cpp
index c0c11bf0b..77f0dc1bc 100644
--- a/trunk/src/protocol/srs_rtmp_handshake.cpp
+++ b/trunk/src/protocol/srs_rtmp_handshake.cpp
@@ -484,7 +484,7 @@ namespace _srs_internal
key_block::key_block()
{
- offset = (int32_t)rand();
+ offset = (int32_t)srs_random();
random0 = NULL;
random1 = NULL;
@@ -566,7 +566,7 @@ namespace _srs_internal
digest_block::digest_block()
{
- offset = (int32_t)rand();
+ offset = (int32_t)srs_random();
random0 = NULL;
random1 = NULL;
diff --git a/trunk/src/protocol/srs_rtmp_utility.cpp b/trunk/src/protocol/srs_rtmp_utility.cpp
index 8c9b9908a..206253b8a 100644
--- a/trunk/src/protocol/srs_rtmp_utility.cpp
+++ b/trunk/src/protocol/srs_rtmp_utility.cpp
@@ -116,19 +116,25 @@ void srs_vhost_resolve(string& vhost, string& app, string& param)
void srs_random_generate(char* bytes, int size)
{
- static bool _random_initialized = false;
- if (!_random_initialized) {
- srand(0);
- _random_initialized = true;
- srs_trace("srand initialized the random.");
- }
-
for (int i = 0; i < size; i++) {
// the common value in [0x0f, 0xf0]
- bytes[i] = 0x0f + (rand() % (256 - 0x0f - 0x0f));
+ bytes[i] = 0x0f + (srs_random() % (256 - 0x0f - 0x0f));
}
}
+long srs_random()
+{
+ static bool _random_initialized = false;
+ if (!_random_initialized) {
+ _random_initialized = true;
+
+ srandom((unsigned int)srs_get_system_startup_time_us());
+ srs_trace("srandom initialized the random.");
+ }
+
+ return random();
+}
+
string srs_generate_tc_url(string ip, string vhost, string app, string port, string param)
{
string tcUrl = "rtmp://";
diff --git a/trunk/src/protocol/srs_rtmp_utility.hpp b/trunk/src/protocol/srs_rtmp_utility.hpp
index 9344410c5..4a7211097 100644
--- a/trunk/src/protocol/srs_rtmp_utility.hpp
+++ b/trunk/src/protocol/srs_rtmp_utility.hpp
@@ -80,6 +80,8 @@ extern void srs_vhost_resolve(
* generate ramdom data for handshake.
*/
extern void srs_random_generate(char* bytes, int size);
+// Generate random value, use srandom(now_us) to init seed if not initialized.
+extern long srs_random();
/**
* generate the tcUrl.