DDNS.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Utils;
  3. use App\Utils\Library\Templates\DNS;
  4. use Cache;
  5. use Log;
  6. use ReflectionClass;
  7. /**
  8. * Class DDNS 域名解析.
  9. */
  10. class DDNS
  11. {
  12. private DNS $dns;
  13. public function __construct(private readonly ?string $domain = null)
  14. {
  15. if ($domain) {
  16. foreach (glob(app_path('Utils/DDNS').'/*.php') as $file) {
  17. $class = 'App\\Utils\\DDNS\\'.basename($file, '.php');
  18. $reflectionClass = new ReflectionClass($class);
  19. if (sysConfig('ddns_mode') === $reflectionClass->getConstant('KEY')) {
  20. $this->dns = new $class($domain);
  21. break;
  22. }
  23. }
  24. }
  25. }
  26. public function destroy(string $type = '', string $ip = ''): void
  27. { // 删除解析记录
  28. if ($this->dns->destroy($type, $ip)) {
  29. Log::notice("【DDNS】删除:$this->domain 成功");
  30. } else {
  31. Log::alert("【DDNS】删除:$this->domain 失败,请手动删除!");
  32. }
  33. }
  34. public function update(string $latest_ip, string $original_ip, string $type = 'A'): void
  35. { // 修改解析记录
  36. if ($this->dns->update($latest_ip, $original_ip, $type)) {
  37. Log::info("【DDNS】更新 $this->domain :$original_ip => $latest_ip 类型:$type 成功");
  38. } else {
  39. Log::warning("【DDNS】更新 $this->domain :$original_ip => $latest_ip 类型:$type 失败,请手动设置!");
  40. }
  41. }
  42. public function store(string $ip, string $type = 'A'): void
  43. { // 添加解析记录
  44. if ($this->dns->store($ip, $type)) {
  45. Log::info("【DDNS】添加:$ip => $this->domain 类型:$type 成功");
  46. } else {
  47. Log::warning("【DDNS】添加:$ip => $this->domain 类型:$type 失败,请手动设置!");
  48. }
  49. }
  50. public function getLabels(): array
  51. {
  52. return Cache::rememberForever('ddns_get_Labels_'.app()->getLocale(), static function () {
  53. $labels[trans('common.status.closed')] = '';
  54. foreach (glob(app_path('Utils/DDNS').'/*.php') as $file) {
  55. $class = 'App\\Utils\\DDNS\\'.basename($file, '.php');
  56. $reflectionClass = new ReflectionClass($class);
  57. $labels[$reflectionClass->getConstant('LABEL')] = $reflectionClass->getConstant('KEY');
  58. }
  59. return $labels;
  60. });
  61. }
  62. }