From 2bcd590ce9afb76427de723a461992d29f574ee3 Mon Sep 17 00:00:00 2001 From: net909 Date: Thu, 19 Dec 2024 18:55:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81namesilo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Domain.php | 6 +- app/lib/DnsHelper.php | 16 ++- app/lib/dns/aliyun.php | 6 +- app/lib/dns/namesilo.php | 221 ++++++++++++++++++++++++++++++++++++ app/view/domain/record.html | 2 +- 5 files changed, 242 insertions(+), 9 deletions(-) create mode 100644 app/lib/dns/namesilo.php diff --git a/app/controller/Domain.php b/app/controller/Domain.php index 2392fde..ad315e5 100644 --- a/app/controller/Domain.php +++ b/app/controller/Domain.php @@ -362,7 +362,7 @@ class Domain extends BaseController $domainRecords = $dns->getDomainRecords($page, $limit, $keyword, $subdomain, $value, $type, $line, $status); if (!$domainRecords) return json(['total' => 0, 'rows' => []]); - if (empty($keyword) && empty($subdomain) && empty($type) && empty($line) && empty($status) && empty($value) && $domainRecords['total'] != $drow['recordcount']) { + if (empty($keyword) && empty($subdomain) && empty($type) && isNullOrEmpty($line) && empty($status) && empty($value) && $domainRecords['total'] != $drow['recordcount']) { Db::name('domain')->where('id', $id)->update(['recordcount' => $domainRecords['total']]); } @@ -373,7 +373,7 @@ class Domain extends BaseController } $dnstype = Db::name('account')->where('id', $drow['aid'])->value('type'); - if ($dnstype == 'baidu') { + if ($dnstype == 'baidu' || $dnstype == 'namesilo') { return json($domainRecords['list']); } @@ -641,7 +641,7 @@ class Domain extends BaseController } else if ($action == 'line') { $line = input('post.line', null, 'trim'); - if (empty($recordinfo) || empty($line)) { + if (empty($recordinfo) || isNullOrEmpty($line)) { return json(['code' => -1, 'msg' => '参数不能为空']); } diff --git a/app/lib/DnsHelper.php b/app/lib/DnsHelper.php index d1fd5f3..831b1c7 100644 --- a/app/lib/DnsHelper.php +++ b/app/lib/DnsHelper.php @@ -103,6 +103,18 @@ class DnsHelper 'log' => false, 'weight' => false, ], + 'namesilo' => [ + 'name' => 'NameSilo', + 'config' => [ + 'ak' => '账户名', + 'sk' => 'API Key', + ], + 'remark' => 0, + 'status' => false, + 'redirect' => false, + 'log' => false, + 'weight' => false, + ], ]; public static $line_name = [ @@ -129,7 +141,7 @@ class DnsHelper } /** - * @return DnsInterface|false + * @return DnsInterface|bool */ public static function getModel($aid, $domain = null, $domainid = null) { @@ -147,7 +159,7 @@ class DnsHelper } /** - * @return DnsInterface|false + * @return DnsInterface|bool */ public static function getModel2($config) { diff --git a/app/lib/dns/aliyun.php b/app/lib/dns/aliyun.php index 5a37a95..f53c3c3 100644 --- a/app/lib/dns/aliyun.php +++ b/app/lib/dns/aliyun.php @@ -83,7 +83,7 @@ class aliyun implements DnsInterface 'Status' => $row['Status'] == 'ENABLE' ? '1' : '0', 'Weight' => isset($row['Weight']) ? $row['Weight'] : null, 'Remark' => isset($row['Remark']) ? $row['Remark'] : null, - 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', $row['UpdateTimestamp'] / 1000) : null, + 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', intval($row['UpdateTimestamp'] / 1000)) : null, ]; } return ['total' => $data['TotalCount'], 'list' => $list]; @@ -111,7 +111,7 @@ class aliyun implements DnsInterface 'Status' => $row['Status'] == 'ENABLE' ? '1' : '0', 'Weight' => isset($row['Weight']) ? $row['Weight'] : null, 'Remark' => isset($row['Remark']) ? $row['Remark'] : null, - 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', $row['UpdateTimestamp'] / 1000) : null, + 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', intval($row['UpdateTimestamp'] / 1000)) : null, ]; } return ['total' => $data['TotalCount'], 'list' => $list]; @@ -137,7 +137,7 @@ class aliyun implements DnsInterface 'Status' => $data['Status'] == 'ENABLE' ? '1' : '0', 'Weight' => isset($data['Weight']) ? $data['Weight'] : null, 'Remark' => isset($data['Remark']) ? $data['Remark'] : null, - 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', $data['UpdateTimestamp'] / 1000) : null, + 'UpdateTime' => isset($row['UpdateTimestamp']) ? date('Y-m-d H:i:s', intval($data['UpdateTimestamp'] / 1000)) : null, ]; } return false; diff --git a/app/lib/dns/namesilo.php b/app/lib/dns/namesilo.php new file mode 100644 index 0000000..7b0eaa6 --- /dev/null +++ b/app/lib/dns/namesilo.php @@ -0,0 +1,221 @@ +apikey = $config['sk']; + $this->domain = $config['domain']; + } + + public function getError() + { + return $this->error; + } + + public function check() + { + if ($this->getDomainList() !== false) { + return true; + } + return false; + } + + //获取域名列表 + public function getDomainList($KeyWord = null, $PageNumber = 1, $PageSize = 20) + { + $param = ['page' => $PageNumber, 'pageSize' => $PageSize]; + $data = $this->send_reuqest('listDomains', $param); + if ($data) { + $list = []; + if($data['domains']){ + foreach ($data['domains'] as $row) { + $list[] = [ + 'DomainId' => $row['domain'], + 'Domain' => $row['domain'], + 'RecordCount' => 0, + ]; + } + } + return ['total' => $data['pager']['total'], 'list' => $list]; + } + return false; + } + + //获取解析记录列表 + public function getDomainRecords($PageNumber = 1, $PageSize = 20, $KeyWord = null, $SubDomain = null, $Value = null, $Type = null, $Line = null, $Status = null) + { + $param = ['domain' => $this->domain]; + $data = $this->send_reuqest('dnsListRecords', $param); + if ($data) { + $list = []; + foreach ($data['resource_record'] as $row) { + $name = $row['host'] == $this->domain ? '@' : str_replace('.'.$this->domain, '', $row['host']); + $list[] = [ + 'RecordId' => $row['record_id'], + 'Domain' => $this->domain, + 'Name' => $name, + 'Type' => $row['type'], + 'Value' => $row['value'], + 'Line' => 'default', + 'TTL' => $row['ttl'], + 'MX' => isset($row['distance']) ? $row['distance'] : null, + 'Status' => '1', + 'Weight' => null, + 'Remark' => null, + 'UpdateTime' => null, + ]; + } + if(!empty($SubDomain)){ + $list = array_values(array_filter($list, function($v) use ($SubDomain){ + return $v['Name'] == $SubDomain; + })); + }else{ + if(!empty($KeyWord)){ + $list = array_values(array_filter($list, function($v) use ($KeyWord){ + return strpos($v['Name'], $KeyWord) !== false || strpos($v['Value'], $KeyWord) !== false; + })); + } + if(!empty($Value)){ + $list = array_values(array_filter($list, function($v) use ($Value){ + return $v['Value'] == $Value; + })); + } + if(!empty($Type)){ + $list = array_values(array_filter($list, function($v) use ($Type){ + return $v['Type'] == $Type; + })); + } + } + return ['total' => count($data['resource_record']), 'list' => $list]; + } + return false; + } + + //获取子域名解析记录列表 + public function getSubDomainRecords($SubDomain, $PageNumber = 1, $PageSize = 20, $Type = null, $Line = null) + { + return $this->getDomainRecords($PageNumber, $PageSize, null, $SubDomain, null, $Type, $Line); + } + + //获取解析记录详细信息 + public function getDomainRecordInfo($RecordId) + { + return false; + } + + //添加解析记录 + public function addDomainRecord($Name, $Type, $Value, $Line = '0', $TTL = 600, $MX = 1, $Weight = null, $Remark = null) + { + $param = ['domain' => $this->domain, 'rrtype' => $Type, 'rrhost' => $Name, 'rrvalue' => $Value, 'rrttl' => $TTL]; + if ($Type == 'MX') $param['rrdistance'] = intval($MX); + $data = $this->send_reuqest('dnsAddRecord', $param); + return is_array($data) ? $data['record_id'] : false; + } + + //修改解析记录 + public function updateDomainRecord($RecordId, $Name, $Type, $Value, $Line = '0', $TTL = 600, $MX = 1, $Weight = null, $Remark = null) + { + $param = ['domain' => $this->domain, 'rrid' => $RecordId, 'rrtype' => $Type, 'rrhost' => $Name, 'rrvalue' => $Value, 'rrttl' => $TTL]; + if ($Type == 'MX') $param['rrdistance'] = intval($MX); + $data = $this->send_reuqest('dnsUpdateRecord', $param); + return is_array($data); + } + + //修改解析记录备注 + public function updateDomainRecordRemark($RecordId, $Remark) + { + return false; + } + + //删除解析记录 + public function deleteDomainRecord($RecordId) + { + $param = ['domain' => $this->domain, 'rrid' => $RecordId]; + $data = $this->send_reuqest('dnsDeleteRecord', $param); + return is_array($data); + } + + //设置解析记录状态 + public function setDomainRecordStatus($RecordId, $Status) + { + return false; + } + + //获取解析记录操作日志 + public function getDomainRecordLog($PageNumber = 1, $PageSize = 20, $KeyWord = null, $StartDate = null, $endDate = null) + { + return false; + } + + //获取解析线路列表 + public function getRecordLine() + { + return ['default' => ['name' => '默认', 'parent' => null]]; + } + + //获取域名信息 + public function getDomainInfo() + { + return false; + } + + //获取域名最低TTL + public function getMinTTL() + { + return false; + } + + private function send_reuqest($operation, $param = null) + { + $url = $this->baseUrl . $operation; + + $params = [ + 'version' => $this->version, + 'type' => 'json', + 'key' => $this->apikey, + ]; + if($param){ + $params = array_merge($params, $param); + } + + $url .= '?' . http_build_query($params); + + try{ + $response = curl_client($url); + }catch(Exception $e){ + $this->setError($e->getMessage()); + return false; + } + + $arr = json_decode($response['body'], true); + if (isset($arr['reply']['code'])) { + if ($arr['reply']['code'] == 300) { + return $arr['reply']; + } else { + $this->setError(isset($arr['reply']['detail']) ? $arr['reply']['detail'] : '未知错误'); + return false; + } + } else { + $this->setError($response['body']); + return false; + } + } + + private function setError($message) + { + $this->error = $message; + //file_put_contents('logs.txt',date('H:i:s').' '.$message."\r\n", FILE_APPEND); + } +} diff --git a/app/view/domain/record.html b/app/view/domain/record.html index 4afdf6e..56d3bc8 100644 --- a/app/view/domain/record.html +++ b/app/view/domain/record.html @@ -236,7 +236,7 @@ td{overflow: hidden;text-overflow: ellipsis;white-space: nowrap;max-width:360px; var recordLine = {$recordLine|json_encode|raw}; var dnsconfig = {$dnsconfig|json_encode|raw}; var defaultLine = recordLine[0].id; -var sidePagination = dnsconfig.type == 'baidu' ? 'client' : 'server'; +var sidePagination = dnsconfig.type == 'baidu' || dnsconfig.type == 'namesilo' ? 'client' : 'server'; var showWeight = dnsconfig.weight; $(document).ready(function(){ updateToolbar();