diff --git a/README.md b/README.md index 9889c94..f0dfe63 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,7 @@ The `tunnel` instance returned to your callback emits the following events |event|args|description| |----|----|----| +|request|info|fires when a request is processed by the tunnel, contains _method_ and _path_ fields| |error|err|fires when an error happens on the tunnel| |close||fires when the tunnel has closed| diff --git a/bin/client b/bin/client index a2f2a29..9903538 100755 --- a/bin/client +++ b/bin/client @@ -26,12 +26,11 @@ var argv = require('yargs') alias: 'port', describe: 'Internal http server port', }) - .option('d', { - alias: 'debug-logs', - describe: 'Print Request Headers', + .option('print-requests', { + describe: 'Print basic request info', }) .require('port') - .boolean('debug-logs') + .boolean('print-requests') .help('help', 'Show this help and exit') .version(require('../package').version) .argv; @@ -47,9 +46,10 @@ var opt = { port: argv.port, local_host: argv['local-host'], subdomain: argv.subdomain, - debug_logs: argv['debug-logs'], }; +const PrintRequests = argv['print-requests']; + lt_client(opt.port, opt, function(err, tunnel) { if (err) { throw err; @@ -64,6 +64,12 @@ lt_client(opt.port, opt, function(err, tunnel) { tunnel.on('error', function(err) { throw err; }); + + if (PrintRequests) { + tunnel.on('request', function(info) { + console.log(new Date().toString(), info.method, info.path); + }); + } }); // vim: ft=javascript diff --git a/lib/Tunnel.js b/lib/Tunnel.js index 9ff972a..9c91daa 100644 --- a/lib/Tunnel.js +++ b/lib/Tunnel.js @@ -77,7 +77,6 @@ Tunnel.prototype._establish = function(info) { info.local_host = opt.local_host; info.local_port = opt.port; - info.debug_logs = opt.debug_logs; var tunnels = self.tunnel_cluster = TunnelCluster(info); @@ -124,6 +123,10 @@ Tunnel.prototype._establish = function(info) { tunnels.open(); }); + tunnels.on('request', function(info) { + self.emit('request', info); + }); + // establish as many tunnels as allowed for (var count = 0 ; count < info.max_conn ; ++count) { tunnels.open(); diff --git a/lib/TunnelCluster.js b/lib/TunnelCluster.js index 46ed550..708b7ba 100644 --- a/lib/TunnelCluster.js +++ b/lib/TunnelCluster.js @@ -30,8 +30,6 @@ TunnelCluster.prototype.open = function() { var local_host = opt.local_host || 'localhost'; var local_port = opt.local_port; - var debug_logs = opt.debug_logs; - debug('establishing tunnel %s:%s <> %s:%s', local_host, local_port, remote_host, remote_port); // connection to localtunnel server @@ -116,15 +114,12 @@ TunnelCluster.prototype.open = function() { } remote.on('data', function(data) { - if (debug_logs) { - var match = data.toString().match(/^(\w+) (\S+)/); - if (match) { - console.log( - new Date().toString(), - `method=${match[1]}`, - `path=${match[2]}` - ); - } + const match = data.toString().match(/^(\w+) (\S+)/); + if (match) { + self.emit('request', { + method: match[1], + path: match[2], + }); } });