can haz wss

This commit is contained in:
AJ ONeal 2017-05-16 02:20:02 -05:00
parent 47d72365cc
commit 3aa1085008
1 changed files with 49 additions and 11 deletions

View File

@ -5,6 +5,7 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
var app = express();
var adminApp = require('./admin').create(deps, conf);
var domainMatches = require('../match-domain').match;
var proxyRoutes = [];
var adminDomains = [
/\blocalhost\.admin\./
@ -112,7 +113,8 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
res.end(require('../proxy-err-resp').getRespBody(err, conf.debug));
});
return function (req, res, next) {
return {
web: function (req, res, next) {
var hostname = req.headers.host.split(':')[0];
var relevant = mod.domains.some(function (pattern) {
return domainMatches(pattern, hostname);
@ -123,6 +125,19 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
} else {
next();
}
}
, ws: function (req, socket, head, next) {
var hostname = req.headers.host.split(':')[0];
var relevant = mod.domains.some(function (pattern) {
return domainMatches(pattern, hostname);
});
if (relevant) {
proxy.ws(req, socket, head);
} else {
next();
}
}
};
}
@ -164,7 +179,9 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
(conf.http.modules || []).forEach(function (mod) {
if (mod.name === 'proxy') {
app.use(createProxyRoute(mod));
var proxyRoute = createProxyRoute(mod);
proxyRoutes.push(proxyRoute);
app.use(proxyRoute.web);
}
else if (mod.name === 'static') {
app.use(createStaticRoute(mod));
@ -175,5 +192,26 @@ module.exports.create = function (deps, conf, greenlockMiddleware) {
});
app.use(respond404);
return require('http').createServer(app);
var server = require('http').createServer(function (req, res) {
app(req, res)
});
server.on('upgrade', function (req, socket, head) {
if (!proxyRoutes.length) {
socket.end();
}
function proxyWs() {
var proxyRoute = proxyRoutes.shift();
if (!proxyRoute) {
socket.end();
}
proxyRoute.ws(req, socket, head, proxyWs);
}
proxyWs();
});
return server;
};