[PATCH] Use X-Forwarded-For in logs and in listclients

Odin Omdal Hørthe <[email protected]>
Newsgroups gmane.comp.audio.icecast.devel
Message-ID <[email protected]>
In order to show the real ip of proxied clients, I have written a
small patch that fetches and uses X-Forwarded-For instead of IP when
it exists.

Quite obviously this should only be enabled when you know you have a
proxy in front of you.

I haven't looked at how to make configuration options, this first
version[1] is only for enabling that function.


There are different ways this could be used in configuration. Either
as "if IP is this (e.g. <127.0.0.1>)" then look for X-Forwarded-For.
Or just a; "use this header, instead of IP" where you can write either
X-Forwarded-For, or X-Real-IP.

The problem with X-Forwarded-For is that it can be a list, and that
would probably break scripts which only expect one IP. So, on my
server I have actually changed it to X-Real-IP, because I have nginx
sending that out. However, X-Forwarded-For is more normal in default
systems, so I used that in the patch.

Now it's on the internet in case anyone ever searches for this and needs it.


I would be happy to do further work on the patch so that it could be
applied to Icecast.


[1] Well, second, the first version changed admin.c to use the more
sensible if-codes like logger.c. But combining refactoring/cleanup and
function in one patch is not correct.

-- 
Beste helsing,
Odin Hørthe Omdal <[email protected]>
http://velmont.no

_______________________________________________
Icecast-dev mailing list
[email protected]
http://lists.xiph.org/mailman/listinfo/icecast-dev
icecast_use_x-forwarded-for_v2.patch (text/x-patch, 2 KB)
diff -Naur org//admin.c new//admin.c
--- org//admin.c	2010-10-09 14:18:04.514190647 +0200
+++ new//admin.c	2010-10-10 01:35:48.497043001 +0200
@@ -635,7 +635,7 @@
     avl_node *client_node;
     client_t *current;
     char buf[22];
-    const char *userAgent = NULL;
+    const char *userAgent = NULL, *realIP = NULL;
     time_t now = time(NULL);
 
     doc = xmlNewDoc (XMLSTR("1.0"));
@@ -654,7 +654,13 @@
     while(client_node) {
         current = (client_t *)client_node->key;
         listenernode = xmlNewChild(srcnode, NULL, XMLSTR("listener"), NULL);
-        xmlNewChild(listenernode, NULL, XMLSTR("IP"), XMLSTR(current->con->ip));
+        realIP = httpp_getvar(current->parser, "x-forwarded-for");
+        if (realIP) {
+            xmlNewChild(listenernode, NULL, XMLSTR("IP"), XMLSTR(realIP));
+        }
+        else {
+            xmlNewChild(listenernode, NULL, XMLSTR("IP"), XMLSTR(current->con->ip));
+        }
         userAgent = httpp_getvar(current->parser, "user-agent");
         if (userAgent) {
             xmlNewChild(listenernode, NULL, XMLSTR("UserAgent"), XMLSTR(userAgent));
diff -Naur org//logging.c new//logging.c
--- org//logging.c	2010-10-09 14:18:04.514190647 +0200
+++ new//logging.c	2010-10-09 14:18:04.514190647 +0200
@@ -115,7 +115,7 @@
     struct tm thetime;
     time_t now;
     time_t stayed;
-    const char *referrer, *user_agent, *username;
+    const char *referrer, *user_agent, *username, *real_ip;
 
     now = time(NULL);
 
@@ -145,13 +145,17 @@
     if (referrer == NULL)
         referrer = "-";
 
+    real_ip = httpp_getvar (client->parser, "x-forwarded-for");
+    if (real_ip == NULL)
+        real_ip = client->con->ip;
+
     user_agent = httpp_getvar (client->parser, "user-agent");
     if (user_agent == NULL)
         user_agent = "-";
 
     log_write_direct (accesslog,
             "%s - %s [%s] \"%s\" %d %" PRIu64 " \"%s\" \"%s\" %lu",
-            client->con->ip,
+            real_ip,
             username,
             datebuf,
             reqbuf,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.