正在显示
15 个修改的文件
包含
376 行增加
和
9 行删除
| @@ -46,6 +46,7 @@ url: rtmp://127.0.0.1:1935/live/livestream | @@ -46,6 +46,7 @@ url: rtmp://127.0.0.1:1935/live/livestream | ||
| 46 | * nginx v1.5.0: 139524 lines <br/> | 46 | * nginx v1.5.0: 139524 lines <br/> |
| 47 | 47 | ||
| 48 | ### History | 48 | ### History |
| 49 | +* v0.3, 2013-11-04, support vhosts specified config. | ||
| 49 | * v0.3, 2013-11-02, support listen multiple ports. | 50 | * v0.3, 2013-11-02, support listen multiple ports. |
| 50 | * v0.3, 2013-11-02, support config file in nginx-conf style. | 51 | * v0.3, 2013-11-02, support config file in nginx-conf style. |
| 51 | * v0.3, 2013-10-29, support pithy print log message specified by stage. | 52 | * v0.3, 2013-10-29, support pithy print log message specified by stage. |
| 1 | listen 1935 19350; | 1 | listen 1935 19350; |
| 2 | vhost __defaultVhost__ { | 2 | vhost __defaultVhost__ { |
| 3 | - application live { | ||
| 4 | - no_delay on; | ||
| 5 | - allow all; | ||
| 6 | - } | 3 | + enabled on; |
| 4 | + gop_cache on; | ||
| 5 | + #refer github.com github.io; | ||
| 6 | + #refer_publish github.com github.io; | ||
| 7 | + refer_play github.com github.io .com; | ||
| 7 | } | 8 | } |
| 9 | +vhost winlinvip.github.com { | ||
| 10 | + gop_cache off; | ||
| 11 | +} | ||
| 12 | + |
| @@ -91,7 +91,7 @@ MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" | @@ -91,7 +91,7 @@ MODULE_FILES=("srs_core" "srs_core_log" "srs_core_server" | ||
| 91 | "srs_core_auto_free" "srs_core_protocol" "srs_core_amf0" | 91 | "srs_core_auto_free" "srs_core_protocol" "srs_core_amf0" |
| 92 | "srs_core_stream" "srs_core_source" "srs_core_codec" | 92 | "srs_core_stream" "srs_core_source" "srs_core_codec" |
| 93 | "srs_core_complex_handshake" "srs_core_pithy_print" | 93 | "srs_core_complex_handshake" "srs_core_pithy_print" |
| 94 | - "srs_core_config") | 94 | + "srs_core_config" "srs_core_refer") |
| 95 | MODULE_DIR="src/core" . auto/modules.sh | 95 | MODULE_DIR="src/core" . auto/modules.sh |
| 96 | CORE_OBJS="${MODULE_OBJS[@]}" | 96 | CORE_OBJS="${MODULE_OBJS[@]}" |
| 97 | 97 |
| @@ -33,6 +33,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -33,6 +33,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 33 | #include <srs_core_source.hpp> | 33 | #include <srs_core_source.hpp> |
| 34 | #include <srs_core_server.hpp> | 34 | #include <srs_core_server.hpp> |
| 35 | #include <srs_core_pithy_print.hpp> | 35 | #include <srs_core_pithy_print.hpp> |
| 36 | +#include <srs_core_config.hpp> | ||
| 37 | +#include <srs_core_refer.hpp> | ||
| 36 | 38 | ||
| 37 | #define SRS_PULSE_TIMEOUT_MS 100 | 39 | #define SRS_PULSE_TIMEOUT_MS 100 |
| 38 | #define SRS_SEND_TIMEOUT_MS 5000000L | 40 | #define SRS_SEND_TIMEOUT_MS 5000000L |
| @@ -45,6 +47,7 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) | @@ -45,6 +47,7 @@ SrsClient::SrsClient(SrsServer* srs_server, st_netfd_t client_stfd) | ||
| 45 | req = new SrsRequest(); | 47 | req = new SrsRequest(); |
| 46 | res = new SrsResponse(); | 48 | res = new SrsResponse(); |
| 47 | rtmp = new SrsRtmp(client_stfd); | 49 | rtmp = new SrsRtmp(client_stfd); |
| 50 | + refer = new SrsRefer(); | ||
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | SrsClient::~SrsClient() | 53 | SrsClient::~SrsClient() |
| @@ -53,6 +56,7 @@ SrsClient::~SrsClient() | @@ -53,6 +56,7 @@ SrsClient::~SrsClient() | ||
| 53 | srs_freep(req); | 56 | srs_freep(req); |
| 54 | srs_freep(res); | 57 | srs_freep(res); |
| 55 | srs_freep(rtmp); | 58 | srs_freep(rtmp); |
| 59 | + srs_freep(refer); | ||
| 56 | } | 60 | } |
| 57 | 61 | ||
| 58 | int SrsClient::do_cycle() | 62 | int SrsClient::do_cycle() |
| @@ -79,11 +83,25 @@ int SrsClient::do_cycle() | @@ -79,11 +83,25 @@ int SrsClient::do_cycle() | ||
| 79 | srs_error("rtmp connect vhost/app failed. ret=%d", ret); | 83 | srs_error("rtmp connect vhost/app failed. ret=%d", ret); |
| 80 | return ret; | 84 | return ret; |
| 81 | } | 85 | } |
| 86 | + srs_verbose("rtmp connect app success"); | ||
| 87 | + | ||
| 88 | + if ((ret = check_vhost()) != ERROR_SUCCESS) { | ||
| 89 | + srs_error("check vhost failed. ret=%d", ret); | ||
| 90 | + return ret; | ||
| 91 | + } | ||
| 92 | + srs_verbose("check vhost success."); | ||
| 93 | + | ||
| 82 | srs_trace("rtmp connect app success. " | 94 | srs_trace("rtmp connect app success. " |
| 83 | "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s", | 95 | "tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%s, app=%s", |
| 84 | req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(), | 96 | req->tcUrl.c_str(), req->pageUrl.c_str(), req->swfUrl.c_str(), |
| 85 | req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), | 97 | req->schema.c_str(), req->vhost.c_str(), req->port.c_str(), |
| 86 | req->app.c_str()); | 98 | req->app.c_str()); |
| 99 | + | ||
| 100 | + if ((ret = refer->check(req->pageUrl, config->get_refer(req->vhost))) != ERROR_SUCCESS) { | ||
| 101 | + srs_error("check refer failed. ret=%d", ret); | ||
| 102 | + return ret; | ||
| 103 | + } | ||
| 104 | + srs_verbose("check refer success."); | ||
| 87 | 105 | ||
| 88 | if ((ret = rtmp->set_window_ack_size(2.5 * 1000 * 1000)) != ERROR_SUCCESS) { | 106 | if ((ret = rtmp->set_window_ack_size(2.5 * 1000 * 1000)) != ERROR_SUCCESS) { |
| 89 | srs_error("set window acknowledgement size failed. ret=%d", ret); | 107 | srs_error("set window acknowledgement size failed. ret=%d", ret); |
| @@ -114,7 +132,8 @@ int SrsClient::do_cycle() | @@ -114,7 +132,8 @@ int SrsClient::do_cycle() | ||
| 114 | srs_error("identify client failed. ret=%d", ret); | 132 | srs_error("identify client failed. ret=%d", ret); |
| 115 | return ret; | 133 | return ret; |
| 116 | } | 134 | } |
| 117 | - srs_verbose("identify client success. type=%d, stream_name=%s", type, req->stream.c_str()); | 135 | + req->strip(); |
| 136 | + srs_trace("identify client success. type=%d, stream_name=%s", type, req->stream.c_str()); | ||
| 118 | 137 | ||
| 119 | // TODO: read from config. | 138 | // TODO: read from config. |
| 120 | int chunk_size = 4096; | 139 | int chunk_size = 4096; |
| @@ -127,7 +146,15 @@ int SrsClient::do_cycle() | @@ -127,7 +146,15 @@ int SrsClient::do_cycle() | ||
| 127 | // find a source to publish. | 146 | // find a source to publish. |
| 128 | SrsSource* source = SrsSource::find(req->get_stream_url()); | 147 | SrsSource* source = SrsSource::find(req->get_stream_url()); |
| 129 | srs_assert(source != NULL); | 148 | srs_assert(source != NULL); |
| 130 | - srs_info("source found, url=%s", req->get_stream_url().c_str()); | 149 | + |
| 150 | + SrsConfDirective* conf = config->get_gop_cache(req->vhost); | ||
| 151 | + bool enabled_cache = true; | ||
| 152 | + if (conf && conf->arg0() == "off") { | ||
| 153 | + enabled_cache = false; | ||
| 154 | + } | ||
| 155 | + source->set_cache(enabled_cache); | ||
| 156 | + | ||
| 157 | + srs_info("source found, url=%s, enabled_cache=%d", req->get_stream_url().c_str(), enabled_cache); | ||
| 131 | 158 | ||
| 132 | switch (type) { | 159 | switch (type) { |
| 133 | case SrsClientPlay: { | 160 | case SrsClientPlay: { |
| @@ -174,10 +201,44 @@ int SrsClient::do_cycle() | @@ -174,10 +201,44 @@ int SrsClient::do_cycle() | ||
| 174 | return ret; | 201 | return ret; |
| 175 | } | 202 | } |
| 176 | 203 | ||
| 204 | +int SrsClient::check_vhost() | ||
| 205 | +{ | ||
| 206 | + int ret = ERROR_SUCCESS; | ||
| 207 | + | ||
| 208 | + srs_assert(req != NULL); | ||
| 209 | + | ||
| 210 | + SrsConfDirective* vhost = config->get_vhost(req->vhost); | ||
| 211 | + if (vhost == NULL) { | ||
| 212 | + ret = ERROR_RTMP_VHOST_NOT_FOUND; | ||
| 213 | + srs_error("vhost %s not found. ret=%d", req->vhost.c_str(), ret); | ||
| 214 | + return ret; | ||
| 215 | + } | ||
| 216 | + | ||
| 217 | + SrsConfDirective* conf = NULL; | ||
| 218 | + if ((conf = vhost->get(RTMP_VHOST_ENABLED)) != NULL && conf->arg0() == "off") { | ||
| 219 | + ret = ERROR_RTMP_VHOST_NOT_FOUND; | ||
| 220 | + srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret); | ||
| 221 | + return ret; | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + if (req->vhost != vhost->arg0()) { | ||
| 225 | + srs_trace("vhost change from %s to %s", req->vhost.c_str(), vhost->arg0().c_str()); | ||
| 226 | + req->vhost = vhost->arg0(); | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + return ret; | ||
| 230 | +} | ||
| 231 | + | ||
| 177 | int SrsClient::playing(SrsSource* source) | 232 | int SrsClient::playing(SrsSource* source) |
| 178 | { | 233 | { |
| 179 | int ret = ERROR_SUCCESS; | 234 | int ret = ERROR_SUCCESS; |
| 180 | 235 | ||
| 236 | + if ((ret = refer->check(req->pageUrl, config->get_refer_play(req->vhost))) != ERROR_SUCCESS) { | ||
| 237 | + srs_error("check play_refer failed. ret=%d", ret); | ||
| 238 | + return ret; | ||
| 239 | + } | ||
| 240 | + srs_verbose("check play_refer success."); | ||
| 241 | + | ||
| 181 | SrsConsumer* consumer = NULL; | 242 | SrsConsumer* consumer = NULL; |
| 182 | if ((ret = source->create_consumer(consumer)) != ERROR_SUCCESS) { | 243 | if ((ret = source->create_consumer(consumer)) != ERROR_SUCCESS) { |
| 183 | srs_error("create consumer failed. ret=%d", ret); | 244 | srs_error("create consumer failed. ret=%d", ret); |
| @@ -258,6 +319,12 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) | @@ -258,6 +319,12 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) | ||
| 258 | { | 319 | { |
| 259 | int ret = ERROR_SUCCESS; | 320 | int ret = ERROR_SUCCESS; |
| 260 | 321 | ||
| 322 | + if ((ret = refer->check(req->pageUrl, config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) { | ||
| 323 | + srs_error("check publish_refer failed. ret=%d", ret); | ||
| 324 | + return ret; | ||
| 325 | + } | ||
| 326 | + srs_verbose("check publish_refer success."); | ||
| 327 | + | ||
| 261 | SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER); | 328 | SrsPithyPrint pithy_print(SRS_STAGE_PUBLISH_USER); |
| 262 | 329 | ||
| 263 | while (true) { | 330 | while (true) { |
| @@ -36,6 +36,7 @@ class SrsRtmp; | @@ -36,6 +36,7 @@ class SrsRtmp; | ||
| 36 | class SrsRequest; | 36 | class SrsRequest; |
| 37 | class SrsResponse; | 37 | class SrsResponse; |
| 38 | class SrsSource; | 38 | class SrsSource; |
| 39 | +class SrsRefer; | ||
| 39 | 40 | ||
| 40 | /** | 41 | /** |
| 41 | * the client provides the main logic control for RTMP clients. | 42 | * the client provides the main logic control for RTMP clients. |
| @@ -47,12 +48,14 @@ private: | @@ -47,12 +48,14 @@ private: | ||
| 47 | SrsRequest* req; | 48 | SrsRequest* req; |
| 48 | SrsResponse* res; | 49 | SrsResponse* res; |
| 49 | SrsRtmp* rtmp; | 50 | SrsRtmp* rtmp; |
| 51 | + SrsRefer* refer; | ||
| 50 | public: | 52 | public: |
| 51 | SrsClient(SrsServer* srs_server, st_netfd_t client_stfd); | 53 | SrsClient(SrsServer* srs_server, st_netfd_t client_stfd); |
| 52 | virtual ~SrsClient(); | 54 | virtual ~SrsClient(); |
| 53 | protected: | 55 | protected: |
| 54 | virtual int do_cycle(); | 56 | virtual int do_cycle(); |
| 55 | private: | 57 | private: |
| 58 | + virtual int check_vhost(); | ||
| 56 | virtual int playing(SrsSource* source); | 59 | virtual int playing(SrsSource* source); |
| 57 | virtual int publish(SrsSource* source, bool is_fmle); | 60 | virtual int publish(SrsSource* source, bool is_fmle); |
| 58 | virtual int get_peer_ip(); | 61 | virtual int get_peer_ip(); |
| @@ -102,6 +102,33 @@ SrsConfDirective::~SrsConfDirective() | @@ -102,6 +102,33 @@ SrsConfDirective::~SrsConfDirective() | ||
| 102 | directives.clear(); | 102 | directives.clear(); |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | +std::string SrsConfDirective::arg0() | ||
| 106 | +{ | ||
| 107 | + if (args.size() > 0) { | ||
| 108 | + return args.at(0); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + return ""; | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +std::string SrsConfDirective::arg1() | ||
| 115 | +{ | ||
| 116 | + if (args.size() > 1) { | ||
| 117 | + return args.at(1); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + return ""; | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +std::string SrsConfDirective::arg2() | ||
| 124 | +{ | ||
| 125 | + if (args.size() > 2) { | ||
| 126 | + return args.at(2); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + return ""; | ||
| 130 | +} | ||
| 131 | + | ||
| 105 | SrsConfDirective* SrsConfDirective::at(int index) | 132 | SrsConfDirective* SrsConfDirective::at(int index) |
| 106 | { | 133 | { |
| 107 | return directives.at(index); | 134 | return directives.at(index); |
| @@ -437,6 +464,73 @@ int Config::parse_options(int argc, char** argv) | @@ -437,6 +464,73 @@ int Config::parse_options(int argc, char** argv) | ||
| 437 | return ret; | 464 | return ret; |
| 438 | } | 465 | } |
| 439 | 466 | ||
| 467 | +SrsConfDirective* Config::get_vhost(std::string vhost) | ||
| 468 | +{ | ||
| 469 | + srs_assert(root); | ||
| 470 | + | ||
| 471 | + for (int i = 0; i < (int)root->directives.size(); i++) { | ||
| 472 | + SrsConfDirective* conf = root->at(i); | ||
| 473 | + | ||
| 474 | + if (conf->name != "vhost") { | ||
| 475 | + continue; | ||
| 476 | + } | ||
| 477 | + | ||
| 478 | + if (conf->arg0() == vhost) { | ||
| 479 | + return conf; | ||
| 480 | + } | ||
| 481 | + } | ||
| 482 | + | ||
| 483 | + if (vhost != RTMP_VHOST_DEFAULT) { | ||
| 484 | + return get_vhost(RTMP_VHOST_DEFAULT); | ||
| 485 | + } | ||
| 486 | + | ||
| 487 | + return NULL; | ||
| 488 | +} | ||
| 489 | + | ||
| 490 | +SrsConfDirective* Config::get_gop_cache(std::string vhost) | ||
| 491 | +{ | ||
| 492 | + SrsConfDirective* conf = get_vhost(vhost); | ||
| 493 | + | ||
| 494 | + if (!conf) { | ||
| 495 | + return NULL; | ||
| 496 | + } | ||
| 497 | + | ||
| 498 | + return conf->get("gop_cache"); | ||
| 499 | +} | ||
| 500 | + | ||
| 501 | +SrsConfDirective* Config::get_refer(std::string vhost) | ||
| 502 | +{ | ||
| 503 | + SrsConfDirective* conf = get_vhost(vhost); | ||
| 504 | + | ||
| 505 | + if (!conf) { | ||
| 506 | + return NULL; | ||
| 507 | + } | ||
| 508 | + | ||
| 509 | + return conf->get("refer"); | ||
| 510 | +} | ||
| 511 | + | ||
| 512 | +SrsConfDirective* Config::get_refer_play(std::string vhost) | ||
| 513 | +{ | ||
| 514 | + SrsConfDirective* conf = get_vhost(vhost); | ||
| 515 | + | ||
| 516 | + if (!conf) { | ||
| 517 | + return NULL; | ||
| 518 | + } | ||
| 519 | + | ||
| 520 | + return conf->get("refer_play"); | ||
| 521 | +} | ||
| 522 | + | ||
| 523 | +SrsConfDirective* Config::get_refer_publish(std::string vhost) | ||
| 524 | +{ | ||
| 525 | + SrsConfDirective* conf = get_vhost(vhost); | ||
| 526 | + | ||
| 527 | + if (!conf) { | ||
| 528 | + return NULL; | ||
| 529 | + } | ||
| 530 | + | ||
| 531 | + return conf->get("refer_publish"); | ||
| 532 | +} | ||
| 533 | + | ||
| 440 | SrsConfDirective* Config::get_listen() | 534 | SrsConfDirective* Config::get_listen() |
| 441 | { | 535 | { |
| 442 | return root->get("listen"); | 536 | return root->get("listen"); |
| @@ -32,6 +32,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -32,6 +32,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 32 | #include <vector> | 32 | #include <vector> |
| 33 | #include <string> | 33 | #include <string> |
| 34 | 34 | ||
| 35 | +// default vhost for rtmp | ||
| 36 | +#define RTMP_VHOST_DEFAULT "__defaultVhost__" | ||
| 37 | + | ||
| 38 | +// conf node: enabled. | ||
| 39 | +#define RTMP_VHOST_ENABLED "enabled" | ||
| 40 | + | ||
| 35 | class SrsFileBuffer | 41 | class SrsFileBuffer |
| 36 | { | 42 | { |
| 37 | public: | 43 | public: |
| @@ -61,6 +67,9 @@ public: | @@ -61,6 +67,9 @@ public: | ||
| 61 | public: | 67 | public: |
| 62 | SrsConfDirective(); | 68 | SrsConfDirective(); |
| 63 | virtual ~SrsConfDirective(); | 69 | virtual ~SrsConfDirective(); |
| 70 | + std::string arg0(); | ||
| 71 | + std::string arg1(); | ||
| 72 | + std::string arg2(); | ||
| 64 | SrsConfDirective* at(int index); | 73 | SrsConfDirective* at(int index); |
| 65 | SrsConfDirective* get(std::string _name); | 74 | SrsConfDirective* get(std::string _name); |
| 66 | public: | 75 | public: |
| @@ -87,6 +96,11 @@ public: | @@ -87,6 +96,11 @@ public: | ||
| 87 | virtual ~Config(); | 96 | virtual ~Config(); |
| 88 | public: | 97 | public: |
| 89 | virtual int parse_options(int argc, char** argv); | 98 | virtual int parse_options(int argc, char** argv); |
| 99 | + virtual SrsConfDirective* get_vhost(std::string vhost); | ||
| 100 | + virtual SrsConfDirective* get_gop_cache(std::string vhost); | ||
| 101 | + virtual SrsConfDirective* get_refer(std::string vhost); | ||
| 102 | + virtual SrsConfDirective* get_refer_play(std::string vhost); | ||
| 103 | + virtual SrsConfDirective* get_refer_publish(std::string vhost); | ||
| 90 | virtual SrsConfDirective* get_listen(); | 104 | virtual SrsConfDirective* get_listen(); |
| 91 | private: | 105 | private: |
| 92 | virtual int parse_argv(int& i, char** argv); | 106 | virtual int parse_argv(int& i, char** argv); |
| @@ -65,6 +65,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | @@ -65,6 +65,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 65 | #define ERROR_RTMP_TRY_SIMPLE_HS 311 | 65 | #define ERROR_RTMP_TRY_SIMPLE_HS 311 |
| 66 | #define ERROR_RTMP_CH_SCHEMA 312 | 66 | #define ERROR_RTMP_CH_SCHEMA 312 |
| 67 | #define ERROR_RTMP_PACKET_SIZE 313 | 67 | #define ERROR_RTMP_PACKET_SIZE 313 |
| 68 | +#define ERROR_RTMP_VHOST_NOT_FOUND 314 | ||
| 69 | +#define ERROR_RTMP_ACCESS_DENIED 315 | ||
| 68 | 70 | ||
| 69 | #define ERROR_SYSTEM_STREAM_INIT 400 | 71 | #define ERROR_SYSTEM_STREAM_INIT 400 |
| 70 | #define ERROR_SYSTEM_PACKET_INVALID 401 | 72 | #define ERROR_SYSTEM_PACKET_INVALID 401 |
trunk/src/core/srs_core_refer.cpp
0 → 100755
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013 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_core_refer.hpp> | ||
| 25 | + | ||
| 26 | +#include <srs_core_error.hpp> | ||
| 27 | +#include <srs_core_log.hpp> | ||
| 28 | +#include <srs_core_config.hpp> | ||
| 29 | + | ||
| 30 | +int SrsRefer::check(std::string page_url, SrsConfDirective* refer) | ||
| 31 | +{ | ||
| 32 | + int ret = ERROR_SUCCESS; | ||
| 33 | + | ||
| 34 | + if (!refer) { | ||
| 35 | + srs_verbose("ignore refer check for page_url=%s", page_url.c_str()); | ||
| 36 | + return ret; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + for (int i = 0; i < (int)refer->args.size(); i++) { | ||
| 40 | + if ((ret = check_single_refer(page_url, refer->args.at(i))) == ERROR_SUCCESS) { | ||
| 41 | + srs_verbose("check refer success. page_url=%s, refer=%s", | ||
| 42 | + page_url.c_str(), refer->args.at(i).c_str()); | ||
| 43 | + return ret; | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + ret = ERROR_RTMP_ACCESS_DENIED; | ||
| 48 | + srs_error("check refer failed. ret=%d", ret); | ||
| 49 | + | ||
| 50 | + return ret; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +int SrsRefer::check_single_refer(std::string page_url, std::string refer) | ||
| 54 | +{ | ||
| 55 | + int ret = ERROR_SUCCESS; | ||
| 56 | + | ||
| 57 | + size_t pos = std::string::npos; | ||
| 58 | + | ||
| 59 | + std::string domain_name = page_url; | ||
| 60 | + if ((pos = domain_name.find("://")) != std::string::npos) { | ||
| 61 | + domain_name = domain_name.substr(pos + 3); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + if ((pos = domain_name.find("/")) != std::string::npos) { | ||
| 65 | + domain_name = domain_name.substr(0, pos); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + if ((pos = domain_name.find(":")) != std::string::npos) { | ||
| 69 | + domain_name = domain_name.substr(0, pos); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + pos = domain_name.find(refer); | ||
| 73 | + if (pos == std::string::npos) { | ||
| 74 | + ret = ERROR_RTMP_ACCESS_DENIED; | ||
| 75 | + } | ||
| 76 | + // match primary domain. | ||
| 77 | + if (pos != domain_name.length() - refer.length()) { | ||
| 78 | + ret = ERROR_RTMP_ACCESS_DENIED; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + if (ret != ERROR_SUCCESS) { | ||
| 82 | + srs_verbose("access denied, page_url=%s, domain_name=%s, refer=%s, ret=%d", | ||
| 83 | + page_url.c_str(), domain_name.c_str(), refer.c_str(), ret); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + return ret; | ||
| 87 | +} | ||
| 88 | + |
trunk/src/core/srs_core_refer.hpp
0 → 100755
| 1 | +/* | ||
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013 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_CORE_REFER_HPP | ||
| 25 | +#define SRS_CORE_REFER_HPP | ||
| 26 | + | ||
| 27 | +/* | ||
| 28 | +#include <srs_core_refer.hpp> | ||
| 29 | +*/ | ||
| 30 | +#include <srs_core.hpp> | ||
| 31 | + | ||
| 32 | +#include <string> | ||
| 33 | + | ||
| 34 | +class SrsConfDirective; | ||
| 35 | + | ||
| 36 | +class SrsRefer | ||
| 37 | +{ | ||
| 38 | +public: | ||
| 39 | + /** | ||
| 40 | + * to check the refer. | ||
| 41 | + * @param page_url the client page url. | ||
| 42 | + * @param refer the refer in config. | ||
| 43 | + */ | ||
| 44 | + virtual int check(std::string page_url, SrsConfDirective* refer); | ||
| 45 | +private: | ||
| 46 | + virtual int check_single_refer(std::string page_url, std::string refer); | ||
| 47 | +}; | ||
| 48 | + | ||
| 49 | +#endif |
| @@ -110,6 +110,8 @@ int SrsRequest::discovery_app() | @@ -110,6 +110,8 @@ int SrsRequest::discovery_app() | ||
| 110 | return ret; | 110 | return ret; |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | + strip(); | ||
| 114 | + | ||
| 113 | return ret; | 115 | return ret; |
| 114 | } | 116 | } |
| 115 | 117 | ||
| @@ -117,8 +119,7 @@ std::string SrsRequest::get_stream_url() | @@ -117,8 +119,7 @@ std::string SrsRequest::get_stream_url() | ||
| 117 | { | 119 | { |
| 118 | std::string url = ""; | 120 | std::string url = ""; |
| 119 | 121 | ||
| 120 | - //url += vhost; | ||
| 121 | - | 122 | + url += vhost; |
| 122 | url += "/"; | 123 | url += "/"; |
| 123 | url += app; | 124 | url += app; |
| 124 | url += "/"; | 125 | url += "/"; |
| @@ -127,6 +128,30 @@ std::string SrsRequest::get_stream_url() | @@ -127,6 +128,30 @@ std::string SrsRequest::get_stream_url() | ||
| 127 | return url; | 128 | return url; |
| 128 | } | 129 | } |
| 129 | 130 | ||
| 131 | +void SrsRequest::strip() | ||
| 132 | +{ | ||
| 133 | + trim(vhost, "/ \n\r\t"); | ||
| 134 | + trim(app, "/ \n\r\t"); | ||
| 135 | + trim(stream, "/ \n\r\t"); | ||
| 136 | +} | ||
| 137 | + | ||
| 138 | +std::string& SrsRequest::trim(std::string& str, std::string chs) | ||
| 139 | +{ | ||
| 140 | + for (int i = 0; i < (int)chs.length(); i++) { | ||
| 141 | + char ch = chs.at(i); | ||
| 142 | + | ||
| 143 | + for (std::string::iterator it = str.begin(); it != str.end();) { | ||
| 144 | + if (ch == *it) { | ||
| 145 | + it = str.erase(it); | ||
| 146 | + } else { | ||
| 147 | + ++it; | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + return str; | ||
| 153 | +} | ||
| 154 | + | ||
| 130 | SrsResponse::SrsResponse() | 155 | SrsResponse::SrsResponse() |
| 131 | { | 156 | { |
| 132 | stream_id = SRS_DEFAULT_SID; | 157 | stream_id = SRS_DEFAULT_SID; |
| @@ -66,6 +66,9 @@ struct SrsRequest | @@ -66,6 +66,9 @@ struct SrsRequest | ||
| 66 | */ | 66 | */ |
| 67 | virtual int discovery_app(); | 67 | virtual int discovery_app(); |
| 68 | virtual std::string get_stream_url(); | 68 | virtual std::string get_stream_url(); |
| 69 | + virtual void strip(); | ||
| 70 | +private: | ||
| 71 | + std::string& trim(std::string& str, std::string chs); | ||
| 69 | }; | 72 | }; |
| 70 | 73 | ||
| 71 | /** | 74 | /** |
| @@ -391,6 +391,19 @@ void SrsSource::on_unpublish() | @@ -391,6 +391,19 @@ void SrsSource::on_unpublish() | ||
| 391 | srs_trace("clear cache/metadata/sequence-headers when unpublish."); | 391 | srs_trace("clear cache/metadata/sequence-headers when unpublish."); |
| 392 | } | 392 | } |
| 393 | 393 | ||
| 394 | +void SrsSource::set_cache(bool enabled) | ||
| 395 | +{ | ||
| 396 | + enable_gop_cache = enabled; | ||
| 397 | + | ||
| 398 | + if (!enabled) { | ||
| 399 | + srs_info("disable gop cache, clear %d packets.", (int)gop_cache.size()); | ||
| 400 | + clear_gop_cache(); | ||
| 401 | + return; | ||
| 402 | + } | ||
| 403 | + | ||
| 404 | + srs_info("enable gop cache"); | ||
| 405 | +} | ||
| 406 | + | ||
| 394 | int SrsSource::cache_last_gop(SrsSharedPtrMessage* msg) | 407 | int SrsSource::cache_last_gop(SrsSharedPtrMessage* msg) |
| 395 | { | 408 | { |
| 396 | int ret = ERROR_SUCCESS; | 409 | int ret = ERROR_SUCCESS; |
| @@ -134,6 +134,7 @@ public: | @@ -134,6 +134,7 @@ public: | ||
| 134 | virtual int create_consumer(SrsConsumer*& consumer); | 134 | virtual int create_consumer(SrsConsumer*& consumer); |
| 135 | virtual void on_consumer_destroy(SrsConsumer* consumer); | 135 | virtual void on_consumer_destroy(SrsConsumer* consumer); |
| 136 | virtual void on_unpublish(); | 136 | virtual void on_unpublish(); |
| 137 | + virtual void set_cache(bool enabled); | ||
| 137 | private: | 138 | private: |
| 138 | /** | 139 | /** |
| 139 | * only for h264 codec | 140 | * only for h264 codec |
| @@ -12,6 +12,8 @@ file | @@ -12,6 +12,8 @@ file | ||
| 12 | ..\core\srs_core_server.cpp, | 12 | ..\core\srs_core_server.cpp, |
| 13 | ..\core\srs_core_config.hpp, | 13 | ..\core\srs_core_config.hpp, |
| 14 | ..\core\srs_core_config.cpp, | 14 | ..\core\srs_core_config.cpp, |
| 15 | + ..\core\srs_core_refer.hpp, | ||
| 16 | + ..\core\srs_core_refer.cpp, | ||
| 15 | ..\core\srs_core_conn.hpp, | 17 | ..\core\srs_core_conn.hpp, |
| 16 | ..\core\srs_core_conn.cpp, | 18 | ..\core\srs_core_conn.cpp, |
| 17 | ..\core\srs_core_client.hpp, | 19 | ..\core\srs_core_client.hpp, |
-
请 注册 或 登录 后发表评论