forked from PDXostc/rvi_dynamic_agents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagenthandler.py
460 lines (385 loc) · 16.7 KB
/
agenthandler.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import can
import time
import datetime
import psutil
import subprocess
import json
import threading
import websocket
import sys
import os
import base64
import agenthandler_config as settings
try:
import thread
#TODO use Threading instead of _thread in python3
except ImportError:
import _thread as thread
#DEBUG MESSAGING PRINTOUTS
DEBUG = settings.DEBUG_TOGGLE
#Agent Handler Globals
#agent_pool = Agents that are currently registered and that we are keeping track of
#agent_map = map of our agent_pool that will be stored in a memory to keep running even if case of power cycles
#running_agents = dictionary of running processes which can be terminated if needed
#expire_monitor_threads = threads that are started to watch for an agent expiration
running_agents = {}
expire_monitors = {}
agent_map = []
agent_pool = []
expire_monitor_threads = {}
#RVI Params
#The services that we must register in order for the agenthandler to receive an agent+run and a service to terminate
#The agent_report_service is the agent_report service which agents can invoke to send data to
services_to_register = []
services_to_register.append(settings.NEW_AGENT_SERVICE)
services_to_register.append(settings.TERMINATE_AGENT_SERVICE)
agent_report_service = settings.RVI_AGENT_REPORT_SERVICE
#Get the RVI websocket server location to connect to
host=settings.RVI_WS_HOST
#Global lock variable for threads to grab when they are performing an action which should not be interrupted
lock = threading.Lock()
#The message should be a pythonic dictionary containing key value pairs of whatever you wish to send
def report(message):
lock.acquire()
message_dict = {}
message_dict['payload'] = message
message_dict['timestamp'] = str(time.time())
message_dict['agent_id'] = sys.argv[0][7:len(sys.argv[0])-3]
payload = {'jsonrpc':"2.0", 'id':str(time.time()), 'method':"message"}
payload['params'] = {'service_name':agent_report_service,
'timeout':(int(time.time())+60), 'parameters':message_dict}
try:
ws1 = websocket.create_connection(host)
if DEBUG:
print(payload)
ws1.send(json.dumps(payload))
# ws.close()
except:
if DEBUG:
print('Could not send agent_report')
lock.release()
#terminate_agent accepts an agent_id which is a string which represents the agent_name in the global agent_pool
def terminate_agent(agent_id):
lock.acquire()
launch_command = None
expiration_date = None
pwd = os.getcwd()
save_path = pwd + settings.AGENT_SAVE_DIRECTORY
#Grab the agent's corresponding launch_command and expiration_date
for agent in agent_pool:
if agent['agent_name'] == agent_id:
launch_command = agent['launch']
expiration_date = agent['expires']
break
else:
pass
#load the path the agent's code exists on
try:
tempdeletepath = os.path.join(save_path, launch_command.split()[1])
except:
if DEBUG:
print("Could not get tempdeletepath")
#Terminate the subprocess that contains the running agent
try:
running_agents[agent_id].terminate()
if DEBUG:
print('---------------Terminating----------------')
print(agent_id + launch_command + str(expiration_date))
print('------------------------------------------')
except:
if DEBUG:
print('No running agent with id:' + agent_id)
#remove the agent from the agent_pool and update the hard_coded file
try:
agent_pool.remove({'agent_name':agent_id, 'launch':launch_command, 'expires':expiration_date})
except:
if DEBUG:
print('Agent does not exist in agent_pool')
print(agent_pool)
try:
agent_map = open('agent_map.txt', 'w+')
json.dump(agent_pool, agent_map)
agent_map.close()
except:
if DEBUG:
print('Could not write current agent pool to memory')
agent_ids = []
for agent in agent_pool:
agent_ids.append(agent['launch'].split()[1])
#Double check that process is actually killed if subprocess.terminate() did not kill it
#Will kill any python things that the agent spawned
to_terminate = [agent_id]
agent_pids = psutil.pids()
for pid in agent_pids:
try:
if any(x in psutil.Process(pid).cmdline()[1] for x in to_terminate):
if DEBUG:
print('----------------Terminated----------------')
print(psutil.Process(pid).cmdline())
print(pid)
print('------------------------------------------')
psutil.Process(pid).terminate()
except:
continue
#Remove the agent's code on our local filesystem
try:
os.remove(tempdeletepath)
except:
if DEBUG:
print('Could not delete file')
lock.release()
#agent_expiration_monitor will take in an agent_id much like the terminate agent function and will create a thread
#that is tracked in expire_mointor_threads dict that will monitor the agent so that if it dies prematurely will try
#to restart that agent or if it expires based on the unix epoch time will call the terminate function.
def agent_expiration_monitor(agent_id):
expiration_date = None
count = 0
expire_monitor_threads[agent_id] = threading.current_thread()
for agent in agent_pool:
if agent['agent_name'] == agent_id:
launch_command = agent['launch']
expiration_date = agent['expires']
break
else:
pass
if expiration_date is not None:
while (expiration_date - time.time()) >= 0:
if running_agents[agent_id].poll() is None:
pass
elif count <= 5:
launch_command = None
for agent in agent_pool:
if agent['agent_name'] == agent_id:
launch_command = agent['launch']
expiration_date = agent['expires']
break
else:
pass
if launch_command == None:
break
if DEBUG:
print('Restarting: ' + agent_id)
split_launch_command = launch_command.split()
split_launch_command[1] = settings.AGENT_SAVE_DIRECTORY[1:]+split_launch_command[1]
running_agents[agent_id] = subprocess.Popen(split_launch_command)
time.sleep(1)
count += 1
else:
break
if DEBUG:
print(agent_id +' expiration_date is set at: ' + str(expiration_date))
print(agent_id +' system time is set at: ' + str(time.time()))
print(expiration_date - time.time())
time.sleep(1)
terminate_agent(agent_id)
else:
if DEBUG:
print('Agent:'+agent_id+' does not exist')
#Registering an agent requires 3 parameters
#agent_id = string unique name of the id to create and save into the global agent_pool
#launch_command = string of how to launch the agent (e.g. python3 myscript.py <variables>)
#expiration_date = time in unix epoch format for when we should terminate the agent.
def register_agent(agent_id, launch_command, expiration_date):
lock.acquire()
if time.time() < expiration_date:
agent_pool.append({'agent_name':agent_id, 'launch':launch_command, 'expires':expiration_date})
agent_map = open('agent_map.txt', 'w+')
json.dump(agent_pool, agent_map)
agent_map.close()
run_agent(agent_id = agent_id)
else:
if DEBUG:
print('Cannot register and run agent since it is already expired')
lock.release()
#Start up the agent and make it known in our running agent_pool global. Takes in the agent_id string
def run_agent(agent_id):
expiration_date = None
launch_command = None
for agent in agent_pool:
if agent['agent_name'] == agent_id:
launch_command = agent['launch']
expiration_date = agent['expires']
break
else:
pass
if expiration_date is not None and launch_command is not None:
if time.time() < expiration_date:
split_launch_command = launch_command.split()
split_launch_command[1] = "agents/"+split_launch_command[1]
running_agents[agent_id] = subprocess.Popen(split_launch_command)
if DEBUG:
print('-----------------Starting-----------------')
print(agent_id + ' with command ' + launch_command)
print('------------------------------------------')
expire_monitors[agent_id] = threading.Thread(target=agent_expiration_monitor, args=(agent_id,))
expire_monitors[agent_id].start()
else:
if DEBUG:
print('Agent has already expired will terminate from system')
terminate_agent(agent_id)
return
else:
if DEBUG:
print('Agent does not exist')
return
#/*TODO*/# Turn this into a python package which we can just instantiate an instance and register and receive messages
####################################################################################################################
#######################################WEBSOCKET SERVER PRELIM TESTING##############################################
####################################################################################################################
def on_message(ws, message):
message_dict = json.loads(message)
# lock = threading.Lock()
if DEBUG:
print(message)
if message_dict['method'] == 'message':
print("###########THIS IS A MESSAGE#############")
for key, value in message_dict.items():
print(key, value)
print("############END OF MESSAGE###############")
##############################################################################################################
##############################################################################################################
########################################Check for the correct parameters######################################
##############################################################################################################
if message_dict['method'] == 'message' and (message_dict['params']['service_name'][1:] ==
settings.NEW_AGENT_SERVICE):
pwd = os.getcwd()
save_path = pwd + settings.AGENT_SAVE_DIRECTORY
try:
params = message_dict['params']['parameters']
if DEBUG:
print(params)
agent_name = params['agent']
launch_cmd = params['launch'] #for now launch_cmd will be "<python/python3/whatever> <AAAA.py>"
expires = float(params['expires'])
tempsavepath = os.path.join(save_path, launch_cmd.split()[1])
#############Save The Agent##############
lock.acquire()
savefile = open(tempsavepath, "w+")
savefile.write(base64.b64decode(params['agent_code'].encode('UTF-8')).decode('UTF-8'))
savefile.close()
lock.release()
if DEBUG:
print('forwarding message payload to agent_register')
print('agent_name: ' + agent_name)
print('launch_cmd: ' + launch_cmd)
print('expires: ' + str(expires))
try:
register_agent(agent_id=agent_name, launch_command=launch_cmd, expiration_date=expires)
except:
if DEBUG:
print('agent_register forwarding failed')
except:
if DEBUG:
print('Incorrect Parameters will not forward to agent_register')
elif message_dict['method'] == 'message' and (message_dict['params']['service_name'][1:] ==
settings.TERMINATE_AGENT_SERVICE):
try:
params = message_dict['params']['parameters']
terminate_target = params['agent']
try:
terminate_agent(agent_id=terminate_target)
except:
if DEBUG:
print('Could not terminate/find corresponding agent_id')
pass
except:
if DEBUG:
print('Incorrect Parameters | No Agent to terminate')
else:
if DEBUG:
print('Not a message/ Not a matching service')
pass
##############################################################################################################
##############################################################################################################
def on_error(ws, error):
if DEBUG:
print(error)
def on_close(ws):
if DEBUG:
print("### closed ###")
def on_open(ws):
def run(*args):
payload = {}
payload['json-rpc'] = "2.0"
payload['id'] = "0"
payload['method'] = "register_service"
for service_name in services_to_register:
payload['params'] = {"service_name":service_name}
ws.send(json.dumps(payload))
opening = threading.Thread(target=run)
opening.start()
####################################################################################################################
####################################################################################################################
####################################################################################################################
#If agenthandler is called to run as the main agenthandler task and not just importing the RVI report messages
if __name__ == "__main__":
#Attempt to load in our previous agent mapping if not create the agent map file which will store our mapping
try:
agent_map = open('agent_map.txt', 'r+')
agent_pool = json.load(agent_map)
agent_map.close()
except:
agent_map = open('agent_map.txt', 'w+')
json.dump(agent_pool, agent_map)
agent_map.close()
#Check to see if any agents are running and terminate so we can remap and keep track of them
if DEBUG:
print(agent_pool)
print(len(agent_pool))
if len(agent_pool) > 0:
agent_ids = []
for agent in agent_pool:
agent_ids.append(agent['launch'].split()[1])
if DEBUG:
print(agent_ids)
agent_pids = psutil.pids()
for pid in agent_pids:
try:
if any(x in psutil.Process(pid).cmdline()[1] for x in agent_ids):
if DEBUG:
print('----------------Terminated----------------')
print(psutil.Process(pid).cmdline())
print(pid)
print('------------------------------------------')
psutil.Process(pid).terminate()
except:
continue
temp_agent_pool = agent_pool[:]
for agent in temp_agent_pool:
try:
if DEBUG:
print(agent['agent_name'] + ' is trying to relaunch agents')
run_agent(agent_id = agent['agent_name'])
except:
if DEBUG:
print('Nothing in temp agent pool')
####################################################################################################################
#######################################WEBSOCKET SERVER PRELIM TESTING##############################################
####################################################################################################################
if DEBUG:
websocket.enableTrace(True)
else:
websocket.enableTrace(False)
while True:
if len(sys.argv) < 2:
host = settings.RVI_WS_HOST
else:
host = sys.argv[1]
ws = websocket.WebSocketApp(host,
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
if ws.run_forever() is None:
if DEBUG:
print('No RVI. Wait and retry.')
time.sleep(2)
continue
try:
while True:
time.sleep(1.0)
except KeyboardInterrupt:
print('^C received, shutting down server')
####################################################################################################################
####################################################################################################################
####################################################################################################################