forked from spiritLHL/qinglong_auto_tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcks_push_alql.py
395 lines (357 loc) · 13.7 KB
/
cks_push_alql.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# 作者仓库:https://github.com/spiritLHL/qinglong_auto_tools
# 觉得不错麻烦点个star谢谢
# 频道:https://t.me/qinglong_auto_tools
'''
cron: 1
new Env('多容器 二叉树分发ck');
'''
# 在脚本管理里修改这个文件的配置,然后保存,然后禁用 二叉树分发ck 这个任务,有需要再点运行
# 记得修改定时,定时在你的转换ck脚本运行完后分发即可
# 主青龙,wskey容器,事先需要在容器里创建应用,给所有权限,然后重启容器,应用设置才会生效,
# 本分发脚本不含转ck功能,只分发,默认分发的环境变量名为JD_COOKIE,其他变量名自己按照下面注释改
# 分发不含wskey的ck的分容器,事先需要在分容器里创建应用,给所有权限,然后重启容器,应用设置才会生效
# 按照格式有几个写几个,没有的空的删除
# 备份ck的容器,里面传入所有转换后的ck,,事先需要在备份容器里创建应用,给所有权限,然后重启容器,应用设置才会生效
# ps:跑一些需要所有ck脚本的备份容器,你懂的
'''
# ec_config.txt中填写如下设置
# 二叉树分发ck
### 主青龙
cks_push_alql_cilent_id1="xxxxxxxx"
cks_push_alql_cilent_secret1="xxxxxxxxxx"
cks_push_alql_url1="http://xxxxxxxx:xxxx/"
### 副青龙
cks_push_alql_N_c=""
cks_push_alql_client_ids=["",""]
cks_push_alql_client_secrets=["",""]
cks_push_alql_urllist=["http://xxxxxxxxx:xxxx/",""]
### 备份容器
cks_push_alql_cilent_id_che=["xxxxxxxx"]
cks_push_alql_cilent_secret_che=["xxxxxxxxxx"]
cks_push_alql_che=["http://xxxxxxxx:xxxx/"]
'''
# client_id1=""
# client_secret1=""
# url1 = "http://ip:端口/"
# client_ids=['','','']
# client_secrets=['','','']
# urllist = ["http://xxxx:xxxx/","",""]
# client_id_che=''
# client_secret_che=''
# che = "http://xxxxx:xxx/"
# 分容器默认配置,如果在配置ec_config.txt中填写有该配置,则会覆盖下面默认40
N_c = 40
import os
import time
import json
import re
try:
import requests
except Exception as e:
print(e, "\n缺少requests 模块,请执行命令安装:python3 -m pip install requests")
exit(3)
try:
with open("ec_config.txt", "r", encoding="utf-8") as fp:
t = fp.readlines()
try:
for i in t:
try:
temp = re.findall(r"cks_push_alql_cilent_id1=\"(.*?)\"", i)[0]
client_id1 = temp
if client_id1 == "":
print("cks_push_alql_cilent_id1 未填写")
except:
pass
except:
print("cks_push_alql_cilent_id1 未创建")
exit(3)
try:
for i in t:
try:
temp = re.findall(r"cks_push_alql_cilent_secret1=\"(.*?)\"", i)[0]
client_secret1 = temp
if client_secret1 == "":
print("cks_push_alql_cilent_secret1 未填写")
except:
pass
except:
print("cks_push_alql_cilent_secret1 未创建")
exit(3)
try:
for i in t:
try:
temp = re.findall(r"cks_push_alql_url1=\"(.*?)\"", i)[0]
url1 = temp
if url1 == "":
print("cks_push_alql_url1 未填写")
except:
pass
except:
print("cks_push_alql_url1 未创建")
exit(3)
except:
print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")
try:
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_client_ids=\[(.*?)\]", i)[0] + "]"
try:
client_ids = json.loads(temp)
except:
print("cks_push_alql_client_ids 填写有误")
if client_ids == []:
print("cks_push_alql_client_ids 未填写")
except:
pass
except:
print("cks_push_alql_client_ids 未创建")
exit(3)
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_client_secrets=\[(.*?)\]", i)[0] + "]"
try:
client_secrets = json.loads(temp)
except:
print("cks_push_alql_client_secrets 填写有误")
if client_secrets == []:
print("cks_push_alql_client_secrets 未填写")
except:
pass
except:
print("cks_push_alql_client_secrets 未创建")
exit(3)
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_urllist=\[(.*?)\]", i)[0] + "]"
try:
urllist = json.loads(temp)
except:
print("cks_push_alql_urllist 填写有误")
if urllist == []:
print("cks_push_alql_urllist 未填写")
except:
pass
except:
print("cks_push_alql_urllist 未创建")
exit(3)
except:
print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")
try:
try:
for i in t:
try:
temp = re.findall(r"cks_push_alql_N_c=\"(.*?)\"", i)[0]
try:
N_c = int(temp)
except:
print("cks_push_alql_N_c 填写有误")
if temp == "40":
print("cks_push_alql_N_c 未在ec_config.txt配置中更改,使用默认配置分发40")
except:
pass
except:
print("cks_push_alql_N_c 未创建")
exit(3)
except:
print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")
try:
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_cilent_id_che=\[(.*?)\]", i)[0] + "]"
client_id_che = json.loads(temp)
if client_id_che == "":
print("cks_push_alql_cilent_id_che 未填写")
except:
pass
except:
print("cks_push_alql_cilent_id_che 未创建")
exit(3)
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_cilent_secret_che=\[(.*?)\]", i)[0] + "]"
client_secret_che = json.loads(temp)
if client_secret_che == "":
print("cks_push_alql_cilent_secret_che 未填写")
except:
pass
except:
print("cks_push_alql_cilent_secret_che 未创建")
exit(3)
try:
for i in t:
try:
temp = "[" + re.findall(r"cks_push_alql_che=\[(.*?)\]", i)[0] + "]"
che = json.loads(temp)
if che == "":
print("cks_push_alql_che 未填写")
except:
pass
except:
print("cks_push_alql_che 未创建")
exit(3)
except:
print("找不到配置文件或配置文件有错误, 请填写ec_config.txt")
requests.packages.urllib3.disable_warnings()
def gettimestamp():
return str(int(time.time() * 1000))
def gettoken(self, url_token):
r = requests.get(url_token).text
res = json.loads(r)["data"]["token"]
self.headers.update({"Authorization": "Bearer " + res})
def login(self, baseurl, client_id_temp, client_secret_temp):
url_token = baseurl + 'open/auth/token?client_id=' + client_id_temp + '&client_secret=' + client_secret_temp
gettoken(self, url_token)
def getitem(self, baseurl, key, typ):
url = baseurl + typ + "/envs?searchValue=%s&t=%s" % (key, gettimestamp())
r = self.get(url)
item = json.loads(r.text)["data"]
return item
def getckitem(self, baseurl, key, typ):
url = baseurl + typ + "/envs?searchValue=JD_COOKIE&t=%s" % gettimestamp() # JD_COOKIE为默认的环境变量名,该变量里的值默认含pt_pin和pt_key,其他类似默认按照下面注释改
r = self.get(url)
for i in json.loads(r.text)["data"]:
if key in i["value"]:
return i
return []
def update(self, baseurl, typ, text, qlid):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
data = {
"name": "JD_COOKIE",
"value": text,
"_id": qlid
}
r = self.put(url, data=json.dumps(data))
if json.loads(r.text)["code"] == 200:
return True
else:
return False
def insert(self, baseurl, typ, text):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
data = []
data_json = {
"value": text,
"name": "JD_COOKIE"
}
data.append(data_json)
r = self.post(url, json.dumps(data))
if json.loads(r.text)["code"] == 200:
return True
else:
return False
if __name__ == '__main__':
s = requests.session()
login(s, url1, client_id1, client_secret1)
wskeys = getitem(s, url1, "JD_COOKIE", "open") # JD_COOKIE为默认转发的主青龙的环境变量名
ck_list = []
c_list = []
for i in wskeys:
tp = i['value']
ptpin = re.findall(r"pt_pin=(.*?);", tp)[0] # 默认获取的变量里的pt_pin=xxx;里的xxx
ptpin = "pt_pin=" + ptpin + ';'
ptkey = re.findall(r"pt_key=(.*?);", tp)[0] # 默认获取的变量里的pt_key=xxxx;里的xxxx
ptkey = "pt_key=" + ptkey + ';'
c = ptkey + ptpin
c_list.append(c)
ck = ptkey + ptpin + '\n'
ck_list.append(ck)
if os.path.exists("./allck.txt"):
os.remove("allck.txt")
with open("./allck.txt", "w") as f:
f.writelines(ck_list)
count = 0
temp = []
ucount = 0
for j in c_list:
count += 1
temp.append(j)
if (count % N_c) == 0: # 分配40个
a = requests.session()
url_token = urllist[ucount] + 'open/auth/token?client_id=' + client_ids[ucount] + '&client_secret=' + \
client_secrets[ucount]
gettoken(a, url_token)
co = 0
for k in temp:
co += 1
ptpin = re.findall(r"pt_pin=(.*?);", k)[0]
ptpin = "pt_pin=" + ptpin
item = getckitem(a, urllist[ucount], ptpin, "open")
if item != []:
try:
qlid = item["_id"]
except:
qlid = item["id"]
if update(a, urllist[ucount], "open", k, qlid):
print("第%s个JD_COOKIE更新成功,pin为%s" % (co, ptpin[7:]))
else:
print("第%s个JD_COOKIE更新失败,pin为%s" % (co, ptpin[7:]))
else:
if insert(a, urllist[ucount], "open", k):
print("第%s个JD_COOKIE添加成功" % co)
else:
print("第%s个JD_COOKIE添加失败" % co)
temp = []
ucount += 1
print("第%s个容器更新成功" % ucount)
a = requests.session()
url_token = urllist[-1] + 'open/auth/token?client_id=' + client_ids[-1] + '&client_secret=' + client_secrets[-1]
gettoken(a, url_token)
co = 0
if temp != []: # 剩余放到最后一个容器里
for k in temp:
co += 1
ptpin = re.findall(r"pt_pin=(.*?);", k)[0]
ptpin = "pt_pin=" + ptpin
item = getckitem(a, urllist[-1], ptpin, "open")
if item != []:
try:
qlid = item["_id"]
except:
qlid = item["id"]
if update(a, urllist[-1], "open", k, qlid):
print("第%s个JD_COOKIE更新成功,pin为%s" % (co, ptpin[7:]))
else:
print("第%s个JD_COOKIE更新失败,pin为%s" % (co, ptpin[7:]))
else:
if insert(a, urllist[-1], "open", k):
print("第%s个JD_COOKIE添加成功" % co)
else:
print("第%s个JD_COOKIE添加失败" % co)
print("最后一个容器更新成功")
print('分发CK完毕')
print()
else:
print('分发CK完毕')
print()
for i in range(0, len(che)):
b = requests.session()
url_token = che[i] + 'open/auth/token?client_id=' + client_id_che[i] + '&client_secret=' + client_secret_che[i]
gettoken(b, url_token)
co = 0
for chek in c_list: # 备份ck
co += 1
ptpin = re.findall(r"pt_pin=(.*?);", chek)[0]
ptpin = "pt_pin=" + ptpin
item = getckitem(b, che[i], ptpin, "open")
if item != []:
try:
qlid = item["_id"]
except:
qlid = item["id"]
if update(b, che[i], "open", chek, qlid):
print("第%s个JD_COOKIE更新成功,pin为%s" % (co, ptpin[7:]))
else:
print("第%s个JD_COOKIE更新失败,pin为%s" % (co, ptpin[7:]))
else:
if insert(b, che[i], "open", chek):
print("第%s个JD_COOKIE添加成功" % co)
else:
print("第%s个JD_COOKIE添加失败" % co)
print('备份容器{} 更新完毕\n'.format(i + 1))
print("分发结束")