From cadea6dcaf401d726c44ace82eb5a62ac259164f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=94=E5=A7=AC=E6=A1=91?= Date: Mon, 12 Oct 2020 11:48:08 +0800 Subject: [PATCH] Add DNSPod to DDNS Module --- app/Components/DDNS.php | 3 + app/Components/DDNS/Aliyun.php | 1 - app/Components/DDNS/DNSPod.php | 148 ++++++++++++++++++ resources/views/admin/config/system.blade.php | 5 +- 4 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 app/Components/DDNS/DNSPod.php diff --git a/app/Components/DDNS.php b/app/Components/DDNS.php index ff417a6d..99ea438e 100644 --- a/app/Components/DDNS.php +++ b/app/Components/DDNS.php @@ -3,6 +3,7 @@ namespace App\Components; use App\Components\DDNS\Aliyun; +use App\Components\DDNS\DNSPod; use App\Components\DDNS\Namesilo; use Log; @@ -32,6 +33,8 @@ class DDNS return (new Aliyun($domain)); case 'namesilo': return new Namesilo($domain); + case 'dnspod': + return new DNSPod($domain); default: Log::error("未知渠道:".sysConfig('ddns_mode')); diff --git a/app/Components/DDNS/Aliyun.php b/app/Components/DDNS/Aliyun.php index 2cb13c07..d92c3d5a 100644 --- a/app/Components/DDNS/Aliyun.php +++ b/app/Components/DDNS/Aliyun.php @@ -149,5 +149,4 @@ class Aliyun return false; } - } diff --git a/app/Components/DDNS/DNSPod.php b/app/Components/DDNS/DNSPod.php new file mode 100644 index 00000000..c4c98a7f --- /dev/null +++ b/app/Components/DDNS/DNSPod.php @@ -0,0 +1,148 @@ +analysisDomain(); + + if ($domainInfo) { + return $this->send('Record.Create', [ + 'domain_id' => $domainInfo[2], + 'sub_domain' => $domainInfo[1], + 'record_type' => $type, + 'record_line_id' => 0, + 'value' => $ip, + ]); + } + + return false; + } + + public function analysisDomain() + { + $domainList = $this->domainList(); + if ($domainList) { + foreach ($domainList as $key => $domain) { + if (strpos(self::$subDomain, $domain) !== false) { + return [$domain, rtrim(substr(self::$subDomain, 0, -(strlen($domain))), '.'), $key]; + } + } + } + + return false; + } + + public function domainList() + { + $domainList = $this->send('Domain.List', ['type' => 'mine']); + if ($domainList) { + return Arr::pluck($domainList['domains'], 'name', 'id'); + } + + return false; + } + + private function send($action, $data = []) + { + $public = [ + 'login_token' => sysConfig('ddns_key').','.sysConfig('ddns_secret'), + 'format' => 'json', + ]; + $parameters = array_merge($data, $public); + + $response = Http::timeout(15)->asForm()->post(self::$apiHost.$action, $parameters); + $message = $response->json(); + + if ($response->failed() || ($message && Arr::has($message, 'status.code') && $message['status']['code'] !== "1")) { + if ($message && Arr::has($message, 'status.code') && $message['status']['code'] !== "1") { + $error = $message['status']['message']; + } else { + $error = $response->body(); + } + Log::error('[DNSPod - '.$action.'] 请求失败:'.$error); + + return false; + } + + return $message; + } + + public function destroy($type) + { + $records = $this->getRecordId($type); + $domainInfo = $this->analysisDomain(); + if ($records && $domainInfo) { + $count = 0; + foreach ($records as $record) { + $result = $this->send('Record.Remove', ['domain_id' => $domainInfo[2], 'record_id' => $record]); + if ($result) { + $count++; + } + } + + return $count; + } + + return false; + } + + public function getRecordId($type = null) + { + $domainInfo = $this->analysisDomain(); + if ($domainInfo) { + $parameters = ['domain_id' => $domainInfo[2], 'sub_domain' => $domainInfo[1]]; + if ($type) { + $parameters['record_type'] = $type; + } + $records = $this->send('Record.List', $parameters); + if ($records && Arr::has($records, 'records')) { + return Arr::pluck($records['records'], 'id'); + } + } + + return false; + } + + public function update($ip, $type) + { + $recordId = $this->getRecordId($type); + $domainInfo = $this->analysisDomain(); + if ($recordId && $domainInfo) { + return $this->send('Record.Modify', [ + 'domain_id' => $domainInfo[2], + 'record_id' => $recordId[0], + 'sub_domain' => $domainInfo[1], + 'record_type' => $type, + 'record_line_id' => 0, + 'value' => $ip, + ]); + } + + return false; + } + + public function version() + { + $version = $this->send('Info.Version'); + if ($version) { + return $version['status']['message']; + } + + return false; + } +} diff --git a/resources/views/admin/config/system.blade.php b/resources/views/admin/config/system.blade.php index b823882f..e3c9afcb 100644 --- a/resources/views/admin/config/system.blade.php +++ b/resources/views/admin/config/system.blade.php @@ -562,13 +562,14 @@ + 添加/编辑/删除节点的【域名、ipv4、ipv6】时,自动更新对应内容至DNS服务商
- +
@@ -584,7 +585,7 @@
- +