Permalink
Browse files

tunnels and remote testling browsers work!

  • Loading branch information...
1 parent 0007c18 commit 6d8422412e7f1384d0915494913488303d34753d @substack committed Jun 30, 2012
Showing with 36 additions and 34 deletions.
  1. +7 −13 bin/cmd.js
  2. +29 −20 lib/testling/tunnel.js
  3. +0 −1 lib/testling/visit.js
View
@@ -3,7 +3,6 @@
var launcher = require('browser-launcher');
var testlingVisit = require('../lib/testling/visit');
var createServers = require('../lib/servers');
-var spawn = require('child_process').spawn;
var argv = require('optimist')
.option('headless', { default : true, type : 'boolean' })
@@ -14,14 +13,7 @@ var argv = require('optimist')
argv.files = argv.files || argv._;
var tunnel = require('../lib/testling/tunnel');
-if (argv._[0] === 'tunnel') {
- tunnel(argv.server, function (err, cmd) {
- if (err) return console.error(err);
- console.log('# ' + cmd.join(' '));
- spawn(cmd[0], cmd.slice(1), { customFds : [ 0, 1, 2 ] });
- });
- return;
-}
+if (argv._[0] === 'tunnel') return tunnel(argv.server);
createServers(argv, function (uri, ports) {
if (argv.browser === 'echo') {
@@ -32,12 +24,14 @@ createServers(argv, function (uri, ports) {
if (/^testling\./.test(argv.browser)) {
if (!tunnel.running) {
- console.error(
- "# Make sure the testling tunnel is running or this won't work."
- + '\n# Do: `testling tunnel` to start a tunnel.'
+ console.error('Tunnel not running. Do `testling tunnel`'
+ + ' to start an ssh tunnel first.'
);
+ process.exit(1);
+ return;
}
- testlingVisit(uri, argv, function (err, res) {
+ var u = uri.replace(/^http:\/\/[^\/]+/, tunnel.config.addr);
+ testlingVisit(u, argv, function (err, res) {
if (err) return console.error(err);
});
return;
View
@@ -2,48 +2,57 @@ var withAuth = require('./auth');
var fs = require('fs');
var path = require('path');
var request = require('request');
+var spawn = require('child_process').spawn;
var configDir = path.join(
process.env.HOME || process.env.USERPROFILE,
'.config'
);
var mkdirp = require('mkdirp');
mkdirp.sync(configDir);
-var pidFile = path.join(configDir, 'testling_tunnels.pid');
-
-var running = false;
-if ((path.existsSync || fs.existsSync)(pidFile)) {
- var pid = parseInt(fs.readFileSync(pidFile, 'utf8').trim(), 10);
- if (pid && pid > 0) {
- try {
- kill(pid, 0);
- running = true; // didn't get ESRCH
- }
- catch (err) {}
- }
-}
+var file = path.join(configDir, 'testling_tunnel.json');
exports = module.exports = function (addr) {
- if (running) {
+ if (exports.running) {
console.error('# tunnel appears to be already running, trying anyhow');
}
withAuth(function (err, auth) {
- if (err) return cb(err);
+ if (err) return console.error(err);
var u = 'http://' + auth.prefix + '@testling.com/tunnel';
request(u, function checkBody (err, res, body) {
- if (err) return cb(err);
+ if (err) return console.error(err);
- var m = /ssh -NR (\d+:)\S+ (\S+)/.exec(body);
+ var m = /ssh -NR (\d+):\S+ (\S+)/.exec(body);
if (m) {
- cb(null, [ 'ssh', '-NR', m[1] + addr, m[2] ]);
+ var ps = spawn('ssh', [ '-NR', m[1] + ':' + addr, m[2] ], {
+ customFds : [ 0, 1, 2 ]
+ });
+ var cfg = {
+ pid : ps.pid,
+ port : m[1],
+ addr : 'http://tunnel.browserling.com:' + m[1],
+ };
+ var s = JSON.stringify(cfg);
+
+ fs.writeFile(file, s, function (err) {
+ if (err) return console.error(err);
+ });
}
else if (/open a tunnel with:/i.test(body)) {
request(u + '/open', checkBody)
}
- else cb('unexpected response from server: ' + body)
+ else console.error('unexpected response from server: ' + body)
});
});
};
-exports.running = running;
+if ((path.existsSync || fs.existsSync)(file)) {
+ var json = JSON.parse(fs.readFileSync(file));
+ exports.config = json;
+ try {
+ process.kill(json.pid, 0);
+ exports.running = true; // didn't get ESRCH
+ }
+ catch (err) {}
+}
View
@@ -2,7 +2,6 @@ var request = require('request');
var withAuth = require('./auth');
module.exports = function (uri, opts, cb) {
- var turi = process.env.TUNNEL;
var b = opts.browser.replace(/^testling\./, '').split('/');
var browser = b[0];
var version = opts.version || b[1];

0 comments on commit 6d84224

Please sign in to comment.