tested normal and error conditions for a few different things

This commit is contained in:
AJ ONeal 2018-06-30 16:18:22 -06:00
parent abf73259a6
commit 32e148961d
3 changed files with 51 additions and 20 deletions

View File

@ -73,7 +73,7 @@ module.exports.assign = function (state, tun, cb) {
cb(null, false); cb(null, false);
return; return;
} }
cb(null, getNetConn(sshPort)); getNetConn(sshPort, cb);
} }
var handlers = {}; var handlers = {};
@ -86,6 +86,7 @@ module.exports.assign = function (state, tun, cb) {
state.httpRedirectServer = require('http').createServer(state.greenlock.middleware(state.redirectHttps)); state.httpRedirectServer = require('http').createServer(state.greenlock.middleware(state.redirectHttps));
} }
state.httpRedirectServer.emit('connection', socket); state.httpRedirectServer.emit('connection', socket);
process.nextTick(function () { socket.resume(); });
}; };
handlers.https = function (tlsSocket) { handlers.https = function (tlsSocket) {
console.log('Encrypted', tlsSocket.encrypted, tlsSocket.remoteAddress, tlsSocket.remotePort); console.log('Encrypted', tlsSocket.encrypted, tlsSocket.remoteAddress, tlsSocket.remotePort);
@ -98,6 +99,7 @@ module.exports.assign = function (state, tun, cb) {
}); });
} }
state.defaultHttpServer.emit('connection', tlsSocket); state.defaultHttpServer.emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
}; };
function getNetConn(port, cb) { function getNetConn(port, cb) {
@ -113,19 +115,16 @@ module.exports.assign = function (state, tun, cb) {
, remoteAddress: tun.address , remoteAddress: tun.address
, remotePort: tun.port , remotePort: tun.port
}; };
var conn = net.createConnection(netOpts); var conn = net.createConnection(netOpts, function () {
conn.once('connect', function () {
// this will happen before 'data' or 'readable' is triggered // this will happen before 'data' or 'readable' is triggered
// We use the data from the netOpts object so that the createConnection function has // We use the data from the netOpts object so that the createConnection function has
// the oppurtunity of removing/changing it if it wants/needs to handle it differently. // the oppurtunity of removing/changing it if it wants/needs to handle it differently.
cb(null, conn); cb(null, conn);
conn.removeListener('error', onError); cb = function () {}; // for error events
});
conn.on('error', function (err) {
cb(err);
}); });
function onError(err) {
if (cb) { cb(err); }
}
conn.on('error', onError);
return conn;
} }
function redirectHttp(cb) { function redirectHttp(cb) {
@ -240,21 +239,16 @@ module.exports.assign = function (state, tun, cb) {
function invokeHandler(conf, tlsSocket, tun, id) { function invokeHandler(conf, tlsSocket, tun, id) {
if (parseInt(conf.handler, 10)) { if (parseInt(conf.handler, 10)) {
// TODO http-proxy with proper headers and ws support // TODO http-proxy with proper headers and ws support
//tlsSocket.pause(); getNetConn(conf.handler, function (err, conn) {
var conn = getNetConn(conf.handler, function (err/*, conn*/) { process.nextTick(function () { tlsSocket.resume(); });
if (err) { if (err) {
console.log("[DEBUG] need to handle error");
require('./handlers/local-app-error.js')({ handler: conf.handler, socket: tlsSocket }); require('./handlers/local-app-error.js')({ handler: conf.handler, socket: tlsSocket });
return; return;
} }
console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'");
//conn.pipe(tlsSocket); conn.pipe(tlsSocket);
//tlsSocket.pipe(conn); tlsSocket.pipe(conn);
//tlsSocket.resume();
}); });
conn.pipe(tlsSocket);
tlsSocket.pipe(conn);
tlsSocket.resume();
return; return;
} }
var handle = tun.name || tun.port; var handle = tun.name || tun.port;
@ -272,6 +266,7 @@ module.exports.assign = function (state, tun, cb) {
tlsSocket._id = id; tlsSocket._id = id;
if (handlerservers[conf.handler]) { if (handlerservers[conf.handler]) {
handlerservers[conf.handler].emit('connection', tlsSocket); handlerservers[conf.handler].emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
return; return;
} }
@ -299,6 +294,7 @@ module.exports.assign = function (state, tun, cb) {
if (handler) { if (handler) {
handlerservers[conf.handler] = http.createServer(handler); handlerservers[conf.handler] = http.createServer(handler);
handlerservers[conf.handler].emit('connection', tlsSocket); handlerservers[conf.handler].emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
return; return;
} }
@ -334,8 +330,6 @@ module.exports.assign = function (state, tun, cb) {
tlsSocket._handle.onread(firstChunk.length, firstChunk); tlsSocket._handle.onread(firstChunk.length, firstChunk);
trySsh({ data: firstChunk }, function (err, conn) { trySsh({ data: firstChunk }, function (err, conn) {
process.nextTick(function () { tlsSocket.resume(); });
if (conn) { if (conn) {
conn.pipe(tlsSocket); conn.pipe(tlsSocket);
tlsSocket.pipe(conn); tlsSocket.pipe(conn);

10
tests/README.md Normal file
View File

@ -0,0 +1,10 @@
There are a number of conditions and whatnot that must be tested in more-or-less real-world conditions.
telebit http 3000 // have an app listening on localhost:3000
telebit http 4545 // do not have an app listening
telebit ssh auto // do have ssh listening on localhost:22
telebit ssh 4545 // do have ssh listenening
telebit tcp 3000 // have an echo server listening on localhost:3000
telebit tcp 4545 // no server listening

27
tests/echo.js Normal file
View File

@ -0,0 +1,27 @@
'use strict';
var net = require('net');
var server = net.createServer(function (conn) {
function echo(chunk) {
conn.write(chunk);
if (chunk.length <= 10 && /\b(q|quit|end|cancel)\b/i.test(chunk.toString('utf8'))) {
conn.end();
conn.removeListener('data', echo);
}
}
conn.on('data', echo);
// NOTE: early versions of telebit do not support a 'connection' event
// and therefore will say hello after the first message from the client
conn.write(
"[Echo Server] Hello! I'm an echo server.\n"
+ "[Echo Server] I try to be your friend but when I see things like q|quit|end|cancel, I give up.\n"
);
});
server.on('error', function (err) {
console.error("[echo server]");
console.error(err);
});
server.listen(process.argv[2] || 3000, function () {
console.info("Listening on", this.address());
console.info('ctrl+c to cancel');
});