diff --git a/server/app.js b/server/app.js index 4186f04d..a94779b8 100644 --- a/server/app.js +++ b/server/app.js @@ -45,7 +45,7 @@ function startClustered(redis_enabled) { return Number(s) % len; }; - var server = net.createServer({ pauseOnConnect: true }, function(connection) { + var server = net.createServer({ pauseOnConnect: true }, function(connection, a) { var worker = workers[worker_index(connection.remoteAddress, num_processes)]; worker.send('sticky-session:connection', connection); }).listen(port); @@ -55,10 +55,8 @@ function startClustered(redis_enabled) { } function startSingle(clustered, redis_enabled) { - var client = require('./client.js'); - var admin = require('./admin.js'); var server; - var admin_server; + var client = require('./client.js'); try { var cert_config = require(path.join(path.join(__dirname, 'config'), 'cert_config.js')); var fs = require('fs'); @@ -72,18 +70,13 @@ function startSingle(clustered, redis_enabled) { }; var https = require('https'); - server = https.Server(credentials, client); - admin_server = https.Server(credentials, admin); - + server = https.Server(credentials, routingFunction); } catch(err){ console.log("Starting without https (probably on localhost)"); - server = http.Server(client); - admin_server = http.Server(admin); + server = http.createServer(routingFunction); //add = ",http://localhost:80*,http://localhost:8080*,localhost:8080*, localhost:8082*,http://zoff.dev:80*,http://zoff.dev:8080*,zoff.dev:8080*, zoff.dev:8082*"; } - - if(clustered) { app .use( vhost('*', function(req, res) { @@ -93,7 +86,7 @@ function startSingle(clustered, redis_enabled) { server.emit("request", req, res); }) ) .use( vhost('admin.*', function(req, res) { - admin_server.emit("request", req, res); + server.emit("request", req, res); }) ) .listen(onListen); //server.listen(onListen); @@ -106,7 +99,7 @@ function startSingle(clustered, redis_enabled) { server.emit("request", req, res); }) ) .use( vhost('admin.*', function(req, res) { - admin_server.emit("request", req, res); + server.emit("request", req, res); }) ) .listen(port, onListen); //server.listen(port, onListen); @@ -126,6 +119,7 @@ function startSingle(clustered, redis_enabled) { socketIO.listen(server); } + process.on('message', function(message, connection) { if (message !== 'sticky-session:connection') { return; @@ -139,3 +133,16 @@ function startSingle(clustered, redis_enabled) { function onListen() { console.log("Started with pid [" + process.pid + "]"); } + +function routingFunction(req, res, next) { + var client = require('./client.js'); + var admin = require('./admin.js'); + var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; + var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); + + if(subdomain.length > 1 && subdomain[0] == "admin") { + admin(req, res, next); + } else { + client(req, res, next); + } +} diff --git a/server/routing/client/router.js b/server/routing/client/router.js index 33987d91..b3e71fb0 100644 --- a/server/routing/client/router.js +++ b/server/routing/client/router.js @@ -46,7 +46,7 @@ function root(req, res, next) { try{ var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); - if(url != "zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { + if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { res.redirect("https://zoff.me"); return; } @@ -77,7 +77,7 @@ function channel(req, res, next) { try{ var url = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host.split(":")[0]; var subdomain = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'].split(".") : req.headers.host.split(":")[0].split("."); - if(url != "zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { + if(url != "zoff.me" && url != "admin.localhost" && url != "admin.zoff.me" && url != "remote.zoff.me" && url != "fb.zoff.me" && url != "remote.localhost" && url != "localhost") { res.redirect("https://zoff.me"); return; }