DDNS.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Components;
  3. use App\Components\DDNS\Aliyun;
  4. use App\Components\DDNS\CloudFlare;
  5. use App\Components\DDNS\DNSPod;
  6. use App\Components\DDNS\Namesilo;
  7. use Log;
  8. /**
  9. * Class DDNS 域名解析.
  10. */
  11. class DDNS
  12. {
  13. /**
  14. * 删除解析记录.
  15. *
  16. * @param string $domain 域名
  17. * @param string|null $type
  18. */
  19. public static function destroy(string $domain, $type = null)
  20. {
  21. if (self::dnsProvider($domain)->destroy($type)) {
  22. Log::info("【DDNS】删除:{$domain} 成功");
  23. } else {
  24. Log::info("【DDNS】删除:{$domain} 失败,请手动删除!");
  25. }
  26. }
  27. private static function dnsProvider($domain)
  28. {
  29. switch (sysConfig('ddns_mode')) {
  30. case 'aliyun':
  31. return new Aliyun($domain);
  32. case 'namesilo':
  33. return new Namesilo($domain);
  34. case 'dnspod':
  35. return new DNSPod($domain);
  36. case 'cloudflare':
  37. return new CloudFlare($domain);
  38. default:
  39. Log::error('【DDNS】未知渠道:'.sysConfig('ddns_mode'));
  40. return false;
  41. }
  42. }
  43. /**
  44. * 修改解析记录.
  45. *
  46. * @param string $domain 域名
  47. * @param string $ip ip地址
  48. * @param string $type 记录类型,默认为 A
  49. */
  50. public static function update(string $domain, string $ip, string $type = 'A')
  51. {
  52. if (self::dnsProvider($domain)->update($ip, $type)) {
  53. Log::info("【DDNS】更新:{$ip} => {$domain} 类型:{$type} 成功");
  54. } else {
  55. Log::info("【DDNS】更新:{$ip} => {$domain} 类型:{$type} 失败,请手动设置!");
  56. }
  57. }
  58. /**
  59. * 添加解析记录.
  60. *
  61. * @param string $domain 域名
  62. * @param string $ip ip地址
  63. * @param string $type 记录类型,默认为 A
  64. */
  65. public static function store(string $domain, string $ip, string $type = 'A')
  66. {
  67. if (self::dnsProvider($domain)->store($ip, $type)) {
  68. Log::info("【DDNS】添加:{$ip} => {$domain} 类型:{$type} 成功");
  69. } else {
  70. Log::info("【DDNS】添加:{$ip} => {$domain} 类型:{$type} 失败,请手动设置!");
  71. }
  72. }
  73. }