diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index c3c6a6960..9c6343860 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -49,7 +49,6 @@ using namespace std; #include #include #include -#include // the longest time to wait for a process to quit. #define SRS_PROCESS_QUIT_TIMEOUT_MS 1000 @@ -1274,34 +1273,3 @@ void srs_api_dump_summaries(SrsJsonObject* obj) sys->set("conn_srs", SrsJsonAny::integer(nrs->nb_conn_srs)); } -string srs_get_original_ip(ISrsHttpMessage* r) -{ - string x_forwarded_for, x_real_ip; - for (int i = 0; i < r->request_header_count(); i++) { - string k = r->request_header_key_at(i); - if (k == "X-Forwarded-For") { - x_forwarded_for = r->request_header_value_at(i); - } else if (k == "X-Real-IP") { - x_real_ip = r->request_header_value_at(i); - } - } - - if (!x_forwarded_for.empty()) { - size_t pos = string::npos; - if ((pos = x_forwarded_for.find(",")) == string::npos) { - return x_forwarded_for; - } - return x_forwarded_for.substr(0, pos); - } - - if (!x_real_ip.empty()) { - size_t pos = string::npos; - if ((pos = x_real_ip.find(":")) == string::npos) { - return x_real_ip; - } - return x_real_ip.substr(0, pos); - } - - return ""; -} - diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index d4a8c64e3..5e4d09afe 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -40,7 +40,6 @@ class SrsKbps; class SrsBuffer; class SrsJsonObject; -class ISrsHttpMessage; // Convert level in string to log level in int. // @return the log level defined in SrsLogLevel. @@ -650,8 +649,5 @@ extern bool srs_is_boolean(const std::string& str); // Dump summaries for /api/v1/summaries. extern void srs_api_dump_summaries(SrsJsonObject* obj); -// Get the original ip from query and header by proxy. -extern std::string srs_get_original_ip(ISrsHttpMessage* r); - #endif diff --git a/trunk/src/service/srs_service_conn.hpp b/trunk/src/service/srs_service_conn.hpp index 02841a260..6a7308f00 100644 --- a/trunk/src/service/srs_service_conn.hpp +++ b/trunk/src/service/srs_service_conn.hpp @@ -26,12 +26,17 @@ #include +#include + // The connection interface for all HTTP/RTMP/RTSP object. class ISrsConnection { public: ISrsConnection(); virtual ~ISrsConnection(); +public: + // Get remote ip address. + virtual std::string remote_ip() = 0; }; // The manager for connection. diff --git a/trunk/src/service/srs_service_http_conn.cpp b/trunk/src/service/srs_service_http_conn.cpp index 3189eb838..2d0cb3ba2 100644 --- a/trunk/src/service/srs_service_http_conn.cpp +++ b/trunk/src/service/srs_service_http_conn.cpp @@ -35,6 +35,7 @@ using namespace std; #include #include #include +#include SrsHttpParser::SrsHttpParser() { @@ -372,12 +373,12 @@ srs_error_t SrsHttpMessage::set_url(string url, bool allow_jsonp) return err; } -SrsConnection* SrsHttpMessage::connection() +ISrsConnection* SrsHttpMessage::connection() { return owner_conn; } -void SrsHttpMessage::set_connection(SrsConnection* conn) +void SrsHttpMessage::set_connection(ISrsConnection* conn) { owner_conn = conn; } @@ -628,8 +629,8 @@ SrsRequest* SrsHttpMessage::to_request(string vhost) } // Set ip by remote ip of connection. - if (conn) { - req->ip = conn->remote_ip(); + if (owner_conn) { + req->ip = owner_conn->remote_ip(); } // Overwrite by ip from proxy. diff --git a/trunk/src/service/srs_service_http_conn.hpp b/trunk/src/service/srs_service_http_conn.hpp index f8eaaa23f..e93b2fa48 100644 --- a/trunk/src/service/srs_service_http_conn.hpp +++ b/trunk/src/service/srs_service_http_conn.hpp @@ -30,7 +30,7 @@ #include -class SrsConnection; +class ISrsConnection; class SrsFastStream; class SrsRequest; class ISrsReader; @@ -103,7 +103,7 @@ private: bool infinite_chunked; // Use a buffer to read and send ts file. // The transport connection, can be NULL. - SrsConnection* owner_conn; + ISrsConnection* owner_conn; private: uint8_t _method; uint16_t _status; @@ -143,8 +143,8 @@ public: virtual srs_error_t set_url(std::string url, bool allow_jsonp); public: // Get the owner connection, maybe NULL. - virtual SrsConnection* connection(); - virtual void set_connection(SrsConnection* conn); + virtual ISrsConnection* connection(); + virtual void set_connection(ISrsConnection* conn); public: virtual uint8_t method(); virtual uint16_t status_code(); diff --git a/trunk/src/service/srs_service_utility.cpp b/trunk/src/service/srs_service_utility.cpp index e6312d272..6c140dd1b 100644 --- a/trunk/src/service/srs_service_utility.cpp +++ b/trunk/src/service/srs_service_utility.cpp @@ -39,6 +39,7 @@ using namespace std; #include #include #include +#include bool srs_string_is_http(string url) { @@ -326,3 +327,28 @@ string srs_get_public_internet_address() return ""; } +string srs_get_original_ip(ISrsHttpMessage* r) +{ + SrsHttpHeader* h = r->header(); + + string x_forwarded_for = h->get("X-Forwarded-For"); + if (!x_forwarded_for.empty()) { + size_t pos = string::npos; + if ((pos = x_forwarded_for.find(",")) == string::npos) { + return x_forwarded_for; + } + return x_forwarded_for.substr(0, pos); + } + + string x_real_ip = h->get("X-Real-IP"); + if (!x_real_ip.empty()) { + size_t pos = string::npos; + if ((pos = x_real_ip.find(":")) == string::npos) { + return x_real_ip; + } + return x_real_ip.substr(0, pos); + } + + return ""; +} + diff --git a/trunk/src/service/srs_service_utility.hpp b/trunk/src/service/srs_service_utility.hpp index d9896c13d..70b56547e 100644 --- a/trunk/src/service/srs_service_utility.hpp +++ b/trunk/src/service/srs_service_utility.hpp @@ -32,6 +32,8 @@ #include +class ISrsHttpMessage; + // Whether the url is starts with http:// or https:// extern bool srs_string_is_http(std::string url); extern bool srs_string_is_rtmp(std::string url); @@ -58,5 +60,8 @@ extern std::string srs_get_public_internet_address(); extern bool srs_net_device_is_internet(std::string ifname); extern bool srs_net_device_is_internet(const sockaddr* addr); +// Get the original ip from query and header by proxy. +extern std::string srs_get_original_ip(ISrsHttpMessage* r); + #endif diff --git a/trunk/src/utest/srs_utest_http.cpp b/trunk/src/utest/srs_utest_http.cpp index 4ad2cbe68..508ac3666 100644 --- a/trunk/src/utest/srs_utest_http.cpp +++ b/trunk/src/utest/srs_utest_http.cpp @@ -33,6 +33,7 @@ using namespace std; #include #include #include +#include class MockMSegmentsReader : public ISrsReader { @@ -1677,3 +1678,45 @@ VOID TEST(ProtocolHTTPTest, HTTPMessageUpdate) EXPECT_EQ(-1, m.content_length()); } } + +VOID TEST(ProtocolHTTPTest, GetOriginalIP) +{ + if (true) { + SrsHttpHeader h; + h.set("X-Forwarded-For", "10.11.12.13"); + + SrsHttpMessage m; + m.set_header(&h, false); + + EXPECT_STREQ("10.11.12.13", srs_get_original_ip(&m).c_str()); + } + + if (true) { + SrsHttpHeader h; + h.set("X-Forwarded-For", "192.193.194.195,10.11.12.13"); + + SrsHttpMessage m; + m.set_header(&h, false); + + EXPECT_STREQ("192.193.194.195", srs_get_original_ip(&m).c_str()); + } + + if (true) { + SrsHttpHeader h; + h.set("X-Real-IP", "172.14.42.78"); + + SrsHttpMessage m; + m.set_header(&h, false); + + EXPECT_STREQ("172.14.42.78", srs_get_original_ip(&m).c_str()); + } + + if (true) { + SrsHttpHeader h; + + SrsHttpMessage m; + m.set_header(&h, false); + + EXPECT_STREQ("", srs_get_original_ip(&m).c_str()); + } +}