diff --git a/lib/handlers/local-app-error.js b/lib/handlers/local-app-error.js
new file mode 100644
index 0000000..5c60911
--- /dev/null
+++ b/lib/handlers/local-app-error.js
@@ -0,0 +1,19 @@
+'use strict';
+
+module.exports = function (opts) {
+ console.log("Could not connect");
+ var socket = opts.socket;
+ var handler = opts.handler;
+ var http = require('http');
+ var server = http.createServer(function (req, res) {
+ console.log('responding to thing');
+ res.statusCode = 500;
+ res.setHeader('Content-Type', 'text/html');
+ res.end(""
+ + "
Couldn't Connect"
+ + "Could not connect to localhost:" + handler + ""
+ + "");
+ });
+ //server.emit('connection', socket);
+ socket.end("Could not connect to localhost:" + handler);
+};
diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js
index 12827bb..184d850 100644
--- a/lib/sorting-hat.js
+++ b/lib/sorting-hat.js
@@ -113,16 +113,19 @@ module.exports.assign = function (state, tun, cb) {
, remoteAddress: tun.address
, remotePort: tun.port
};
- var conn = net.createConnection(netOpts, function () {
+ var conn = net.createConnection(netOpts);
+ conn.once('connect', function () {
// this will happen before 'data' or 'readable' is triggered
// 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.
cb(null, conn);
- cb = function () {}; // for error events
- });
- conn.on('error', function (err) {
- cb(err);
+ conn.removeListener('error', onError);
});
+ function onError(err) {
+ if (cb) { cb(err); }
+ }
+ conn.on('error', onError);
+ return conn;
}
function redirectHttp(cb) {
@@ -237,18 +240,21 @@ module.exports.assign = function (state, tun, cb) {
function invokeHandler(conf, tlsSocket, tun, id) {
if (parseInt(conf.handler, 10)) {
// TODO http-proxy with proper headers and ws support
- getNetConn(conf.handler, function (err, conn) {
+ //tlsSocket.pause();
+ var conn = getNetConn(conf.handler, function (err/*, conn*/) {
if (err) {
- // TODO direct to site with error page
- console.error("probably couldn't connect to handler");
- tlsSocket.write("Couldn't connect to localhost:" + conf.handler);
- tlsSocket.end();
+ console.log("[DEBUG] need to handle error");
+ require('./handlers/local-app-error.js')({ handler: conf.handler, socket: tlsSocket });
return;
}
console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'");
- conn.pipe(tlsSocket);
- tlsSocket.pipe(conn);
+ //conn.pipe(tlsSocket);
+ //tlsSocket.pipe(conn);
+ //tlsSocket.resume();
});
+ conn.pipe(tlsSocket);
+ tlsSocket.pipe(conn);
+ tlsSocket.resume();
return;
}
var handle = tun.name || tun.port;