added dpd remote command

This commit is contained in:
Ritchie Martori
2012-07-06 12:30:49 -07:00
parent 0af9bd0c20
commit 6d03aa2b84
9 changed files with 83 additions and 23 deletions

12
bin/dpd
View File

@@ -10,7 +10,8 @@ var program = require('commander')
, shelljs = require('shelljs/global')
, mongod = require('../lib/util/mongod')
, path = require('path')
, tty = require('tty');
, tty = require('tty')
, remote = require('../lib/client/remote');
/**
* Get the version number from the package.json
@@ -95,7 +96,7 @@ program
.description('generate a key for remote access')
.action(function() {
var Keys = require('../lib/keys')
, keys = new Keys('.dpd/keys.json');
, keys = new Keys();
keys.create(function(err, key) {
if(err) return console.error(err);
@@ -103,6 +104,13 @@ program
});
});
program
.command('remote [host]')
.description('connect to a remote host')
.action(function(host) {
remote.createRemote(host, program.port || 3000);
})
program
.command('*')
.description('\t[default] start the server in the current project in development mode\n' +

42
lib/client/remote.js Normal file
View File

@@ -0,0 +1,42 @@
/*!
* A proxy client for connecting to a remote instance of dpd
*/
var http = require('http')
, util = require('util')
, request = require('request')
, Keys = require('../keys')
, debug = require('debug')('remote')
, httpProxy = require('http-proxy')
, url = require('url');
/*!
* Create a remote and host it at the given port.
*/
exports.createRemote = function(remote, port) {
debug('proxying to %s', remote);
if(remote.substr(0,7) !== 'http://') remote = 'http://' + remote;
var remoteUrl = url.parse(remote);
var keys = new Keys();
var server = httpProxy.createServer(function(req, res, proxy) {
var buffer = httpProxy.buffer(req);
debug('[%s] %s', req.method, req.url);
keys.getLocal(function(err, key) {
debug('key [%s]', key);
if(err) return res.end('error reading .dpd/keys.json: ' + err.message);
proxy.proxyRequest(req, res, {
host: remoteUrl.hostname,
port: remoteUrl.port || 80,
buffer: buffer
});
});
});
server.listen(port || 2403);
server.on('listening', function() {
console.log('remote dashboard is available at http://localhost:%s/dashboard', port || 2403)
})
};

View File

@@ -1,5 +1,4 @@
var Server = require('../server')
, Client = require('./index')
, os = require('os')
, exec = require('child_process').exec
, sh = require('shelljs')

View File

@@ -6,7 +6,7 @@ var fs = require('fs')
*/
function Keys(path) {
this.path = path;
this.path = path || '.dpd/keys.json';
}
module.exports = Keys;
@@ -80,4 +80,20 @@ Keys.prototype.writeFile = function(data, fn) {
}
fs.writeFile(this.path, str, fn);
}
/*
* Get the first local key
*/
Keys.prototype.getLocal = function(fn) {
this.readFile(function(err, data) {
if(err) return fn(err);
if(data && typeof data == 'object') {
fn(null, Object.keys(data)[0]);
} else {
fn()
}
});
}

View File

@@ -75,7 +75,7 @@ function Server(options) {
var sessionStore = this.sessions = new SessionStore('sessions', this.db, this.sockets);
// persist keys in a store
var keys = this.keys = new Keys('./.dpd/keys.json');
var keys = this.keys = new Keys();
this.on('request', function (req, res) {
if(req.url.indexOf('/socket.io/') === 0) return;

View File

@@ -21,7 +21,8 @@
"mkdirp": "*",
"debug": "*",
"scrubber": "*",
"shelljs": "git://github.com/dallonf/shelljs.git"
"shelljs": "git://github.com/dallonf/shelljs.git",
"node-proxy": "0.8.x"
},
"devDependencies": {
"mocha": "*",

View File

@@ -1,16 +0,0 @@
var Client = require('../lib/client').Client;
describe('Client', function() {
describe('.createStore(namespace)', function() {
// it('should return a new connection', function() {
// var client = new Client()
// , store = client.createStore('my-store');
// expect(typeof store.find).to.equal('function');
// expect(typeof store.insert).to.equal('function');
// expect(typeof store.update).to.equal('function');
// expect(typeof store.remove).to.equal('function');
// expect(typeof store.rename).to.equal('function');
// });
});
});

View File

@@ -45,4 +45,14 @@ describe('Keys', function() {
});
});
});
describe('.getLocal(fn)', function() {
it('should get the first local key', function(done) {
var keys = new Keys(__dirname + '/support/keys.json');
keys.getLocal(function(err, key) {
expect(key).to.exist;
done(err);
});
});
});
});

File diff suppressed because one or more lines are too long