Porkbun.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Utils\DDNS;
  3. use App\Utils\Library\Templates\DNS;
  4. use Arr;
  5. use Cache;
  6. use Http;
  7. use Log;
  8. use RuntimeException;
  9. class Porkbun implements DNS
  10. {
  11. // 开发依据: https://porkbun.com/api/json/v3/documentation
  12. private const API_ENDPOINT = 'https://porkbun.com/api/json/v3/';
  13. public const KEY = 'porkbun';
  14. public const LABEL = 'Porkbun';
  15. private string $apiKey;
  16. private string $secretKey;
  17. private array $domainInfo;
  18. public function __construct(private readonly string $subdomain)
  19. {
  20. $this->apiKey = sysConfig('ddns_key');
  21. $this->secretKey = sysConfig('ddns_secret');
  22. $this->domainInfo = $this->parseDomainInfo();
  23. }
  24. private function parseDomainInfo(): array
  25. {
  26. $domains = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  27. return array_column($this->sendRequest('domain/listAll')['domains'] ?? [], 'domain');
  28. });
  29. if ($domains) {
  30. $matched = Arr::first($domains, fn ($domain) => str_contains($this->subdomain, $domain));
  31. }
  32. if (empty($matched)) {
  33. throw new RuntimeException('['.self::LABEL." — domain/listAll] The subdomain $this->subdomain does not match any domain in your account.");
  34. }
  35. return [
  36. 'sub' => rtrim(substr($this->subdomain, 0, -strlen($matched)), '.'),
  37. 'domain' => $matched,
  38. ];
  39. }
  40. private function sendRequest(string $uri, array $parameters = []): array|bool
  41. {
  42. $response = Http::timeout(15)->retry(3, 1000)->baseUrl(self::API_ENDPOINT)->asJson()->post($uri, ['apikey' => $this->apiKey, 'secretapikey' => $this->secretKey, ...$parameters]);
  43. $data = $response->json();
  44. if ($response->successful()) {
  45. return $data ?? true;
  46. }
  47. if ($data) {
  48. Log::error('['.self::LABEL." — $uri] 返回错误信息: ".$data['message'] ?? 'Unknown error');
  49. } else {
  50. Log::error('['.self::LABEL." — $uri] 请求失败");
  51. }
  52. exit(400);
  53. }
  54. public function store(string $ip, string $type): bool
  55. {
  56. return (bool) $this->sendRequest("dns/create/{$this->domainInfo['domain']}", ['name' => $this->domainInfo['sub'], 'type' => $type, 'content' => $ip]);
  57. }
  58. public function update(string $latest_ip, string $original_ip, string $type): bool
  59. {
  60. $recordIds = $this->getRecordIds($type, $original_ip);
  61. if ($recordIds) {
  62. $this->sendRequest("dns/edit/{$this->domainInfo['domain']}/$recordIds[0]", ['type' => $type, 'content' => $latest_ip]);
  63. return true;
  64. }
  65. return false;
  66. }
  67. private function getRecordIds(string $type, string $ip): array
  68. {
  69. if ($type) {
  70. $response = $this->sendRequest("dns/retrieveByNameType/{$this->domainInfo['domain']}/$type/{$this->domainInfo['sub']}");
  71. } else {
  72. $response = $this->sendRequest("dns/retrieve/{$this->domainInfo['domain']}");
  73. }
  74. if (isset($response['records'])) {
  75. $records = $response['records'];
  76. if (! $type) {
  77. $records = array_filter($records, function ($record) {
  78. return $record['name'] === $this->subdomain;
  79. });
  80. }
  81. if ($ip) {
  82. $records = array_filter($records, static function ($record) use ($ip) {
  83. return $record['content'] === $ip;
  84. });
  85. }
  86. return array_column($records, 'id');
  87. }
  88. return [];
  89. }
  90. public function destroy(string $type, string $ip): int|bool
  91. {
  92. if (! $ip) {
  93. return $this->sendRequest("dns/deleteByNameType/{$this->domainInfo['domain']}/$type/{$this->domainInfo['sub']}");
  94. }
  95. $recordIds = $this->getRecordIds($type, $ip);
  96. $deletedCount = 0;
  97. foreach ($recordIds as $recordId) {
  98. if ($this->sendRequest("dns/delete/{$this->domainInfo['domain']}/$recordId")) {
  99. $deletedCount++;
  100. }
  101. }
  102. return $deletedCount;
  103. }
  104. }