-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStunServices.py
112 lines (101 loc) · 3.93 KB
/
StunServices.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
import json
import os
import sys
import threading
import time
import traceback
from subModules.LogRecorders import Log, LL as L
from subModules.AllForwarder import PortForwards
class StunServices(threading.Thread):
def __init__(self, set_time=600):
super().__init__()
# 创建数据 ===================
self.map_list = {} # 映射列表
self.put_time = time.time()
self.set_time = set_time
self.api_logs = Log(
"StunConnects",
"StunConnects",
"StunConnects"
).log
# 启动线程 ===================
self.start()
# 启动线程 =======================
def run(self):
self.open()
# 服务内容 =========================================================
def open(self):
self.load()
# for task_name in self.map_list:
# try:
# self.map_list[task_name].join()
# except (RuntimeError, Exception):
# continue
# 结束服务 =========================================================
def stop(self):
for task_name in self.map_list:
self.map_list[task_name].kill()
# 载入数据 =========================================================
def load(self):
conf_path = "StunConnects.json"
if not os.path.exists(conf_path):
return False
with open(conf_path, "r", encoding="utf-8") as conf_file:
conf_data = json.loads(conf_file.read())
if "update_time" in conf_data:
self.set_time = conf_data["update_time"]
if "tasker_list" in conf_data:
for task_now in conf_data["tasker_list"]:
# print(task_now)
self.deal(task_now)
# 处理变更 =========================================================
def deal(self, task_dict):
url_text = task_dict['url_text']
# 新增任务 ==============================
if url_text not in self.map_list:
# 任务启用 ==========================
if task_dict['map_flag']:
self.api_logs("新增任务: " + url_text)
self.task(task_dict)
return True
else:
old_task = self.map_list[url_text]
# 停止并删除当前
if not task_dict['map_flag']:
self.api_logs("停止任务: " + url_text)
old_task.kill()
self.map_list.pop(url_text)
return False
elif int(old_task.local_port) != int(task_dict['map_port']) \
or old_task.proxy_urls != url_text \
or old_task.time != self.set_time \
or old_task.proxy_type != task_dict['map_type']:
# print(int(old_task.local_port) != int(task_dict['map_port']))
# print(old_task.proxy_urls != url_text)
# print(old_task.time != self.set_time, old_task.time, self.set_time)
# print(old_task.proxy_type != task_dict['map_type'])
self.api_logs("任务变更: " + url_text)
# traceback.print_stack(sys._getframe())
old_task.kill()
self.task(task_dict)
return True
else:
self.api_logs("暂无变更: " + url_text)
return True
# 新增任务 =========================================================
def task(self, task_dict):
url_text = task_dict['url_text']
if not task_dict['map_flag']:
return False
map_task = PortForwards(
task_dict['map_port'],
"0.0.0.0",
proxy_type=task_dict['map_type'],
proxy_urls=url_text,
server_tip="StunConnects",
in_dog_var = self.set_time
)
self.map_list[url_text] = map_task
map_task.start()
if __name__ == '__main__':
stu = StunServices()