diff --git a/.github/workflows/portable.yml b/.github/workflows/portable.yml index accbeaa9..57f3966e 100644 --- a/.github/workflows/portable.yml +++ b/.github/workflows/portable.yml @@ -26,6 +26,7 @@ jobs: - run: php tests/windows/default-branch.php - run: php tests/windows/lock-path-guard.php - run: php tests/windows/hosted-verifier.php + - run: php tests/system-dns.php - name: Synthetic authenticated mapping TLS if: runner.os == 'Linux' timeout-minutes: 2 diff --git a/src/Civ13/Civ13.php b/src/Civ13/Civ13.php index 313364fb..6cf3c785 100644 --- a/src/Civ13/Civ13.php +++ b/src/Civ13/Civ13.php @@ -40,6 +40,7 @@ use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Psr\Log\LoggerInterface; +use Civ13\ServerDirectory\SystemDns; use React\Cache\CacheInterface; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; @@ -770,7 +771,7 @@ private function resolveOptions(array &$options = []): array } $options['loop'] = $options['loop'] ?? Loop::get(); - $options['browser'] = $options['browser'] ?? new Browser($options['loop']); + $options['browser'] = $options['browser'] ?? SystemDns::browser($options['loop']); $options['filesystem'] = $options['filesystem'] ?? FileSystemFactory::create(); return $options; diff --git a/src/Civ13/HostedVerificationSource.php b/src/Civ13/HostedVerificationSource.php index eeb57815..0082a686 100644 --- a/src/Civ13/HostedVerificationSource.php +++ b/src/Civ13/HostedVerificationSource.php @@ -33,8 +33,7 @@ public static function authenticatedFetcher(\React\EventLoop\LoopInterface $loop private static function authenticatedBrowser(\React\EventLoop\LoopInterface $loop): \React\Http\Browser { - $connector = new \React\Socket\Connector(self::authenticatedConnectorOptions(), $loop); - return new \React\Http\Browser($connector, $loop); + return ServerDirectory\SystemDns::browser($loop, self::authenticatedConnectorOptions()); } /** Pure policy seam: tests must not trigger Windows system-DNS discovery subprocesses. */ diff --git a/src/Civ13/MessageCommand/Civ13MessageCommand.php b/src/Civ13/MessageCommand/Civ13MessageCommand.php index 8902da20..4080caf6 100644 --- a/src/Civ13/MessageCommand/Civ13MessageCommand.php +++ b/src/Civ13/MessageCommand/Civ13MessageCommand.php @@ -18,6 +18,7 @@ use Discord\Discord; use Discord\Parts\Channel\Message; use Psr\Log\LoggerInterface; +use Civ13\ServerDirectory\SystemDns; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\Http\Browser; @@ -72,7 +73,7 @@ protected function getBrowserProperty(): Browser { return isset($this->civ13->browser) ? $this->civ13->browser - : new Browser($this->loop ?? Loop::get()); // Workaround for PHPUnit tests + : SystemDns::browser($this->loop ?? Loop::get()); // Workaround for PHPUnit tests } /** Backs the `discord` magic property. */ diff --git a/src/Civ13/ServerDirectory/SystemDns.php b/src/Civ13/ServerDirectory/SystemDns.php index 27f5d838..ff7b322d 100644 --- a/src/Civ13/ServerDirectory/SystemDns.php +++ b/src/Civ13/ServerDirectory/SystemDns.php @@ -14,6 +14,9 @@ namespace Civ13\ServerDirectory; use React\Dns\Config\Config; +use React\EventLoop\LoopInterface; +use React\Http\Browser; +use React\Socket\Connector; /** * Reads the system DNS servers without React's WMIC lookup. @@ -27,6 +30,23 @@ final class SystemDns { private static ?Config $cached = null; + /** Build an HTTP client without asking react/socket to run its WMIC lookup. */ + public static function browser(LoopInterface $loop, array $options = []): Browser + { + $options['dns'] ??= self::connectorConfig(); + return new Browser(new Connector($options, $loop), $loop); + } + + /** Preserve react/socket's normal fallback if no system DNS servers are found. */ + public static function connectorConfig(?callable $shell = null): Config + { + $config = self::load($shell); + if (!$config->nameservers) { + $config->nameservers[] = '8.8.8.8'; + } + return $config; + } + /** @param null|callable(string): (string|false|null) $shell injected by tests */ public static function load(?callable $shell = null): Config { @@ -36,9 +56,8 @@ public static function load(?callable $shell = null): Config if (DIRECTORY_SEPARATOR !== '\\' && $shell === null) { return clone (self::$cached = Config::loadSystemConfigBlocking()); } - $output = ($shell ?? 'shell_exec')( - 'powershell -NoProfile -NonInteractive -Command "(Get-DnsClientServerAddress).ServerAddresses"' - ); + $command = 'powershell -NoProfile -NonInteractive -Command "(Get-DnsClientServerAddress).ServerAddresses" 2>NUL'; + $output = $shell !== null ? $shell($command) : (function_exists('shell_exec') ? shell_exec($command) : null); $config = new Config(); $config->nameservers = self::parse(is_string($output) ? $output : ''); if ($shell === null) { diff --git a/src/Civ14/GameServer.php b/src/Civ14/GameServer.php index 2eee634d..06fba833 100644 --- a/src/Civ14/GameServer.php +++ b/src/Civ14/GameServer.php @@ -25,6 +25,7 @@ use Discord\Parts\Embed\Embed; use Discord\Parts\Thread\Thread; use Psr\Log\LoggerInterface; +use Civ13\ServerDirectory\SystemDns; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use React\EventLoop\TimerInterface; @@ -461,7 +462,7 @@ protected function getBrowserProperty(): Browser { return isset($this->civ13->browser) ? $this->civ13->browser - : new Browser($this->loop ?? Loop::get()); // Workaround for civ13->browser property not set in PHPUnit tests + : SystemDns::browser($this->loop ?? Loop::get()); // Workaround for civ13->browser property not set in PHPUnit tests } /** diff --git a/tests/system-dns.php b/tests/system-dns.php index 2f990b22..78f574a0 100644 --- a/tests/system-dns.php +++ b/tests/system-dns.php @@ -3,7 +3,27 @@ declare(strict_types=1); // Offline check for Civ13\SystemDns: no real shell, no network. -require __DIR__ . '/../vendor/autoload.php'; +namespace Civ13\ServerDirectory { + function shell_exec(string $command): ?string + { + if (str_contains($command, 'wmic') || !str_contains($command, 'Get-DnsClientServerAddress')) { + throw new \RuntimeException('Unexpected resolver discovery command'); + } + $GLOBALS['systemDnsFixtureCalls'] = ($GLOBALS['systemDnsFixtureCalls'] ?? 0) + 1; + return "192.0.2.1\n"; + } +} + +namespace React\Dns\Config { + function shell_exec(string $command): never + { + throw new \LogicException('Implicit ReactPHP Windows DNS discovery reached'); + } +} + +namespace { +$loader = require $argv[1] ?? __DIR__ . '/../vendor/autoload.php'; +$loader->addPsr4('Civ13\\', __DIR__.'/../src/Civ13/', true); use Civ13\ServerDirectory\SystemDns; @@ -34,4 +54,34 @@ $check($config->nameservers === ['1.1.1.1'], 'PowerShell output becomes the nameserver list'); $check(!str_contains($seen, 'wmic') && str_contains($seen, 'Get-DnsClientServerAddress'), 'asks PowerShell, not WMIC'); +$check(SystemDns::connectorConfig(static fn (string $command): ?string => null)->nameservers === ['8.8.8.8'], + 'HTTP connector fallback stays equivalent to ReactPHP when discovery is empty'); +$check(SystemDns::connectorConfig(static fn (string $command): string => "192.0.2.1\r\n192.0.2.2")->nameservers === ['192.0.2.1', '192.0.2.2'], + 'HTTP connector receives all discovered nameservers in order'); + +// Construct real HTTP clients without issuing requests. The configured +// resolver branch avoids ReactPHP's implicit DNS discovery on Windows. +$loop = new React\EventLoop\StreamSelectLoop(); +set_error_handler(static function (int $no, string $message) use ($check): bool { + $check(false, "HTTP client construction emitted $message"); + return true; +}); +$check(SystemDns::browser($loop, ['dns' => false]) instanceof React\Http\Browser, 'explicit DNS option bypasses discovery'); +$check(($GLOBALS['systemDnsFixtureCalls'] ?? 0) === 0, 'explicit options did not run the Windows discovery fixture'); +$check(SystemDns::browser($loop) instanceof React\Http\Browser, 'ordinary HTTP Browser constructs with explicit DNS'); +$authenticated = new ReflectionMethod(Civ13\HostedVerificationSource::class, 'authenticatedBrowser'); +$check($authenticated->invoke(null, $loop) instanceof React\Http\Browser, 'authenticated verification Browser constructs without implicit DNS'); +restore_error_handler(); +if (DIRECTORY_SEPARATOR === '\\') { + $check(($GLOBALS['systemDnsFixtureCalls'] ?? 0) === 1, 'HTTP clients share one PowerShell discovery'); + $caught = false; + try { + new React\Http\Browser(null, $loop); + } catch (LogicException $error) { + $caught = $error->getMessage() === 'Implicit ReactPHP Windows DNS discovery reached'; + } + $check($caught, 'negative control detects an unconfigured ReactPHP Browser'); +} + exit($failures === 0 ? 0 : 1); +}