1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 06:42:42 +01:00

Allow the Aphlict server to bind to localhost

Summary: If you are running the Aphlict server behind a reverse proxy (such as `nginx`) then there's no need to bind to `0.0.0.0`. Add a `--client-host` flag to `aphlict_server.js` to allow binding to a different hostname. Also changed the other flags for consistency and clarity.

Test Plan: Started, stopped and debug the Aphlict server.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D11288
This commit is contained in:
Joshua Spence 2015-01-09 09:59:33 +11:00
parent 8ddb9e2875
commit 638cf20c9d
5 changed files with 69 additions and 29 deletions

View file

@ -4,18 +4,21 @@ final class PhabricatorAphlictManagementDebugWorkflow
extends PhabricatorAphlictManagementWorkflow { extends PhabricatorAphlictManagementWorkflow {
public function didConstruct() { public function didConstruct() {
parent::didConstruct();
$this $this
->setName('debug') ->setName('debug')
->setSynopsis( ->setSynopsis(
pht( pht(
'Start the notifications server in the foreground and print large '. 'Start the notifications server in the foreground and print large '.
'volumes of diagnostic information to the console.')) 'volumes of diagnostic information to the console.'));
->setArguments(array());
} }
public function execute(PhutilArgumentParser $args) { public function execute(PhutilArgumentParser $args) {
$this->willLaunch(true); parent::execute($args);
return $this->launch(true); $this->setDebug(true);
$this->willLaunch();
return $this->launch();
} }
} }

View file

@ -4,13 +4,15 @@ final class PhabricatorAphlictManagementRestartWorkflow
extends PhabricatorAphlictManagementWorkflow { extends PhabricatorAphlictManagementWorkflow {
public function didConstruct() { public function didConstruct() {
parent::didConstruct();
$this $this
->setName('restart') ->setName('restart')
->setSynopsis(pht('Stop, then start the notifications server.')) ->setSynopsis(pht('Stop, then start the notifications server.'));
->setArguments(array());
} }
public function execute(PhutilArgumentParser $args) { public function execute(PhutilArgumentParser $args) {
parent::execute($args);
$err = $this->executeStopCommand(); $err = $this->executeStopCommand();
if ($err) { if ($err) {
return $err; return $err;

View file

@ -4,13 +4,14 @@ final class PhabricatorAphlictManagementStartWorkflow
extends PhabricatorAphlictManagementWorkflow { extends PhabricatorAphlictManagementWorkflow {
public function didConstruct() { public function didConstruct() {
parent::didConstruct();
$this $this
->setName('start') ->setName('start')
->setSynopsis(pht('Start the notifications server.')) ->setSynopsis(pht('Start the notifications server.'));
->setArguments(array());
} }
public function execute(PhutilArgumentParser $args) { public function execute(PhutilArgumentParser $args) {
parent::execute($args);
return $this->executeStartCommand(); return $this->executeStartCommand();
} }

View file

@ -3,6 +3,26 @@
abstract class PhabricatorAphlictManagementWorkflow abstract class PhabricatorAphlictManagementWorkflow
extends PhabricatorManagementWorkflow { extends PhabricatorManagementWorkflow {
private $debug = false;
private $clientHost;
public function didConstruct() {
$this
->setArguments(
array(
array(
'name' => 'client-host',
'param' => 'hostname',
'help' => pht('Hostname to bind to for the client server.'),
),
));
}
public function execute(PhutilArgumentParser $args) {
$this->clientHost = $args->getArg('client-host');
return 0;
}
final public function getPIDPath() { final public function getPIDPath() {
return PhabricatorEnv::getEnvConfig('notification.pidfile'); return PhabricatorEnv::getEnvConfig('notification.pidfile');
} }
@ -27,6 +47,10 @@ abstract class PhabricatorAphlictManagementWorkflow
exit(1); exit(1);
} }
protected final function setDebug($debug) {
$this->debug = $debug;
}
public static function requireExtensions() { public static function requireExtensions() {
self::mustHaveExtension('pcntl'); self::mustHaveExtension('pcntl');
self::mustHaveExtension('posix'); self::mustHaveExtension('posix');
@ -50,7 +74,7 @@ abstract class PhabricatorAphlictManagementWorkflow
} }
} }
final protected function willLaunch($debug = false) { final protected function willLaunch() {
$console = PhutilConsole::getConsole(); $console = PhutilConsole::getConsole();
$pid = $this->getPID(); $pid = $this->getPID();
@ -70,14 +94,14 @@ abstract class PhabricatorAphlictManagementWorkflow
} }
// Make sure we can write to the PID file. // Make sure we can write to the PID file.
if (!$debug) { if (!$this->debug) {
Filesystem::writeFile($this->getPIDPath(), ''); Filesystem::writeFile($this->getPIDPath(), '');
} }
// First, start the server in configuration test mode with --test. This // First, start the server in configuration test mode with --test. This
// will let us error explicitly if there are missing modules, before we // will let us error explicitly if there are missing modules, before we
// fork and lose access to the console. // fork and lose access to the console.
$test_argv = $this->getServerArgv($debug); $test_argv = $this->getServerArgv();
$test_argv[] = '--test=true'; $test_argv[] = '--test=true';
execx( execx(
@ -87,7 +111,7 @@ abstract class PhabricatorAphlictManagementWorkflow
$test_argv); $test_argv);
} }
private function getServerArgv($debug) { private function getServerArgv() {
$ssl_key = PhabricatorEnv::getEnvConfig('notification.ssl-key'); $ssl_key = PhabricatorEnv::getEnvConfig('notification.ssl-key');
$ssl_cert = PhabricatorEnv::getEnvConfig('notification.ssl-cert'); $ssl_cert = PhabricatorEnv::getEnvConfig('notification.ssl-cert');
@ -100,9 +124,9 @@ abstract class PhabricatorAphlictManagementWorkflow
$log = PhabricatorEnv::getEnvConfig('notification.log'); $log = PhabricatorEnv::getEnvConfig('notification.log');
$server_argv = array(); $server_argv = array();
$server_argv[] = '--port='.$client_uri->getPort(); $server_argv[] = '--client-port='.$client_uri->getPort();
$server_argv[] = '--admin='.$server_uri->getPort(); $server_argv[] = '--admin-port='.$server_uri->getPort();
$server_argv[] = '--host='.$server_uri->getDomain(); $server_argv[] = '--admin-host='.$server_uri->getDomain();
if ($ssl_key) { if ($ssl_key) {
$server_argv[] = '--ssl-key='.$ssl_key; $server_argv[] = '--ssl-key='.$ssl_key;
@ -112,10 +136,14 @@ abstract class PhabricatorAphlictManagementWorkflow
$server_argv[] = '--ssl-cert='.$ssl_cert; $server_argv[] = '--ssl-cert='.$ssl_cert;
} }
if (!$debug) { if (!$this->debug) {
$server_argv[] = '--log='.$log; $server_argv[] = '--log='.$log;
} }
if ($this->clientHost) {
$server_argv[] = '--client-host='.$this->clientHost;
}
return $server_argv; return $server_argv;
} }
@ -124,10 +152,10 @@ abstract class PhabricatorAphlictManagementWorkflow
return $root.'/support/aphlict/server/aphlict_server.js'; return $root.'/support/aphlict/server/aphlict_server.js';
} }
final protected function launch($debug = false) { final protected function launch() {
$console = PhutilConsole::getConsole(); $console = PhutilConsole::getConsole();
if ($debug) { if ($this->debug) {
$console->writeOut(pht("Starting Aphlict server in foreground...\n")); $console->writeOut(pht("Starting Aphlict server in foreground...\n"));
} else { } else {
Filesystem::writeFile($this->getPIDPath(), getmypid()); Filesystem::writeFile($this->getPIDPath(), getmypid());
@ -137,16 +165,16 @@ abstract class PhabricatorAphlictManagementWorkflow
'%s %s %Ls', '%s %s %Ls',
$this->getNodeBinary(), $this->getNodeBinary(),
$this->getAphlictScriptPath(), $this->getAphlictScriptPath(),
$this->getServerArgv($debug)); $this->getServerArgv());
if (!$debug) { if (!$this->debug) {
declare(ticks = 1); declare(ticks = 1);
pcntl_signal(SIGINT, array($this, 'cleanup')); pcntl_signal(SIGINT, array($this, 'cleanup'));
pcntl_signal(SIGTERM, array($this, 'cleanup')); pcntl_signal(SIGTERM, array($this, 'cleanup'));
} }
register_shutdown_function(array($this, 'cleanup')); register_shutdown_function(array($this, 'cleanup'));
if ($debug) { if ($this->debug) {
$console->writeOut("Launching server:\n\n $ ".$command."\n\n"); $console->writeOut("Launching server:\n\n $ ".$command."\n\n");
$err = phutil_passthru('%C', $command); $err = phutil_passthru('%C', $command);

View file

@ -9,9 +9,10 @@ JX.require('lib/AphlictLog', __dirname);
function parse_command_line_arguments(argv) { function parse_command_line_arguments(argv) {
var config = { var config = {
port: 22280, 'client-port': 22280,
admin: 22281, 'admin-port': 22281,
host: '127.0.0.1', '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, 'ssl-key': null,
'ssl-cert': null, 'ssl-cert': null,
@ -30,8 +31,8 @@ function parse_command_line_arguments(argv) {
config[matches[1]] = matches[2]; config[matches[1]] = matches[2];
} }
config.port = parseInt(config.port, 10); config['client-port'] = parseInt(config['client-port'], 10);
config.admin = parseInt(config.admin, 10); config['admin-port'] = parseInt(config['admin-port'], 10);
return config; return config;
} }
@ -95,11 +96,16 @@ if (ssl_config.enabled) {
var https_server = https.createServer({ var https_server = https.createServer({
key: ssl_config.key, key: ssl_config.key,
cert: ssl_config.cert cert: ssl_config.cert
}, https_discard_handler).listen(config.port); }, https_discard_handler).listen(
config['client-port'],
config['client-host']);
ws = new WebSocket.Server({server: https_server}); ws = new WebSocket.Server({server: https_server});
} else { } else {
ws = new WebSocket.Server({port: config.port}); ws = new WebSocket.Server({
port: config['client-port'],
host: config['client-host'],
});
} }
ws.on('connection', function(ws) { ws.on('connection', function(ws) {
@ -234,6 +240,6 @@ http.createServer(function(request, response) {
response.writeHead(404, 'Not Found'); response.writeHead(404, 'Not Found');
response.end(); response.end();
} }
}).listen(config.admin, config.host); }).listen(config['admin-port'], config['admin-host']);
debug.log('Started Server (PID %d)', process.pid); debug.log('Started Server (PID %d)', process.pid);