From 7ac1bead49f32e4cc448c1a945fae92316945315 Mon Sep 17 00:00:00 2001 From: netkas Date: Wed, 8 Jan 2025 14:53:48 -0500 Subject: [PATCH] Refactor DNS mocking implementation. --- .../Classes/CliCommands/InitializeCommand.php | 10 ++--- .../Configuration/InstanceConfiguration.php | 10 +++++ src/Socialbox/Classes/ServerResolver.php | 41 ++++++------------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/Socialbox/Classes/CliCommands/InitializeCommand.php b/src/Socialbox/Classes/CliCommands/InitializeCommand.php index c978e1a..c44c240 100644 --- a/src/Socialbox/Classes/CliCommands/InitializeCommand.php +++ b/src/Socialbox/Classes/CliCommands/InitializeCommand.php @@ -360,14 +360,14 @@ } } - // Handle Mock Servers environment variables (SB_INSTANCE_MOCK_SERVER_*) + // Handle Mock Servers environment variables (SB_INSTANCE_DNS_MOCK_*) $mockServers = []; foreach(self::getMockServerValues() as $mockServer) { $mockServer = explode(' ', $mockServer); if(count($mockServer) !== 2) { - Logger::getLogger()->warning(sprintf('Invalid Mock Server format: %s', implode(' ', $mockServer))); + Logger::getLogger()->warning(sprintf('Invalid DNS Mock Server format: %s', implode(' ', $mockServer))); continue; } @@ -399,7 +399,7 @@ } /** - * Retrieves all environment variable values that start with the prefix 'SB_INSTANCE_MOCK_SERVER_'. + * Retrieves all environment variable values that start with the prefix 'SB_INSTANCE_DNS_MOCK_'. * * @return array An array of environment variable values filtered by the specified prefix. */ @@ -411,7 +411,7 @@ // Filter variables that start with the specified prefix $filtered = array_filter($envVars, function ($key) { - return str_starts_with($key, 'SB_INSTANCE_MOCK_SERVER_'); + return str_starts_with($key, 'SB_INSTANCE_DNS_MOCK_'); }, ARRAY_FILTER_USE_KEY); // Return only the values as an array @@ -453,7 +453,7 @@ " SB_CACHE_USERNAME - The cache username (default: null)\n" . " SB_CACHE_PASSWORD - The cache password (default: null)\n" . " SB_CACHE_DATABASE - The cache database (default: 0)\n" . - " SB_INSTANCE_MOCK_SERVER_* - Mock server environment variables, format: ( ), eg; SB_INSTANCE_MOCK_SERVER_N64: teapot.com \n"; + " SB_INSTANCE_DNS_MOCK_* - Mock server environment variables, format: ( ), eg; SB_INSTANCE_DNS_MOCK_N64: teapot.com \n"; } /** diff --git a/src/Socialbox/Classes/Configuration/InstanceConfiguration.php b/src/Socialbox/Classes/Configuration/InstanceConfiguration.php index 431b9c4..7609e49 100644 --- a/src/Socialbox/Classes/Configuration/InstanceConfiguration.php +++ b/src/Socialbox/Classes/Configuration/InstanceConfiguration.php @@ -8,6 +8,7 @@ private string $name; private ?string $domain; private ?string $rpcEndpoint; + private array $dnsMocks; /** * Constructor that initializes object properties with the provided data. @@ -21,6 +22,7 @@ $this->name = $data['name']; $this->domain = $data['domain']; $this->rpcEndpoint = $data['rpc_endpoint']; + $this->dnsMocks = $data['dns_mocks']; } /** @@ -55,4 +57,12 @@ { return $this->rpcEndpoint; } + + /** + * @return array + */ + public function getDnsMocks(): array + { + return $this->dnsMocks; + } } \ No newline at end of file diff --git a/src/Socialbox/Classes/ServerResolver.php b/src/Socialbox/Classes/ServerResolver.php index d8338ca..c34138c 100644 --- a/src/Socialbox/Classes/ServerResolver.php +++ b/src/Socialbox/Classes/ServerResolver.php @@ -10,7 +10,6 @@ class ServerResolver { - private static bool $mockingEnabled = false; private static array $mockedRecords = []; /** @@ -25,18 +24,23 @@ */ public static function resolveDomain(string $domain, bool $useDatabase=true): DnsRecord { - // Return the mocked record if mocking is enabled - if(self::$mockingEnabled) + // Return the mocked record if the mocking record is set + if(isset(self::$mockedRecords[$domain])) { - if(isset(self::$mockedRecords[$domain])) - { - return self::$mockedRecords[$domain]; - } + return self::$mockedRecords[$domain]; + } + + // Return the mocked record from the configuration if one is set + if(isset(Configuration::getInstanceConfiguration()->getDnsMocks()[$domain])) + { + return DnsHelper::parseTxt(Configuration::getInstanceConfiguration()->getDnsMocks()[$domain]); } // Check the database if enabled if ($useDatabase) { + // Return from the database cache if one exists + // TODO: Implement renewal here $resolvedServer = ResolvedDnsRecordsManager::getDnsRecord($domain); if ($resolvedServer !== null) { @@ -44,12 +48,12 @@ } } + // Resolve DNS & Records $txtRecords = self::dnsGetTxtRecords($domain); if ($txtRecords === false) { throw new ResolutionException(sprintf("Failed to resolve DNS TXT records for %s", $domain)); } - $fullRecord = self::concatenateTxtRecords($txtRecords); try @@ -106,27 +110,6 @@ return $fullRecordBuilder; } - /** - * Determines if mocking is enabled. - * - * @return bool True if mocking is enabled, false otherwise. - */ - public static function isMockingEnabled(): bool - { - return self::$mockingEnabled; - } - - /** - * Enables or disables mocking functionality. - * - * @param bool $enabled Indicates whether mocking should be enabled (true) or disabled (false). - * @return void - */ - public static function setMockingEnabled(bool $enabled): void - { - self::$mockingEnabled = $enabled; - } - /** * Retrieves the mocked records. *