For #2424, query the latest available version. 2.0.273

2.0release
winlin 4 years ago
parent 6864e1ca6d
commit f1db22f261

@ -339,6 +339,7 @@ Remark:
## History ## History
* v2.0, 2021-06-25, For [#2424](https://github.com/ossrs/srs/issues/2424), query the latest available version. 2.0.273
* <strong>v2.0, 2020-01-25, [2.0 release8(2.0.272)][r2.0r8] released. 87292 lines.</strong> * <strong>v2.0, 2020-01-25, [2.0 release8(2.0.272)][r2.0r8] released. 87292 lines.</strong>
* v2.0, 2020-01-08, Merge [#1554][bug #1554], support logrotate copytruncate. 2.0.272 * 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 * v2.0, 2020-01-05, Merge [#1551][bug #1551], fix memory leak in RTSP stack. 2.0.270

@ -63,6 +63,11 @@ work_dir ./;
# default: off # default: off
asprocess 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 # heartbeat/stats sections
############################################################################################# #############################################################################################

2
trunk/configure vendored

@ -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_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_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_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="" DEFINES=""
# add each modules for app # add each modules for app
for SRS_MODULE in ${SRS_MODULES[*]}; do for SRS_MODULE in ${SRS_MODULES[*]}; do

@ -2212,6 +2212,18 @@ bool SrsConfig::get_asprocess()
return SRS_CONF_PERFER_FALSE(conf->arg0()); 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<SrsConfDirective*> SrsConfig::get_stream_casters() vector<SrsConfDirective*> SrsConfig::get_stream_casters()
{ {
srs_assert(root); srs_assert(root);

@ -386,6 +386,8 @@ public:
virtual std::string get_work_dir(); virtual std::string get_work_dir();
// whether use asprocess mode. // whether use asprocess mode.
virtual bool get_asprocess(); virtual bool get_asprocess();
// Whether query the latest available version of SRS.
virtual bool whether_query_latest_version();
// stream_caster section // stream_caster section
public: public:
/** /**

@ -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 <srs_app_latest_version.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_protocol_json.hpp>
#include <srs_rtmp_utility.hpp>
#include <srs_app_config.hpp>
#include <srs_app_http_conn.hpp>
#include <srs_app_http_client.hpp>
#include <srs_app_utility.hpp>
#include <unistd.h>
#include <sstream>
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;
}

@ -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 <srs_app_latest_version.hpp>
*/
#include <srs_core.hpp>
#include <srs_app_thread.hpp>
#include <string>
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

@ -48,6 +48,7 @@ using namespace std;
#include <srs_app_statistic.hpp> #include <srs_app_statistic.hpp>
#include <srs_app_caster_flv.hpp> #include <srs_app_caster_flv.hpp>
#include <srs_core_mem_watch.hpp> #include <srs_core_mem_watch.hpp>
#include <srs_app_latest_version.hpp>
// signal defines. // signal defines.
#define SIGNAL_RELOAD SIGHUP #define SIGNAL_RELOAD SIGHUP
@ -486,6 +487,7 @@ SrsServer::SrsServer()
pid_fd = -1; pid_fd = -1;
signal_manager = NULL; signal_manager = NULL;
latest_version_ = new SrsLatestVersion();
handler = NULL; handler = NULL;
ppid = ::getppid(); ppid = ::getppid();
@ -540,6 +542,7 @@ void SrsServer::destroy()
} }
srs_freep(signal_manager); srs_freep(signal_manager);
srs_freep(latest_version_);
} }
void SrsServer::dispose() void SrsServer::dispose()
@ -652,7 +655,18 @@ int SrsServer::initialize_st()
int SrsServer::initialize_signal() 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() int SrsServer::acquire_pid_file()

@ -55,6 +55,7 @@ class SrsTcpListener;
#ifdef SRS_AUTO_STREAM_CASTER #ifdef SRS_AUTO_STREAM_CASTER
class SrsAppCasterFlv; class SrsAppCasterFlv;
#endif #endif
class SrsLatestVersion;
// listener type for server to identify the connection, // listener type for server to identify the connection,
// that is, use different type to process the connection. // that is, use different type to process the connection.
@ -267,6 +268,8 @@ private:
* signal manager which convert gignal to io message. * signal manager which convert gignal to io message.
*/ */
SrsSignalManager* signal_manager; SrsSignalManager* signal_manager;
// To query the latest available version of SRS.
SrsLatestVersion* latest_version_;
/** /**
* handle in server cycle. * handle in server cycle.
*/ */

@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// current release version // current release version
#define VERSION_MAJOR 2 #define VERSION_MAJOR 2
#define VERSION_MINOR 0 #define VERSION_MINOR 0
#define VERSION_REVISION 272 #define VERSION_REVISION 273
// generated by configure, only macros. // generated by configure, only macros.
#include <srs_auto_headers.hpp> #include <srs_auto_headers.hpp>

Loading…
Cancel
Save