transform Host header when local-host is defined explicitly

This commit is contained in:
Tymoteusz Paszun
2014-05-09 15:07:38 +02:00
parent 8768329fdd
commit 828cb2afcb
3 changed files with 53 additions and 8 deletions

View File

@@ -11,12 +11,11 @@ var argv = require('optimist')
describe: 'request this subdomain' describe: 'request this subdomain'
}) })
.options('local-host', { .options('local-host', {
describe: 'tunnel traffic to this host instead of localhost' describe: 'tunnel traffic to this host instead of localhost, override Host header to this host'
}) })
.options('version', { .options('version', {
describe: 'print version and exit' describe: 'print version and exit'
}) })
.default('local-host', 'localhost')
.describe('port', 'internal http server port') .describe('port', 'internal http server port')
.argv; .argv;

View File

@@ -60,7 +60,7 @@ TunnelCluster.prototype.open = function() {
var remote_host = opt.remote_host; var remote_host = opt.remote_host;
var remote_port = opt.remote_port; var remote_port = opt.remote_port;
var local_host = opt.local_host; var local_host = opt.local_host || 'localhost';
var local_port = opt.local_port; var local_port = opt.local_port;
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);
@@ -133,10 +133,11 @@ TunnelCluster.prototype.open = function() {
var stream = remote; var stream = remote;
// if user requested something other than localhost // if user requested specific local host
// then we use host header transform to replace the host header // then we use host header transform to replace the host header
if (local_host !== 'localhost') { if (opt.local_host) {
stream = remote.pipe(HeaderHostTransformer({ host: local_host })); debug('transform Host header to %s', opt.local_host);
stream = remote.pipe(HeaderHostTransformer({ host: opt.local_host }));
} }
stream.pipe(local).pipe(remote); stream.pipe(local).pipe(remote);
@@ -224,7 +225,7 @@ Tunnel.prototype._establish = function(info) {
var self = this; var self = this;
var opt = self._opt; var opt = self._opt;
info.local_host = opt.local_host || 'localhost'; info.local_host = opt.local_host;
info.local_port = opt.port; info.local_port = opt.port;
var tunnels = self.tunnel_cluster = TunnelCluster(info); var tunnels = self.tunnel_cluster = TunnelCluster(info);

View File

@@ -68,7 +68,51 @@ test('request specific domain', function(done) {
}); });
}); });
suite('local-host'); suite('--local-host localhost');
test('setup localtunnel client', function(done) {
var opt = {
local_host: 'localhost'
};
localtunnel(test._fake_port, opt, function(err, tunnel) {
assert.ifError(err);
assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url));
test._fake_url = tunnel.url;
done();
});
});
test('override Host header with local-host', function(done) {
var uri = test._fake_url;
var parsed = url.parse(uri);
var opt = {
host: parsed.host,
port: 443,
headers: {
host: parsed.hostname
},
path: '/'
};
var req = https.request(opt, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
assert.equal(body, 'localhost');
done();
});
});
req.end();
});
suite('--local-host 127.0.0.1');
test('setup localtunnel client', function(done) { test('setup localtunnel client', function(done) {
var opt = { var opt = {
@@ -111,3 +155,4 @@ test('override Host header with local-host', function(done) {
req.end(); req.end();
}); });