Newer
Older
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
# -*- coding:utf-8 -*-
# 作者仓库:https://jihulab.com/spiritlhl/qinglong_auto_tools.git
# 觉得不错麻烦点个star谢谢
# 频道:https://t.me/qinglong_auto_tools
'''
cron: 1
new Env('单容器 二叉树随机ck顺序');
'''
# 初次运行生成原始顺序模板文件 trigger_cookies.json,备份原始ck文件 allck.txt
# 可每次运行重新生成模板,在配置文件中配置 export ec_write_cks="true" 开启该功能
# 默认保持前6位ck顺序不变,有需要在配置文件中配置 export ec_head_cks="具体几个" 更改数量
# 可配置随机顺序时给ck备注标上原始顺序,如果备注已存在,则保留原始备注不更改
# 禁用的ck自动后置,非ck变量全部自动前置
# 默认任务定时自行修改
import os
import time
import json
import re
import random
print("不要在主容器运行!!!本脚本每运行一次少一个ck,请在分容器运行!!!")
try:
import requests
except Exception as e:
print(e, "\n缺少requests 模块,请执行命令安装:python3 -m pip install requests")
exit(3)
requests.packages.urllib3.disable_warnings()
ql_auth_path = '/ql/config/auth.json'
try:
head = int(os.environ["ec_head_cks"])
print("已配置保留前{}位ck顺序做车头".format(head))
except:
head = 6
print("#默认只保留前6位ck做车头,有需求")
print("#请在配置文件中配置\nexport ec_head_cks=\"具体几个\" \n#更改车头数量\n")
print("===================================")
print("已设置保留前{}位顺序不改变\n".format(head))
def __get_token() -> str or None:
with open(ql_auth_path, 'r', encoding='utf-8') as f:
j_data = json.load(f)
return j_data.get('token')
def __get__headers() -> dict:
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': 'Bearer ' + __get_token()
}
return headers
# 查询环境变量
def get_envs(name: str = None) -> list:
params = {
't': int(time.time() * 1000)
}
if name is not None:
params['searchValue'] = name
res = requests.get(ql_url + '/api/envs', headers=__get__headers(), params=params)
j_data = res.json()
if j_data['code'] == 200:
return j_data['data']
return []
def gettimestamp():
return str(int(time.time() * 1000))
def gettoken(self):
self.headers.update({"Authorization": "Bearer " + __get_token()})
def login(self):
gettoken(self)
def getallenv(self, baseurl, typ):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
r = self.get(url)
item = json.loads(r.text)["data"]
return item
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, value, qlid, remarks):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
data = {
"name": "JD_COOKIE",
"value": value,
"_id": qlid,
"remarks": remarks
}
r = self.put(url, data=json.dumps(data))
if json.loads(r.text)["code"] == 200:
return True
else:
data = {
"name": "JD_COOKIE",
"value": value,
"id": qlid,
"remarks": remarks
}
r = self.put(url, data=json.dumps(data))
if json.loads(r.text)["code"] == 200:
return True
return False
def insert(self, baseurl, typ, name, value, remarks):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
data = []
if remarks == None:
data_json = {
"value": value,
"name": name
}
else:
data_json = {
"value": value,
"name": name,
'remarks': remarks
}
data.append(data_json)
r = self.post(url, json.dumps(data))
if json.loads(r.text)["code"] == 200:
return r.json()['data']
else:
return False
def delete(self, baseurl, typ, value):
url = baseurl + typ + "/envs?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
data = value
r = self.delete(url, data=json.dumps(data))
if json.loads(r.text)["code"] == 200:
return True
else:
return False
def disable(self, baseurl, typ, ids):
url = baseurl + typ + "/envs/disable?t=%s" % gettimestamp()
self.headers.update({"Content-Type": "application/json;charset=UTF-8", 'Connection': 'close'})
r = self.put(url, data=json.dumps(ids))
if json.loads(r.text)["code"] == 200:
return True
else:
return False
if __name__ == '__main__':
s = requests.session()
login(s)
try:
ql_url = 'http://localhost:5700/'
cookies = getitem(s, ql_url, "JD_COOKIE", "api")
except:
ql_url = 'http://localhost:5600/'
cookies = getitem(s, ql_url, "JD_COOKIE", "api")
# # 备份ck
# if os.path.exists("./allck.txt") == False:
# temp = []
# for i in cookies:
# temp.append(i["value"] + "\n")
# with open("./allck.txt", "w") as ffp:
# ffp.writelines(temp)
# print("初次运行,自动备份ck到allck.txt文件里,有需要恢复初始状态自取\n")
# else:
# print("脚本管理根目录下allck.txt文件已存在,有需要恢复初始状态自取\n")
# pass
# 备份
tt = "trigger_cookies.json"
allenv = getallenv(s, ql_url, "api")
try:
os.environ['ec_backup_ck']
ec_backup_ck = os.environ['ec_backup_ck']
except:
ec_backup_ck = 'true'
if ec_backup_ck == 'true' and os.path.exists('./' + tt) != True:
with open(tt, "w", encoding="utf-8") as fp:
json.dump(allenv, fp)
print("已备份原有环境变量至{},有需要还原请使用二叉树还原环境变量脚本还原".format(tt))
print("如果不需要备份,请设置export ec_backup_ck=\"false\"")
else:
print("上次备份文件{}还存在或已配置无需备份,本次运行不进行备份".format(tt))
####################################################################################
try:
ql_url = 'http://localhost:5700/'
cookies = getitem(s, ql_url, "JD_COOKIE", "api")
except:
ql_url = 'http://localhost:5600/'
cookies = getitem(s, ql_url, "JD_COOKIE", "api")
allenv = getallenv(s, ql_url, "api")
jdcount = 0
result_list = []
he_list = []
head_list = []
random_list = []
disable_list = []
for i in allenv:
if "JD_COOKIE" == i["name"]:
jdcount += 1
else:
he_list.append(i)
continue
if jdcount <= head:
head_list.append(i)
continue
if i["status"] != 0:
disable_list.append(i)
continue
head_len = len(head_list) + len(he_list)
# 随机化启用的cookie顺序,保留指定位置不变
res = random.sample(range(head, len(cookies)), len(cookies) - head)
print("固定的ck顺序")
print("pin\t\t\t\t上次\t 本次")
for i in range(0, head):
c = re.findall(r"pt_pin=(.*?);", cookies[i]["value"])[0][-16:].ljust(16)
print("{}\t\t{}\t\t{}".format(c, i + 1, i + 1))
print("本次随机ck的顺序")
print("pin\t\t\t\t上次\t 本次")
temp = []
ct = 1
for i in res:
random_list.append(cookies[i - 1])
c = re.findall(r"pt_pin=(.*?);", cookies[i - 1]["value"])[0][-16:].ljust(16)
print("{}\t\t{}\t\t{}".format(c, i, ct + head))
ct += 1
result_list.extend(head_list)
# 与后置黑号脚本匹配
tp1 = []
tp2 = []
try:
ec_blackkey = os.environ["ec_blackkey"]
for i in random_list:
try:
if i['remarks'] == ec_blackkey:
tp2.append(i)
else:
tp1.append(i)
except:
tp1.append(i)
random_list = tp1 + tp2
except:
pass
tp1 = []
tp2 = []
try:
for i in random_list:
try:
if i['remarks'] == '黑号':
tp2.append(i)
else:
tp1.append(i)
except:
tp1.append(i)
random_list = tp1 + tp2
except:
pass
# 去重
tp1 = []
value_tp = []
for i in random_list:
if i['value'] not in value_tp:
tp1.append(i)
value_tp.append(i['value'])
random_list = tp1
he_count = len(he_list)
result_list.extend(random_list)
result_list.extend(disable_list)
# 去重
tp1 = []
value_tp = []
for i in result_list:
if i['value'] not in value_tp:
tp1.append(i)
value_tp.append(i['value'])
result_list = tp1
he_list.extend(result_list)
result_list = he_list
for i in allenv:
try:
c = delete(s, ql_url, "api", [i['_id']])
except:
c = delete(s, ql_url, "api", [i['id']])
time.sleep(2)
print(len(result_list))
for i in result_list:
try:
i['remarks']
c = insert(s, ql_url, "api", i['name'], i['value'], i['remarks'])
if i["status"] != 0:
try:
disable(s, ql_url, 'api', [c[0]['_id']])
except:
disable(s, ql_url, 'api', [c[0]['id']])
except:
c = insert(s, ql_url, "api", i['name'], i['value'], None)
if i["status"] != 0:
try:
disable(s, ql_url, 'api', [c[0]['_id']])
except:
disable(s, ql_url, 'api', [c[0]['id']])
print("已前置非ck变量共{}个,车头ck共{}个,随机顺序ck共{}个,后置禁用ck共{}个".format(he_count, len(head_list), len(random_list),
len(disable_list)))
print("============================================")
print("脚本执行完毕")