From f171efbe86b8edcf9d430ec185ab21b6b726a62c Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 5 Feb 2026 14:23:41 +0100 Subject: [PATCH 1/2] Support mongodb+srv connection string in connection test tool --- tools/connect.php | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/tools/connect.php b/tools/connect.php index 1b7dfa567..d3089ed30 100644 --- a/tools/connect.php +++ b/tools/connect.php @@ -8,8 +8,15 @@ function getHosts(string $uri): array $parsed = parse_url($uri); - if (isset($parsed['scheme']) && $parsed['scheme'] !== 'mongodb') { - // TODO: Resolve SRV records (https://github.com/mongodb/specifications/blob/master/source/initial-dns-seedlist-discovery/initial-dns-seedlist-discovery.md) + if (! isset($parsed['scheme'])) { + throw new RuntimeException('URI must contain a scheme'); + } + + if ($parsed['scheme'] === 'mongodb+srv') { + return getHostsFromSrv($uri); + } + + if ($parsed['scheme'] !== 'mongodb') { throw new RuntimeException('Unsupported scheme: ' . $parsed['scheme']); } @@ -18,6 +25,36 @@ function getHosts(string $uri): array return explode(',', $hosts); } +/** + * Resolves SRV records for a MongoDB+SRV URI according to the specification: + * https://github.com/mongodb/specifications/blob/master/source/initial-dns-seedlist-discovery/initial-dns-seedlist-discovery.md + * + * @return list List of host:port strings + */ +function getHostsFromSrv(string $uri): array +{ + $parsed = parse_url($uri); + if (! isset($parsed['host'])) { + throw new RuntimeException('SRV URI must contain a host'); + } + + $domain = $parsed['host']; + $srvRecord = '_mongodb._tcp.' . $domain; + + $records = dns_get_record($srvRecord, DNS_SRV); + if ($records === false || count($records) === 0) { + throw new RuntimeException('No SRV records found for ' . $srvRecord); + } + + $hosts = []; + foreach ($records as $record) { + $port = $record['port'] ?? 27017; + $hosts[] = $record['target'] . ':' . $port; + } + + return $hosts; +} + /** @param resource $stream */ function streamWrite($stream, string $data): int { @@ -127,7 +164,14 @@ function connect(string $host, bool $ssl): void $uri = $argv[1] ?? 'mongodb://127.0.0.1'; printf("Looking up MongoDB at %s\n", $uri); $hosts = getHosts($uri); -$ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=true') !== false; + +if (str_starts_with($uri, 'mongodb+srv://')) { + // For mongodb+srv, SSL is always true unless explicitly set to false + $ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=false') !== false; +} else { + // For other connection strings, SSL is true if indicated + $ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=true') !== false; +} printf("Found %d host(s) in the URI. Will attempt to connect to each.\n", count($hosts)); From 6e0aac063e9c7efcdfe9aa7601ad2c011e73ef82 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 6 Feb 2026 10:37:52 +0100 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jérôme Tamarelle Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/connect.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/connect.php b/tools/connect.php index d3089ed30..2fdfec531 100644 --- a/tools/connect.php +++ b/tools/connect.php @@ -38,21 +38,17 @@ function getHostsFromSrv(string $uri): array throw new RuntimeException('SRV URI must contain a host'); } - $domain = $parsed['host']; - $srvRecord = '_mongodb._tcp.' . $domain; + $srvRecord = '_mongodb._tcp.' . $parsed['host']; $records = dns_get_record($srvRecord, DNS_SRV); if ($records === false || count($records) === 0) { throw new RuntimeException('No SRV records found for ' . $srvRecord); } - $hosts = []; - foreach ($records as $record) { - $port = $record['port'] ?? 27017; - $hosts[] = $record['target'] . ':' . $port; - } - - return $hosts; + return array_map( + static fn (array $record) => $record['target'] . ':' . ($record['port'] ?? 27017), + $records, + ); } /** @param resource $stream */ @@ -167,7 +163,7 @@ function connect(string $host, bool $ssl): void if (str_starts_with($uri, 'mongodb+srv://')) { // For mongodb+srv, SSL is always true unless explicitly set to false - $ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=false') !== false; + $ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=false') === false; } else { // For other connection strings, SSL is true if indicated $ssl = stripos(parse_url($uri, PHP_URL_QUERY) ?? '', 'ssl=true') !== false;