From 08f21d8b878b6c1490e6d6bb3d44aa640a88e9ca Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sat, 4 Jul 2020 17:56:03 +0200 Subject: Factorize how snuffleupagus gets client's ip addr --- src/sp_utils.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/sp_utils.c') diff --git a/src/sp_utils.c b/src/sp_utils.c index 40cf44e..1bd37fe 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c @@ -7,6 +7,21 @@ bool sp_zend_string_equals(const zend_string* s1, const zend_string* s2) { !memcmp(ZSTR_VAL(s1), ZSTR_VAL(s2), ZSTR_LEN(s1)); } +static const char* default_ipaddr = "0.0.0.0"; +const char* get_ipaddr() { + const char* client_ip = getenv("REMOTE_ADDR"); + if (client_ip) { + return client_ip; + } + + const char* fwd_ip = getenv("HTTP_X_FORWARDED_FOR"); + if (fwd_ip) { + return fwd_ip; + } + + return default_ipaddr; +} + void sp_log_msg(char const* feature, int type, const char* fmt, ...) { char* msg; va_list args; @@ -15,10 +30,7 @@ void sp_log_msg(char const* feature, int type, const char* fmt, ...) { vspprintf(&msg, 0, fmt, args); va_end(args); - const char *client_ip = getenv("REMOTE_ADDR"); - if (!client_ip) { - client_ip = "0.0.0.0"; - } + const char* client_ip = get_ipaddr(); switch (SNUFFLEUPAGUS_G(config).log_media) { case SP_SYSLOG: openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); -- cgit v1.3