From 638cf20c9de4d982249f3d8b9d533e34492f52b9 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Fri, 9 Jan 2015 09:59:33 +1100 Subject: [PATCH] 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 --- ...bricatorAphlictManagementDebugWorkflow.php | 11 ++-- ...icatorAphlictManagementRestartWorkflow.php | 6 ++- ...bricatorAphlictManagementStartWorkflow.php | 5 +- .../PhabricatorAphlictManagementWorkflow.php | 54 ++++++++++++++----- support/aphlict/server/aphlict_server.js | 22 +++++--- 5 files changed, 69 insertions(+), 29 deletions(-) diff --git a/src/applications/aphlict/management/PhabricatorAphlictManagementDebugWorkflow.php b/src/applications/aphlict/management/PhabricatorAphlictManagementDebugWorkflow.php index 510e95d4c9..269301fddc 100644 --- a/src/applications/aphlict/management/PhabricatorAphlictManagementDebugWorkflow.php +++ b/src/applications/aphlict/management/PhabricatorAphlictManagementDebugWorkflow.php @@ -4,18 +4,21 @@ final class PhabricatorAphlictManagementDebugWorkflow extends PhabricatorAphlictManagementWorkflow { public function didConstruct() { + parent::didConstruct(); $this ->setName('debug') ->setSynopsis( pht( 'Start the notifications server in the foreground and print large '. - 'volumes of diagnostic information to the console.')) - ->setArguments(array()); + 'volumes of diagnostic information to the console.')); } public function execute(PhutilArgumentParser $args) { - $this->willLaunch(true); - return $this->launch(true); + parent::execute($args); + $this->setDebug(true); + + $this->willLaunch(); + return $this->launch(); } } diff --git a/src/applications/aphlict/management/PhabricatorAphlictManagementRestartWorkflow.php b/src/applications/aphlict/management/PhabricatorAphlictManagementRestartWorkflow.php index 4772587b5f..8d96ecd988 100644 --- a/src/applications/aphlict/management/PhabricatorAphlictManagementRestartWorkflow.php +++ b/src/applications/aphlict/management/PhabricatorAphlictManagementRestartWorkflow.php @@ -4,13 +4,15 @@ final class PhabricatorAphlictManagementRestartWorkflow extends PhabricatorAphlictManagementWorkflow { public function didConstruct() { + parent::didConstruct(); $this ->setName('restart') - ->setSynopsis(pht('Stop, then start the notifications server.')) - ->setArguments(array()); + ->setSynopsis(pht('Stop, then start the notifications server.')); } public function execute(PhutilArgumentParser $args) { + parent::execute($args); + $err = $this->executeStopCommand(); if ($err) { return $err; diff --git a/src/applications/aphlict/management/PhabricatorAphlictManagementStartWorkflow.php b/src/applications/aphlict/management/PhabricatorAphlictManagementStartWorkflow.php index 38c9ab88ea..3e1ab8a135 100644 --- a/src/applications/aphlict/management/PhabricatorAphlictManagementStartWorkflow.php +++ b/src/applications/aphlict/management/PhabricatorAphlictManagementStartWorkflow.php @@ -4,13 +4,14 @@ final class PhabricatorAphlictManagementStartWorkflow extends PhabricatorAphlictManagementWorkflow { public function didConstruct() { + parent::didConstruct(); $this ->setName('start') - ->setSynopsis(pht('Start the notifications server.')) - ->setArguments(array()); + ->setSynopsis(pht('Start the notifications server.')); } public function execute(PhutilArgumentParser $args) { + parent::execute($args); return $this->executeStartCommand(); } diff --git a/src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php b/src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php index abf77b11f2..cc21d68d6f 100644 --- a/src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php +++ b/src/applications/aphlict/management/PhabricatorAphlictManagementWorkflow.php @@ -3,6 +3,26 @@ abstract class PhabricatorAphlictManagementWorkflow 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() { return PhabricatorEnv::getEnvConfig('notification.pidfile'); } @@ -27,6 +47,10 @@ abstract class PhabricatorAphlictManagementWorkflow exit(1); } + protected final function setDebug($debug) { + $this->debug = $debug; + } + public static function requireExtensions() { self::mustHaveExtension('pcntl'); self::mustHaveExtension('posix'); @@ -50,7 +74,7 @@ abstract class PhabricatorAphlictManagementWorkflow } } - final protected function willLaunch($debug = false) { + final protected function willLaunch() { $console = PhutilConsole::getConsole(); $pid = $this->getPID(); @@ -70,14 +94,14 @@ abstract class PhabricatorAphlictManagementWorkflow } // Make sure we can write to the PID file. - if (!$debug) { + if (!$this->debug) { Filesystem::writeFile($this->getPIDPath(), ''); } // First, start the server in configuration test mode with --test. This // will let us error explicitly if there are missing modules, before we // fork and lose access to the console. - $test_argv = $this->getServerArgv($debug); + $test_argv = $this->getServerArgv(); $test_argv[] = '--test=true'; execx( @@ -87,7 +111,7 @@ abstract class PhabricatorAphlictManagementWorkflow $test_argv); } - private function getServerArgv($debug) { + private function getServerArgv() { $ssl_key = PhabricatorEnv::getEnvConfig('notification.ssl-key'); $ssl_cert = PhabricatorEnv::getEnvConfig('notification.ssl-cert'); @@ -100,9 +124,9 @@ abstract class PhabricatorAphlictManagementWorkflow $log = PhabricatorEnv::getEnvConfig('notification.log'); $server_argv = array(); - $server_argv[] = '--port='.$client_uri->getPort(); - $server_argv[] = '--admin='.$server_uri->getPort(); - $server_argv[] = '--host='.$server_uri->getDomain(); + $server_argv[] = '--client-port='.$client_uri->getPort(); + $server_argv[] = '--admin-port='.$server_uri->getPort(); + $server_argv[] = '--admin-host='.$server_uri->getDomain(); if ($ssl_key) { $server_argv[] = '--ssl-key='.$ssl_key; @@ -112,10 +136,14 @@ abstract class PhabricatorAphlictManagementWorkflow $server_argv[] = '--ssl-cert='.$ssl_cert; } - if (!$debug) { + if (!$this->debug) { $server_argv[] = '--log='.$log; } + if ($this->clientHost) { + $server_argv[] = '--client-host='.$this->clientHost; + } + return $server_argv; } @@ -124,10 +152,10 @@ abstract class PhabricatorAphlictManagementWorkflow return $root.'/support/aphlict/server/aphlict_server.js'; } - final protected function launch($debug = false) { + final protected function launch() { $console = PhutilConsole::getConsole(); - if ($debug) { + if ($this->debug) { $console->writeOut(pht("Starting Aphlict server in foreground...\n")); } else { Filesystem::writeFile($this->getPIDPath(), getmypid()); @@ -137,16 +165,16 @@ abstract class PhabricatorAphlictManagementWorkflow '%s %s %Ls', $this->getNodeBinary(), $this->getAphlictScriptPath(), - $this->getServerArgv($debug)); + $this->getServerArgv()); - if (!$debug) { + if (!$this->debug) { declare(ticks = 1); pcntl_signal(SIGINT, array($this, 'cleanup')); pcntl_signal(SIGTERM, array($this, 'cleanup')); } register_shutdown_function(array($this, 'cleanup')); - if ($debug) { + if ($this->debug) { $console->writeOut("Launching server:\n\n $ ".$command."\n\n"); $err = phutil_passthru('%C', $command); diff --git a/support/aphlict/server/aphlict_server.js b/support/aphlict/server/aphlict_server.js index d4c94ca831..9649c50c8a 100644 --- a/support/aphlict/server/aphlict_server.js +++ b/support/aphlict/server/aphlict_server.js @@ -9,9 +9,10 @@ JX.require('lib/AphlictLog', __dirname); function parse_command_line_arguments(argv) { var config = { - port: 22280, - admin: 22281, - host: '127.0.0.1', + 'client-port': 22280, + 'admin-port': 22281, + 'client-host': '0.0.0.0', + 'admin-host': '127.0.0.1', log: '/var/log/aphlict.log', 'ssl-key': null, 'ssl-cert': null, @@ -30,8 +31,8 @@ function parse_command_line_arguments(argv) { config[matches[1]] = matches[2]; } - config.port = parseInt(config.port, 10); - config.admin = parseInt(config.admin, 10); + config['client-port'] = parseInt(config['client-port'], 10); + config['admin-port'] = parseInt(config['admin-port'], 10); return config; } @@ -95,11 +96,16 @@ if (ssl_config.enabled) { var https_server = https.createServer({ key: ssl_config.key, 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}); } 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) { @@ -234,6 +240,6 @@ http.createServer(function(request, response) { response.writeHead(404, 'Not Found'); response.end(); } -}).listen(config.admin, config.host); +}).listen(config['admin-port'], config['admin-host']); debug.log('Started Server (PID %d)', process.pid);