You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
6.6 KiB
163 lines
6.6 KiB
|
2 years ago
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/**
|
||
|
2 years ago
|
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
||
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
2 years ago
|
*/
|
||
|
1 year ago
|
namespace OCP\SetupCheck;
|
||
|
2 years ago
|
|
||
|
|
use Generator;
|
||
|
|
use OCP\Http\Client\IClientService;
|
||
|
|
use OCP\Http\Client\IResponse;
|
||
|
|
use OCP\IConfig;
|
||
|
|
use OCP\IURLGenerator;
|
||
|
1 year ago
|
use OCP\L10N\IFactory;
|
||
|
2 years ago
|
use Psr\Log\LoggerInterface;
|
||
|
2 years ago
|
|
||
|
|
/**
|
||
|
|
* Common trait for setup checks that need to use requests to the same server and check the response
|
||
|
1 year ago
|
* @since 31.0.0
|
||
|
2 years ago
|
*/
|
||
|
|
trait CheckServerResponseTrait {
|
||
|
|
protected IConfig $config;
|
||
|
|
protected IURLGenerator $urlGenerator;
|
||
|
|
protected IClientService $clientService;
|
||
|
2 years ago
|
protected LoggerInterface $logger;
|
||
|
2 years ago
|
|
||
|
|
/**
|
||
|
|
* Common helper string in case a check could not fetch any results
|
||
|
1 year ago
|
* @since 31.0.0
|
||
|
2 years ago
|
*/
|
||
|
|
protected function serverConfigHelp(): string {
|
||
|
1 year ago
|
$l10n = \OCP\Server::get(IFactory::class)->get('lib');
|
||
|
|
return $l10n->t('To allow this check to run you have to make sure that your Web server can connect to itself. Therefore it must be able to resolve and connect to at least one of its `trusted_domains` or the `overwrite.cli.url`. This failure may be the result of a server-side DNS mismatch or outbound firewall rule.');
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get all possible URLs that need to be checked for a local request test.
|
||
|
1 year ago
|
* This takes all `trusted_domains` and the CLI overwrite URL into account.
|
||
|
2 years ago
|
*
|
||
|
1 year ago
|
* @param string $url The absolute path (absolute URL without host but with web-root) to test starting with a /
|
||
|
|
* @param bool $isRootRequest Set to remove the web-root from URL and host (e.g. when requesting a path in the domain root like '/.well-known')
|
||
|
1 year ago
|
* @return list<string> List of possible absolute URLs
|
||
|
1 year ago
|
* @since 31.0.0
|
||
|
2 years ago
|
*/
|
||
|
1 year ago
|
protected function getTestUrls(string $url, bool $isRootRequest = false): array {
|
||
|
1 year ago
|
$url = '/' . ltrim($url, '/');
|
||
|
2 years ago
|
|
||
|
1 year ago
|
$webroot = rtrim($this->urlGenerator->getWebroot(), '/');
|
||
|
1 year ago
|
if ($isRootRequest === false && $webroot !== '' && str_starts_with($url, $webroot)) {
|
||
|
|
// The URL contains the web-root but also the base url does so,
|
||
|
|
// so we need to remove the web-root from the URL.
|
||
|
1 year ago
|
$url = substr($url, strlen($webroot));
|
||
|
|
}
|
||
|
|
|
||
|
1 year ago
|
// Base URLs to test
|
||
|
|
$baseUrls = [];
|
||
|
2 years ago
|
|
||
|
1 year ago
|
// Try overwrite.cli.url first, it’s supposed to be how the server contacts itself
|
||
|
1 year ago
|
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
|
||
|
|
if ($cliUrl !== '') {
|
||
|
1 year ago
|
// The CLI URL already contains the web-root, so we need to normalize it if requested
|
||
|
|
$baseUrls[] = $this->normalizeUrl(
|
||
|
1 year ago
|
$cliUrl,
|
||
|
1 year ago
|
$isRootRequest
|
||
|
1 year ago
|
);
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
1 year ago
|
// Try URL generator second
|
||
|
|
// The base URL also contains the webroot so also normalize it
|
||
|
|
$baseUrls[] = $this->normalizeUrl(
|
||
|
1 year ago
|
$this->urlGenerator->getBaseUrl(),
|
||
|
1 year ago
|
$isRootRequest
|
||
|
2 years ago
|
);
|
||
|
|
|
||
|
1 year ago
|
/* Last resort: trusted domains */
|
||
|
1 year ago
|
$trustedDomains = $this->config->getSystemValue('trusted_domains', []);
|
||
|
|
foreach ($trustedDomains as $host) {
|
||
|
1 year ago
|
if (str_contains($host, '*')) {
|
||
|
|
/* Ignore domains with a wildcard */
|
||
|
|
continue;
|
||
|
|
}
|
||
|
1 year ago
|
$baseUrls[] = $this->normalizeUrl("https://$host$webroot", $isRootRequest);
|
||
|
|
$baseUrls[] = $this->normalizeUrl("http://$host$webroot", $isRootRequest);
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
1 year ago
|
return array_map(fn (string $host) => $host . $url, array_values(array_unique($baseUrls)));
|
||
|
2 years ago
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
2 years ago
|
* Run a HTTP request to check header
|
||
|
|
* @param string $method The HTTP method to use
|
||
|
1 year ago
|
* @param string $url The absolute path (URL with webroot but without host) to check, can be the output of `IURLGenerator`
|
||
|
|
* @param bool $isRootRequest If set the webroot is removed from URLs to make the request target the host's root. Example usage are the /.well-known URLs in the root path.
|
||
|
|
* @param array{ignoreSSL?: bool, httpErrors?: bool, options?: array} $options HTTP client related options, like
|
||
|
1 year ago
|
* [
|
||
|
|
* // Ignore invalid SSL certificates (e.g. self signed)
|
||
|
|
* 'ignoreSSL' => true,
|
||
|
|
* // Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
|
||
|
|
* 'httpErrors' => true,
|
||
|
1 year ago
|
* // Additional options for the HTTP client (see `IClient`)
|
||
|
|
* 'options' => [],
|
||
|
1 year ago
|
* ]
|
||
|
2 years ago
|
*
|
||
|
2 years ago
|
* @return Generator<int, IResponse>
|
||
|
1 year ago
|
* @since 31.0.0
|
||
|
2 years ago
|
*/
|
||
|
1 year ago
|
protected function runRequest(string $method, string $url, array $options = [], bool $isRootRequest = false): Generator {
|
||
|
2 years ago
|
$options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options);
|
||
|
|
|
||
|
2 years ago
|
$client = $this->clientService->newClient();
|
||
|
2 years ago
|
$requestOptions = $this->getRequestOptions($options['ignoreSSL'], $options['httpErrors']);
|
||
|
|
$requestOptions = array_merge($requestOptions, $options['options'] ?? []);
|
||
|
2 years ago
|
|
||
|
1 year ago
|
foreach ($this->getTestUrls($url, $isRootRequest) as $testURL) {
|
||
|
2 years ago
|
try {
|
||
|
2 years ago
|
yield $client->request($method, $testURL, $requestOptions);
|
||
|
2 years ago
|
} catch (\Throwable $e) {
|
||
|
|
$this->logger->debug('Can not connect to local server for running setup checks', ['exception' => $e, 'url' => $testURL]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
2 years ago
|
/**
|
||
|
1 year ago
|
* Get HTTP client options
|
||
|
|
* @param bool $ignoreSSL If set SSL errors are ignored (e.g. self-signed certificates)
|
||
|
|
* @since 31.0.0
|
||
|
2 years ago
|
*/
|
||
|
1 year ago
|
private function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array {
|
||
|
2 years ago
|
$requestOptions = [
|
||
|
|
'connect_timeout' => 10,
|
||
|
2 years ago
|
'http_errors' => $httpErrors,
|
||
|
2 years ago
|
'nextcloud' => [
|
||
|
|
'allow_local_address' => true,
|
||
|
|
],
|
||
|
|
];
|
||
|
|
if ($ignoreSSL) {
|
||
|
|
$requestOptions['verify'] = false;
|
||
|
|
}
|
||
|
|
return $requestOptions;
|
||
|
|
}
|
||
|
1 year ago
|
|
||
|
|
/**
|
||
|
|
* Strip a trailing slash and remove the webroot if requested.
|
||
|
|
* @param string $url The URL to normalize. Should be an absolute URL containing scheme, host and optionally web-root.
|
||
|
|
* @param bool $removeWebroot If set the web-root is removed from the URL and an absolute URL with only the scheme and host (optional port) is returned
|
||
|
|
* @since 31.0.0
|
||
|
|
*/
|
||
|
|
private function normalizeUrl(string $url, bool $removeWebroot): string {
|
||
|
|
if ($removeWebroot) {
|
||
|
|
$segments = parse_url($url);
|
||
|
|
if (!isset($segments['scheme']) || !isset($segments['host'])) {
|
||
|
|
throw new \InvalidArgumentException('URL is missing scheme or host');
|
||
|
|
}
|
||
|
|
|
||
|
|
$port = isset($segments['port']) ? (':' . $segments['port']) : '';
|
||
|
|
return $segments['scheme'] . '://' . $segments['host'] . $port;
|
||
|
|
}
|
||
|
|
return rtrim($url, '/');
|
||
|
|
}
|
||
|
2 years ago
|
}
|