xtn-tools-pro 1.0.0.5.7__py3-none-any.whl → 1.0.0.5.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -410,7 +410,7 @@ class GoFun:
410
410
  :return:
411
411
  """
412
412
  try:
413
- task_item = self.download_queue.get(block=block, timeout=timeout)
413
+ task_item = download_queue.get(block=block, timeout=timeout)
414
414
  return task_item
415
415
  except Exception as e:
416
416
  self.logger.critical(f"get_download_task 获取下载任务 报错 {e}")
@@ -424,7 +424,7 @@ class GoFun:
424
424
  :return:
425
425
  """
426
426
  try:
427
- task_item = self.upload_queue.put(task_item)
427
+ task_item = upload_queue.put(task_item)
428
428
  return task_item
429
429
  except Exception as e:
430
430
  self.logger.critical(f"update_upload_task 更新任务 报错 {e}")
@@ -0,0 +1,387 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: utf-8 -*-
3
+
4
+ # 说明:
5
+ #
6
+ # History:
7
+ # Date Author Version Modification
8
+ # --------------------------------------------------------------------------------------------------
9
+ # 2025/1/18 xiatn V00.01.000 新建
10
+ # --------------------------------------------------------------------------------------------------
11
+ import time
12
+ import random
13
+ import inspect
14
+ import requests
15
+ import threading
16
+ import multiprocessing
17
+ import concurrent.futures
18
+ from multiprocessing import Process
19
+
20
+
21
+ class GoFunTaskV2:
22
+ def __init__(self, ini_dict, logger, go_task_function):
23
+ self.logger = logger
24
+
25
+ # 读取配置信息
26
+ host = ini_dict.get('host', '') # 域名
27
+ port = ini_dict.get('port', 0) # 端口
28
+ task = ini_dict.get('task', '') # 任务
29
+ auto = ini_dict.get('auto', '') # token
30
+ processes_num = ini_dict.get('processes_num', 0) # 进程数
31
+ thread_num = ini_dict.get('thread_num', 0) # 线程数
32
+ restart_time = ini_dict.get('restart_time', 0) # 间隔x秒强制重启
33
+ restart_time = 30 * 60 if restart_time <= 0 else restart_time # 间隔x秒强制重启时间不传默认60分钟
34
+ update_proxies_time = ini_dict.get('update_proxies_time', 0) # 间隔x秒更新代理
35
+ upload_task_tine = ini_dict.get('upload_task_tine', 0) # 回写间隔
36
+
37
+ # 拼接地址
38
+ if port:
39
+ task_host = f"http://{host}:{port}"
40
+ else:
41
+ task_host = f"http://{host}"
42
+ download_url = task_host + "/filter_server/phone/get"
43
+ upload_url = task_host + "/filter_server/phone/update"
44
+ update_proxy_url = task_host + f"/filter_server/proxy/random/get?taskType={task}&limit=1"
45
+
46
+ # 获取网络ip
47
+ external_ip = self.__get_external_ip()
48
+
49
+ # 全部配置信息
50
+ self.__ini_info = {
51
+ "host": host,
52
+ "port": int(port),
53
+ "task": task,
54
+ "auto": auto,
55
+ "processes_num": int(processes_num),
56
+ "thread_num": int(thread_num),
57
+ "restart_time": int(restart_time),
58
+ "update_proxies_time": int(update_proxies_time),
59
+ "upload_task_tine": int(upload_task_tine),
60
+ "download_url": download_url, # 获取任务地址
61
+ "upload_url": upload_url, # 回写任务地址
62
+ "update_proxy_url": update_proxy_url, # 更新代理地址
63
+ "external_ip": external_ip,
64
+ }
65
+
66
+ logger.debug(
67
+ f"\n无敌框架来咯~~~当前设置配置如下:"
68
+ f"\n\t功能函数重启间隔:{restart_time};进程数:{processes_num};线程数:{thread_num}"
69
+ f"\n\t代理更新间隔:{update_proxies_time};回写间隔{upload_task_tine};\n"
70
+ )
71
+
72
+ # 主子进程
73
+ task_create_process_cnt = 1
74
+ while True:
75
+ try:
76
+ logger.warning(f"主子进程 第{task_create_process_cnt}次打开进程;")
77
+ task_create_process = multiprocessing.Process(target=self._create_process,
78
+ name="task_create_process",
79
+ args=(self.__ini_info, logger, go_task_function)
80
+ )
81
+ task_create_process.start()
82
+ time.sleep(restart_time)
83
+ task_create_process.terminate() # 关闭所有进程
84
+ task_create_process.join()
85
+ task_create_process.kill()
86
+ logger.warning(f"主子进程 第{task_create_process_cnt}次关闭进程;")
87
+ except Exception as e:
88
+ logger.critical(f"主子进程 异常 - {e};")
89
+
90
+ task_create_process_cnt += 1
91
+
92
+ def __get_external_ip(self):
93
+ """
94
+ 获取当前网络ip
95
+ :return:
96
+ """
97
+ while True:
98
+ try:
99
+ rp = requests.get('https://httpbin.org/ip')
100
+ rp_json = rp.json()
101
+ self.logger.warning(f"当前网络ip --> {rp_json}")
102
+ return rp_json['origin']
103
+ except Exception as e:
104
+ self.logger.critical(f"获取当前网络ip{e}")
105
+
106
+ def _create_process(self, ini_info, logger, go_task_function):
107
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
108
+ caller_name = caller.function # 获取调用者的函数名
109
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
110
+ if caller_name != "run" or caller_class is None:
111
+ raise Exception("错误调用")
112
+
113
+ # 共享任务队列
114
+ manager = multiprocessing.Manager()
115
+ download_queue = multiprocessing.Queue()
116
+ upload_queue = multiprocessing.Queue()
117
+ proxies_dict = manager.dict()
118
+
119
+ # 下载回写代理
120
+ download_and_upload_task_process = multiprocessing.Process(target=self._download_and_upload_task,
121
+ name="_download_and_upload_task",
122
+ args=(ini_info, logger, download_queue,
123
+ upload_queue, proxies_dict)
124
+ )
125
+ download_and_upload_task_process.start()
126
+
127
+ # gofun
128
+ go_task_fun_task_process = multiprocessing.Process(target=self._go_task_fun_task,
129
+ name="_go_task_fun_task",
130
+ args=(ini_info, logger, download_queue,
131
+ upload_queue, proxies_dict, go_task_function)
132
+ )
133
+ go_task_fun_task_process.start()
134
+
135
+ while True:
136
+ logger.info("进程正常...")
137
+ time.sleep(10)
138
+
139
+ def _download_and_upload_task(self, ini_info, logger, download_queue, upload_queue, proxies_dict):
140
+ """
141
+ 使用3个线程 打开 获取任务、回写任务、代理维护
142
+ :param ini_info:
143
+ :param logger:
144
+ :param download_queue:
145
+ :param upload_queue:
146
+ :param proxies_dict:
147
+ :return:
148
+ """
149
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
150
+ caller_name = caller.function # 获取调用者的函数名
151
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
152
+ if caller_name != "run" or caller_class is None:
153
+ raise Exception("错误调用")
154
+
155
+ # 获取任务
156
+ thread_download_task = threading.Thread(target=self.__download_task,
157
+ args=(download_queue, ini_info, logger))
158
+ thread_download_task.start()
159
+
160
+ # 回写任务
161
+ thread_upload_task = threading.Thread(target=self.__upload_task,
162
+ args=(upload_queue, ini_info, logger))
163
+ thread_upload_task.start()
164
+
165
+ # 维护代理
166
+ thread_update_proxy = threading.Thread(target=self.__update_proxy,
167
+ args=(proxies_dict, ini_info, logger))
168
+ thread_update_proxy.start()
169
+
170
+ def __download_task(self, download_queue, ini_info, logger):
171
+ """
172
+ 获取任务
173
+ :param queue:
174
+ :return:
175
+ """
176
+ download_url = ini_info["download_url"]
177
+ auto = ini_info["auto"]
178
+ task = ini_info["task"]
179
+ headers = {"Authorization": auto}
180
+ params = {"taskType": task}
181
+ while True:
182
+ try:
183
+ qsize = download_queue.qsize()
184
+ logger.info(f"当前队列剩余任务数:{qsize}")
185
+ if qsize >= 10:
186
+ time.sleep(2)
187
+ continue
188
+ resp = requests.get(download_url, headers=headers, params=params, timeout=5)
189
+ json_data = resp.json()
190
+ result_list = json_data.get("result", [])
191
+
192
+ if len(result_list) <= 0:
193
+ # 判断任务响应是否为空
194
+ time.sleep(2)
195
+ continue
196
+ for task_item in result_list:
197
+ phone_item = task_item["phone"]
198
+ if not phone_item.isdigit(): # 判断是否全是整数(不包括小数点或负号)
199
+ continue
200
+ download_queue.put(task_item)
201
+ logger.warning(f"成功获取任务个数:{len(result_list)}")
202
+ except Exception as e:
203
+ logger.critical(f"获取任务请求异常:{e}")
204
+ time.sleep(2)
205
+
206
+ def __upload_task(self, upload_queue, ini_info, logger):
207
+ """
208
+ 回写任务
209
+ :return:
210
+ """
211
+ upload_url = ini_info["upload_url"]
212
+ external_ip = ini_info["external_ip"]
213
+ auto = ini_info["auto"]
214
+ task = ini_info["task"]
215
+ upload_task_tine = ini_info["upload_task_tine"]
216
+ headers = {"Authorization": auto}
217
+ params = {"taskType": task}
218
+ while True:
219
+ # 判断队列是否有值
220
+ empty = upload_queue.empty()
221
+ if empty:
222
+ time.sleep(2)
223
+ continue
224
+
225
+ # 循环全部获取队列的任务
226
+ result_list = []
227
+ try:
228
+ while True:
229
+ task_item = upload_queue.get_nowait()
230
+ taskNo = task_item["taskNo"]
231
+ phone = task_item["phone"]
232
+ isRegistered = task_item["isRegistered"]
233
+ country_region = task_item["country_region"]
234
+ full_phone = f"{country_region}{phone}"
235
+ task_item = {
236
+ 'taskNo': taskNo,
237
+ 'phone': full_phone,
238
+ 'isRegistered': isRegistered
239
+ }
240
+ result_list.append(task_item)
241
+ except Exception as e:
242
+ logger.critical(f"循环全部获取队列的任务{e}")
243
+
244
+ # 回写任务
245
+ data = {"result": result_list, "remoteAddr": external_ip}
246
+ while True:
247
+ try:
248
+ resp = requests.post(upload_url, json=data, headers=headers, params=params, timeout=5)
249
+ json_data = resp.json()
250
+ # logger.warning(f"成功回写任务个数:{len(result_list)},{json_data},{data}")
251
+ logger.warning(f"成功回写任务个数:{len(result_list)},{json_data}")
252
+ break
253
+ except Exception as e:
254
+ logger.critical(f"回写异常,{len(result_list)},{e}")
255
+
256
+ if not upload_task_tine:
257
+ # 一直执行 不退出
258
+ continue
259
+ time.sleep(upload_task_tine)
260
+
261
+ def __update_proxy(self, proxies_dict, ini_info, logger):
262
+ """
263
+ 更新代理
264
+ :return:
265
+ """
266
+ update_proxy_url = ini_info["update_proxy_url"]
267
+ auto = ini_info["auto"]
268
+ update_proxies_time = ini_info["update_proxies_time"]
269
+ headers = {"Authorization": auto}
270
+
271
+ while True:
272
+ try:
273
+ if not proxies_dict.get("status"):
274
+ resp = requests.get(update_proxy_url, headers=headers, timeout=5)
275
+ json_data = resp.json()
276
+ status_code = resp.status_code
277
+ result_list = json_data.get("result", [])
278
+ if not result_list or status_code != 200:
279
+ logger.critical(f"获取代理响应异常:{status_code} {len(result_list)} {json_data}")
280
+ time.sleep(2)
281
+
282
+ proxies_dict['http'] = 'http://' + random.choice(result_list)
283
+ proxies_dict['https'] = 'http://' + random.choice(result_list)
284
+ proxies_dict['status'] = True
285
+ logger.warning(f"成功获取代理:{proxies_dict}")
286
+
287
+ if not update_proxies_time:
288
+ # 一直执行 不退出
289
+ continue
290
+
291
+ time.sleep(update_proxies_time)
292
+ proxies_dict['status'] = False
293
+ except Exception as e:
294
+ logger.critical(f"获取代理请求异常:{e}")
295
+ time.sleep(2)
296
+
297
+ def _go_task_fun_task(self, ini_info, logger, download_queue, upload_queue, proxies_dict, go_task_function):
298
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
299
+ caller_name = caller.function # 获取调用者的函数名
300
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
301
+ if caller_name != "run" or caller_class is None:
302
+ raise Exception("错误调用")
303
+
304
+ processes_num = ini_info["processes_num"]
305
+ thread_num = ini_info["thread_num"]
306
+ restart_time = ini_info["restart_time"]
307
+
308
+ processes_num = 1 if processes_num <= 0 else processes_num
309
+ thread_num = 1 if thread_num <= 0 else thread_num
310
+
311
+ go_task_fun_cnt = 0
312
+ processes_start_list = []
313
+ while True:
314
+ try:
315
+ if not processes_start_list:
316
+ go_task_fun_cnt += 1
317
+ logger.info(
318
+ f"第{go_task_fun_cnt}次,进程数:{processes_num},线程数:{thread_num},等待{restart_time}秒强制下一次")
319
+ for i in range(processes_num):
320
+ p = Process(target=self._run_with_timeout,
321
+ args=(
322
+ download_queue, upload_queue, proxies_dict, thread_num, logger,
323
+ go_task_function))
324
+ processes_start_list.append(p)
325
+ p.start()
326
+
327
+ if not restart_time:
328
+ # 一直执行 不退出
329
+ continue
330
+
331
+ time.sleep(restart_time)
332
+ # 关闭所有进程
333
+ for p in processes_start_list:
334
+ p.terminate()
335
+ p.join() # 等待进程确实结束
336
+ processes_start_list = []
337
+
338
+ except Exception as e:
339
+ logger.critical(f"_go_task_fun_task-{e}")
340
+
341
+ def _run_with_timeout(self, download_queue, upload_queue, proxies_dict, thread_num, logger, go_task_function):
342
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
343
+ caller_name = caller.function # 获取调用者的函数名
344
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
345
+ if caller_name != "run" or caller_class is None:
346
+ raise Exception("错误调用")
347
+
348
+ with concurrent.futures.ThreadPoolExecutor(max_workers=thread_num) as executor:
349
+ # 提交10个函数到线程池中执行
350
+ futures = [executor.submit(go_task_function, self, download_queue, upload_queue, proxies_dict, logger)
351
+ for _ in range(thread_num)]
352
+
353
+ # 等待所有线程完成
354
+ for future in concurrent.futures.as_completed(futures):
355
+ future.result()
356
+
357
+ def get_download_task(self, download_queue, block, timeout):
358
+ """
359
+ 获取下载任务
360
+ :param download_queue:下载队列
361
+ :param block: 是否阻塞等待 True阻塞/False不阻塞
362
+ :param timeout:
363
+ :return:
364
+ """
365
+ try:
366
+ task_item = download_queue.get(block=block, timeout=timeout)
367
+ return task_item
368
+ except Exception as e:
369
+ self.logger.critical(f"get_download_task 获取下载任务 {download_queue, block, timeout} 报错 {e}")
370
+ return False
371
+
372
+ def update_upload_task(self, upload_queue, task_item):
373
+ """
374
+ 更新任务
375
+ :param upload_queue:
376
+ :param task_item:
377
+ :return:
378
+ """
379
+ try:
380
+ task_item = upload_queue.put(task_item)
381
+ return task_item
382
+ except Exception as e:
383
+ self.logger.critical(f"update_upload_task 更新任务 {upload_queue, task_item} 报错 {e}")
384
+ return False
385
+
386
+ def reboot_process(self):
387
+ pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.0.5.7
3
+ Version: 1.0.0.5.9
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -10,7 +10,8 @@ xtn_tools_pro/proxy/XiaoXiangProxy.py,sha256=45SsMZDc-3Ta4THX38vScjaeRBJSp420AJw
10
10
  xtn_tools_pro/proxy/__init__.py,sha256=WRwh6s2lruMu5buh0ejo9EK54kWT_VQhCsFGNFAmcyo,418
11
11
  xtn_tools_pro/proxy/proxy.py,sha256=No6E1pFY5yx2F4976pXPrLtq-QEVp79KupzcufjSN58,8703
12
12
  xtn_tools_pro/task_pro/__init__.py,sha256=nK3U47hWwE1H875ieEToH9r-jzXHS-PXk8cDstOvRE8,418
13
- xtn_tools_pro/task_pro/go_fun.py,sha256=RJJ80h2pHK4l_RqUrh653-rXu7RYqYBzM36Gdoh_txI,19154
13
+ xtn_tools_pro/task_pro/go_fun.py,sha256=hWEt2uJ9FCvJH7PhVZttS-11A7J6zbRKwX7c5YLYQag,19144
14
+ xtn_tools_pro/task_pro/go_fun_v2.py,sha256=yvT5FzgAOcog6xj6AQUR_OIM5d2Adgda13XADvVL4mI,16807
14
15
  xtn_tools_pro/utils/__init__.py,sha256=I1_n_NP23F2lBqlF4EOlnOdLYxM8M4pbn63UhJN1hRE,418
15
16
  xtn_tools_pro/utils/crypto.py,sha256=RZ5AET4udlraACWMeNF-17JiZ2R6Ahb47_j4tjkV7LE,3190
16
17
  xtn_tools_pro/utils/file_utils.py,sha256=VfdIxog4s1UW5NpKkCvQsUs9qHjLoNCnstZbnftkT4w,2046
@@ -19,8 +20,8 @@ xtn_tools_pro/utils/log.py,sha256=m0WtTWkkwtrki1ftP8vCDR8bMfK2gcfUGx5J2x2IlLQ,10
19
20
  xtn_tools_pro/utils/retry.py,sha256=0wjHsR5DBBKpv4naMfxiky8kprrZes4WURIfFQ4H708,1657
20
21
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
21
22
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
22
- xtn_tools_pro-1.0.0.5.7.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- xtn_tools_pro-1.0.0.5.7.dist-info/METADATA,sha256=RWqsoDfeJhEPORf32bJ3w9qwXz5UWRrQrT6q-MfS3jQ,455
24
- xtn_tools_pro-1.0.0.5.7.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
- xtn_tools_pro-1.0.0.5.7.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
- xtn_tools_pro-1.0.0.5.7.dist-info/RECORD,,
23
+ xtn_tools_pro-1.0.0.5.9.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
+ xtn_tools_pro-1.0.0.5.9.dist-info/METADATA,sha256=W6KJIF8sdRC12Hu1NezCxL3krDaWf6dWvbcY6gGR1nA,455
25
+ xtn_tools_pro-1.0.0.5.9.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
26
+ xtn_tools_pro-1.0.0.5.9.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
27
+ xtn_tools_pro-1.0.0.5.9.dist-info/RECORD,,