CloudFlare.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $zones = $zoneInfo['result'];
  23. foreach ($zones as $zone) {
  24. if (strpos(self::$subDomain, $zone['name']) !== false) {
  25. return [$zone['name'], rtrim(substr(self::$subDomain, 0, -(strlen($zone['name']))), '.'), $zone['id']];
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31. public function store($ip, $type)
  32. {
  33. if ($this->zoneIdentifier) {
  34. return $this->send('create', ['type' => $type, 'name' => self::$subDomain, 'content' => $ip, 'ttl' => 120]);
  35. }
  36. return false;
  37. }
  38. private function send($action, $data = [], $id = null)
  39. {
  40. if ($this->zoneIdentifier) {
  41. switch ($action) {
  42. case 'get':
  43. $response = $this->client->get(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records', $data);
  44. break;
  45. case 'create':
  46. $response = $this->client->post(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records', $data);
  47. break;
  48. case 'update':
  49. $response = $this->client->put(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records/'.$id,
  50. $data);
  51. break;
  52. case 'delete':
  53. $response = $this->client->delete(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records/'.$id);
  54. break;
  55. default:
  56. return false;
  57. }
  58. $message = $response->json();
  59. if ($message && !$response->failed()) {
  60. return $message;
  61. }
  62. Log::error('[CloudFlare API] - ['.$action.'] 请求失败:'.var_export($message, true));
  63. }
  64. return false;
  65. }
  66. public function update($ip, $type)
  67. {
  68. $recordId = $this->getRecordId($type);
  69. if ($this->zoneIdentifier && $recordId) {
  70. return $this->send('update', ['type' => $type, 'name' => self::$subDomain, 'content' => $ip, 'ttl' => 120], $recordId[0]);
  71. }
  72. return false;
  73. }
  74. private function getRecordId($type = null)
  75. {
  76. $parameters['name'] = self::$subDomain;
  77. if ($type) {
  78. $parameters['type'] = $type;
  79. }
  80. $dnsList = $this->send('get', $parameters);
  81. if ($dnsList && Arr::has($dnsList, 'result.0.id')) {
  82. $dnsRecords = $dnsList['result'];
  83. $data = null;
  84. foreach ($dnsRecords as $record) {
  85. $data[] = $record['id'];
  86. }
  87. return $data ?: false;
  88. }
  89. return false;
  90. }
  91. public function destroy($type)
  92. {
  93. $records = $this->getRecordId($type);
  94. if ($records && $this->zoneIdentifier) {
  95. $count = 0;
  96. foreach ($records as $record) {
  97. $result = $this->send('delete', [], $record);
  98. if ($result && Arr::has($result, 'result.id')) {
  99. $count++;
  100. }
  101. }
  102. return $count;
  103. }
  104. return false;
  105. }
  106. }