CloudFlare.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Components\DDNS;
  3. use Arr;
  4. use Http;
  5. use Log;
  6. class CloudFlare
  7. {
  8. private static $apiHost = 'https://api.cloudflare.com/client/v4/';
  9. private static $subDomain;
  10. private $zoneIdentifier;
  11. private $client;
  12. public function __construct($subDomain)
  13. {
  14. self::$subDomain = $subDomain;
  15. $this->zoneIdentifier = $this->getZone();
  16. $this->client = Http::withHeaders(['X-Auth-Key' => sysConfig('ddns_secret'), 'X-Auth-Email' => sysConfig('ddns_key')]);
  17. }
  18. private function getZone()
  19. {
  20. $zoneInfo = $this->client->get(self::$apiHost.'zones')->json();
  21. if ($zoneInfo && Arr::has($zoneInfo, 'result.0.id')) {
  22. foreach ($zoneInfo['result'] as $zone) {
  23. if (str_contains(self::$subDomain, $zone['name'])) {
  24. return [$zone['name'], rtrim(substr(self::$subDomain, 0, -strlen($zone['name'])), '.'), $zone['id']];
  25. }
  26. }
  27. }
  28. return [];
  29. }
  30. public function store($ip, $type)
  31. {
  32. if ($this->zoneIdentifier) {
  33. return $this->send('create', ['type' => $type, 'name' => self::$subDomain, 'content' => $ip, 'ttl' => 120]);
  34. }
  35. return false;
  36. }
  37. private function send($action, $data = [], $id = null)
  38. {
  39. if ($this->zoneIdentifier) {
  40. switch ($action) {
  41. case 'get':
  42. $response = $this->client->get(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records', $data);
  43. break;
  44. case 'create':
  45. $response = $this->client->post(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records', $data);
  46. break;
  47. case 'update':
  48. $response = $this->client->put(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records/'.$id, $data);
  49. break;
  50. case 'delete':
  51. $response = $this->client->delete(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records/'.$id);
  52. break;
  53. default:
  54. return false;
  55. }
  56. $message = $response->json();
  57. if ($message && ! $response->failed()) {
  58. return $message;
  59. }
  60. Log::error('[CloudFlare API] - ['.$action.'] 请求失败:'.var_export($message, true));
  61. }
  62. return false;
  63. }
  64. public function update($ip, $type)
  65. {
  66. $recordId = $this->getRecordId($type);
  67. if ($this->zoneIdentifier && $recordId) {
  68. return $this->send('update', ['type' => $type, 'name' => self::$subDomain, 'content' => $ip, 'ttl' => 120], $recordId[0]);
  69. }
  70. return false;
  71. }
  72. private function getRecordId($type = null)
  73. {
  74. $parameters['name'] = self::$subDomain;
  75. if ($type) {
  76. $parameters['type'] = $type;
  77. }
  78. $dnsList = $this->send('get', $parameters);
  79. if ($dnsList && Arr::has($dnsList, 'result.0.id')) {
  80. $dnsRecords = $dnsList['result'];
  81. $data = null;
  82. foreach ($dnsRecords as $record) {
  83. $data[] = $record['id'];
  84. }
  85. return $data ?: false;
  86. }
  87. return false;
  88. }
  89. public function destroy($type)
  90. {
  91. $records = $this->getRecordId($type);
  92. if ($records && $this->zoneIdentifier) {
  93. $count = 0;
  94. foreach ($records as $record) {
  95. $result = $this->send('delete', [], $record);
  96. if ($result && Arr::has($result, 'result.id')) {
  97. $count++;
  98. }
  99. }
  100. return $count;
  101. }
  102. return false;
  103. }
  104. }