Add support for mock servers and DNS mocking.

This commit is contained in:
netkas 2025-01-08 04:17:06 -05:00
parent bfe8064a94
commit e784137480
2 changed files with 59 additions and 1 deletions

View file

@ -3,11 +3,13 @@
namespace Socialbox\Classes\CliCommands; namespace Socialbox\Classes\CliCommands;
use Exception; use Exception;
use ncc\ThirdParty\Symfony\Process\Exception\InvalidArgumentException;
use PDOException; use PDOException;
use Socialbox\Abstracts\CacheLayer; use Socialbox\Abstracts\CacheLayer;
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Classes\Cryptography; use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Database; use Socialbox\Classes\Database;
use Socialbox\Classes\DnsHelper;
use Socialbox\Classes\Logger; use Socialbox\Classes\Logger;
use Socialbox\Classes\Resources; use Socialbox\Classes\Resources;
use Socialbox\Enums\DatabaseObjects; use Socialbox\Enums\DatabaseObjects;
@ -358,12 +360,64 @@
} }
} }
// Handle Mock Servers environment variables (SB_INSTANCE_MOCK_SERVER_*)
$mockServers = [];
foreach(self::getMockServerValues() as $mockServer)
{
$mockServer = explode(' ', $mockServer);
if(count($mockServer) !== 2)
{
Logger::getLogger()->warning(sprintf('Invalid Mock Server format: %s', implode(' ', $mockServer)));
continue;
}
$domain = $mockServer[1];
try
{
$txt = DnsHelper::parseTxt($mockServer[2]);
}
catch(InvalidArgumentException $e)
{
Logger::getLogger()->warning(sprintf('Invalid TXT record format for %s: %s', $domain, $e->getMessage()));
continue;
}
$mockServers[$domain] = $txt;
}
if(count($mockServers) > 0)
{
Logger::getLogger()->info('Setting Mock Servers...');
Configuration::getConfigurationLib()->set('instance.mock_servers', $mockServers);
}
// Apply changes & reload the configuration // Apply changes & reload the configuration
Logger::getLogger()->info('Updating configuration...'); Logger::getLogger()->info('Updating configuration...');
Configuration::getConfigurationLib()->save(); // Save Configuration::getConfigurationLib()->save(); // Save
Configuration::reload(); // Reload Configuration::reload(); // Reload
} }
/**
* Retrieves all environment variable values that start with the prefix 'SB_INSTANCE_MOCK_SERVER_'.
*
* @return array An array of environment variable values filtered by the specified prefix.
*/
private static function getMockServerValues(): array
{
// Fetch all environment variables
$envVars = getenv();
// Filter variables that start with the specified prefix
$filtered = array_filter($envVars, function ($key)
{
return str_starts_with($key, 'SB_INSTANCE_MOCK_SERVER_');
}, ARRAY_FILTER_USE_KEY);
// Return only the values as an array
return array_values($filtered);
}
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -398,7 +452,8 @@
" SB_CACHE_PORT - The cache port (default: 6379)\n" . " SB_CACHE_PORT - The cache port (default: 6379)\n" .
" SB_CACHE_USERNAME - The cache username (default: null)\n" . " SB_CACHE_USERNAME - The cache username (default: null)\n" .
" SB_CACHE_PASSWORD - The cache password (default: null)\n" . " SB_CACHE_PASSWORD - The cache password (default: null)\n" .
" SB_CACHE_DATABASE - The cache database (default: 0)\n"; " SB_CACHE_DATABASE - The cache database (default: 0)\n" .
" SB_INSTANCE_MOCK_SERVER_* - Mock server environment variables, format: (<domain> <txt>), eg; SB_INSTANCE_MOCK_SERVER_N64: teapot.com <txt>\n";
} }
/** /**

View file

@ -42,6 +42,9 @@
$config->setDefault('instance.name', "Socialbox Server"); $config->setDefault('instance.name', "Socialbox Server");
$config->setDefault('instance.domain', null); $config->setDefault('instance.domain', null);
$config->setDefault('instance.rpc_endpoint', null); $config->setDefault('instance.rpc_endpoint', null);
// DNS Mocking Configuration, usually used for testing purposes
// Allows the user to mock a domain to use a specific TXT record
$config->setDefault('instance.dns_mocks', []);
// Security Configuration // Security Configuration
$config->setDefault('security.display_internal_exceptions', false); $config->setDefault('security.display_internal_exceptions', false);