forked from aaPanel/BaoTa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterDns.py
321 lines (285 loc) · 10.8 KB
/
clusterDns.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#coding: utf-8
# +-------------------------------------------------------------------
# | 集群架构 - DNS管理模块
# +-------------------------------------------------------------------
# | Copyright (c) 2015-2099 宝塔软件(https://bt.cn) All rights reserved.
# +-------------------------------------------------------------------
# | Author: hwliang <[email protected]>
# +-------------------------------------------------------------------
import public,os,sys,json,time,random
import requests
from OpenSSL import crypto
import sys, os
import time
import copy
import json
import base64
import hashlib
import binascii
import urllib
if sys.version_info[0] == 2: # python2
import urlparse
from urlparse import urljoin
import urllib2
import cryptography.hazmat
import cryptography.hazmat.backends
import cryptography.hazmat.primitives.serialization
else: # python3
from urllib.parse import urlparse
from urllib.parse import urljoin
import cryptography
import platform
import hmac
try:
import requests
except:
public.ExecShell('pip install requests')
import requests
try:
import OpenSSL
except:
public.ExecShell('pip install pyopenssl')
import OpenSSL
import random
import datetime
import logging
from hashlib import sha1
class clusterDns:
def __init__(self):
pass
class aliyun:
key = None
secret = None
url = "https://alidns.aliyuncs.com"
def __init__(self, key, secret, ):
self.key = str(key).strip()
self.secret = str(secret).strip()
def sign(self, parameters):
'''
@name 签名
@author hwliang<2020-10-30>
@param parameters<dict> 被签名的参数
@return string
'''
def percent_encode(encodeStr):
encodeStr = str(encodeStr)
if sys.version_info[0] == 3:
import urllib.request
res = urllib.request.quote(encodeStr, '')
else:
res = urllib2.quote(encodeStr, '')
res = res.replace('+', '%20')
res = res.replace('*', '%2A')
res = res.replace('%7E', '~')
return res
sortedParameters = sorted(parameters.items(), key=lambda parameters: parameters[0])
canonicalizedQueryString = ''
for (k, v) in sortedParameters:
canonicalizedQueryString += '&' + percent_encode(k) + '=' + percent_encode(v)
stringToSign = 'GET&%2F&' + percent_encode(canonicalizedQueryString[1:])
if sys.version_info[0] == 2:
h = hmac.new(self.secret + "&", stringToSign, sha1)
signature = base64.encodestring(h.digest()).strip()
else:
h = hmac.new(bytes(self.secret + "&", encoding="utf8"), stringToSign.encode('utf8'), sha1)
signature = base64.encodebytes(h.digest()).strip()
return signature
def get_params(self,action,pdata={}):
'''
@name 构造请求参数
@author hwliang<2020-10-30>
@param action<string> 请求动作
@return dict
'''
randomint = random.randint(11111111111111, 99999999999999)
now = datetime.datetime.utcnow()
otherStyleTime = now.strftime("%Y-%m-%dT%H:%M:%SZ")
paramsdata = {
"Action":action,
"Format": "json",
"Version": "2015-01-09",
"SignatureMethod": "HMAC-SHA1",
"Timestamp": otherStyleTime,
"SignatureVersion": "1.0",
"SignatureNonce": str(randomint),
"Lang": 'cn',
"AccessKeyId": self.key
}
for k in pdata.keys():
paramsdata[k] = pdata[k]
paramsdata['Signature'] = self.sign(paramsdata)
return paramsdata
def check_result(self,req):
'''
@name 检查响应结果
@author hwliang<2020-10-30>
@param req<requests> 响应结果
@return mixed
'''
result = req.json()
if req.status_code != 200:
if result['Code'] == 'IncorrectDomainUser' or result['Code'] == 'InvalidDomainName.NoExist':
return public.returnMsg(False,"这个阿里云账户下面不存在这个域名")
elif result['Code'] == 'InvalidAccessKeyId.NotFound' or result['Code'] == 'SignatureDoesNotMatch':
return public.returnMsg(False,"API密钥错误")
else:
return public.returnMsg(False,result['Message'])
return result
def get_domain_list(self,args = None):
'''
@name 获取域名列表
@author hwliang<2020-10-30>
@param args<dict_obj> 前端参数
@return list
'''
paramsdata = self.get_params('DescribeDomains')
req = requests.get(url=self.url, params=paramsdata,verify=False)
result = self.check_result(req)
return result
def add_record(self,domain,s_type,host,value):
'''
@name 添加解析记录
@author hwliang<2020-10-30>
@param args<dict_obj> 前端参数
@return list
'''
paramsdata = {}
paramsdata['DomainName'] = domain
paramsdata['RR'] = host
paramsdata['Type'] = s_type
paramsdata['Value'] = value
paramsdata = self.get_params('AddDomainRecord',paramsdata)
req = requests.get(url=self.url, params=paramsdata,verify=False)
result = self.check_result(req)
if 'status' in result:
if not result['status']: return result
return public.returnMsg(True,'添加成功')
def query_recored_items(self, domain, host=None, s_type=None, page=1, psize=200):
'''
@name 获取解析列表
@author hwliang<2020-10-30>
@param domain<string> 域名
@param host<string> 记录值关键词
@param s_type<string> 记录类型关键词
@param page<int> 分页
@param psize<int> 每页行数
@return list
'''
paramsdata = {}
paramsdata['DomainName'] = domain
paramsdata['PageNumber'] = page
paramsdata['PageSize'] = psize
if host: paramsdata['RRKeyWord'] = host
if s_type: paramsdata['TypeKeyWord'] = s_type
paramsdata = self.get_params('DescribeDomainRecords',paramsdata)
req = requests.get(url=self.url, params=paramsdata,verify=False)
result = self.check_result(req)
return result
def query_recored_id(self, domain, host, s_type="A"):
'''
@name 获取解析标识
@author hwliang<2020-10-30>
@param domain<string> 域名
@param zone<string> 记录值关键词
@param tipe<string> 记录类型关键词
@return int or None
'''
record_id = None
recoreds = self.query_recored_items(domain, host, s_type=s_type)
recored_list = recoreds.get("DomainRecords", {}).get("Record", [])
recored_item_list = [i for i in recored_list if i["RR"] == host]
if len(recored_item_list):
record_id = recored_item_list[0]["RecordId"]
return record_id
def remove_record(self,domain,host,s_type = 'A'):
'''
@name 删除解析记录
@author hwliang<2020-10-30>
@param domain<string> 域名
@param host<string> 记录值关键词
@param s_type<string> 记录类型关键词
@return dict
'''
record_id = self.query_recored_id(domain,host,s_type)
if not record_id:
return public.returnMsg(False,"找不到域名的record_id: {}".format(domain))
paramsdata = {}
paramsdata['RecordId'] = record_id
paramsdata = self.get_params('DeleteDomainRecord',paramsdata)
req = requests.get(url=self.url, params=paramsdata,verify=False)
result = self.check_result(req)
if 'status' in result:
if not result['status']: return result
return public.returnMsg(True,'删除成功')
class dnspod:
dns_provider_name = "dnspod"
url = 'https://dnsapi.cn/'
http_timeout = 60
dnspod_id = None
dnspod_api_key = None
dnspod_login = None
def __init__(self, dnspod_id, dnspod_api_key):
self.dnspod_id = dnspod_id
self.dnspod_api_key = dnspod_api_key
self.dnspod_login = "{0},{1}".format(self.dnspod_id, self.dnspod_api_key)
def get_params(self):
'''
@name 构造请求参数
@author hwliang<2020-10-30>
@return dict
'''
params = {
"format": "json",
"login_token": self.dnspod_login,
"lang":'cn',
"error_on_empty":'no'
}
return params
def get_domain_list(self,args=None):
'''
@name 域取域名列表
@author hwliang<2020-10-30>
@return dict
'''
url = urljoin(self.url, "Domain.List")
params = self.get_params()
req = requests.post(url, data=params, timeout=self.http_timeout).json()
return req
def get_record_list(self,domain):
url = urljoin(self.url, "Record.List")
params = self.get_params()
params['domain'] = domain
req = requests.post(url, data=params, timeout=self.http_timeout).json()
return req
def add_record(self,domain,host,value,s_type):
url = urljoin(self.url, "Record.Create")
params = self.get_params()
params['record_type'] = s_type
params['domain'] = domain
params['sub_domain'] = host
params['value'] = value
params['record_line_id'] = '0'
req = requests.post(url, data=params, timeout=self.http_timeout).json()
if req["status"]["code"] != "1":
raise ValueError(
"Error creating dnspod dns record: status_code={status_code} response={response}".format(
status_code=req["status"]["code"],
response=req["status"]["message"],
)
)
def remove_record(self,domain,host,s_type):
url = urljoin(self.url, "Record.List")
params = self.get_params()
params['record_type'] = s_type
params['domain'] = domain
params['subdomain'] = host
list_dns_response = requests.post(url, data=params, timeout=self.http_timeout).json()
urlr = urljoin(self.url, "Record.Remove")
for i in range(0, len(list_dns_response["records"])):
if list_dns_response["records"][i]['name'] != host:
continue
record_id = list_dns_response["records"][i]["id"]
params = self.get_params()
params['domain'] = domain
params['record_id'] = record_id
requests.post(urlr, data=params, timeout=self.http_timeout).json()