Skip to content

Commit 84025db

Browse files
add wilcard middleware
1 parent 1a940cd commit 84025db

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

examples/wildcard.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var port = 8081;
4+
5+
var Proxy = require('../');
6+
var proxy = Proxy();
7+
8+
proxy.use(Proxy.wildcard);
9+
10+
proxy.onError(function(ctx, err, errorKind) {
11+
// ctx may be null
12+
var url = (ctx && ctx.clientToProxyRequest) ? ctx.clientToProxyRequest.url : '';
13+
console.error(errorKind + ' on ' + url + ':', err);
14+
});
15+
16+
proxy.listen({ port: port });
17+
console.log('listening on ' + port);

lib/middleware/wildcard.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
module.exports = {
4+
5+
onCertificateRequired: function (hostname, callback) {
6+
var self = this;
7+
var wildcardHost = hostname.replace(/^(.+)(\.[^\.]{4,}(\.[^\.]{1,3})*\.[^\.]+)$/, function(match, group1, group2) {
8+
return group1.replace(/[^\.]+/g, '*') + group2;
9+
});
10+
var fileName = wildcardHost.replace(/\*/g, '_');
11+
return callback(null, {
12+
keyFile: self.sslCaDir + '/keys/' + fileName + '.key',
13+
certFile: self.sslCaDir + '/certs/' + fileName + '.pem',
14+
hosts: [wildcardHost]
15+
});
16+
return this;
17+
}
18+
};

lib/proxy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = function() {
2121

2222

2323
module.exports.gunzip = require('./middleware/gunzip');
24+
module.exports.wildcard = require('./middleware/wildcard');
2425

2526
var Proxy = function() {
2627
this.onRequestHandlers = [];

0 commit comments

Comments
 (0)