-
Notifications
You must be signed in to change notification settings - Fork 51
/
serv.py
350 lines (322 loc) · 12.9 KB
/
serv.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding:utf-8 -*-
import json
import logging
import re
import requests
from lxml import etree
from logger import Mylogger
import asyncio
class Serv00(object):
def __init__(self,pannelnum,logininfo,hostname):
self.logger = Mylogger.getCommonLogger("Serv00.log",logging.INFO,1)
#basepath = 'https://panel'+str(pannelnum)+'.serv00.com/'
basepath = 'https://'+hostname+'/'
self.loginReferer = basepath +'login/'
self.portlistReferer = basepath
self.delPortReferer = basepath+'port/'
self.addPortReferer = basepath+'port/add'
self.addwebsiteReferer = basepath+'www/add'
self.headers = {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.8',
'Cache-Control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
'Connection': 'keep-alive',
'Referer': self.loginReferer
}
self.portToken = None
self.websiteToken = None
self.session = requests.session()
self.url = self.loginReferer
self.portUrl = self.delPortReferer
self.addPortUrl = self.addPortReferer
self.addWebsiteUrl = self.addwebsiteReferer
self.getWebsitesUrl = basepath+"www/"
self.getSSLWebsitesUrl = basepath+"ssl/www"
self.addSertificateUrl = basepath+"ssl/www/sni/add/"
self.runAppPermissionUrl = basepath+'permissions/binexec'
proxy = '127.0.0.1:10809'
self.proxies = {
'http': 'http://' + proxy,
'https': 'https://' + proxy,
}
self.logininfo = logininfo
self.pannelnum = pannelnum
self.islogin = self.login()
def login(self):
rep = self.session.get(self.loginReferer)
html = etree.HTML(rep.text)
token = html.xpath('//*[@id="centerlogin"]/div[1]/div[1]/form/input/@value')[0]
self.logininfo['csrfmiddlewaretoken'] = token
response = self.session.post(self.url,data=self.logininfo,headers = self.headers)
print(response.status_code)
return response.status_code == 200
def getports(self):
headers = self.headers
headers['Referer'] = self.portlistReferer
portrsp = self.session.get(self.portUrl,headers = headers)
porthtml = portrsp.text
html = etree.HTML(porthtml)
portlist = html.xpath('//*[@id="port_list"]/tbody//td[2]/@data-order')
return portlist
#获取port相关token
def getPortToken(self):
token = self.portToken
if token is None:
rep = self.session.get(self.addPortUrl)
html = etree.HTML(rep.text)
token = html.xpath('//*[@id="content-wrapper"]/form/input/@value')[0]
self.portToken = token
return token
#请求添加端口
def addport(self,port):
headers = self.headers
headers['Referer'] = self.addPortReferer
data = {}
if port is not None:
port = port
data['id_port-placeholder-1'] = port
else:
port = 'random'
data['csrfmiddlewaretoken'] = self.getPortToken()
data['port'] = port
data['port_type'] = 'tcp'
data['description'] = ''
resp = self.session.post(self.addPortUrl,data=data,headers = headers)
#print(resp.text)
if resp and resp.status_code == 200:
return port
else:
self.logger.info(f"{port} add port erro :{resp.status_code}")
return 0
#请求删除端口
def delport(self,port):
headers = self.headers
headers['Referer'] = self.delPortReferer
data = {}
data['csrfmiddlewaretoken'] = self.getPortToken()
data['del_port'] = port
data['del_port_type'] = 'tcp'
resp = self.session.post(self.portUrl,data=data,headers = headers)
return resp and resp.status_code == 200
def getloginPorts(self):
islogin = self.islogin
if islogin:
ports = self.getports()
print(ports)
return ports
def getWebsites(self):
resp = self.session.get(self.getWebsitesUrl)
reshtml = resp.text
html = etree.HTML(reshtml)
webSites = html.xpath('//*[@id="www_domain_list"]//tr/td[2]/text()')
res=[]
for chu in webSites:
ele = re.sub('\s', '', ''.join(chu))
if ele:
res.append(ele)
return res
#添加网址
def addWebsite(self,domain,port):
headers = self.headers
headers['Referer'] = self.addwebsiteReferer
data = {}
data['csrfmiddlewaretoken'] = self.getWebsiteToken()
#//*[@id="id_domain"]
data['domain'] = domain
data['type'] = 'proxy'
data['proxy_target'] = 'localhost'
data['pointer_target'] = 'webmail'
data['proxy_port'] = port
data['environment'] = 'production'
resp = self.session.post(self.addWebsiteUrl,data=data,headers = headers)
#print(resp.text)
if resp and resp.status_code == 200:
self.logger.info(f"{domain} addWebsite success {resp.status_code}")
return domain
else:
self.logger.info(f"{domain} addWebsite erro {resp.status_code}")
return 0
#获取port相关token
def getWebsiteToken(self):
token = self.websiteToken
if token is None:
rep = self.session.get(self.addWebsiteUrl)
html = etree.HTML(rep.text)
#//*[@id="www_add_form"]/input
token = html.xpath('//*[@id="www_add_form"]/input/@value')
self.websiteToken = token
return token
#获取ip列表
def getSSLWebsites(self):
headers = self.headers
url = self.getSSLWebsitesUrl
headers['Referer'] = self.getReferer(url)
portrsp = self.session.get(url,headers = headers)
porthtml = portrsp.text
html = etree.HTML(porthtml)
#//*[@id="DataTables_Table_0"]/tbody/tr[1]/td[2]/text()
ips = html.xpath('//td[2]/text()')
#//*[@id="DataTables_Table_0"]/tbody/tr[1]/td[3]/span
nums = html.xpath('//td[3]/text()')
return dict(zip(ips, nums)),ips,nums
def getReferer(self, url):
if url:
paths = url.split("/")
newurl = "/".join(paths[:-1])+"/"
return newurl
def getSertificateToken(self,ip):
rep = self.session.get(self.addSertificateUrl+ip)
html = etree.HTML(rep.text)
token = html.xpath('//form/input/@value')[1]
return token
#自动添加证书
def addSertificate(self,ip,domain):
url = self.addSertificateUrl+ip
headers = self.headers
headers['Referer'] = self.getReferer(self.addSertificateUrl)
data = {}
data['csrfmiddlewaretoken'] = self.getSertificateToken(ip)
#//*[@id="id_domain"]
data['cert_type'] = 'le'
data['ip'] = ip
data['domain'] = domain
data['cert_cert'] = '(二进制)'
data['cert_key'] = '(二进制)'
resp = self.session.post(url, data=data, headers=headers)
#print(resp.text)
if resp and resp.status_code == 200:
self.logger.info(f"addSertificate success {resp.status_code}")
return domain
else:
self.logger.info(f"{domain} addSertificate erro {resp.status_code}")
return 0
#自动添加证书
def delSertificate(self,domain):
url = self.getWebsitesUrl
headers = self.headers
headers['Referer'] = self.getReferer(self.getWebsitesUrl)
data = {}
data['csrfmiddlewaretoken'] = self.getWebsiteToken()
#//*[@id="id_domain"]
data['del_domain'] = domain
data['del_files'] = ''
data['del_dns'] = 'on'
resp = self.session.post(url, data=data, headers=headers)
#print(resp.text)
if resp and resp.status_code == 200:
self.logger.info(f"{domain} delSertificate success")
return domain
else:
self.logger.info(f"{domain} delSertificate erro {resp.status_code}")
return 0
#查询是否开启app权限
def runAppPermission(self):
headers = self.headers
url = self.runAppPermissionUrl
headers['Referer'] = self.getReferer(url)
portrsp = self.session.get(url,headers = headers)
porthtml = portrsp.text
html = etree.HTML(porthtml)
#//*[@id="DataTables_Table_0"]/tbody/tr[1]/td[2]/text()
res = html.xpath('//input/@placeholder')[0]
return res
def getToken(self,url):
rep = self.session.get(url)
html = etree.HTML(rep.text)
token = html.xpath('//form/input/@value')[1]
return token
#开启运行app权限
def enableAppPermission(self):
enableRes = self.runAppPermission()
if enableRes == 'Enabled':
return 1
else:
headers = self.headers
url = self.runAppPermissionUrl
headers['Referer'] = url
data = {}
data['csrfmiddlewaretoken'] = self.getToken(url)
data['action'] = 'on'
resp = self.session.post(url, data=data, headers=headers)
if resp and resp.status_code == 200:
self.logger.info(f"enableAppPermission success {resp.status_code}")
return 1
else:
self.logger.info(f"enableAppPermission erro {resp.status_code}")
return 0
"""
处理主方法
domains:域名集合
ports:端口
isForce:是否强行删除
cnameType:是否是cf cname配置方式
"""
def runMain(self,domains,ports,isForce,cnameType):
#serv = Serv00(pannelnum,logininfo,hostname)
serv = self
enableRes = serv.enableAppPermission()
serv.logger.info(f"{domains} enableAppPermission state::{enableRes}")
length = len(domains)
#服务器只提供2个id,只帮申请2个端口
if ports and len(ports)>0:
results = []
ip_nums,ips,nums = serv.getSSLWebsites()
serv.logger.info(f"get ips : {ips} nums:{nums}")
if ips and len(ips)>0:
for index, ip in enumerate(ips):
servDomain = domains[index]
if ip_nums[ip] == '1':
if not cnameType or isForce:
serv.delSertificate(servDomain)
else:
serv.logger.info(f"{servDomain} ssl certificate exisit...")
break
else:
if not cnameType:
#绑定proxy端口
#websites = serv.getWebsites()
res = serv.addWebsite(servDomain, ports[index])
if res and res == servDomain:
serv.logger.info(f"{servDomain} add ssl certificate start,please waiting...")
#申请证书
res = serv.addSertificate(ips[index], servDomain)
if res and res == servDomain:
results.append(res)
serv.logger.info(f"{servDomain} add ssl certificate success")
else:
serv.logger.info(f"{servDomain} add ssl certificate start,please waiting...")
res = serv.addSertificate(ips[index], servDomain)
if res and res == servDomain:
results.append(res)
serv.logger.info(f"{servDomain} add ssl certificate success")
if length<=1:
break
return results
if __name__ == '__main__':
with open('user_info3.json', 'r') as f:
userInfo = json.load(f)
accounts = userInfo['accounts']
if accounts and len(accounts) > 0:
sSHClients = []
for account in accounts:
pannelnum = account['pannelnum']
username = account['username']
password = account['password']
logininfo = {
"username":username,
"password":password
}
SERVER_TYPE = 1
if 'server_type' in account and account['server_type'] == 2:
SERVER_TYPE = 2
HOSTNAME = 'panel.ct8.pl'
else:
HOSTNAME = 'panel' + str(pannelnum) + '.serv00.com'
serv00 = Serv00(pannelnum,logininfo,HOSTNAME)
serv00.delSertificate("junjie2.junx888.us.kg")
#serv00.runMain(["junx123.cloudns.ch","vl.junx888.us.kg"],[8006,8007])
#serv00.runAppPermission()
#serv00.enableAppPermission()
#serv00.runMain(domain)
#asyncio.run(Serv00.runMain(domain))