From 41fb6146b08e029c5736ef01fd62c032cb5b360f Mon Sep 17 00:00:00 2001 From: John Stevenson Date: Fri, 19 Apr 2024 13:18:55 +0100 Subject: [PATCH] Improve proxy reporting in Diagnose command (#11932) --- src/Composer/Command/DiagnoseCommand.php | 40 +++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Composer/Command/DiagnoseCommand.php b/src/Composer/Command/DiagnoseCommand.php index fc594f7d9..d06031076 100644 --- a/src/Composer/Command/DiagnoseCommand.php +++ b/src/Composer/Command/DiagnoseCommand.php @@ -44,6 +44,8 @@ use Composer\XdebugHandler\XdebugHandler; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\ExecutableFinder; +use Composer\Util\Http\ProxyManager; +use Composer\Util\Http\RequestProxy; /** * @author Jordi Boggiano @@ -115,10 +117,21 @@ EOT $io->write('Checking https connectivity to packagist: ', false); $this->outputResult($this->checkHttp('https', $config)); - $opts = stream_context_get_options(StreamContextFactory::getContext('http://example.org')); - if (!empty($opts['http']['proxy'])) { + $proxyManager = ProxyManager::getInstance(); + $protos = $config->get('disable-tls') === true ? ['http'] : ['http', 'https']; + try { + foreach ($protos as $proto) { + $proxy = $proxyManager->getProxyForRequest($proto.'://repo.packagist.org'); + if ($proxy->getStatus() !== '') { + $type = $proxy->isSecure() ? 'HTTPS' : 'HTTP'; + $io->write('Checking '.$type.' proxy with '.$proto.': ', false); + $this->outputResult($this->checkHttpProxy($proxy, $proto)); + } + } + } catch (TransportException $e) { $io->write('Checking HTTP proxy: ', false); - $this->outputResult($this->checkHttpProxy()); + $status = $this->checkConnectivityAndComposerNetworkHttpEnablement(); + $this->outputResult(is_string($status) ? $status : $e); } if (count($oauth = $config->get('github-oauth')) > 0) { @@ -298,31 +311,36 @@ EOT } /** - * @return string|true|\Exception + * @return string|\Exception */ - private function checkHttpProxy() + private function checkHttpProxy(RequestProxy $proxy, string $protocol) { $result = $this->checkConnectivityAndComposerNetworkHttpEnablement(); if ($result !== true) { return $result; } - $protocol = extension_loaded('openssl') ? 'https' : 'http'; try { - $json = $this->httpDownloader->get($protocol . '://repo.packagist.org/packages.json')->decodeJson(); + $proxyStatus = $proxy->getStatus(); + + if ($proxy->isExcludedByNoProxy()) { + return 'SKIP Because repo.packagist.org is '.$proxyStatus.''; + } + + $json = $this->httpDownloader->get($protocol.'://repo.packagist.org/packages.json')->decodeJson(); $hash = reset($json['provider-includes']); $hash = $hash['sha256']; $path = str_replace('%hash%', $hash, key($json['provider-includes'])); - $provider = $this->httpDownloader->get($protocol . '://repo.packagist.org/'.$path)->getBody(); + $provider = $this->httpDownloader->get($protocol.'://repo.packagist.org/'.$path)->getBody(); if (hash('sha256', $provider) !== $hash) { - return 'It seems that your proxy is modifying http traffic on the fly'; + return 'It seems that your proxy ('.$proxyStatus.') is modifying '.$protocol.' traffic on the fly'; } + + return 'OK '.$proxyStatus.''; } catch (\Exception $e) { return $e; } - - return true; } /**