test loopback listener

This commit is contained in:
AJ ONeal 2015-12-30 03:36:14 +00:00
parent 0a3b457583
commit 6ecb868ae1
6 changed files with 107 additions and 69 deletions

View File

@ -1,34 +1,3 @@
'use strict'; 'use strict';
var PromiseA = require('bluebird'); module.exports = require('./lib/index.js');
module.exports.create = function (args) {
var promises = [];
if (args.debug) {
console.log('[HP] create holepuncher');
console.log(args);
}
// TODO determine if we have a AAAA local ip or not
// TODO get A and AAAA records
if (-1 !== args.protocols.indexOf('none')) {
promises.push(PromiseA.any(args.ipifyUrls.map(function (ipifyUrl) {
var getIp = require('./lib/external-ip');
return getIp({ hostname: ipifyUrl, debug: args.debug });
})).then(function (ips) {
return ips.map(function (ip) {
// TODO attempt loopback
return ip;
});
}));
}
return PromiseA.all(promises).then(function (results) {
if (args.debug) {
console.log('[HP] all done');
console.log(results);
}
});
};

View File

@ -2,41 +2,8 @@
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
//var dns = PromiseA.promisifyAll(require('dns')); //var dns = PromiseA.promisifyAll(require('dns'));
var https = PromiseA.promisifyAll(require('https'));
var os = require('os'); var os = require('os');
var requestAsync = require('./request');
function requestAsync(opts) {
return new PromiseA(function (resolve, reject) {
var req = https.request(opts, function (res) {
var data = '';
res.on('error', function (err) {
if (opts.debug) {
console.error('[Error] HP: bad request:');
console.error(err);
}
reject(err);
});
res.on('data', function (chunk) {
if (opts.debug > 2) {
console.log('HP: request chunk:');
console.log(chunk);
}
data += chunk.toString('utf8');
});
res.on('end', function () {
if (opts.debug > 2) {
console.log('HP: request complete:');
console.log(data);
}
resolve(data);
});
});
req.on('error', reject);
req.end();
});
}
module.exports = function (opts) { module.exports = function (opts) {
var promises = []; var promises = [];
@ -126,6 +93,7 @@ module.exports = function (opts) {
return { return {
family: iface.family family: iface.family
, address: addr , address: addr
, localAddress: iface.address
}; };
})); }));
}); });

53
lib/index.js Normal file
View File

@ -0,0 +1,53 @@
'use strict';
var PromiseA = require('bluebird');
var loopback = require('./loopback-listener');
module.exports.create = function (args) {
if (args.debug) {
console.log('[HP] create holepuncher');
console.log(args);
}
var servers = loopback.create(args);
var promises = [];
if (args.debug) {
console.log('[HP] create servers');
console.log(servers);
}
if (-1 !== args.protocols.indexOf('none')) {
promises.push(PromiseA.any(args.ipifyUrls.map(function (ipifyUrl) {
var getIp = require('./external-ip');
return getIp({ hostname: ipifyUrl, debug: args.debug });
})).then(function (ips) {
// TODO how (if at all) should ip.address === ip.localAddress be treated differently?
// TODO check local firewall?
// TODO would it ever make sense for a public ip to respond to upnp?
var requestAsync = require('./request');
return PromiseA.all(ips.map(function (ip) {
return requestAsync({
secure: false
, hostname: ip.address
, path: '/api/com.daplie.loopback'
, servername: 'daplie.invalid'
, localAddress: ip.localAddress
, port: 65080
, headers: {
Host: 'daplie.invalid'
}
});
}));
}));
}
return PromiseA.all(promises).then(function (results) {
if (args.debug) {
console.log('[HP] all done');
console.log(results);
}
});
};

View File

@ -19,14 +19,14 @@ module.exports.create = function (opts) {
(opts.plainPorts||[]).forEach(function (plainPort) { (opts.plainPorts||[]).forEach(function (plainPort) {
var plainServer = http.createServer(); var plainServer = http.createServer();
plainServer.__plainPort = plainPort; plainServer.__plainPort = plainPort;
http.on('request', app); plainServer.on('request', app);
results.plainServers.push(plainServer); results.plainServers.push(plainServer);
}); });
(opts.tlsPorts||[]).forEach(function (tlsPort) { (opts.tlsPorts||[]).forEach(function (tlsPort) {
var tlsServer = https.createServer(httpsOptions); var tlsServer = https.createServer(httpsOptions);
tlsServer.__tlsPort = tlsPort; tlsServer.__tlsPort = tlsPort;
http.on('request', app); tlsServer.on('request', app);
results.tlsServers.push(tlsServer); results.tlsServers.push(tlsServer);
}); });

42
lib/request.js Normal file
View File

@ -0,0 +1,42 @@
'use strict';
var PromiseA = require('bluebird');
var https = PromiseA.promisifyAll(require('https'));
var http = PromiseA.promisifyAll(require('http'));
function requestAsync(opts) {
return new PromiseA(function (resolve, reject) {
var httpr = (false === opts.secure) ? http : https;
var req = httpr.request(opts, function (res) {
var data = '';
res.on('error', function (err) {
if (opts.debug) {
console.error('[Error] HP: bad request:');
console.error(err);
}
reject(err);
});
res.on('data', function (chunk) {
if (opts.debug > 2) {
console.log('HP: request chunk:');
console.log(chunk);
}
data += chunk.toString('utf8');
});
res.on('end', function () {
if (opts.debug > 2) {
console.log('HP: request complete:');
console.log(data);
}
resolve(data);
});
});
req.on('error', reject);
req.end();
});
}
module.exports = requestAsync;

View File

@ -9,7 +9,13 @@
"directories": { "directories": {
"example": "examples" "example": "examples"
}, },
"dependencies": {}, "dependencies": {
"bluebird": "^3.1.1",
"express": "^4.13.3",
"localhost.daplie.com-certificates": "^1.1.2",
"request": "^2.67.0",
"scmp": "^1.0.0"
},
"devDependencies": {}, "devDependencies": {},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"