CloudFlare.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, $data);
  50. break;
  51. case 'delete':
  52. $response = $this->client->delete(self::$apiHost.'zones/'.$this->zoneIdentifier[2].'/dns_records/'.$id);
  53. break;
  54. default:
  55. return false;
  56. }
  57. $message = $response->json();
  58. if ($message && ! $response->failed()) {
  59. return $message;
  60. }
  61. Log::error('[CloudFlare API] - ['.$action.'] 请求失败:'.var_export($message, true));
  62. }
  63. return false;
  64. }
  65. public function update($ip, $type)
  66. {
  67. $recordId = $this->getRecordId($type);
  68. if ($this->zoneIdentifier && $recordId) {
  69. return $this->send('update', ['type' => $type, 'name' => self::$subDomain, 'content' => $ip, 'ttl' => 120], $recordId[0]);
  70. }
  71. return false;
  72. }
  73. private function getRecordId($type = null)
  74. {
  75. $parameters['name'] = self::$subDomain;
  76. if ($type) {
  77. $parameters['type'] = $type;
  78. }
  79. $dnsList = $this->send('get', $parameters);
  80. if ($dnsList && Arr::has($dnsList, 'result.0.id')) {
  81. $dnsRecords = $dnsList['result'];
  82. $data = null;
  83. foreach ($dnsRecords as $record) {
  84. $data[] = $record['id'];
  85. }
  86. return $data ?: false;
  87. }
  88. return false;
  89. }
  90. public function destroy($type)
  91. {
  92. $records = $this->getRecordId($type);
  93. if ($records && $this->zoneIdentifier) {
  94. $count = 0;
  95. foreach ($records as $record) {
  96. $result = $this->send('delete', [], $record);
  97. if ($result && Arr::has($result, 'result.id')) {
  98. $count++;
  99. }
  100. }
  101. return $count;
  102. }
  103. return false;
  104. }
  105. }