@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Store pusher remote address and push protocol in PushLog

Summary: Ref T4195. Stores remote address and protocol in the logs, where possible.

Test Plan: Pushed some stuff, looked at the log, saw data.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4195

Differential Revision: https://secure.phabricator.com/D7711

+80 -13
+15 -4
scripts/repository/commit_hook.php
··· 30 30 // Figure out which user is writing the commit. 31 31 32 32 if ($repository->isGit() || $repository->isHg()) { 33 - $username = getenv('PHABRICATOR_USER'); 33 + $username = getenv(DiffusionCommitHookEngine::ENV_USER); 34 34 if (!strlen($username)) { 35 - throw new Exception(pht('usage: PHABRICATOR_USER should be defined!')); 35 + throw new Exception( 36 + pht('usage: %s should be defined!', DiffusionCommitHookEngine::ENV_USER)); 36 37 } 37 38 38 39 // TODO: If this is a Mercurial repository, the hook we're responding to ··· 41 42 42 43 } else if ($repository->isSVN()) { 43 44 // NOTE: In Subversion, the entire environment gets wiped so we can't read 44 - // PHABRICATOR_USER. Instead, we've set "--tunnel-user" to specify the 45 - // correct user; read this user out of the commit log. 45 + // DiffusionCommitHookEngine::ENV_USER. Instead, we've set "--tunnel-user" to 46 + // specify the correct user; read this user out of the commit log. 46 47 47 48 if ($argc < 4) { 48 49 throw new Exception(pht('usage: commit-hook <callsign> <repo> <txn>')); ··· 85 86 } 86 87 87 88 $engine->setStdin($stdin); 89 + 90 + $remote_address = getenv(DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS); 91 + if (strlen($remote_address)) { 92 + $engine->setRemoteAddress($remote_address); 93 + } 94 + 95 + $remote_protocol = getenv(DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL); 96 + if (strlen($remote_protocol)) { 97 + $engine->setRemoteProtocol($remote_protocol); 98 + } 88 99 89 100 try { 90 101 $err = $engine->execute();
+8
src/applications/diffusion/controller/DiffusionPushLogListController.php
··· 41 41 ), 42 42 $callsign), 43 43 $this->getHandle($log->getPusherPHID())->renderLink(), 44 + $log->getRemoteAddress() 45 + ? long2ip($log->getRemoteAddress()) 46 + : null, 47 + $log->getRemoteProtocol(), 44 48 $log->getRefType(), 45 49 $log->getRefName(), 46 50 $log->getRefOldShort(), ··· 54 58 array( 55 59 pht('Repository'), 56 60 pht('Pusher'), 61 + pht('From'), 62 + pht('Via'), 57 63 pht('Type'), 58 64 pht('Name'), 59 65 pht('Old'), ··· 62 68 )) 63 69 ->setColumnClasses( 64 70 array( 71 + '', 72 + '', 65 73 '', 66 74 '', 67 75 '',
+12 -5
src/applications/diffusion/controller/DiffusionServeController.php
··· 318 318 'PATH_INFO' => $request_path, 319 319 320 320 'REMOTE_USER' => $viewer->getUsername(), 321 - 'PHABRICATOR_USER' => $viewer->getUsername(), 322 321 323 322 // TODO: Set these correctly. 324 323 // GIT_COMMITTER_NAME 325 324 // GIT_COMMITTER_EMAIL 326 - ); 325 + ) + $this->getCommonEnvironment($viewer); 327 326 328 327 $input = PhabricatorStartup::getRawInput(); 329 328 ··· 416 415 throw new Exception("Unable to find `hg` in PATH!"); 417 416 } 418 417 419 - $env = array( 420 - 'PHABRICATOR_USER' => $viewer->getUsername(), 421 - ); 418 + $env = $this->getCommonEnvironment($viewer); 422 419 $input = PhabricatorStartup::getRawInput(); 423 420 424 421 $cmd = $request->getStr('cmd'); ··· 555 552 $is_hangup = preg_match($stderr_regexp, $stderr); 556 553 557 554 return $has_pack && $is_hangup; 555 + } 556 + 557 + private function getCommonEnvironment(PhabricatorUser $viewer) { 558 + $remote_addr = $this->getRequest()->getRemoteAddr(); 559 + 560 + return array( 561 + DiffusionCommitHookEngine::ENV_USER => $viewer->getUsername(), 562 + DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS => $remote_addr, 563 + DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'http', 564 + ); 558 565 } 559 566 560 567 }
+33 -2
src/applications/diffusion/engine/DiffusionCommitHookEngine.php
··· 7 7 */ 8 8 final class DiffusionCommitHookEngine extends Phobject { 9 9 10 + const ENV_USER = 'PHABRICATOR_USER'; 11 + const ENV_REMOTE_ADDRESS = 'PHABRICATOR_REMOTE_ADDRESS'; 12 + const ENV_REMOTE_PROTOCOL = 'PHABRICATOR_REMOTE_PROTOCOL'; 13 + 10 14 private $viewer; 11 15 private $repository; 12 16 private $stdin; 13 17 private $subversionTransaction; 14 18 private $subversionRepository; 19 + private $remoteAddress; 20 + private $remoteProtocol; 15 21 22 + public function setRemoteProtocol($remote_protocol) { 23 + $this->remoteProtocol = $remote_protocol; 24 + return $this; 25 + } 26 + 27 + public function getRemoteProtocol() { 28 + return $this->remoteProtocol; 29 + } 30 + 31 + public function setRemoteAddress($remote_address) { 32 + $this->remoteAddress = $remote_address; 33 + return $this; 34 + } 35 + 36 + public function getRemoteAddress() { 37 + return $this->remoteAddress; 38 + } 16 39 17 40 public function setSubversionTransactionInfo($transaction, $repository) { 18 41 $this->subversionTransaction = $transaction; ··· 86 109 $transaction_key = PhabricatorHash::digestForIndex( 87 110 Filesystem::readRandomBytes(64)); 88 111 112 + // If whatever we have here isn't a valid IPv4 address, just store `null`. 113 + // Older versions of PHP return `-1` on failure instead of `false`. 114 + $remote_address = $this->getRemoteAddress(); 115 + $remote_address = max(0, ip2long($remote_address)); 116 + $remote_address = nonempty($remote_address, null); 117 + 118 + $remote_protocol = $this->getRemoteProtocol(); 119 + 89 120 $logs = array(); 90 121 foreach ($updates as $update) { 91 122 $log = PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer()) 92 123 ->setRepositoryPHID($this->getRepository()->getPHID()) 93 124 ->setEpoch(time()) 94 - ->setRemoteAddress(null) // TODO: Populate this where possible. 95 - ->setRemoteProtocol(null) // TODO: Populate this where possible. 125 + ->setRemoteAddress($remote_address) 126 + ->setRemoteProtocol($remote_protocol) 96 127 ->setTransactionKey($transaction_key) 97 128 ->setRefType($update['type']) 98 129 ->setRefNameHash(PhabricatorHash::digestForIndex($update['ref']))
+12 -2
src/applications/diffusion/ssh/DiffusionSSHWorkflow.php
··· 18 18 } 19 19 20 20 public function getEnvironment() { 21 - return array( 22 - 'PHABRICATOR_USER' => $this->getUser()->getUsername(), 21 + $env = array( 22 + DiffusionCommitHookEngine::ENV_USER => $this->getUser()->getUsername(), 23 + DiffusionCommitHookEngine::ENV_REMOTE_PROTOCOL => 'ssh', 23 24 ); 25 + 26 + $ssh_client = getenv('SSH_CLIENT'); 27 + if ($ssh_client) { 28 + // This has the format "<ip> <remote-port> <local-port>". Grab the IP. 29 + $remote_address = head(explode(' ', $ssh_client)); 30 + $env[DiffusionCommitHookEngine::ENV_REMOTE_ADDRESS] = $remote_address; 31 + } 32 + 33 + return $env; 24 34 } 25 35 26 36 abstract protected function executeRepositoryOperations();