error message when localhost app isn't running

This commit is contained in:
AJ ONeal 2018-06-29 17:31:34 -06:00
parent 1bb6a82f77
commit b2b9094d60
1 changed files with 19 additions and 9 deletions

View File

@ -100,7 +100,7 @@ module.exports.assign = function (state, tun, cb) {
state.defaultHttpServer.emit('connection', tlsSocket); state.defaultHttpServer.emit('connection', tlsSocket);
}; };
function getNetConn(port) { function getNetConn(port, cb) {
var netOpts = { var netOpts = {
port: port port: port
, host: '127.0.0.1' , host: '127.0.0.1'
@ -117,8 +117,12 @@ module.exports.assign = function (state, tun, cb) {
// 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 = function () {}; // for error events
});
conn.on('error', function (err) {
cb(err);
}); });
return conn;
} }
function redirectHttp(cb) { function redirectHttp(cb) {
@ -194,8 +198,7 @@ module.exports.assign = function (state, tun, cb) {
function invokeTcpHandler(conf, socket, tun, id, cb) { function invokeTcpHandler(conf, socket, tun, id, cb) {
var conn; var conn;
if (parseInt(conf.handler, 10)) { if (parseInt(conf.handler, 10)) {
conn = getNetConn(conf.handler); getNetConn(conf.handler, cb);
cb(null, conn);
return conn; return conn;
} }
@ -232,13 +235,20 @@ module.exports.assign = function (state, tun, cb) {
} }
var handlerservers = {}; var handlerservers = {};
function invokeHandler(conf, tlsSocket, tun, id) { function invokeHandler(conf, tlsSocket, tun, id) {
var conn;
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
conn = getNetConn(conf.handler); getNetConn(conf.handler, function (err, conn) {
console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); if (err) {
conn.pipe(tlsSocket); // TODO direct to site with error page
tlsSocket.pipe(conn); console.error("probably couldn't connect to handler");
tlsSocket.write("Couldn't connect to localhost:" + conf.handler);
tlsSocket.end();
return;
}
console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'");
conn.pipe(tlsSocket);
tlsSocket.pipe(conn);
});
return; return;
} }
var handle = tun.name || tun.port; var handle = tun.name || tun.port;