Remove dependency on range_check
authorJack Allnutt <jack@allnutt.eu>
Mon, 1 Jul 2013 03:02:31 +0000 (04:02 +0100)
committerJack Allnutt <jack@allnutt.eu>
Mon, 1 Jul 2013 20:11:35 +0000 (21:11 +0100)
package.json
server/weblistener.js

index e904b29ed59b9e26b7dce0ab4c02e0c3c65cac40..4d3354a2ed01878ae023a81c1a3734601284b74f 100644 (file)
@@ -16,7 +16,6 @@
     "node-static": "0.5.9",\r
     "uglify-js": "2.3.6",\r
     "socket.io": "0.9.16",\r
-    "range_check": "0.0.1",\r
     "lodash": "0.9.1",\r
     "daemonize2": "0.4.0-rc.5",\r
     "eventemitter2": "0.4.11",\r
index 651d1f69ff424483f3e12e09c99f098b317bc3c3..89a53d80590f08c92a113138a0ae68bff145c084 100644 (file)
@@ -7,10 +7,10 @@ var ws          = require('socket.io'),
     dns         = require('dns'),
     url         = require('url'),
     _           = require('lodash'),
+    ipaddr      = require('ipaddr.js'),
     Client      = require('./client.js').Client,
     HttpHandler = require('./httphandler.js').HttpHandler,
-    rehash      = require('./rehash.js'),
-    range_check = require('range_check');
+    rehash      = require('./rehash.js');
 
 
 
@@ -98,13 +98,25 @@ util.inherits(WebListener, events.EventEmitter);
 
 function handleHttpRequest(request, response) {
     var uri = url.parse(request.url, true);
-    
+
     // If this isn't a socket.io request, pass it onto the http handler
     if (uri.pathname.substr(0, 10) !== '/socket.io') {
         http_handler.serve(request, response);
     }
 }
 
+function rangeCheck(addr, range) {
+    var i, ranges, parts;
+    ranges = (!_.isArray(range)) ? [range] : range;
+    for (i = 0; i < ranges.length; i++) {
+        parts = ranges[i].split('/');
+        if (ipaddr.process(addr).match(ipaddr.process(parts[0]), parts[1])) {
+            return true;
+        }
+    }
+    return false;
+}
+
 
 /**
  * Get the reverse DNS entry for this connection.
@@ -116,7 +128,7 @@ function authoriseConnection(handshakeData, callback) {
     // If a forwarded-for header is found, switch the source address
     if (handshakeData.headers[global.config.http_proxy_ip_header || 'x-forwarded-for']) {
         // Check we're connecting from a whitelisted proxy
-        if (!global.config.http_proxies || !range_check.in_range(address, global.config.http_proxies)) {
+        if (!global.config.http_proxies || !rangeCheck(address, global.config.http_proxies)) {
             console.log('Unlisted proxy:', address);
             callback(null, false);
             return;
@@ -127,7 +139,7 @@ function authoriseConnection(handshakeData, callback) {
     }
 
     handshakeData.real_address = address;
-    
+
     // If enabled, don't go over the connection limit
     if (global.config.max_client_conns && global.config.max_client_conns > 0) {
         if (global.clients.numOnAddress(address) + 1 > global.config.max_client_conns) {
@@ -143,7 +155,7 @@ function authoriseConnection(handshakeData, callback) {
             } else {
                 handshakeData.revdns = _.first(domains) || address;
             }
-            
+
             // All is well, authorise the connection
             callback(null, true);
         });