forked from Aabyss-Team/ARL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celerytask.py
202 lines (161 loc) · 6.68 KB
/
celerytask.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
import signal
import time
from bson import ObjectId
from app.config import Config
from celery import Celery, platforms, current_task
from app import utils
from app import tasks as wrap_tasks
from app.modules import CeleryAction, TaskSyncStatus, TaskStatus, CeleryRoutingKey
logger = utils.get_logger()
celery = Celery('task', broker=Config.CELERY_BROKER_URL)
celery.conf.update(
task_acks_late=False,
worker_prefetch_multiplier=1,
broker_transport_options={"max_retries": 3, "interval_start": 0, "interval_step": 0.2, "interval_max": 0.5},
)
platforms.C_FORCE_ROOT = True
@celery.task(queue=CeleryRoutingKey.ASSET_TASK)
def arl_task(options):
# 这里不检验 celery_action, 调用的时候区分
run_task(options)
def sigterm_handler(signum, frame):
if not current_task:
return
celery_id = current_task.request.id
routing_key = current_task.request.delivery_info['routing_key']
logger.info(f"Caught signal {signum}, celery_id:{celery_id} terminating {routing_key}...")
logger.info(f"{current_task.request}")
try:
query = {'celery_id': celery_id}
update_data = {"$set": {"status": TaskStatus.STOP, "end_time": utils.curr_date()}}
if routing_key == CeleryRoutingKey.ASSET_TASK:
utils.conn_db('task').update_one(query, update_data)
elif routing_key == CeleryRoutingKey.GITHUB_TASK:
utils.conn_db('github_task').update_one(query, update_data)
except Exception as e:
logger.error(f"update celery_id:{celery_id} status error: {e}")
utils.exit_gracefully(signum, frame)
def run_task(options):
signal.signal(signal.SIGTERM, sigterm_handler)
action = options.get("celery_action")
data = options.get("data")
action_map = {
CeleryAction.DOMAIN_TASK_SYNC_TASK: domain_task_sync,
CeleryAction.DOMAIN_EXEC_TASK: domain_exec,
CeleryAction.IP_EXEC_TASK: ip_exec,
CeleryAction.DOMAIN_TASK: domain_task,
CeleryAction.IP_TASK: ip_task,
CeleryAction.RUN_RISK_CRUISING: run_risk_cruising_task,
CeleryAction.FOFA_TASK: fofa_task,
CeleryAction.GITHUB_TASK_TASK: github_task_task,
CeleryAction.GITHUB_TASK_MONITOR: github_task_monitor,
CeleryAction.ASSET_SITE_UPDATE: asset_site_update,
CeleryAction.ADD_ASSET_SITE_TASK: asset_site_add_task,
CeleryAction.ASSET_WIH_UPDATE: asset_wih_update_task,
}
start_time = time.time()
# 这里监控任务 task_id 和 target 是空的
logger.info("run_task action:{} time: {}".format(action, start_time))
logger.info("name:{}, target:{}, task_id:{}".format(
data.get("name"), data.get("target"), data.get("task_id")))
try:
fun = action_map.get(action)
if fun:
fun(data)
else:
logger.warning("not found {} action".format(action))
except Exception as e:
logger.exception(e)
elapsed = time.time() - start_time
logger.info("end {} elapsed: {}".format(action, elapsed))
@celery.task(queue=CeleryRoutingKey.GITHUB_TASK)
def arl_github(options):
# 这里不检验 celery_action, 调用的时候区分
run_task(options)
def domain_exec(options):
"""域名监测任务"""
scope_id = options.get("scope_id")
domain = options.get("domain")
job_id = options.get("job_id")
monitor_options = options.get("monitor_options")
name = options.get("name")
wrap_tasks.domain_executors(base_domain=domain, job_id=job_id,
scope_id=scope_id, options=monitor_options, name=name)
def domain_task_sync(options):
"""域名同步任务"""
from app.services.syncAsset import sync_asset
scope_id = options.get("scope_id")
task_id = options.get("task_id")
query = {"_id": ObjectId(task_id)}
try:
update = {"$set": {"sync_status": TaskSyncStatus.RUNNING}}
utils.conn_db('task').update_one(query, update)
sync_asset(task_id, scope_id, update_flag=False)
update = {"$set": {"sync_status": TaskSyncStatus.DEFAULT}}
utils.conn_db('task').update_one(query, update)
except Exception as e:
update = {"$set": {"sync_status": TaskSyncStatus.ERROR}}
utils.conn_db('task').update_one(query, update)
logger.exception(e)
def domain_task(options):
"""常规域名任务"""
target = options["target"]
task_options = options["options"]
task_id = options["task_id"]
item = utils.conn_db('task').find_one({"_id": ObjectId(task_id)})
if not item:
logger.info("domain_task not found {} {}".format(target, item))
return
wrap_tasks.domain_task(target, task_id, task_options)
def ip_task(options):
"""常规IP任务"""
target = options["target"]
task_options = options["options"]
task_id = options["task_id"]
wrap_tasks.ip_task(target, task_id, task_options)
def run_risk_cruising_task(options):
task_id = options["task_id"]
wrap_tasks.run_risk_cruising_task(task_id)
def fofa_task(options):
task_id = options["task_id"]
task_options = options["options"]
target = " ".join(options["fofa_ip"])
wrap_tasks.ip_task(target, task_id, task_options)
def ip_exec(options):
"""
IP 监测任务
"""
scope_id = options.get("scope_id")
target = options.get("domain")
job_id = options.get("job_id")
monitor_options = options.get("monitor_options")
name = options.get("name")
wrap_tasks.ip_executor(target=target, scope_id=scope_id,
task_name=name, job_id=job_id,
options=monitor_options)
def github_task_task(options):
task_id = options["task_id"]
keyword = options["keyword"]
wrap_tasks.github_task_task(task_id=task_id, keyword=keyword)
def github_task_monitor(options):
task_id = options["task_id"]
keyword = options["keyword"]
scheduler_id = options["github_scheduler_id"]
wrap_tasks.github_task_monitor(task_id=task_id, keyword=keyword, scheduler_id=scheduler_id)
def asset_site_update(options):
task_id = options["task_id"]
task_options = options["options"]
scope_id = task_options["scope_id"]
scheduler_id = task_options["scheduler_id"]
wrap_tasks.asset_site_update_task(task_id=task_id,
scope_id=scope_id, scheduler_id=scheduler_id)
def asset_wih_update_task(options):
task_id = options["task_id"]
task_options = options["options"]
scope_id = task_options["scope_id"]
scheduler_id = task_options["scheduler_id"]
wrap_tasks.asset_wih_update_task(task_id=task_id,
scope_id=scope_id, scheduler_id=scheduler_id)
def asset_site_add_task(options):
task_id = options["task_id"]
wrap_tasks.run_add_asset_site_task(task_id)