mirror of
https://github.com/ossrs/srs.git
synced 2025-02-12 11:21:52 +00:00
For #1488, add test and improve coverage for parsing client ip.
This commit is contained in:
parent
316cab794a
commit
2115d5d22a
8 changed files with 88 additions and 44 deletions
|
@ -49,7 +49,6 @@ using namespace std;
|
|||
#include <srs_kernel_buffer.hpp>
|
||||
#include <srs_protocol_amf0.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_http_stack.hpp>
|
||||
|
||||
// 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 "";
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -26,12 +26,17 @@
|
|||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
// 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.
|
||||
|
|
|
@ -35,6 +35,7 @@ using namespace std;
|
|||
#include <srs_protocol_utility.hpp>
|
||||
#include <srs_core_autofree.hpp>
|
||||
#include <srs_rtmp_stack.hpp>
|
||||
#include <srs_service_conn.hpp>
|
||||
|
||||
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.
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
|
||||
#include <srs_http_stack.hpp>
|
||||
|
||||
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();
|
||||
|
|
|
@ -39,6 +39,7 @@ using namespace std;
|
|||
#include <srs_kernel_consts.hpp>
|
||||
#include <srs_kernel_log.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_http_stack.hpp>
|
||||
|
||||
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 "";
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@
|
|||
|
||||
#include <srs_service_st.hpp>
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ using namespace std;
|
|||
#include <srs_kernel_file.hpp>
|
||||
#include <srs_utest_kernel.hpp>
|
||||
#include <srs_app_http_static.hpp>
|
||||
#include <srs_service_utility.hpp>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue