winlin

merge with security bug fix.

@@ -389,7 +389,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then @@ -389,7 +389,7 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" 389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks"
390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" 390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge"
391 "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac" 391 "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac"
392 - "srs_app_recv_thread" "srs_app_security") 392 + "srs_app_recv_thread" "srs_app_security" "srs_app_statistic")
393 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh 393 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh
394 APP_OBJS="${MODULE_OBJS[@]}" 394 APP_OBJS="${MODULE_OBJS[@]}"
395 fi 395 fi
@@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #ifdef SRS_AUTO_HTTP_API 26 #ifdef SRS_AUTO_HTTP_API
27 27
28 #include <sstream> 28 #include <sstream>
  29 +#include <set>
29 using namespace std; 30 using namespace std;
30 31
31 #include <srs_kernel_log.hpp> 32 #include <srs_kernel_log.hpp>
@@ -35,6 +36,8 @@ using namespace std; @@ -35,6 +36,8 @@ using namespace std;
35 #include <srs_app_json.hpp> 36 #include <srs_app_json.hpp>
36 #include <srs_kernel_utility.hpp> 37 #include <srs_kernel_utility.hpp>
37 #include <srs_app_utility.hpp> 38 #include <srs_app_utility.hpp>
  39 +#include <srs_app_statistic.hpp>
  40 +#include <srs_protocol_rtmp.hpp>
38 41
39 SrsApiRoot::SrsApiRoot() 42 SrsApiRoot::SrsApiRoot()
40 { 43 {
@@ -122,6 +125,8 @@ SrsApiV1::SrsApiV1() @@ -122,6 +125,8 @@ SrsApiV1::SrsApiV1()
122 handlers.push_back(new SrsApiMemInfos()); 125 handlers.push_back(new SrsApiMemInfos());
123 handlers.push_back(new SrsApiAuthors()); 126 handlers.push_back(new SrsApiAuthors());
124 handlers.push_back(new SrsApiRequests()); 127 handlers.push_back(new SrsApiRequests());
  128 + handlers.push_back(new SrsApiVhosts());
  129 + handlers.push_back(new SrsApiStreams());
125 } 130 }
126 131
127 SrsApiV1::~SrsApiV1() 132 SrsApiV1::~SrsApiV1()
@@ -147,7 +152,9 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -147,7 +152,9 @@ int SrsApiV1::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
147 << __SRS_JFIELD_STR("system_proc_stats", "the system process stats") << __SRS_JFIELD_CONT 152 << __SRS_JFIELD_STR("system_proc_stats", "the system process stats") << __SRS_JFIELD_CONT
148 << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT 153 << __SRS_JFIELD_STR("meminfos", "the meminfo of system") << __SRS_JFIELD_CONT
149 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT 154 << __SRS_JFIELD_STR("authors", "the primary authors and contributors") << __SRS_JFIELD_CONT
150 - << __SRS_JFIELD_STR("requests", "the request itself, for http debug") 155 + << __SRS_JFIELD_STR("requests", "the request itself, for http debug") << __SRS_JFIELD_CONT
  156 + << __SRS_JFIELD_STR("vhosts", "list all vhosts") << __SRS_JFIELD_CONT
  157 + << __SRS_JFIELD_STR("streams", "list streams that match the name or vhost")
151 << __SRS_JOBJECT_END 158 << __SRS_JOBJECT_END
152 << __SRS_JOBJECT_END; 159 << __SRS_JOBJECT_END;
153 160
@@ -500,6 +507,103 @@ int SrsApiAuthors::do_process_request(SrsStSocket* skt, SrsHttpMessage* req) @@ -500,6 +507,103 @@ int SrsApiAuthors::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
500 return res_json(skt, req, ss.str()); 507 return res_json(skt, req, ss.str());
501 } 508 }
502 509
  510 +SrsApiVhosts::SrsApiVhosts()
  511 +{
  512 +}
  513 +
  514 +SrsApiVhosts::~SrsApiVhosts()
  515 +{
  516 +}
  517 +
  518 +bool SrsApiVhosts::can_handle(const char* path, int length, const char** /*pchild*/)
  519 +{
  520 + return srs_path_equals("/vhosts", path, length);
  521 +}
  522 +
  523 +int SrsApiVhosts::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
  524 +{
  525 + std::stringstream ss;
  526 +
  527 + std::set<std::string> vhost_set;
  528 + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool();
  529 + SrsStreamInfoMap::iterator it;
  530 + for (it = pool->begin(); it != pool->end(); it++) {
  531 + if (it->second->_req == NULL)
  532 + continue;
  533 + vhost_set.insert(it->second->_req->vhost);
  534 + }
  535 +
  536 + ss << __SRS_JARRAY_START;
  537 + bool first = true;
  538 + std::set<std::string>::iterator it_set;
  539 + for (it_set = vhost_set.begin(); it_set != vhost_set.end(); it_set++) {
  540 + if (first) {
  541 + first = false;
  542 + } else {
  543 + ss << __SRS_JFIELD_CONT;
  544 + }
  545 +
  546 + ss << "\"" << (*it_set) << "\"";
  547 + }
  548 + ss << __SRS_JARRAY_END;
  549 +
  550 + return res_json(skt, req, ss.str());
  551 +}
  552 +
  553 +SrsApiStreams::SrsApiStreams()
  554 +{
  555 +}
  556 +
  557 +SrsApiStreams::~SrsApiStreams()
  558 +{
  559 +}
  560 +
  561 +bool SrsApiStreams::can_handle(const char* path, int length, const char** /*pchild*/)
  562 +{
  563 + return srs_path_equals("/streams", path, length);
  564 +}
  565 +
  566 +int SrsApiStreams::do_process_request(SrsStSocket* skt, SrsHttpMessage* req)
  567 +{
  568 + std::stringstream ss;
  569 +
  570 + std::string query_name = req->query_get("name");
  571 + std::string query_vhost = req->query_get("vhost");
  572 + if (query_name.size() > 0 || query_vhost.size() > 0) {
  573 + ss << __SRS_JARRAY_START;
  574 + bool first = true;
  575 + SrsStreamInfoMap* pool = SrsStatistic::instance()->get_pool();
  576 + SrsStreamInfoMap::iterator it;
  577 + for (it = pool->begin(); it != pool->end(); it++) {
  578 + SrsRequest* reqinfo = it->second->_req;
  579 + if (reqinfo == NULL)
  580 + continue;
  581 +
  582 + if (reqinfo->stream == query_name || reqinfo->vhost == query_vhost) {
  583 + if (first) {
  584 + first = false;
  585 + } else {
  586 + ss << __SRS_JFIELD_CONT;
  587 + }
  588 +
  589 + ss << __SRS_JOBJECT_START
  590 + << __SRS_JFIELD_STR("name", reqinfo->stream) << __SRS_JFIELD_CONT
  591 + << __SRS_JFIELD_STR("url", reqinfo->tcUrl) << __SRS_JFIELD_CONT
  592 + << __SRS_JFIELD_ORG("clients", 0) << __SRS_JFIELD_CONT
  593 + << __SRS_JFIELD_STR("status", "idle") << __SRS_JFIELD_CONT
  594 + << __SRS_JFIELD_STR("type", "") << __SRS_JFIELD_CONT
  595 + << __SRS_JFIELD_STR("codec", "")
  596 + << __SRS_JOBJECT_END;
  597 + }
  598 + }
  599 + ss << __SRS_JARRAY_END;
  600 + } else {
  601 + return res_error(skt, req, 400, "Bad Request", "unknown query");
  602 + }
  603 +
  604 + return res_json(skt, req, ss.str());
  605 +}
  606 +
503 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler) 607 SrsHttpApi::SrsHttpApi(SrsServer* srs_server, st_netfd_t client_stfd, SrsHttpHandler* _handler)
504 : SrsConnection(srs_server, client_stfd) 608 : SrsConnection(srs_server, client_stfd)
505 { 609 {
@@ -164,6 +164,28 @@ protected: @@ -164,6 +164,28 @@ protected:
164 virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req); 164 virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
165 }; 165 };
166 166
  167 +class SrsApiVhosts : public SrsHttpHandler
  168 +{
  169 +public:
  170 + SrsApiVhosts();
  171 + virtual ~SrsApiVhosts();
  172 +public:
  173 + virtual bool can_handle(const char* path, int length, const char** pchild);
  174 +protected:
  175 + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
  176 +};
  177 +
  178 +class SrsApiStreams : public SrsHttpHandler
  179 +{
  180 +public:
  181 + SrsApiStreams();
  182 + virtual ~SrsApiStreams();
  183 +public:
  184 + virtual bool can_handle(const char* path, int length, const char** pchild);
  185 +protected:
  186 + virtual int do_process_request(SrsStSocket* skt, SrsHttpMessage* req);
  187 +};
  188 +
167 class SrsHttpApi : public SrsConnection 189 class SrsHttpApi : public SrsConnection
168 { 190 {
169 private: 191 private:
@@ -52,6 +52,7 @@ using namespace std; @@ -52,6 +52,7 @@ using namespace std;
52 #include <srs_core_performance.hpp> 52 #include <srs_core_performance.hpp>
53 #include <srs_kernel_utility.hpp> 53 #include <srs_kernel_utility.hpp>
54 #include <srs_app_security.hpp> 54 #include <srs_app_security.hpp>
  55 +#include <srs_app_statistic.hpp>
55 56
56 // when stream is busy, for example, streaming is already 57 // when stream is busy, for example, streaming is already
57 // publishing, when a new client to request to publish, 58 // publishing, when a new client to request to publish,
@@ -395,6 +396,8 @@ int SrsRtmpConn::stream_service_cycle() @@ -395,6 +396,8 @@ int SrsRtmpConn::stream_service_cycle()
395 } 396 }
396 srs_assert(source != NULL); 397 srs_assert(source != NULL);
397 398
  399 + SrsStatistic::instance()->add_request_info(source, req);
  400 +
398 // check ASAP, to fail it faster if invalid. 401 // check ASAP, to fail it faster if invalid.
399 if (type != SrsRtmpConnPlay && !vhost_is_edge) { 402 if (type != SrsRtmpConnPlay && !vhost_is_edge) {
400 // check publish available 403 // check publish available
@@ -1807,4 +1807,3 @@ void SrsSource::destroy_forwarders() @@ -1807,4 +1807,3 @@ void SrsSource::destroy_forwarders()
1807 forwarders.clear(); 1807 forwarders.clear();
1808 } 1808 }
1809 1809
1810 -  
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_app_statistic.hpp>
  25 +
  26 +#include <srs_protocol_rtmp.hpp>
  27 +
  28 +SrsStreamInfo::SrsStreamInfo()
  29 +{
  30 + _req = NULL;
  31 +}
  32 +
  33 +SrsStreamInfo::~SrsStreamInfo()
  34 +{
  35 + if (_req != NULL)
  36 + delete _req;
  37 +}
  38 +
  39 +SrsStatistic *SrsStatistic::_instance = NULL;
  40 +
  41 +SrsStatistic::SrsStatistic()
  42 +{
  43 +
  44 +}
  45 +
  46 +SrsStatistic::~SrsStatistic()
  47 +{
  48 + SrsStreamInfoMap::iterator it;
  49 + for (it = pool.begin(); it != pool.end(); it++) {
  50 + delete it->second;
  51 + }
  52 +}
  53 +
  54 +SrsStreamInfoMap* SrsStatistic::get_pool()
  55 +{
  56 + return &pool;
  57 +}
  58 +
  59 +SrsStreamInfo* SrsStatistic::get(void *p)
  60 +{
  61 + SrsStreamInfoMap::iterator it = pool.find(p);
  62 + if (it == pool.end()) {
  63 + pool[p] = new SrsStreamInfo();
  64 + return pool[p];
  65 + } else {
  66 + return it->second;
  67 + }
  68 +}
  69 +
  70 +void SrsStatistic::add_request_info(void *p, SrsRequest *req)
  71 +{
  72 + SrsStreamInfo *info = get(p);
  73 + if (info->_req == NULL)
  74 + info->_req = req->copy();
  75 +}
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2015 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_APP_STATISTIC_HPP
  25 +#define SRS_APP_STATISTIC_HPP
  26 +
  27 +/*
  28 +#include <srs_app_statistic.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <map>
  34 +
  35 +class SrsRequest;
  36 +
  37 +class SrsStreamInfo
  38 +{
  39 +public:
  40 + SrsStreamInfo();
  41 + virtual ~SrsStreamInfo();
  42 +
  43 + SrsRequest *_req;
  44 +};
  45 +typedef std::map<void*, SrsStreamInfo*> SrsStreamInfoMap;
  46 +
  47 +class SrsStatistic
  48 +{
  49 +public:
  50 + static SrsStatistic *instance()
  51 + {
  52 + if (_instance == NULL) {
  53 + _instance = new SrsStatistic();
  54 + }
  55 + return _instance;
  56 + }
  57 +
  58 + virtual SrsStreamInfoMap* get_pool();
  59 +
  60 + virtual void add_request_info(void *p, SrsRequest *req);
  61 +
  62 +private:
  63 + SrsStatistic();
  64 + virtual ~SrsStatistic();
  65 + static SrsStatistic *_instance;
  66 + SrsStreamInfoMap pool;
  67 + virtual SrsStreamInfo *get(void *p);
  68 +};
  69 +
  70 +#endif