Google.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Google implements DNS
  10. {
  11. // 开发依据: https://developers.google.com/identity/protocols/oauth2/service-account?hl=zh-cn#httprest https://cloud.google.com/dns/docs/apis?hl=zh-cn
  12. public const KEY = 'google';
  13. public const LABEL = 'Google Cloud DNS';
  14. private string $apiEndpoint = 'https://dns.googleapis.com/dns/v1/projects/';
  15. private array $credentials;
  16. private string $token;
  17. private string $zoneID;
  18. public function __construct(private readonly string $subdomain)
  19. {
  20. $this->credentials = json_decode(sysConfig('ddns_secret'), true);
  21. if (! $this->credentials) {
  22. exit(400);
  23. }
  24. $this->apiEndpoint .= "{$this->credentials['project_id']}/";
  25. $this->token = $this->getBearerToken();
  26. $this->zoneID = $this->getZoneIdentifier();
  27. }
  28. private function getBearerToken(): string
  29. {
  30. return Cache::remember('google_token', 3599, function () {
  31. $response = Http::timeout(15)->asForm()->post('https://oauth2.googleapis.com/token', ['grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $this->generateJWT()]);
  32. if ($response->successful() && $data = $response->json()) {
  33. return $data['access_token'];
  34. }
  35. exit(400);
  36. });
  37. }
  38. private function generateJWT(): string
  39. {
  40. $headerEncoded = base64url_encode(json_encode(['alg' => 'RS256', 'typ' => 'JWT', 'kid' => $this->credentials['private_key_id']]));
  41. $now = time();
  42. $payloadEncoded = base64url_encode(json_encode([
  43. 'iss' => $this->credentials['client_email'],
  44. 'scope' => 'https://www.googleapis.com/auth/ndev.clouddns.readwrite',
  45. 'aud' => 'https://oauth2.googleapis.com/token',
  46. 'iat' => $now,
  47. 'exp' => $now + 3600,
  48. ]));
  49. $dataToSign = "$headerEncoded.$payloadEncoded";
  50. openssl_sign($dataToSign, $signature, $this->credentials['private_key'], OPENSSL_ALGO_SHA256);
  51. $signatureEncoded = base64url_encode($signature);
  52. return "$dataToSign.$signatureEncoded";
  53. }
  54. private function getZoneIdentifier(): string
  55. {
  56. $zones = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  57. return array_column($this->sendRequest('ListZones')['managedZones'] ?? [], 'dnsName', 'id');
  58. });
  59. foreach ($zones as $zoneID => $zoneName) {
  60. if (str_contains("$this->subdomain.", $zoneName)) {
  61. return $zoneID;
  62. }
  63. }
  64. throw new RuntimeException('['.self::LABEL." — ListPublicZones] The subdomain $this->subdomain does not match any domain in your account.");
  65. }
  66. private function sendRequest(string $action, array $parameters = [], string $type = 'A'): array|bool
  67. {
  68. $client = Http::timeout(15)->retry(3, 1000)->withToken($this->token)->baseUrl($this->apiEndpoint)->withQueryParameters(['api-version' => '2018-05-01'])->asJson();
  69. $response = match ($action) {
  70. 'ListZones' => $client->get('managedZones'),
  71. 'ListRecordSets' => $client->get("managedZones/$this->zoneID/rrsets", $parameters),
  72. 'CreateRecordSet' => $client->post("managedZones/$this->zoneID/rrsets", $parameters),
  73. 'PatchRecordSet' => $client->patch("managedZones/$this->zoneID/rrsets/$this->subdomain./$type", $parameters),
  74. 'DeleteRecordSet' => $client->delete("managedZones/$this->zoneID/rrsets/$this->subdomain./$type"),
  75. };
  76. $data = $response->json();
  77. if ($response->successful()) {
  78. return $data ?: true;
  79. }
  80. if ($data) {
  81. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['error']['message'] ?? 'Unknown error');
  82. } else {
  83. Log::error('['.self::LABEL." — $action] 请求失败");
  84. }
  85. exit(400);
  86. }
  87. public function store(string $ip, string $type): bool
  88. {
  89. $ips = $this->getRecordIps($type);
  90. if (! $ips) {
  91. return (bool) $this->sendRequest('CreateRecordSet', ['kind' => 'dns#resourceRecordSet', 'name' => "$this->subdomain.", 'type' => $type, 'ttl' => 300, 'rrdatas' => [$ip]]);
  92. }
  93. if (! in_array($ip, $ips, true)) {
  94. $ips[] = $ip;
  95. return $this->updateRecord($type, $ips);
  96. }
  97. return true;
  98. }
  99. private function getRecordIps(string $type): array
  100. {
  101. $parameters = ['name' => "$this->subdomain."];
  102. if ($type) {
  103. $parameters['type'] = $type;
  104. }
  105. $records = $this->sendRequest('ListRecordSets', $parameters)['rrsets'] ?? [];
  106. if ($records) {
  107. return Arr::first($records)['rrdatas'] ?? [];
  108. }
  109. return [];
  110. }
  111. private function updateRecord(string $type, array $ips): bool
  112. {
  113. return (bool) $this->sendRequest('PatchRecordSet', ['kind' => 'dns#resourceRecordSet', 'name' => "$this->subdomain.", 'type' => $type, 'ttl' => 300, 'rrdatas' => array_values($ips)]);
  114. }
  115. public function update(string $latest_ip, string $original_ip, string $type): bool
  116. {
  117. $ips = $this->getRecordIps($type);
  118. if ($ips) {
  119. $ips = array_filter($ips, static fn ($ip) => $ip !== $original_ip);
  120. }
  121. $ips[] = $latest_ip;
  122. return $this->updateRecord($type, $ips);
  123. }
  124. public function destroy(string $type, string $ip): bool
  125. {
  126. if (! $type) {
  127. return $this->sendRequest('DeleteRecordSet') && $this->sendRequest('DeleteRecordSet', [], 'AAAA');
  128. }
  129. if ($ip) {
  130. $ips = array_filter($this->getRecordIps($type), static fn ($hasIp) => $hasIp !== $ip);
  131. if ($ips) {
  132. return $this->updateRecord($type, $ips);
  133. }
  134. }
  135. return (bool) $this->sendRequest('DeleteRecordSet', [], $type);
  136. }
  137. }