From 08676ba81d7dbfcc498d239e0cad8b2960b239d8 Mon Sep 17 00:00:00 2001 From: Fredi Pevcin Date: Thu, 20 Feb 2014 22:32:22 +0100 Subject: [PATCH] Set Host header accordingly to local-host option --- client.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/client.js b/client.js index d0a84cb..91d4cc6 100644 --- a/client.js +++ b/client.js @@ -5,6 +5,33 @@ var EventEmitter = require('events').EventEmitter; var request = require('request'); var debug = require('debug')('localtunnel:client'); +var stream = require('stream'); +var util = require('util'); + +// node v0.10+ use native Transform, else polyfill +var Transform = stream.Transform || + require('readable-stream').Transform; + +var HeaderHostTransformer = function(opts) { + if (!(this instanceof HeaderHostTransformer)) { + return new HeaderHostTransformer(opts); + } + + opts = opts || {} + + var self = this; + self.host = opts.host || 'localhost'; + + Transform.call(this, opts); +} +util.inherits(HeaderHostTransformer, Transform); + +HeaderHostTransformer.prototype._transform = function (chunk, enc, cb) { + var chunk = chunk.toString(); + this.push(chunk.replace(/(\r\nHost: )\S+/, '$1' + this.host)); + cb(); +}; + // manages groups of tunnels var TunnelCluster = function(opt) { if (!(this instanceof TunnelCluster)) { @@ -93,7 +120,8 @@ TunnelCluster.prototype.open = function() { local.once('connect', function() { debug('connected locally'); remote.resume(); - remote.pipe(local).pipe(remote); + + remote.pipe(new HeaderHostTransformer({ host: local_host })).pipe(local).pipe(remote); // when local closes, also get a new remote local.once('close', function(had_error) {