Changeset View
Changeset View
Standalone View
Standalone View
support/aphlict/server/aphlict_server.js
'use strict'; | 'use strict'; | ||||
var JX = require('./lib/javelin').JX; | var JX = require('./lib/javelin').JX; | ||||
var http = require('http'); | var http = require('http'); | ||||
var https = require('https'); | var https = require('https'); | ||||
var util = require('util'); | var util = require('util'); | ||||
var fs = require('fs'); | var fs = require('fs'); | ||||
function parse_command_line_arguments(argv) { | function parse_command_line_arguments(argv) { | ||||
var config = { | var args = { | ||||
'client-port': 22280, | |||||
'admin-port': 22281, | |||||
'client-host': '0.0.0.0', | |||||
'admin-host': '127.0.0.1', | |||||
log: '/var/log/aphlict.log', | log: '/var/log/aphlict.log', | ||||
'ssl-key': null, | test: false, | ||||
'ssl-cert': null, | config: null | ||||
test: false | |||||
}; | }; | ||||
for (var ii = 2; ii < argv.length; ii++) { | for (var ii = 2; ii < argv.length; ii++) { | ||||
var arg = argv[ii]; | var arg = argv[ii]; | ||||
var matches = arg.match(/^--([^=]+)=(.*)$/); | var matches = arg.match(/^--([^=]+)=(.*)$/); | ||||
if (!matches) { | if (!matches) { | ||||
throw new Error('Unknown argument "' + arg + '"!'); | throw new Error('Unknown argument "' + arg + '"!'); | ||||
} | } | ||||
if (!(matches[1] in config)) { | if (!(matches[1] in args)) { | ||||
throw new Error('Unknown argument "' + matches[1] + '"!'); | throw new Error('Unknown argument "' + matches[1] + '"!'); | ||||
} | } | ||||
config[matches[1]] = matches[2]; | args[matches[1]] = matches[2]; | ||||
} | } | ||||
config['client-port'] = parseInt(config['client-port'], 10); | return args; | ||||
config['admin-port'] = parseInt(config['admin-port'], 10); | } | ||||
return config; | function parse_config(args) { | ||||
var data = fs.readFileSync(args.config); | |||||
return JSON.parse(data); | |||||
} | } | ||||
require('./lib/AphlictLog'); | require('./lib/AphlictLog'); | ||||
var debug = new JX.AphlictLog() | var debug = new JX.AphlictLog() | ||||
.addConsole(console); | .addConsole(console); | ||||
var config = parse_command_line_arguments(process.argv); | var args = parse_command_line_arguments(process.argv); | ||||
var config = parse_config(args); | |||||
function set_exit_code(code) { | function set_exit_code(code) { | ||||
process.on('exit', function() { | process.on('exit', function() { | ||||
process.exit(code); | process.exit(code); | ||||
}); | }); | ||||
} | } | ||||
process.on('uncaughtException', function(err) { | process.on('uncaughtException', function(err) { | ||||
var context = null; | var context = null; | ||||
if (err.code == 'EACCES' && err.path == config.log) { | if (err.code == 'EACCES' && err.path == args.log) { | ||||
context = util.format( | context = util.format( | ||||
'Unable to open logfile ("%s"). Check that permissions are set ' + | 'Unable to open logfile ("%s"). Check that permissions are set ' + | ||||
'correctly.', | 'correctly.', | ||||
err.path); | err.path); | ||||
} | } | ||||
var message = [ | var message = [ | ||||
'\n<<< UNCAUGHT EXCEPTION! >>>', | '\n<<< UNCAUGHT EXCEPTION! >>>', | ||||
]; | ]; | ||||
if (context) { | if (context) { | ||||
message.push(context); | message.push(context); | ||||
} | } | ||||
message.push(err.stack); | message.push(err.stack); | ||||
debug.log(message.join('\n\n')); | debug.log(message.join('\n\n')); | ||||
set_exit_code(1); | set_exit_code(1); | ||||
}); | }); | ||||
// Add the logfile so we'll fail if we can't write to it. | // Add the logfile so we'll fail if we can't write to it. | ||||
if (config.log) { | if (args.log) { | ||||
debug.addLog(config.log); | debug.addLog(args.log); | ||||
} | } | ||||
try { | try { | ||||
require('ws'); | require('ws'); | ||||
} catch (ex) { | } catch (ex) { | ||||
throw new Error( | throw new Error( | ||||
'You need to install the Node.js "ws" module for websocket support. ' + | 'You need to install the Node.js "ws" module for websocket support. ' + | ||||
'See "Notifications User Guide: Setup and Configuration" in the ' + | 'See "Notifications User Guide: Setup and Configuration" in the ' + | ||||
'documentation for instructions. ' + ex.toString()); | 'documentation for instructions. ' + ex.toString()); | ||||
} | } | ||||
// NOTE: Require these only after checking for the "ws" module, since they | // NOTE: Require these only after checking for the "ws" module, since they | ||||
// depend on it. | // depend on it. | ||||
require('./lib/AphlictAdminServer'); | require('./lib/AphlictAdminServer'); | ||||
require('./lib/AphlictClientServer'); | require('./lib/AphlictClientServer'); | ||||
var ssl_config = { | var ii; | ||||
enabled: (config['ssl-key'] || config['ssl-cert']) | var servers = []; | ||||
}; | for (ii = 0; ii < config.servers.length; ii++) { | ||||
var spec = config.servers[ii]; | |||||
// Load the SSL certificates (if any were provided) now, so that runs with | spec.listen = spec.listen || '0.0.0.0'; | ||||
// `--test` will see any errors. | |||||
if (ssl_config.enabled) { | |||||
ssl_config.key = fs.readFileSync(config['ssl-key']); | |||||
ssl_config.cert = fs.readFileSync(config['ssl-cert']); | |||||
} else { | |||||
ssl_config.key = null; | |||||
ssl_config.cert = null; | |||||
} | |||||
var servers = []; | if (spec['ssl.key']) { | ||||
spec['ssl.key'] = fs.readFileSync(spec['ssl.key']); | |||||
} | |||||
servers.push({ | if (spec['ssl.cert']){ | ||||
type: 'client', | spec['ssl.cert'] = fs.readFileSync(spec['ssl.cert']); | ||||
port: config['client-port'], | } | ||||
listen: config['client-host'], | |||||
'ssl.key': ssl_config.key, | |||||
'ssl.certificate': ssl_config.cert | |||||
}); | |||||
servers.push({ | servers.push(spec); | ||||
type: 'admin', | } | ||||
port: config['admin-port'], | |||||
listen: config['admin-host'], | |||||
'ssl.key': null, | |||||
'ssl.cert': null | |||||
}); | |||||
// If we're just doing a configuration test, exit here before starting any | // If we're just doing a configuration test, exit here before starting any | ||||
// servers. | // servers. | ||||
if (config.test) { | if (args.test) { | ||||
debug.log('Configuration test OK.'); | debug.log('Configuration test OK.'); | ||||
set_exit_code(0); | set_exit_code(0); | ||||
return; | return; | ||||
} | } | ||||
debug.log('Starting servers (service PID %d).', process.pid); | |||||
var aphlict_servers = []; | var aphlict_servers = []; | ||||
var aphlict_clients = []; | var aphlict_clients = []; | ||||
var aphlict_admins = []; | var aphlict_admins = []; | ||||
var ii; | |||||
for (ii = 0; ii < servers.length; ii++) { | for (ii = 0; ii < servers.length; ii++) { | ||||
var server = servers[ii]; | var server = servers[ii]; | ||||
var is_client = (server.type == 'client'); | var is_client = (server.type == 'client'); | ||||
var http_server; | var http_server; | ||||
if (server['ssl.key']) { | if (server['ssl.key']) { | ||||
var https_config = { | var https_config = { | ||||
key: server['ssl.key'], | key: server['ssl.key'], | ||||
Show All 10 Lines | if (is_client) { | ||||
aphlict_server = new JX.AphlictClientServer(http_server); | aphlict_server = new JX.AphlictClientServer(http_server); | ||||
} else { | } else { | ||||
aphlict_server = new JX.AphlictAdminServer(http_server); | aphlict_server = new JX.AphlictAdminServer(http_server); | ||||
} | } | ||||
aphlict_server.setLogger(debug); | aphlict_server.setLogger(debug); | ||||
aphlict_server.listen(server.port, server.listen); | aphlict_server.listen(server.port, server.listen); | ||||
debug.log( | |||||
'Started %s server (Port %d, %s).', | |||||
server.type, | |||||
server.port, | |||||
server['ssl.key'] ? 'With SSL' : 'No SSL'); | |||||
aphlict_servers.push(aphlict_server); | aphlict_servers.push(aphlict_server); | ||||
if (is_client) { | if (is_client) { | ||||
aphlict_clients.push(aphlict_server); | aphlict_clients.push(aphlict_server); | ||||
} else { | } else { | ||||
aphlict_admins.push(aphlict_server); | aphlict_admins.push(aphlict_server); | ||||
} | } | ||||
} | } | ||||
for (ii = 0; ii < aphlict_admins.length; ii++) { | for (ii = 0; ii < aphlict_admins.length; ii++) { | ||||
var admin_server = aphlict_admins[ii]; | var admin_server = aphlict_admins[ii]; | ||||
admin_server.setClientServers(aphlict_clients); | admin_server.setClientServers(aphlict_clients); | ||||
} | } | ||||
debug.log('Started Server (PID %d)', process.pid); |