Proxy endpoint.
authorDavid Schoonover <dsc@wikimedia.org>
Fri, 8 Jun 2012 22:07:49 +0000 (15:07 -0700)
committerDavid Schoonover <dsc@wikimedia.org>
Fri, 8 Jun 2012 22:07:49 +0000 (15:07 -0700)
lib/server/controllers/index.co [new file with mode: 0644]
lib/server/proxy.co [new file with mode: 0644]
lib/server/server.co
package.co
package.json
test/test-proxy-server.co [new file with mode: 0755]

diff --git a/lib/server/controllers/index.co b/lib/server/controllers/index.co
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/lib/server/proxy.co b/lib/server/proxy.co
new file mode 100644 (file)
index 0000000..15349a5
--- /dev/null
@@ -0,0 +1,34 @@
+_         = require 'underscore'
+url       = require 'url'
+minimatch = require 'minimatch'
+request   = require 'request'
+
+
+matchesList = (list, value) ->
+    for pat of list
+        return true if pat(value)
+    false
+
+ProxyMiddleware = (options={}) ->
+    {whitelist, blacklist} = options = {whitelist:[], blacklist:[], ...options}
+    whitelist .= map -> minimatch.filter it, {+nocase}
+    blacklist .= map -> minimatch.filter it, {+nocase}
+    
+    return (req, res) ->
+        targetUrl = ( req.params.url or url.parse(req.url).pathname.slice(3) ).trim()
+        return res.send {error:'URL required'}, 400 unless targetUrl
+        
+        targetUrl = "http://#targetUrl" unless /^https?:\/\//.test targetUrl
+        target = url.parse targetUrl, true, true
+        
+        if matchesList blacklist, target.hostname
+            return res.send {error:'Domain is blacklisted'}, 403
+        unless matchesList whitelist, target.hostname
+            return res.send {error:'Domain is not whitelisted'}, 403
+        
+        console.log "[Proxy] #targetUrl"
+        request.get targetUrl .pipe res
+
+
+module.exports = exports = ProxyMiddleware
+exports.ProxyMiddleware = ProxyMiddleware
index 9ee2a1f..67bc809 100755 (executable)
@@ -180,10 +180,30 @@ app.get '/datasources/all', (req, res, next) ->
 app.controller require './controllers/datasource'
 
 
+# Data Proxy
+PROXY_DOMAIN_BLACKLIST = <[
+    *.ru
+]>
+PROXY_DOMAIN_WHITELIST = <[
+    wmflabs.org mediawiki.org wikimedia.org wikipedia.org
+    *.wmflabs.org *.mediawiki.org *.wikimedia.org *.wikipedia.org
+]>
+
+if NODE_ENV is 'dev'
+    PROXY_DOMAIN_WHITELIST.push 'localhost', 'github.com', '*.github.com'
+
+
+proxy = require('./proxy')({
+    blacklist: PROXY_DOMAIN_BLACKLIST
+    whitelist: PROXY_DOMAIN_WHITELIST
+})
+app.get '/x',   proxy
+app.get '/x/*', proxy
+
+
+
 app.get '/', (req, res) ->
-    ### XXX: Temporary, while I implement backwards compat with the old data format
     res.render 'dashboard'
-    # res.redirect '/graphs/ohai/edit'
 
 app.get '/geo', (req, res) ->
     res.render 'geo'
index 3b76b1c..8763d2e 100644 (file)
@@ -10,6 +10,16 @@ author                          : 'David Schoonover <dsc@less.ly> (http://less.l
 
 dependencies                    : 
     'coco'                      : '>= 0.7.0'
+    'request'                   : '>= 2.9.0'
+    'underscore'                : '>= 1.3.3'
+    'underscore.string'         : '>= 2.1.1'
+    'seq'                       : '>= 0.3.5'
+    'hashish'                   : '>= 0.0.4'
+    'js-yaml'                   : '>= 0.3.5'
+    'minimatch'                 : '>= 0.2.5'
+    'glob'                      : '>= 3.1.9'
+    'findit'                    : '>= 0.1.2'
+    'remove'                    : '>= 0.1.1'
     'mime'                      : '>= 1.2.5'
     'express'                   : '>= 2.5.9'
     'express-resource'          : '>= 0.2.4'
@@ -19,15 +29,9 @@ dependencies                    :
     'stylus'                    : '>= 0.25.0'
     'nib'                       : '>= 0.4.0'
     'browserify'                : '>= 1.9.4'
-    'seq'                       : '>= 0.3.5'
-    'underscore'                : '>= 1.3.3'
-    'underscore.string'         : '>= 2.1.1'
-    'js-yaml'                   : '>= 0.3.5'
-    'hashish'                   : '>= 0.0.4'
     'backbone'                  : '>= 0.9.1'
-    'findit'                    : '>= 0.1.2'
-    'glob'                      : '>= 3.1.9'
-    'remove'                    : '>= 0.1.1'
+    'd3'                        : '>= 2.9.2'
+    
 devDependencies                 : 
     'buildtools'                : 'https://github.com/dsc/node-buildtools/tarball/master'
     'expresso'                  : '>= 0.9.2'
index 4f375bb..5009511 100644 (file)
   "author": "David Schoonover <dsc@less.ly> (http://less.ly)",
   "dependencies": {
     "coco": ">= 0.7.0",
+    "request": ">= 2.9.0",
+    "underscore": ">= 1.3.3",
+    "underscore.string": ">= 2.1.1",
+    "seq": ">= 0.3.5",
+    "hashish": ">= 0.0.4",
+    "js-yaml": ">= 0.3.5",
+    "minimatch": ">= 0.2.5",
+    "glob": ">= 3.1.9",
+    "findit": ">= 0.1.2",
+    "remove": ">= 0.1.1",
     "mime": ">= 1.2.5",
     "express": ">= 2.5.9",
     "express-resource": ">= 0.2.4",
     "stylus": ">= 0.25.0",
     "nib": ">= 0.4.0",
     "browserify": ">= 1.9.4",
-    "seq": ">= 0.3.5",
-    "underscore": ">= 1.3.3",
-    "underscore.string": ">= 2.1.1",
-    "js-yaml": ">= 0.3.5",
-    "hashish": ">= 0.0.4",
     "backbone": ">= 0.9.1",
-    "findit": ">= 0.1.2",
-    "glob": ">= 3.1.9",
-    "remove": ">= 0.1.1"
+    "d3": ">= 2.9.2"
   },
   "devDependencies": {
     "buildtools": "https://github.com/dsc/node-buildtools/tarball/master",
@@ -43,7 +46,7 @@
   },
   "repository": {
     "type": "git",
-    "url": "git://git@less.ly:kraken-ui.git"
+    "url": "git://less.ly/kraken-ui.git"
   },
   "engine": {
     "node": ">=0.6.2"
diff --git a/test/test-proxy-server.co b/test/test-proxy-server.co
new file mode 100755 (executable)
index 0000000..c3a4609
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env coco
+express  = require 'express'
+
+app = express.createServer()
+
+PROXY_DOMAIN_BLACKLIST = <[
+    *.ru
+]>
+PROXY_DOMAIN_WHITELIST = <[
+    wmflabs.org mediawiki.org wikimedia.org
+    *.wmflabs.org *.mediawiki.org *.wikimedia.org
+    github.com dsc.github.com mbostock.github.com
+]>
+
+
+proxy = require('kraken/server/proxy')({
+    blacklist: PROXY_DOMAIN_BLACKLIST
+    whitelist: PROXY_DOMAIN_WHITELIST
+})
+app.get '/x',   proxy
+app.get '/x/*', proxy
+
+app.listen 8081