Add basic request logging functionality (#178)

This commit is contained in:
Daniel Kezerashvili
2018-04-01 23:36:49 -04:00
committed by Roman Shtylman
parent 14cac6f6c8
commit 32fd1fdcbd
3 changed files with 23 additions and 1 deletions

View File

@@ -25,7 +25,12 @@ var argv = require('yargs')
alias: 'port', alias: 'port',
describe: 'Internal http server port', describe: 'Internal http server port',
}) })
.option('d', {
alias: 'debug-logs',
describe: 'Print Request Headers',
})
.require('port') .require('port')
.boolean('debug-logs')
.help('help', 'Show this help and exit') .help('help', 'Show this help and exit')
.version(require('../package').version) .version(require('../package').version)
.argv; .argv;
@@ -41,6 +46,7 @@ var opt = {
port: argv.port, port: argv.port,
local_host: argv['local-host'], local_host: argv['local-host'],
subdomain: argv.subdomain, subdomain: argv.subdomain,
debug_logs: argv['debug-logs'],
}; };
lt_client(opt.port, opt, function(err, tunnel) { lt_client(opt.port, opt, function(err, tunnel) {

View File

@@ -70,13 +70,14 @@ Tunnel.prototype._init = function(cb) {
Tunnel.prototype._establish = function(info) { Tunnel.prototype._establish = function(info) {
var self = this; var self = this;
var opt = self._opt; var opt = self._opt;
// increase max event listeners so that localtunnel consumers don't get // increase max event listeners so that localtunnel consumers don't get
// warning messages as soon as they setup even one listener. See #71 // warning messages as soon as they setup even one listener. See #71
self.setMaxListeners(info.max_conn + (EventEmitter.defaultMaxListeners || 10)); self.setMaxListeners(info.max_conn + (EventEmitter.defaultMaxListeners || 10));
info.local_host = opt.local_host; info.local_host = opt.local_host;
info.local_port = opt.port; info.local_port = opt.port;
info.debug_logs = opt.debug_logs;
var tunnels = self.tunnel_cluster = TunnelCluster(info); var tunnels = self.tunnel_cluster = TunnelCluster(info);

View File

@@ -30,6 +30,8 @@ TunnelCluster.prototype.open = function() {
var local_host = opt.local_host || 'localhost'; var local_host = opt.local_host || 'localhost';
var local_port = opt.local_port; 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); debug('establishing tunnel %s:%s <> %s:%s', local_host, local_port, remote_host, remote_port);
// connection to localtunnel server // connection to localtunnel server
@@ -113,6 +115,19 @@ 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]}`
);
}
}
});
// tunnel is considered open when remote connects // tunnel is considered open when remote connects
remote.once('connect', function() { remote.once('connect', function() {
self.emit('open', remote); self.emit('open', remote);