Refine get_bw_check_interval in time unit

pull/1651/head
winlin 6 years ago
parent 74c1944e15
commit bb6389143f

@ -144,15 +144,14 @@ srs_error_t SrsBandwidth::bandwidth_check(SrsRtmpServer* rtmp, ISrsProtocolStati
// to prevent bandwidth check attack, // to prevent bandwidth check attack,
// if client request check in the window(specifeid by interval), // if client request check in the window(specifeid by interval),
// directly reject the request. // directly reject the request.
static int64_t last_check_time = 0; static srs_utime_t last_check_time = 0;
int interval_ms = _srs_config->get_bw_check_interval_ms(_req->vhost); srs_utime_t interval = _srs_config->get_bw_check_interval(_req->vhost);
srs_update_system_time_ms(); srs_utime_t time_now = srs_update_system_time_ms() * SRS_UTIME_MILLISECONDS;
int64_t time_now = srs_get_system_time_ms();
// reject the connection in the interval window. // reject the connection in the interval window.
if (last_check_time > 0 && time_now - last_check_time < interval_ms) { if (last_check_time > 0 && time_now - last_check_time < interval) {
_rtmp->response_connect_reject(_req, "bandcheck rejected"); _rtmp->response_connect_reject(_req, "bandcheck rejected");
return srs_error_new(ERROR_SYSTEM_BANDWIDTH_DENIED, "reject, last_check=%" PRId64 ", now=%" PRId64 ", interval=%d", last_check_time, time_now, interval_ms); return srs_error_new(ERROR_SYSTEM_BANDWIDTH_DENIED, "reject, last_check=%" PRId64 ", now=%" PRId64 ", interval=%d", last_check_time, time_now, interval);
} }
// accept and do bandwidth check. // accept and do bandwidth check.

@ -5026,9 +5026,9 @@ string SrsConfig::get_bw_check_key(string vhost)
return conf->arg0(); return conf->arg0();
} }
int SrsConfig::get_bw_check_interval_ms(string vhost) srs_utime_t SrsConfig::get_bw_check_interval(string vhost)
{ {
static int DEFAULT = 30 * 1000; static int64_t DEFAULT = 30 * SRS_UTIME_SECONDS;
SrsConfDirective* conf = get_vhost(vhost); SrsConfDirective* conf = get_vhost(vhost);
if (!conf) { if (!conf) {
@ -5045,7 +5045,7 @@ int SrsConfig::get_bw_check_interval_ms(string vhost)
return DEFAULT; return DEFAULT;
} }
return (int)(::atof(conf->arg0().c_str()) * 1000); return (srs_utime_t)(::atof(conf->arg0().c_str()) * SRS_UTIME_SECONDS);
} }
int SrsConfig::get_bw_check_limit_kbps(string vhost) int SrsConfig::get_bw_check_limit_kbps(string vhost)

@ -34,6 +34,7 @@
#include <srs_app_reload.hpp> #include <srs_app_reload.hpp>
#include <srs_app_async_call.hpp> #include <srs_app_async_call.hpp>
#include <srs_app_thread.hpp> #include <srs_app_thread.hpp>
#include <srs_service_st.hpp>
class SrsRequest; class SrsRequest;
class SrsFileWriter; class SrsFileWriter;
@ -896,12 +897,12 @@ public:
*/ */
virtual std::string get_bw_check_key(std::string vhost); virtual std::string get_bw_check_key(std::string vhost);
/** /**
* the check interval, in ms. * the check interval, in srs_utime_t.
* if the client request check in very short time(in the interval), * if the client request check in very short time(in the interval),
* SRS will reject client. * SRS will reject client.
* @remark this is used to prevent the bandwidth check attack. * @remark this is used to prevent the bandwidth check attack.
*/ */
virtual int get_bw_check_interval_ms(std::string vhost); virtual srs_utime_t get_bw_check_interval(std::string vhost);
/** /**
* the max kbps that user can test, * the max kbps that user can test,
* if exceed the kbps, server will slowdown the send-recv. * if exceed the kbps, server will slowdown the send-recv.

@ -1809,7 +1809,16 @@ VOID TEST(ConfigUnitTest, CheckDefaultValues)
MockSrsConfig conf; MockSrsConfig conf;
if (true) { if (true) {
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF)); EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF));
EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_bw_check_interval_ms("")); EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_bw_check_interval(""));
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"vhost v{}"));
EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_bw_check_interval("v"));
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"vhost v{bandcheck{}}"));
EXPECT_EQ(30 * SRS_UTIME_SECONDS, conf.get_bw_check_interval("v"));
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"vhost v{bandcheck{interval 1.1;}}"));
EXPECT_EQ(srs_utime_t(1.1 * SRS_UTIME_SECONDS), conf.get_bw_check_interval("v"));
} }
} }

Loading…
Cancel
Save