Azure.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Azure implements DNS
  10. {
  11. // 开发依据: https://learn.microsoft.com/en-us/rest/api/eventhub/get-azure-active-directory-token?view=rest-dns-2018-05-01 https://learn.microsoft.com/en-us/rest/api/dns/?view=rest-dns-2018-05-01
  12. private const API_ENDPOINT = 'https://management.azure.com/';
  13. public const KEY = 'azure';
  14. public const LABEL = 'Microsoft Azure';
  15. private string $tenantId;
  16. private string $clientId;
  17. private string $clientSecret;
  18. private string $token;
  19. private string $subscriptionId;
  20. private string $zoneID;
  21. private array $domainInfo;
  22. public function __construct(private readonly string $subdomain)
  23. {
  24. $ids = explode(',', sysConfig('ddns_key'));
  25. $this->tenantId = $ids[1];
  26. $this->clientId = $ids[2];
  27. $this->clientSecret = sysConfig('ddns_secret');
  28. $this->subscriptionId = $ids[0];
  29. $this->token = $this->getBearerToken();
  30. $this->zoneID = $this->getZoneIdentifier();
  31. }
  32. private function getBearerToken(): string
  33. {
  34. return Cache::remember('azure_token', 3599, function () {
  35. $response = Http::timeout(15)->retry(3, 1000)->asForm()->post("https://login.microsoftonline.com/$this->tenantId/oauth2/token",
  36. ['grant_type' => 'client_credentials', 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'resource' => self::API_ENDPOINT]);
  37. if ($response->successful() && $data = $response->json()) {
  38. return $data['access_token'];
  39. }
  40. exit(400);
  41. });
  42. }
  43. private function getZoneIdentifier(): string
  44. {
  45. $zones = Cache::remember('ddns_get_domains', now()->addHour(), function () {
  46. return array_column($this->sendRequest('ListZones')['value'] ?? [], 'name', 'id');
  47. });
  48. foreach ($zones as $zoneID => $zoneName) {
  49. if (str_contains($this->subdomain, $zoneName)) {
  50. $this->domainInfo = [
  51. 'sub' => rtrim(substr($this->subdomain, 0, -strlen($zoneName)), '.'),
  52. 'domain' => $zoneName,
  53. ];
  54. return $zoneID;
  55. }
  56. }
  57. throw new RuntimeException('['.self::LABEL." — ListPublicZones] The subdomain $this->subdomain does not match any domain in your account.");
  58. }
  59. private function sendRequest(string $action, array $payload = [], string $type = 'A'): array|bool
  60. {
  61. $client = Http::timeout(15)->retry(3, 1000)->withToken($this->token)->baseUrl(self::API_ENDPOINT)->withQueryParameters(['api-version' => '2018-05-01'])->asJson();
  62. $response = match ($action) {
  63. 'ListZones' => $client->get("/subscriptions/$this->subscriptionId/providers/Microsoft.Network/dnszones"),
  64. 'ListRecordSets' => $client->get("$this->zoneID/".($type ?: 'all')),
  65. 'CreateRecordSet' => $client->put("$this->zoneID/$type/{$this->domainInfo['sub']}", $payload),
  66. 'DeleteRecordSet' => $client->delete("$this->zoneID/$type/{$this->domainInfo['sub']}"),
  67. };
  68. $data = $response->json();
  69. if ($response->successful()) {
  70. return $data ?: true;
  71. }
  72. if ($data) {
  73. Log::error('['.self::LABEL." — $action] 返回错误信息: ".$data['message'] ?? 'Unknown error');
  74. } else {
  75. Log::error('['.self::LABEL." — $action] 请求失败");
  76. }
  77. exit(400);
  78. }
  79. public function store(string $ip, string $type): bool
  80. {
  81. $ips = $this->getRecordIps($type);
  82. if (! in_array($ip, $ips, true)) {
  83. $ips[] = $ip;
  84. return $this->updateRecord($type, $ips);
  85. }
  86. return true;
  87. }
  88. private function getRecordIps(string $type): array
  89. { // 域名信息
  90. $records = $this->sendRequest('ListRecordSets', [], $type)['value'] ?? [];
  91. $records = array_filter($records, function ($record) {
  92. return $record['name'] === $this->domainInfo['sub'];
  93. });
  94. return Arr::flatten(Arr::first($records)['properties']["{$type}Records"] ?? []);
  95. }
  96. private function updateRecord(string $type, array $ips): bool
  97. {
  98. $ipKey = $type === 'A' ? 'ipv4Address' : 'ipv6Address';
  99. $ipInfo = array_map(static function ($ip) use ($ipKey) {
  100. return [$ipKey => $ip];
  101. }, $ips);
  102. return (bool) $this->sendRequest('CreateRecordSet', ['properties' => ['TTL' => 300, "{$type}Records" => $ipInfo]]);
  103. }
  104. public function update(string $latest_ip, string $original_ip, string $type): bool
  105. {
  106. $ips = $this->getRecordIps($type);
  107. if ($ips) {
  108. $ips = array_filter($ips, static fn ($ip) => $ip !== $original_ip);
  109. }
  110. $ips[] = $latest_ip;
  111. return $this->updateRecord($type, $ips);
  112. }
  113. public function destroy(string $type, string $ip): bool
  114. {
  115. if (! $type) {
  116. return $this->sendRequest('DeleteRecordSet') && $this->sendRequest('DeleteRecordSet', [], 'AAAA');
  117. }
  118. if ($ip) {
  119. $ips = array_filter($this->getRecordIps($type), static fn ($hasIp) => $hasIp !== $ip);
  120. if ($ips) {
  121. return $this->updateRecord($type, $ips);
  122. }
  123. }
  124. return (bool) $this->sendRequest('DeleteRecordSet', [], $type);
  125. }
  126. }