CheckServerResponseTrait.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use Generator;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\Http\Client\IResponse;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use Psr\Log\LoggerInterface;
  32. /**
  33. * Common trait for setup checks that need to use requests to the same server and check the response
  34. */
  35. trait CheckServerResponseTrait {
  36. protected IConfig $config;
  37. protected IURLGenerator $urlGenerator;
  38. protected IClientService $clientService;
  39. protected IL10N $l10n;
  40. protected LoggerInterface $logger;
  41. /**
  42. * Common helper string in case a check could not fetch any results
  43. */
  44. protected function serverConfigHelp(): string {
  45. 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.');
  46. }
  47. /**
  48. * Get all possible URLs that need to be checked for a local request test.
  49. * This takes all `trusted_domains` and the CLI overwrite URL into account.
  50. *
  51. * @param string $url The relative URL to test
  52. * @return string[] List of possible absolute URLs
  53. */
  54. protected function getTestUrls(string $url): array {
  55. $hosts = $this->config->getSystemValue('trusted_domains', []);
  56. $cliUrl = $this->config->getSystemValue('overwrite.cli.url', '');
  57. if ($cliUrl !== '') {
  58. $hosts[] = $cliUrl;
  59. }
  60. $testUrls = array_merge(
  61. [$this->urlGenerator->getAbsoluteURL($url)],
  62. array_map(fn (string $host): string => $host . $url, $hosts),
  63. );
  64. return $testUrls;
  65. }
  66. /**
  67. * Run a HTTP request to check header
  68. * @param string $method The HTTP method to use
  69. * @param string $url The relative URL to check
  70. * @param array{ignoreSSL?: bool, httpErrors?: bool, options?: array} $options Additional options, like
  71. * [
  72. * // Ignore invalid SSL certificates (e.g. self signed)
  73. * 'ignoreSSL' => true,
  74. * // Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
  75. * 'httpErrors' => true,
  76. * ]
  77. *
  78. * @return Generator<int, IResponse>
  79. */
  80. protected function runRequest(string $method, string $url, array $options = []): Generator {
  81. $options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options);
  82. $client = $this->clientService->newClient();
  83. $requestOptions = $this->getRequestOptions($options['ignoreSSL'], $options['httpErrors']);
  84. $requestOptions = array_merge($requestOptions, $options['options'] ?? []);
  85. foreach ($this->getTestUrls($url) as $testURL) {
  86. try {
  87. yield $client->request($method, $testURL, $requestOptions);
  88. } catch (\Throwable $e) {
  89. $this->logger->debug('Can not connect to local server for running setup checks', ['exception' => $e, 'url' => $testURL]);
  90. }
  91. }
  92. }
  93. /**
  94. * Run a HEAD request to check header
  95. * @param string $url The relative URL to check
  96. * @param bool $ignoreSSL Ignore SSL certificates
  97. * @param bool $httpErrors Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
  98. * @return Generator<int, IResponse>
  99. */
  100. protected function runHEAD(string $url, bool $ignoreSSL = true, bool $httpErrors = true): Generator {
  101. return $this->runRequest('HEAD', $url, ['ignoreSSL' => $ignoreSSL, 'httpErrors' => $httpErrors]);
  102. }
  103. protected function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array {
  104. $requestOptions = [
  105. 'connect_timeout' => 10,
  106. 'http_errors' => $httpErrors,
  107. 'nextcloud' => [
  108. 'allow_local_address' => true,
  109. ],
  110. ];
  111. if ($ignoreSSL) {
  112. $requestOptions['verify'] = false;
  113. }
  114. return $requestOptions;
  115. }
  116. }