1
0

CheckServerResponseTrait.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use Generator;
  9. use OCP\Http\Client\IClientService;
  10. use OCP\Http\Client\IResponse;
  11. use OCP\IConfig;
  12. use OCP\IL10N;
  13. use OCP\IURLGenerator;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Common trait for setup checks that need to use requests to the same server and check the response
  17. */
  18. trait CheckServerResponseTrait {
  19. protected IConfig $config;
  20. protected IURLGenerator $urlGenerator;
  21. protected IClientService $clientService;
  22. protected IL10N $l10n;
  23. protected LoggerInterface $logger;
  24. /**
  25. * Common helper string in case a check could not fetch any results
  26. */
  27. protected function serverConfigHelp(): string {
  28. return $this->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.');
  29. }
  30. /**
  31. * Get all possible URLs that need to be checked for a local request test.
  32. * This takes all `trusted_domains` and the CLI overwrite URL into account.
  33. *
  34. * @param string $url The relative URL to test
  35. * @return string[] List of possible absolute URLs
  36. */
  37. protected function getTestUrls(string $url): array {
  38. $hosts = $this->config->getSystemValue('trusted_domains', []);
  39. $cliUrl = $this->config->getSystemValue('overwrite.cli.url', '');
  40. if ($cliUrl !== '') {
  41. $hosts[] = $cliUrl;
  42. }
  43. $testUrls = array_merge(
  44. [$this->urlGenerator->getAbsoluteURL($url)],
  45. array_map(fn (string $host): string => $host . $url, $hosts),
  46. );
  47. return $testUrls;
  48. }
  49. /**
  50. * Run a HTTP request to check header
  51. * @param string $method The HTTP method to use
  52. * @param string $url The relative URL to check
  53. * @param array{ignoreSSL?: bool, httpErrors?: bool, options?: array} $options Additional options, like
  54. * [
  55. * // Ignore invalid SSL certificates (e.g. self signed)
  56. * 'ignoreSSL' => true,
  57. * // Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
  58. * 'httpErrors' => true,
  59. * ]
  60. *
  61. * @return Generator<int, IResponse>
  62. */
  63. protected function runRequest(string $method, string $url, array $options = []): Generator {
  64. $options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options);
  65. $client = $this->clientService->newClient();
  66. $requestOptions = $this->getRequestOptions($options['ignoreSSL'], $options['httpErrors']);
  67. $requestOptions = array_merge($requestOptions, $options['options'] ?? []);
  68. foreach ($this->getTestUrls($url) as $testURL) {
  69. try {
  70. yield $client->request($method, $testURL, $requestOptions);
  71. } catch (\Throwable $e) {
  72. $this->logger->debug('Can not connect to local server for running setup checks', ['exception' => $e, 'url' => $testURL]);
  73. }
  74. }
  75. }
  76. /**
  77. * Run a HEAD request to check header
  78. * @param string $url The relative URL to check
  79. * @param bool $ignoreSSL Ignore SSL certificates
  80. * @param bool $httpErrors Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
  81. * @return Generator<int, IResponse>
  82. */
  83. protected function runHEAD(string $url, bool $ignoreSSL = true, bool $httpErrors = true): Generator {
  84. return $this->runRequest('HEAD', $url, ['ignoreSSL' => $ignoreSSL, 'httpErrors' => $httpErrors]);
  85. }
  86. protected function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array {
  87. $requestOptions = [
  88. 'connect_timeout' => 10,
  89. 'http_errors' => $httpErrors,
  90. 'nextcloud' => [
  91. 'allow_local_address' => true,
  92. ],
  93. ];
  94. if ($ignoreSSL) {
  95. $requestOptions['verify'] = false;
  96. }
  97. return $requestOptions;
  98. }
  99. }