From: David Schoonover Date: Fri, 8 Jun 2012 22:07:49 +0000 (-0700) Subject: Proxy endpoint. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=beda1ef86440533f281334b8b08b81872bfd8c1c;p=kraken-ui.git Proxy endpoint. --- diff --git a/lib/server/controllers/index.co b/lib/server/controllers/index.co new file mode 100644 index 0000000..e69de29 diff --git a/lib/server/proxy.co b/lib/server/proxy.co new file mode 100644 index 0000000..15349a5 --- /dev/null +++ b/lib/server/proxy.co @@ -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 diff --git a/lib/server/server.co b/lib/server/server.co index 9ee2a1f..67bc809 100755 --- a/lib/server/server.co +++ b/lib/server/server.co @@ -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' diff --git a/package.co b/package.co index 3b76b1c..8763d2e 100644 --- a/package.co +++ b/package.co @@ -10,6 +10,16 @@ author : 'David Schoonover (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' diff --git a/package.json b/package.json index 4f375bb..5009511 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,16 @@ "author": "David Schoonover (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", @@ -23,15 +33,8 @@ "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 index 0000000..c3a4609 --- /dev/null +++ b/test/test-proxy-server.co @@ -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