Reverse proxy support
authorDarren <darren@darrenwhitlen.com>
Wed, 17 Oct 2012 00:15:33 +0000 (01:15 +0100)
committerDarren <darren@darrenwhitlen.com>
Wed, 17 Oct 2012 00:15:33 +0000 (01:15 +0100)
server/app.js
server/config.json

index 2d42e1f98444ecc595713c616c2fb9c0a737d7c6..1f06fc15c38505d36c84cf27e7d29eef4314348f 100644 (file)
@@ -96,6 +96,22 @@ StaticFileServer.prototype.serve = function (request, response) {
         request.url = '/';
     }
 
+    // If a forwarded-for header is found, switch the source address
+    if (request.headers['x-forwarded-for']) {
+        // Check we're connecting from a whitelisted proxy
+        if (!kiwi.config.http_proxies
+            || kiwi.config.http_proxies.indexOf(request.connection.remoteAddress) < 0)
+        {
+            console.log('Unlisted proxy:', request.connection.remoteAddress);
+            response.writeHead(503);
+            response.end();
+            return;
+        }
+
+        // We're sent from a whitelisted proxy, replace the hosts
+        request.connection.remoteAddress = request.headers['x-forwarded-for'];
+    }
+
     this.file_server.serve(request, response, function (err) {
         if (err) {
             response.writeHead(err.status, err.headers);
index b59fd1b415cb68af5216f6f1bdd4966519577a36..39da2011e09c0dc095823718276d43655b6b8211 100755 (executable)
@@ -25,6 +25,7 @@
     "handle_http":      true,
     "public_http":      "./../client/",
     "http_base_path":   "/client",
+    "http_proxies":     ["127.0.0.1"],
 
     "max_client_conns": 2,