DDNS.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Utils;
  3. use App\Utils\DDNS\AliYun;
  4. use App\Utils\DDNS\CloudFlare;
  5. use App\Utils\DDNS\DNSPod;
  6. use App\Utils\DDNS\Namesilo;
  7. use App\Utils\Library\Templates\DNS;
  8. use Log;
  9. /**
  10. * Class DDNS 域名解析.
  11. */
  12. class DDNS
  13. {
  14. private DNS $dns;
  15. public function __construct(private readonly string $domain)
  16. {
  17. $this->dns = match (sysConfig('ddns_mode')) {
  18. 'aliyun' => new AliYun($domain),
  19. 'namesilo' => new Namesilo($domain),
  20. 'dnspod' => new DNSPod($domain),
  21. 'cloudflare' => new CloudFlare($domain),
  22. };
  23. }
  24. public function destroy(string $type = '', string $ip = ''): void
  25. { // 删除解析记录
  26. if ($this->dns->destroy($type, $ip)) {
  27. Log::notice("【DDNS】删除:$this->domain 成功");
  28. } else {
  29. Log::alert("【DDNS】删除:$this->domain 失败,请手动删除!");
  30. }
  31. }
  32. public function update(string $latest_ip, string $original_ip, string $type = 'A'): void
  33. { // 修改解析记录
  34. if ($this->dns->update($latest_ip, $original_ip, $type)) {
  35. Log::info("【DDNS】更新 $this->domain :$original_ip => $latest_ip 类型:$type 成功");
  36. } else {
  37. Log::warning("【DDNS】更新 $this->domain :$original_ip => $latest_ip 类型:$type 失败,请手动设置!");
  38. }
  39. }
  40. public function store(string $ip, string $type = 'A'): void
  41. { // 添加解析记录
  42. if ($this->dns->store($ip, $type)) {
  43. Log::info("【DDNS】添加:$ip => $this->domain 类型:$type 成功");
  44. } else {
  45. Log::warning("【DDNS】添加:$ip => $this->domain 类型:$type 失败,请手动设置!");
  46. }
  47. }
  48. }