Skip to content

Commit 1b4ede4

Browse files
committed
added https support to (listen) proxy server
1 parent c598504 commit 1b4ede4

6 files changed

Lines changed: 59 additions & 30 deletions

File tree

bin/webpagetest

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ Object.keys(mapping.commands).forEach(function eachCommand(name) {
147147
formatData = null;
148148
}
149149
},
150-
'listen': function(port) {
151-
defaultAction(port);
152-
options = undefined;
150+
'listen': function(port, opts) {
151+
defaultAction(port, opts);
153152
formatData = function customFormat(err, data) {
154153
if (err) {
155154
output(JSON.stringify({error: err}, null, 2), 1);

lib/mapping.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,20 @@ var options = {
595595
param: 'number',
596596
info: 'the request number [1]'
597597
}
598+
},
599+
'listen': {
600+
'key': {
601+
name: 'key',
602+
key: 'k',
603+
param: 'file',
604+
info: 'private key file to use for SSL'
605+
},
606+
'cert': {
607+
name: 'cert',
608+
key: 'c',
609+
param: 'file',
610+
info: 'public x509 certificate file to use for SSL'
611+
}
598612
}
599613
};
600614

@@ -725,7 +739,8 @@ var commands = {
725739
name: 'listen',
726740
param: 'hostname:port',
727741
optional: true,
728-
info: 'start webpagetest-api server on <hostname>:<port> [hostname:%s]'
742+
options: [options.listen],
743+
info: 'start webpagetest-api proxy server on <hostname>:<port> [hostname:%s]'
729744
}
730745
};
731746

lib/server.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* Released under the MIT License
66
*/
77

8-
var http = require('http'),
8+
var https = require('https'),
9+
http = require('http'),
910
path = require('path'),
1011
url = require('url'),
12+
fs = require('fs'),
1113
mapping = require('./mapping');
1214

1315
var help = {};
@@ -142,31 +144,40 @@ function route(req, res) {
142144
}
143145

144146
module.exports = {
145-
listen: function listen(local, callback) {
146-
var httpServer = http.createServer(route.bind(this));
147+
listen: function listen(local, options, callback) {
148+
var server;
149+
150+
if (fs.existsSync(options.key) && fs.existsSync(options.cert)) {
151+
server = https.createServer({
152+
key: fs.readFileSync(path.resolve(process.cwd(), options.key)),
153+
cert: fs.readFileSync(path.resolve(process.cwd(), options.cert))
154+
}, route.bind(this));
155+
} else {
156+
server = http.createServer(route.bind(this));
157+
}
147158

148-
httpServer.on('error', function listenError(err) {
149-
if (typeof callback === 'function') {
150-
callback(err);
151-
}
152-
}).on('listening', function listenError() {
153-
if (typeof callback === 'function') {
154-
callback(undefined, {
155-
server: httpServer,
156-
protocol: 'http',
159+
server.on('error', function listenError(err) {
160+
if (typeof callback === 'function') {
161+
callback(err);
162+
}
163+
}).on('listening', function listenError() {
164+
if (typeof callback === 'function') {
165+
callback(undefined, {
166+
server: server,
167+
protocol: server instanceof https.Server ? 'https' : 'http',
168+
hostname: local.hostname,
169+
port: local.port,
170+
url: url.format({
171+
protocol: server instanceof https.Server ? 'https' : 'http',
157172
hostname: local.hostname,
158-
port: local.port,
159-
url: url.format({
160-
protocol: 'http',
161-
hostname: local.hostname,
162-
port: local.port
163-
})
164-
});
165-
}
166-
}).listen(local.port);
173+
port: local.port
174+
})
175+
});
176+
}
177+
}).listen(local.port);
167178

168179
buildHelp(url.format(this.config.hostname));
169180

170-
return httpServer;
181+
return server;
171182
}
172183
};

lib/webpagetest.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,12 @@ function getScreenshotImage(id, options, callback) {
644644
return api.call(this, pathname, callback, params, options);
645645
}
646646

647-
function listen(local, callback) {
647+
function listen(local, options, callback) {
648+
callback = callback || options;
649+
options = options === callback ? {} : options;
648650
local = helper.localhost(local, WebPageTest.defaultListenPort);
649651

650-
return server.listen.call(this, local, callback);
652+
return server.listen.call(this, local, options, callback);
651653
}
652654

653655
function getEmbedVideoPlayer(id, options, callback) {

test/fixtures/command-line/help-listen.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33

44
Options:
55

6-
-h, --help output usage information
6+
-h, --help output usage information
7+
-k, --key <file> private key file to use for SSL
8+
-c, --cert <file> public x509 certificate file to use for SSL
79

test/fixtures/command-line/help.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
screenshot [options] <id> get the fully loaded page screenshot in JPG format (PNG if in full resolution)
2525
video [options] <tests> create a video from <tests> (comma separated test ids)
2626
player <id> get a html5 player for a video <id>
27-
listen [hostname:port] start webpagetest-api server on <hostname>:<port> [hostname:7791]
27+
listen [options] [hostname:port] start webpagetest-api proxy server on <hostname>:<port> [hostname:7791]
2828
batch <file> run commands in batch, i.e. one command per line from <file> in parallel
2929

3030
Options:

0 commit comments

Comments
 (0)