1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #1488, add test and improve coverage for parsing client ip.

This commit is contained in:
winlin 2019-12-26 12:07:52 +08:00
parent 316cab794a
commit 2115d5d22a
8 changed files with 88 additions and 44 deletions

View file

@ -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.

View file

@ -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.

View file

@ -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();

View file

@ -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 "";
}

View file

@ -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