diff --git a/src/aphront/AphrontRequest.php b/src/aphront/AphrontRequest.php --- a/src/aphront/AphrontRequest.php +++ b/src/aphront/AphrontRequest.php @@ -557,11 +557,13 @@ } public function getRemoteAddress() { - $address = $_SERVER['REMOTE_ADDR']; - if (!strlen($address)) { + $address = PhabricatorEnv::getRemoteAddress(); + + if (!$address) { return null; } - return substr($address, 0, 64); + + return $address->getAddress(); } public function isHTTPS() { diff --git a/src/aphront/configuration/AphrontApplicationConfiguration.php b/src/aphront/configuration/AphrontApplicationConfiguration.php --- a/src/aphront/configuration/AphrontApplicationConfiguration.php +++ b/src/aphront/configuration/AphrontApplicationConfiguration.php @@ -106,10 +106,18 @@ PhabricatorAccessLog::init(); $access_log = PhabricatorAccessLog::getLog(); PhabricatorStartup::setAccessLog($access_log); + + $address = PhabricatorEnv::getRemoteAddress(); + if ($address) { + $address_string = $address->getAddress(); + } else { + $address_string = '-'; + } + $access_log->setData( array( 'R' => AphrontRequest::getHTTPHeader('Referer', '-'), - 'r' => idx($_SERVER, 'REMOTE_ADDR', '-'), + 'r' => $address_string, 'M' => idx($_SERVER, 'REQUEST_METHOD', '-'), )); diff --git a/src/applications/people/storage/PhabricatorUserLog.php b/src/applications/people/storage/PhabricatorUserLog.php --- a/src/applications/people/storage/PhabricatorUserLog.php +++ b/src/applications/people/storage/PhabricatorUserLog.php @@ -108,18 +108,28 @@ $log->setUserPHID((string)$object_phid); $log->setAction($action); - $log->remoteAddr = (string)idx($_SERVER, 'REMOTE_ADDR', ''); + $address = PhabricatorEnv::getRemoteAddress(); + if ($address) { + $log->remoteAddr = $address->getAddress(); + } else { + $log->remoteAddr = ''; + } return $log; } public static function loadRecentEventsFromThisIP($action, $timespan) { + $address = PhabricatorEnv::getRemoteAddress(); + if (!$address) { + return array(); + } + return id(new PhabricatorUserLog())->loadAllWhere( 'action = %s AND remoteAddr = %s AND dateCreated > %d ORDER BY dateCreated DESC', $action, - idx($_SERVER, 'REMOTE_ADDR'), - time() - $timespan); + $address->getAddress(), + PhabricatorTime::getNow() - $timespan); } public function save() { diff --git a/src/infrastructure/env/PhabricatorEnv.php b/src/infrastructure/env/PhabricatorEnv.php --- a/src/infrastructure/env/PhabricatorEnv.php +++ b/src/infrastructure/env/PhabricatorEnv.php @@ -818,12 +818,12 @@ return false; } - $address = idx($_SERVER, 'REMOTE_ADDR'); + $address = self::getRemoteAddress(); if (!$address) { throw new Exception( pht( 'Unable to test remote address against cluster whitelist: '. - 'REMOTE_ADDR is not defined.')); + 'REMOTE_ADDR is not defined or not valid.')); } return self::isClusterAddress($address); @@ -844,6 +844,19 @@ ->containsAddress($address); } + public static function getRemoteAddress() { + $address = idx($_SERVER, 'REMOTE_ADDR'); + if (!$address) { + return null; + } + + try { + return PhutilIPAddress::newAddress($address); + } catch (Exception $ex) { + return null; + } + } + /* -( Internals )---------------------------------------------------------- */ diff --git a/src/infrastructure/ssh/PhabricatorSSHWorkflow.php b/src/infrastructure/ssh/PhabricatorSSHWorkflow.php --- a/src/infrastructure/ssh/PhabricatorSSHWorkflow.php +++ b/src/infrastructure/ssh/PhabricatorSSHWorkflow.php @@ -95,7 +95,13 @@ // This has the format " ". Grab the IP. $remote_address = head(explode(' ', $ssh_client)); - return $remote_address; + try { + $address = PhutilIPAddress::newAddress($remote_address); + } catch (Exception $ex) { + return null; + } + + return $address->getAddress(); } }