2024-09-30 03:00:02 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Socialbox\Classes;
|
|
|
|
|
2024-10-25 14:23:43 -04:00
|
|
|
use Socialbox\Exceptions\DatabaseOperationException;
|
2024-09-30 03:00:02 -04:00
|
|
|
use Socialbox\Exceptions\ResolutionException;
|
2024-10-25 14:23:43 -04:00
|
|
|
use Socialbox\Managers\ResolvedServersManager;
|
2024-09-30 03:00:02 -04:00
|
|
|
use Socialbox\Objects\ResolvedServer;
|
|
|
|
|
|
|
|
class ServerResolver
|
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
private const string PATTERN = '/v=socialbox;sb-rpc=(https?:\/\/[^;]+);sb-key=([^;]+)/';
|
|
|
|
|
2024-09-30 03:00:02 -04:00
|
|
|
/**
|
|
|
|
* Resolves a given domain to fetch the RPC endpoint and public key from its DNS TXT records.
|
|
|
|
*
|
|
|
|
* @param string $domain The domain to be resolved.
|
|
|
|
* @return ResolvedServer An instance of ResolvedServer containing the endpoint and public key.
|
|
|
|
* @throws ResolutionException If the DNS TXT records cannot be resolved or if required information is missing.
|
2024-10-25 14:23:43 -04:00
|
|
|
* @throws DatabaseOperationException
|
2024-09-30 03:00:02 -04:00
|
|
|
*/
|
2024-10-31 12:18:44 -04:00
|
|
|
public static function resolveDomain(string $domain, bool $useDatabase=true): ResolvedServer
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
// First query the database to check if the domain is already resolved
|
2024-10-31 12:18:44 -04:00
|
|
|
if($useDatabase)
|
2024-10-25 14:23:43 -04:00
|
|
|
{
|
2024-10-31 12:18:44 -04:00
|
|
|
$resolvedServer = ResolvedServersManager::getResolvedServer($domain);
|
|
|
|
if($resolvedServer !== null)
|
2024-10-25 14:23:43 -04:00
|
|
|
{
|
2024-10-31 12:18:44 -04:00
|
|
|
return $resolvedServer->toResolvedServer();
|
2024-10-25 14:23:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$txtRecords = self::dnsGetTxtRecords($domain);
|
2024-09-30 03:00:02 -04:00
|
|
|
if ($txtRecords === false)
|
|
|
|
{
|
|
|
|
throw new ResolutionException(sprintf("Failed to resolve DNS TXT records for %s", $domain));
|
|
|
|
}
|
|
|
|
|
2024-10-25 14:23:43 -04:00
|
|
|
$fullRecord = self::concatenateTxtRecords($txtRecords);
|
|
|
|
|
|
|
|
if (preg_match(self::PATTERN, $fullRecord, $matches))
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
$endpoint = trim($matches[1]);
|
|
|
|
$publicKey = trim(str_replace(' ', '', $matches[2]));
|
|
|
|
|
|
|
|
if (empty($endpoint))
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
throw new ResolutionException(sprintf("Failed to resolve RPC endpoint for %s", $domain));
|
2024-09-30 03:00:02 -04:00
|
|
|
}
|
2024-10-25 14:23:43 -04:00
|
|
|
|
|
|
|
if (empty($publicKey))
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
throw new ResolutionException(sprintf("Failed to resolve public key for %s", $domain));
|
2024-09-30 03:00:02 -04:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:23:43 -04:00
|
|
|
return new ResolvedServer($endpoint, $publicKey);
|
|
|
|
}
|
|
|
|
else
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
throw new ResolutionException(sprintf("Failed to find valid SocialBox record for %s", $domain));
|
2024-09-30 03:00:02 -04:00
|
|
|
}
|
2024-10-25 14:23:43 -04:00
|
|
|
}
|
2024-09-30 03:00:02 -04:00
|
|
|
|
2024-10-25 14:23:43 -04:00
|
|
|
/**
|
|
|
|
* Retrieves the TXT records for a given domain using the dns_get_record function.
|
|
|
|
*
|
|
|
|
* @param string $domain The domain name to fetch TXT records for.
|
|
|
|
* @return array|false An array of DNS TXT records on success, or false on failure.
|
|
|
|
*/
|
|
|
|
private static function dnsGetTxtRecords(string $domain)
|
|
|
|
{
|
|
|
|
return dns_get_record($domain, DNS_TXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenates an array of TXT records into a single string.
|
|
|
|
*
|
|
|
|
* @param array $txtRecords An array of TXT records, where each record is expected to have a 'txt' key.
|
|
|
|
* @return string A concatenated string of all TXT records.
|
|
|
|
*/
|
|
|
|
private static function concatenateTxtRecords(array $txtRecords): string
|
|
|
|
{
|
|
|
|
$fullRecordBuilder = '';
|
|
|
|
|
|
|
|
foreach ($txtRecords as $txt)
|
2024-09-30 03:00:02 -04:00
|
|
|
{
|
2024-10-25 14:23:43 -04:00
|
|
|
if (isset($txt['txt']))
|
|
|
|
{
|
|
|
|
$fullRecordBuilder .= trim($txt['txt'], '" ');
|
|
|
|
}
|
2024-09-30 03:00:02 -04:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:23:43 -04:00
|
|
|
return $fullRecordBuilder;
|
2024-09-30 03:00:02 -04:00
|
|
|
}
|
|
|
|
}
|