Skip to content

Commit

Permalink
Fix #1501, use request ip for origin cluster. 3.0.66
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 30, 2019
1 parent 488f16f commit e5285ec
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 31 additions & 17 deletions trunk/src/app/srs_app_coworkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string> 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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_coworkers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/app/srs_app_http_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <srs_auto_headers.hpp>
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/kernel/srs_kernel_consts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/kernel/srs_kernel_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e5285ec

Please sign in to comment.