2014-08-12 11:09:18 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
process.chdir(__dirname);
|
|
|
|
|
|
|
|
var cluster = require('cluster');
|
|
|
|
var config = require('config');
|
|
|
|
var log = require('npmlog');
|
|
|
|
|
|
|
|
log.level = config.log.level;
|
|
|
|
|
|
|
|
// Handle error conditions
|
|
|
|
process.on('SIGTERM', function() {
|
|
|
|
log.warn('exit', 'Exited on SIGTERM');
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGINT', function() {
|
|
|
|
log.warn('exit', 'Exited on SIGINT');
|
|
|
|
process.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', function(err) {
|
|
|
|
log.error('uncaughtException ', err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
// MASTER process
|
|
|
|
|
|
|
|
cluster.on('fork', function(worker) {
|
|
|
|
log.info('cluster', 'Forked worker #%s [pid:%s]', worker.id, worker.process.pid);
|
|
|
|
});
|
|
|
|
|
|
|
|
cluster.on('exit', function(worker) {
|
|
|
|
log.warn('cluster', 'Worker #%s [pid:%s] died', worker.id, worker.process.pid);
|
|
|
|
setTimeout(function() {
|
|
|
|
cluster.fork();
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fork a single worker
|
|
|
|
cluster.fork();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WORKER process
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var compression = require('compression');
|
|
|
|
var app = express();
|
|
|
|
var server = require('http').Server(app);
|
|
|
|
var io = require('socket.io')(server);
|
|
|
|
var net = require('net');
|
|
|
|
|
|
|
|
// Setup logger. Stream all http logs to general logger
|
|
|
|
app.use(require('morgan')(config.log.http, {
|
|
|
|
'stream': {
|
|
|
|
'write': function(line) {
|
|
|
|
if ((line = (line || '').trim())) {
|
|
|
|
log.http('express', line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Do not advertise Express
|
|
|
|
app.disable('x-powered-by');
|
2014-08-12 11:09:18 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// web server config
|
|
|
|
//
|
|
|
|
|
|
|
|
var port = process.env.PORT || 8585,
|
|
|
|
development = process.argv[2] === '--dev';
|
|
|
|
|
|
|
|
// set HTTP headers
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
// HSTS
|
|
|
|
res.set('Strict-Transport-Security', 'max-age=16070400; includeSubDomains');
|
|
|
|
// CSP
|
2014-08-13 10:16:28 -04:00
|
|
|
var iframe = development ? "http://" + req.hostname + ":" + port : "https://" + req.hostname; // allow iframe to load assets
|
2014-09-26 10:34:31 -04:00
|
|
|
res.set('Content-Security-Policy', "default-src 'self' " + iframe + "; object-src 'none'; connect-src *; style-src 'self' 'unsafe-inline' " + iframe + "; img-src *");
|
2014-09-19 15:51:37 -04:00
|
|
|
// set Cache-control Header (for AppCache)
|
|
|
|
res.set('Cache-control', 'public, max-age=0');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use('/appcache.manifest', function(req, res, next) {
|
|
|
|
res.set('Cache-control', 'no-cache');
|
|
|
|
next();
|
2014-08-12 11:09:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// redirect all http traffic to https
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if ((!req.secure) && (req.get('X-Forwarded-Proto') !== 'https') && !development) {
|
|
|
|
res.redirect('https://' + req.hostname + req.url);
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// use gzip compression
|
|
|
|
app.use(compression());
|
|
|
|
|
|
|
|
// server static files
|
2014-09-19 15:51:37 -04:00
|
|
|
app.use(express.static(__dirname + '/dist'));
|
2014-08-12 11:09:18 -04:00
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
//
|
|
|
|
// Socket.io proxy
|
|
|
|
//
|
|
|
|
|
|
|
|
io.on('connection', function(socket) {
|
|
|
|
|
|
|
|
log.info('io', 'New connection [%s]', socket.conn.id);
|
|
|
|
|
|
|
|
var idCounter = 0;
|
|
|
|
|
|
|
|
socket.on('open', function(data, fn) {
|
|
|
|
var socketId = ++idCounter;
|
|
|
|
var tcp;
|
|
|
|
|
|
|
|
log.verbose('io', 'Open request to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
|
|
|
|
|
|
|
tcp = net.connect(data.port, data.host, function() {
|
|
|
|
log.verbose('io', 'Opened tcp connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
|
|
|
|
|
|
|
tcp.on('data', function(chunk) {
|
|
|
|
log.silly('io', 'Received %s bytes from %s:%s [%s:%s]', chunk.length, data.host, data.port, socket.conn.id, socketId);
|
|
|
|
socket.emit('data-' + socketId, chunk);
|
|
|
|
});
|
|
|
|
|
|
|
|
tcp.on('error', function(err) {
|
|
|
|
log.verbose('io', 'Error for %s:%s [%s:%s]: %s', data.host, data.port, socket.conn.id, socketId, err.message);
|
|
|
|
socket.emit('error-' + socketId, err.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
tcp.on('end', function() {
|
|
|
|
socket.emit('end-' + socketId);
|
|
|
|
});
|
|
|
|
|
|
|
|
tcp.on('close', function() {
|
|
|
|
log.verbose('io', 'Closed tcp connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
|
|
|
socket.emit('close-' + socketId);
|
|
|
|
|
|
|
|
socket.removeAllListeners('data-' + socketId);
|
|
|
|
socket.removeAllListeners('end-' + socketId);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('data-' + socketId, function(chunk, fn) {
|
|
|
|
if (!chunk || !chunk.length) {
|
|
|
|
if (typeof fn === 'function') {
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log.silly('io', 'Sending %s bytes to %s:%s [%s:%s]', chunk.length, data.host, data.port, socket.conn.id, socketId);
|
|
|
|
tcp.write(chunk, function() {
|
|
|
|
if (typeof fn === 'function') {
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('end-' + socketId, function() {
|
|
|
|
log.verbose('io', 'Received request to close connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
|
|
|
tcp.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (typeof fn === 'function') {
|
|
|
|
fn(socketId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('disconnect', function() {
|
|
|
|
log.info('io', 'Closed connection [%s]', socket.conn.id);
|
|
|
|
socket.removeAllListeners();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-12 11:09:18 -04:00
|
|
|
//
|
|
|
|
// start server
|
|
|
|
//
|
|
|
|
|
2014-08-13 10:16:28 -04:00
|
|
|
server.listen(port);
|
2014-08-12 11:09:18 -04:00
|
|
|
if (development) {
|
|
|
|
console.log(' > starting in development mode');
|
|
|
|
}
|
|
|
|
console.log(' > listening on http://localhost:' + port + '\n');
|