92 lines
2.4 KiB
PHP
92 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Repositories\Whois\WhoisRepositoryInterface;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Exception;
|
|
|
|
class WhoisService
|
|
{
|
|
protected $whoisRepository;
|
|
|
|
public function __construct(WhoisRepositoryInterface $whoisRepository)
|
|
{
|
|
$this->whoisRepository = $whoisRepository;
|
|
}
|
|
|
|
/**
|
|
* Get domain whois information
|
|
*
|
|
* @param string $domain
|
|
* @return array
|
|
* @throws Exception
|
|
*/
|
|
public function getDomainInfo(string $domain): array
|
|
{
|
|
try {
|
|
// Check cache first
|
|
$cachedData = $this->whoisRepository->getDomainInfo($domain);
|
|
if ($cachedData) {
|
|
return $cachedData;
|
|
}
|
|
|
|
// Make API request to whois.inet.vn
|
|
$response = Http::timeout(30)->get("https://whois.inet.vn/api/whois/domainspecify/{$domain}");
|
|
|
|
if (!$response->successful()) {
|
|
throw new Exception(__('Failed to retrieve domain information'));
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
// Cache the result
|
|
$this->whoisRepository->cacheDomainInfo($domain, $data);
|
|
|
|
Log::info('Domain whois query successful', ['domain' => $domain]);
|
|
|
|
return $data;
|
|
|
|
} catch (Exception $e) {
|
|
Log::error('Domain whois query failed', [
|
|
'domain' => $domain,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if domain is available
|
|
*
|
|
* @param string $domain
|
|
* @return bool
|
|
*/
|
|
public function isDomainAvailable(string $domain): bool
|
|
{
|
|
try {
|
|
$info = $this->getDomainInfo($domain);
|
|
return isset($info['code']) && $info['code'] !== '0';
|
|
} catch (Exception $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get domain status in Vietnamese
|
|
*
|
|
* @param string $domain
|
|
* @return string
|
|
*/
|
|
public function getDomainStatus(string $domain): string
|
|
{
|
|
try {
|
|
$info = $this->getDomainInfo($domain);
|
|
return $info['message'] ?? __('Unknown status');
|
|
} catch (Exception $e) {
|
|
return __('Unable to check domain status');
|
|
}
|
|
}
|
|
}
|