Vercel.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Vercel implements DNS
  10. {
  11. // 开发依据: https://vercel.com/docs/rest-api
  12. private const API_ENDPOINT = 'https://api.vercel.com/';
  13. public const KEY = 'vercel';
  14. public const LABEL = 'Vercel';
  15. private string $teamID;
  16. private string $token;
  17. private array $domainInfo;
  18. public function __construct(private readonly string $subdomain)
  19. {
  20. $this->teamID = sysConfig('ddns_key');
  21. $this->token = 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('DescribeDomains')['domains'] ?? [], 'name');
  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." — DescribeDomains] 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 $action, array $parameters = [], string $recordId = ''): array
  41. {
  42. $client = Http::timeout(15)->retry(3, 1000)->withToken($this->token)->baseUrl(self::API_ENDPOINT)->withQueryParameters(['teamId' => $this->teamID])->asJson();
  43. $response = match ($action) {
  44. 'DescribeDomains' => $client->get('v5/domains'),
  45. 'DescribeSubDomainRecords' => $client->get("v4/domains/{$this->domainInfo['domain']}/records"),
  46. 'AddDomainRecord' => $client->post("v2/domains/{$this->domainInfo['domain']}/records", $parameters),
  47. 'UpdateDomainRecord' => $client->patch("v1/domains/records/$recordId", $parameters),
  48. 'DeleteDomainRecord' => $client->delete("v2/domains/{$this->domainInfo['domain']}/records/$recordId"),
  49. };
  50. $data = $response->json();
  51. if ($response->successful()) {
  52. return $data;
  53. }
  54. if ($data) {
  55. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['error']['message'] ?? 'Unknown error');
  56. } else {
  57. Log::error('['.self::LABEL." — $action] 请求失败");
  58. }
  59. exit(400);
  60. }
  61. public function store(string $ip, string $type): bool
  62. {
  63. return (bool) $this->sendRequest('AddDomainRecord', ['name' => $this->domainInfo['sub'], 'type' => $type, 'value' => $ip]);
  64. }
  65. public function update(string $latest_ip, string $original_ip, string $type): bool
  66. {
  67. $recordIds = $this->getRecordIds($type, $original_ip);
  68. if ($recordIds) {
  69. $this->sendRequest('UpdateDomainRecord', ['value' => $latest_ip], $recordIds[0]);
  70. return true;
  71. }
  72. return false;
  73. }
  74. private function getRecordIds(string $type, string $ip): array
  75. {
  76. $response = $this->sendRequest('DescribeSubDomainRecords');
  77. if (isset($response['records'])) {
  78. $records = $response['records'];
  79. if ($ip) {
  80. $records = array_filter($records, function ($record) use ($ip) {
  81. return $record['value'] === $ip && $record['name'] === $this->domainInfo['sub'];
  82. });
  83. } elseif ($type) {
  84. $records = array_filter($records, function ($record) use ($type) {
  85. return $record['type'] === $type && $record['name'] === $this->domainInfo['sub'];
  86. });
  87. } else {
  88. $records = array_filter($records, function ($record) {
  89. return $record['name'] === $this->domainInfo['sub'];
  90. });
  91. }
  92. return array_column($records, 'id');
  93. }
  94. return [];
  95. }
  96. public function destroy(string $type, string $ip): bool|int
  97. {
  98. $recordIds = $this->getRecordIds($type, $ip);
  99. $deletedCount = 0;
  100. foreach ($recordIds as $recordId) {
  101. if ($this->sendRequest('DeleteDomainRecord', $recordId) === []) {
  102. $deletedCount++;
  103. }
  104. }
  105. return $deletedCount;
  106. }
  107. }