File size: 10,613 Bytes
17e971c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | from sslModel.base import sslBase
import public
import hmac
import hashlib
import json
import time
import requests
from typing import Dict, Any, Optional, Tuple
class main(sslBase):
def __init__(self):
super().__init__()
def __init_data(self, data):
self.account_id = data["AccountID"]
self.access_key = data["AccessKey"]
self.secret_key = data["SecretKey"]
self.base_url = "https://dmp.bt.cn"
def _generate_signature(self, method: str, path: str, body: str) -> Tuple[str, str]:
"""生成 API 签名"""
timestamp = str(int(time.time()))
signing_string = f"{self.account_id}\n{timestamp}\n{method.upper()}\n{path}\n{body}"
signature = hmac.new(
self.secret_key.encode('utf-8'),
signing_string.encode('utf-8'),
hashlib.sha256
).hexdigest()
return timestamp, signature
def _request(self, method: str, path: str, data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""发起 API 请求"""
url = self.base_url + path
body_str = ""
body_bytes = None
if data is not None:
body_str = json.dumps(data, separators=(',', ':'))
body_bytes = body_str.encode('utf-8')
timestamp, signature = self._generate_signature(method, path, body_str)
headers = {
"Content-Type": "application/json",
"X-Account-ID": self.account_id,
"X-Access-Key": self.access_key,
"X-Timestamp": timestamp,
"X-Signature": signature
}
response = requests.request(
method=method.upper(),
url=url,
data=body_bytes,
headers=headers
)
result = response.json()
if not result.get("status"):
raise Exception(f"API 请求失败: {result.get('msg')}")
return result
def create_dns_record(self, get):
domain_name = get.domain_name
domain_dns_value = get.domain_dns_value
record_type = 'TXT'
if 'record_type' in get:
record_type = get.record_type
mx = 0
if record_type == 'MX':
if not get.get('mx'):
return public.returnMsg(False, 'MX记录类型必须填写MX值')
mx = int(get.mx)
domain_name, sub_domain, _ = self.extract_zone(domain_name)
if not sub_domain:
sub_domain = '@'
domain_data = self.get_domain_list(get)
if not domain_data['status']:
return domain_data
domain_list = domain_data['data']
domain_id = None
for d in domain_list:
if d['name'] == domain_name:
domain_id = d['id']
break
if not domain_id:
return public.returnMsg(False, '域名不存在,请先添加域名')
try:
body = {
"domain_type": 1,
"domain_id": domain_id,
"record": sub_domain,
"type": record_type,
"value": domain_dns_value,
"TTL": int(get.ttl) if 'ttl' in get else 600,
"MX": mx,
"line": "默认",
"remark": get.remark if 'remark' in get else "",
"view_id": 0,
}
res = self._request("POST", "/api/v1/dns/record/create", body)
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "添加解析记录失败: {}".format(e))
if not res.get("status"):
return public.returnMsg(False, "添加解析记录失败: {}".format(res.get("msg")))
return public.returnMsg(True, "添加解析记录成功")
def delete_dns_record(self, get):
domain_name = get.domain_name
domain_name, sub_domain, _ = self.extract_zone(domain_name)
domain_data = self.get_domain_list(get)
if not domain_data['status']:
return domain_data
domain_list = domain_data['data']
domain_id = None
for d in domain_list:
if d['name'] == domain_name:
domain_id = d['id']
break
if not domain_id:
return public.returnMsg(False, '域名不存在,请先添加域名')
record_id = get.RecordId
try:
body = {
"record_id": record_id,
"domain_type":1,
"domain_id":domain_id
}
res = self._request("POST", "/api/v1/dns/record/delete", body)
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "删除解析记录失败: {}".format(e))
if not res.get("status"):
return public.returnMsg(False, "删除解析记录失败: {}".format(res.get("msg")))
return public.returnMsg(True, "删除解析记录成功")
def update_dns_record(self, get):
domain_name = get.domain_name
domain_dns_value = get.domain_dns_value
record_type = 'TXT'
if 'record_type' in get:
record_type = get.record_type
mx = 0
if record_type == 'MX':
if not get.get('mx'):
return public.returnMsg(False, 'MX记录类型必须填写MX值')
mx = int(get.mx)
domain_name, sub_domain, _ = self.extract_zone(domain_name)
if not sub_domain:
sub_domain = '@'
domain_data = self.get_domain_list(get)
if not domain_data['status']:
return domain_data
domain_list = domain_data['data']
domain_id = None
for d in domain_list:
if d['name'] == domain_name:
domain_id = d['id']
break
if not domain_id:
return public.returnMsg(False, '域名不存在,请先添加域名')
record_id = get.RecordId
try:
body = {
"record_id": record_id,
"domain_type": 1,
"domain_id": domain_id,
"record": sub_domain,
"type": record_type,
"value": domain_dns_value,
"TTL": int(get.ttl) if 'ttl' in get else 600,
"MX": mx,
"line": "默认",
"remark": get.remark if 'remark' in get else "",
"view_id": 0,
}
res = self._request("POST", "/api/v1/dns/record/update", body)
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "修改解析记录失败: {}".format(e))
if not res.get("status"):
return public.returnMsg(False, "修改解析记录失败: {}".format(res.get("msg")))
return public.returnMsg(True, "修改解析记录成功")
def set_dns_record_status(self, get):
domain_name = get.domain_name
domain_name, sub_domain, _ = self.extract_zone(domain_name)
domain_data = self.get_domain_list(get)
if not domain_data['status']:
return domain_data
domain_list = domain_data['data']
domain_id = None
for d in domain_list:
if d['name'] == domain_name:
domain_id = d['id']
break
if not domain_id:
return public.returnMsg(False, '域名不存在,请先添加域名')
record_id = get.RecordId
try:
body = {
"record_id": record_id,
"domain_type":1,
"domain_id":domain_id,
}
res = self._request("POST", "/api/v1/dns/record/{}".format("start" if get.status == '0' else "pause"), body)
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "设置解析记录状态失败: {}".format(e))
if not res.get("status"):
return public.returnMsg(False, "设置解析记录状态失败: {}".format(res.get("msg")))
return public.returnMsg(True, "设置解析记录状态成功")
def get_dns_record(self, get):
domain_name = get.domain_name
domain_data = self.get_domain_list(get)
if not domain_data['status']:
return domain_data
domain_list = domain_data['data']
domain_id = None
for d in domain_list:
if d['name'] == domain_name:
domain_id = d['id']
break
if not domain_id:
return public.returnMsg(False, '域名不存在,请先添加域名')
try:
body = {
"domain_id": domain_id,
"searchKey": "record",
"domain_type": 1,
"p": get.p,
"row": get.limit,
}
if "search" in get and get.search:
body["searchValue"] = get.search
res = self._request("POST", "/api/v1/dns/record/list", body)
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "获取解析记录失败: {}".format(e))
data = {"info": {"record_total": res.get("data", {}).get("count", 0)}, "list": []}
for i in res.get("data", {}).get("data", []):
data["list"].append({
"RecordId": i["record_id"],
"name": i["record"]+"."+domain_name if i["record"] != "@" else domain_name,
"type": i["type"],
"value": i["value"],
"ttl": i["TTL"],
"mx": i.get("MX", 0),
"status": "暂停" if i["state"] == 2 else "启用",
"line": i.get("line", "默认"),
"remark": i.get("remark", ""),
})
return data
def get_domain_list(self, get):
self.__init_data(self.get_dns_data(None)[get.dns_id])
try:
res = self._request("POST", "/api/v1/domain/manage/list", {"p":1,"rows":10, "status":1})
except Exception as e:
public.print_log(e)
return public.returnMsg(False, "获取域名列表失败: {}".format(e))
data = []
local_domain_list = [d['domain'] for d in public.M('ssl_domains').field('domain').select()]
for i in res.get("data", {}).get("data", []):
data.append({
"id": i["id"],
"name": i["full_domain"],
"sync": 0 if i["full_domain"] in local_domain_list else 1,
})
return {"status": True, "msg": "获取成功", "data": data}
|