CloudFlare.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 CloudFlare implements DNS
  10. {
  11. // 开发依据: https://developers.cloudflare.com/api/
  12. private string $apiEndpoint;
  13. public const KEY = 'cloudflare';
  14. public const LABEL = 'CloudFlare';
  15. private array $auth;
  16. public function __construct(private readonly string $subdomain)
  17. {
  18. $this->apiEndpoint = 'https://api.cloudflare.com/client/v4/zones/';
  19. $this->auth = ['X-Auth-Key' => sysConfig('ddns_secret'), 'X-Auth-Email' => sysConfig('ddns_key')];
  20. $zoneIdentifier = $this->getZoneIdentifier();
  21. if ($zoneIdentifier) {
  22. $this->apiEndpoint .= $zoneIdentifier.'/dns_records/';
  23. }
  24. }
  25. private function getZoneIdentifier(): string
  26. {
  27. $zones = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  28. return $this->sendRequest('list');
  29. });
  30. if ($zones) {
  31. $matched = Arr::first($zones, fn ($zone) => str_contains($this->subdomain, $zone['name']));
  32. }
  33. if (empty($matched)) {
  34. throw new RuntimeException("[CloudFlare – list] The subdomain {$this->subdomain} does not match any domain in your account.");
  35. }
  36. return $matched['id'];
  37. }
  38. private function sendRequest(string $action, array $parameters = [], ?string $identifier = null): array
  39. {
  40. $client = Http::timeout(10)->retry(3, 1000)->withHeaders($this->auth)->baseUrl($this->apiEndpoint)->asJson();
  41. $response = match ($action) {
  42. 'list' => $client->get(''),
  43. 'get' => $client->get('', $parameters),
  44. 'create' => $client->post('', $parameters),
  45. 'update' => $client->put($identifier, $parameters),
  46. 'delete' => $client->delete($identifier),
  47. };
  48. $data = $response->json();
  49. if ($data) {
  50. if ($data['success'] && $response->ok()) {
  51. return $data['result'] ?? [];
  52. }
  53. Log::error('[CloudFlare - '.$action.'] 返回错误信息:'.$data['errors'][0]['message'] ?? 'Unknown error');
  54. } else {
  55. Log::error('[CloudFlare - '.$action.'] 请求失败');
  56. }
  57. exit(400);
  58. }
  59. public function store(string $ip, string $type): bool
  60. {
  61. $result = $this->sendRequest('create', ['content' => $ip, 'name' => $this->subdomain, 'type' => $type]);
  62. return ! empty($result);
  63. }
  64. public function update(string $latest_ip, string $original_ip, string $type): bool
  65. {
  66. $recordIds = $this->getRecordIds($type, $original_ip);
  67. if ($recordIds) {
  68. $ret = $this->sendRequest('update', ['content' => $latest_ip, 'name' => $this->subdomain, 'type' => $type], $recordIds[0]);
  69. }
  70. return (bool) ($ret ?? false);
  71. }
  72. private function getRecordIds(string $type, string $ip): array|false
  73. {
  74. $records = $this->sendRequest('get', ['content' => $ip, 'name' => $this->subdomain, 'type' => $type]);
  75. if ($records) {
  76. return array_column($records, 'id');
  77. }
  78. return false;
  79. }
  80. public function destroy(string $type, string $ip): int
  81. {
  82. $recordIds = $this->getRecordIds($type, $ip);
  83. $deletedCount = 0;
  84. foreach ($recordIds as $recordId) {
  85. if ($this->sendRequest('delete', [], $recordId)) {
  86. $deletedCount++;
  87. }
  88. }
  89. return $deletedCount;
  90. }
  91. }