telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit ca4e752c35e8a6254d85fae156cc256f2658de2a
parent 2bd72dfe1fbd08d31f124e3f92f1a3771a2b1da6
Author: Frederic Cambus <fred@statdns.com>
Date:   Thu,  4 Oct 2018 23:16:15 +0200

Handle the 'X-Forwarded-For' HTTP header if present

Diffstat:
Msrc/ip.c | 11+++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/ip.c b/src/ip.c @@ -22,7 +22,8 @@ int ip(struct http_request *); int ip(struct http_request *req) { - char addr[INET6_ADDRSTRLEN]; + const char *visitor_ip; + char *ip, addr[INET6_ADDRSTRLEN]; if (req->owner->addrtype == AF_INET) { inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv4.sin_addr), addr, sizeof(addr)); @@ -30,7 +31,13 @@ ip(struct http_request *req) inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv6.sin6_addr), addr, sizeof(addr)); } - http_response(req, 200, addr, strlen(addr)); + if (http_request_header(req, "X-Forwarded-For", &visitor_ip)) { + ip = kore_strdup(visitor_ip); + } else { + ip = addr; + } + + http_response(req, 200, ip, strlen(ip)); return (KORE_RESULT_OK); }