From 09b65aff96c8d83a16bf1e7d9e9fac1660dad990 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 26 Dec 2019 10:37:16 +0800 Subject: [PATCH 1/2] For #1488, pass client ip to http callback. 2.0.269 --- README.md | 1 + trunk/src/app/srs_app_conn.cpp | 6 ++++++ trunk/src/app/srs_app_conn.hpp | 2 ++ trunk/src/app/srs_app_http_conn.cpp | 5 +++++ trunk/src/core/srs_core.hpp | 2 +- 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c282c30d8..da8dfbc7a 100755 --- a/README.md +++ b/README.md @@ -338,6 +338,7 @@ Remark: ## History +* v2.0, 2019-12-26, For [#1488][bug #1488], pass client ip to http callback. 2.0.269 * v2.0, 2019-12-23, Fix [srs-librtmp #22](https://github.com/ossrs/srs-librtmp/issues/22), parse vhost splited by single seperator. 2.0.268 * v2.0, 2019-12-23, Fix [srs-librtmp #25](https://github.com/ossrs/srs-librtmp/issues/25), build srs-librtmp on windows. 2.0.267 * v2.0, 2019-12-13, Support openssl versions greater than 1.1.0. 2.0.266 diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 06858e94b..2ee282681 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -23,6 +23,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +using namespace std; + #include #include #include @@ -117,6 +119,10 @@ int SrsConnection::srs_id() return id; } +string SrsConnection::remote_ip() { + return ip; +} + void SrsConnection::expire() { expired = true; diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index 0c12a5e0c..29b956c8c 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -130,6 +130,8 @@ public: * get the srs id which identify the client. */ virtual int srs_id(); + // Get the remote ip of peer. + virtual std::string remote_ip(); /** * set connection to expired. */ diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 0a94def33..ccc4db4c7 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -870,6 +870,11 @@ SrsRequest* SrsHttpMessage::to_request(string vhost) srs_discovery_tc_url(req->tcUrl, req->schema, req->host, req->vhost, req->app, req->stream, req->port, req->param); req->as_http(); + + // Set ip by remote ip of connection. + if (conn) { + req->ip = conn->remote_ip(); + } return req; } diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 68600f5e5..bb1461400 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 268 +#define VERSION_REVISION 269 // generated by configure, only macros. #include From d36773088314d5fed5940c620480a5d991ff1226 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 26 Dec 2019 11:33:26 +0800 Subject: [PATCH 2/2] For #1488, support parsing original ip from header for proxy --- trunk/src/app/srs_app_http_conn.cpp | 6 ++++++ trunk/src/app/srs_app_utility.cpp | 32 +++++++++++++++++++++++++++++ trunk/src/app/srs_app_utility.hpp | 4 ++++ 3 files changed, 42 insertions(+) diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index ccc4db4c7..2813a70e4 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -875,6 +875,12 @@ SrsRequest* SrsHttpMessage::to_request(string vhost) if (conn) { req->ip = conn->remote_ip(); } + + // Overwrite by ip from proxy. + string oip = srs_get_original_ip(this); + if (!oip.empty()) { + req->ip = oip; + } return req; } diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index a64587fa3..cf5aabdcc 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -47,6 +47,7 @@ using namespace std; #include #include #include +#include // the longest time to wait for a process to quit. #define SRS_PROCESS_QUIT_TIMEOUT_MS 1000 @@ -1541,3 +1542,34 @@ void srs_api_dump_summaries(std::stringstream& ss) << SRS_JOBJECT_END; } +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 87340a24b..208349474 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -41,6 +41,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class SrsKbps; class SrsStream; +class ISrsHttpMessage; // client open socket and connect to server. extern int srs_socket_connect(std::string server, int port, int64_t timeout, st_netfd_t* pstfd); @@ -689,5 +690,8 @@ extern bool srs_is_boolean(const std::string& str); // dump summaries for /api/v1/summaries. extern void srs_api_dump_summaries(std::stringstream& ss); +// Get the original ip from query and header by proxy. +extern std::string srs_get_original_ip(ISrsHttpMessage* r); + #endif