Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/portable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Civ13/Civ13.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/Civ13/HostedVerificationSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
3 changes: 2 additions & 1 deletion src/Civ13/MessageCommand/Civ13MessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
25 changes: 22 additions & 3 deletions src/Civ13/ServerDirectory/SystemDns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
{
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/Civ14/GameServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

/**
Expand Down
52 changes: 51 additions & 1 deletion tests/system-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Loading