From e5285ecabfcb861ae9b4dba25a27a862a712d278 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 30 Nov 2019 18:50:24 +0800 Subject: [PATCH] Fix #1501, use request ip for origin cluster. 3.0.66 --- README.md | 1 + trunk/src/app/srs_app_coworkers.cpp | 48 ++++++++++++++++--------- trunk/src/app/srs_app_coworkers.hpp | 2 +- trunk/src/app/srs_app_http_api.cpp | 4 +-- trunk/src/core/srs_core.hpp | 2 +- trunk/src/kernel/srs_kernel_consts.hpp | 2 ++ trunk/src/kernel/srs_kernel_utility.cpp | 4 +-- 7 files changed, 40 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 654735080e..f189abc498 100755 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ Please select according to languages: ### V3 changes +* v3.0, 2019-11-30, Fix #1501, use request ip for origin cluster. 3.0.66 * v3.0, 2019-11-30, Random tid for docker. 3.0.65 * v3.0, 2019-11-30, Refine debug info for edge. 3.0.64 * v3.0, 2019-10-30, Cover protocol stack RTMP. 3.0.63 diff --git a/trunk/src/app/srs_app_coworkers.cpp b/trunk/src/app/srs_app_coworkers.cpp index be9cba5d06..48338f6a07 100644 --- a/trunk/src/app/srs_app_coworkers.cpp +++ b/trunk/src/app/srs_app_coworkers.cpp @@ -58,30 +58,44 @@ SrsCoWorkers* SrsCoWorkers::instance() return _instance; } -SrsJsonAny* SrsCoWorkers::dumps(string vhost, string app, string stream) +SrsJsonAny* SrsCoWorkers::dumps(string vhost, string host, string app, string stream) { SrsRequest* r = find_stream_info(vhost, app, stream); if (!r) { // TODO: FIXME: Find stream from our origin util return to the start point. return SrsJsonAny::null(); } - - vector service_ports = _srs_config->get_listens(); - if (service_ports.empty()) { - return SrsJsonAny::null(); + + // The service port parsing from listen port. + string listen_host; + int listen_port = SRS_CONSTS_RTMP_DEFAULT_PORT; + vector listen_hostports = _srs_config->get_listens(); + if (!listen_hostports.empty()) { + string list_hostport = listen_hostports.at(0); + + if (list_hostport.find(":") != string::npos) { + srs_parse_hostport(list_hostport, listen_host, listen_port); + } else { + listen_port = ::atoi(list_hostport.c_str()); + } } - - string service_ip = srs_get_public_internet_address(); - string service_hostport = service_ports.at(0); - - int service_port = SRS_CONSTS_RTMP_DEFAULT_PORT; - if (service_hostport.find(":") != string::npos) { - string service_host; - srs_parse_hostport(service_hostport, service_host, service_port); - } else { - service_port = ::atoi(service_hostport.c_str()); + + // The ip of server, we use the request host as ip, if listen host is localhost or loopback. + // For example, the server may behind a NAT(192.x.x.x), while its ip is a docker ip(172.x.x.x), + // we should use the NAT(192.x.x.x) address as it's the exposed ip. + // @see https://github.com/ossrs/srs/issues/1501 + string service_ip; + if (listen_host != SRS_CONSTS_LOCALHOST && listen_host != SRS_CONSTS_LOOPBACK && listen_host != SRS_CONSTS_LOOPBACK6) { + service_ip = listen_host; } - + if (service_ip.empty()) { + service_ip = host; + } + if (service_ip.empty()) { + service_ip = srs_get_public_internet_address(); + } + + // The backend API endpoint. string backend = _srs_config->get_http_api_listen(); if (backend.find(":") == string::npos) { backend = service_ip + ":" + backend; @@ -92,7 +106,7 @@ SrsJsonAny* SrsCoWorkers::dumps(string vhost, string app, string stream) return SrsJsonAny::object() ->set("ip", SrsJsonAny::str(service_ip.c_str())) - ->set("port", SrsJsonAny::integer(service_port)) + ->set("port", SrsJsonAny::integer(listen_port)) ->set("vhost", SrsJsonAny::str(r->vhost.c_str())) ->set("api", SrsJsonAny::str(backend.c_str())) ->set("routers", routers); diff --git a/trunk/src/app/srs_app_coworkers.hpp b/trunk/src/app/srs_app_coworkers.hpp index b7be73dcae..23094ddb66 100644 --- a/trunk/src/app/srs_app_coworkers.hpp +++ b/trunk/src/app/srs_app_coworkers.hpp @@ -46,7 +46,7 @@ class SrsCoWorkers public: static SrsCoWorkers* instance(); public: - virtual SrsJsonAny* dumps(std::string vhost, std::string app, std::string stream); + virtual SrsJsonAny* dumps(std::string vhost, std::string host, std::string app, std::string stream); private: virtual SrsRequest* find_stream_info(std::string vhost, std::string app, std::string stream); public: diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index a41d74d01a..9eb7ed2cac 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -1297,7 +1297,7 @@ srs_error_t SrsGoApiClusters::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMess ->set("stream", SrsJsonAny::str(stream.c_str()))); SrsCoWorkers* coworkers = SrsCoWorkers::instance(); - data->set("origin", coworkers->dumps(vhost, app, stream)); + data->set("origin", coworkers->dumps(vhost, ip, app, stream)); return srs_api_response(w, r, obj->dumps()); } @@ -1342,7 +1342,7 @@ srs_error_t SrsHttpApi::do_cycle() { srs_error_t err = srs_success; - srs_trace("api get peer ip success. ip=%s", ip.c_str()); + srs_trace("API server client, ip=%s", ip.c_str()); // initialize parser if ((err = parser->initialize(HTTP_REQUEST, true)) != srs_success) { diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 8fdfe44a66..a25f6b0962 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -27,7 +27,7 @@ // The version config. #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define VERSION_REVISION 65 +#define VERSION_REVISION 66 // The macros generated by configure script. #include diff --git a/trunk/src/kernel/srs_kernel_consts.hpp b/trunk/src/kernel/srs_kernel_consts.hpp index bb6d852921..8df891fd26 100644 --- a/trunk/src/kernel/srs_kernel_consts.hpp +++ b/trunk/src/kernel/srs_kernel_consts.hpp @@ -116,6 +116,8 @@ /////////////////////////////////////////////////////////// #define SRS_CONSTS_NULL_FILE "/dev/null" #define SRS_CONSTS_LOCALHOST "127.0.0.1" +#define SRS_CONSTS_LOOPBACK "0.0.0.0" +#define SRS_CONSTS_LOOPBACK6 "::" // The signal defines. // To reload the config file and apply new config. diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 93ea6a8951..9a2b7c46c9 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -231,9 +231,9 @@ string srs_any_address_for_listener() } if (ipv6_active && !ipv4_active) { - return "::"; + return SRS_CONSTS_LOOPBACK6; } - return "0.0.0.0"; + return SRS_CONSTS_LOOPBACK; } void srs_parse_endpoint(string hostport, string& ip, int& port)