1616from tornado .httpserver import HTTPServer
1717from tornado .ioloop import IOLoop
1818
19- from plugins .core import PluginControllerForApi
2019from model .visitor import VulnsLookupVisitor
2120
2221import utils .logs as logger
@@ -41,10 +40,10 @@ def stopServer():
4140 _http_server .stop ()
4241
4342
44- def startAPIs (plugin_manager , model_controller , mapper_manager , hostname , port ):
43+ def startAPIs (plugin_controller , model_controller , hostname , port ):
4544 global _rest_controllers
4645 global _http_server
47- _rest_controllers = [PluginControllerAPI (plugin_manager , mapper_manager ), ModelControllerAPI (model_controller )]
46+ _rest_controllers = [PluginControllerAPI (plugin_controller ), ModelControllerAPI (model_controller )]
4847
4948 app = Flask ('APISController' )
5049
@@ -92,12 +91,12 @@ def getRoutes(self):
9291 def badRequest (self , message ):
9392 error = 400
9493 return jsonify (error = error ,
95- message = message ), error
94+ message = message )
9695
9796 def noContent (self , message ):
9897 code = 204
9998 return jsonify (code = code ,
100- message = message ), code
99+ message = message )
101100
102101 def ok (self , message ):
103102 code = 200
@@ -287,54 +286,71 @@ def statusCheck(self):
287286
288287
289288class PluginControllerAPI (RESTApi ):
290- def __init__ (self , plugin_manager , mapper_manager ):
291- self .plugin_controller = PluginControllerForApi (
292- "PluginController" ,
293- plugin_manager .getPlugins (),
294- mapper_manager )
289+ def __init__ (self , plugin_controller ):
290+ self .plugin_controller = plugin_controller
295291
296292 def getRoutes (self ):
297293 routes = []
298294 routes .append (Route (path = '/cmd/input' ,
299- view_func = self .postCmdInput ,
300- methods = ['POST' ]))
295+ view_func = self .postCmdInput ,
296+ methods = ['POST' ]))
301297 routes .append (Route (path = '/cmd/output' ,
302- view_func = self .postCmdOutput ,
303- methods = ['POST' ]))
298+ view_func = self .postCmdOutput ,
299+ methods = ['POST' ]))
304300 routes .append (Route (path = '/cmd/active-plugins' ,
305- view_func = self .clearActivePlugins ,
306- methods = ['DELETE' ]))
301+ view_func = self .clearActivePlugins ,
302+ methods = ['DELETE' ]))
307303 return routes
308304
309- def pluginAvailable (self , new_cmd , output_file ):
305+ def pluginAvailable (self , plugin , cmd ):
310306 code = 200
311307 return jsonify (code = code ,
312- cmd = new_cmd ,
313- custom_output_file = output_file )
308+ cmd = cmd ,
309+ plugin = plugin )
314310
315311 def postCmdInput (self ):
316312 json_data = request .get_json ()
317313 if 'cmd' in json_data .keys ():
318- cmd = json_data .get ('cmd' )
319- has_plugin , new_cmd , output_file = self .plugin_controller .\
320- processCommandInput (cmd )
321- if has_plugin :
322- return self .pluginAvailable (new_cmd , output_file )
323- return self .noContent ("no plugin available for cmd" )
324- #cmd not sent, bad request
325- return self .badRequest ("cmd parameter not sent" )
314+ if 'pid' in json_data .keys ():
315+ if 'pwd' in json_data .keys ():
316+ try :
317+ cmd = base64 .b64decode (json_data .get ('cmd' ))
318+ pwd = base64 .b64decode (json_data .get ('pwd' ))
319+ except :
320+ cmd = ''
321+ pwd = ''
322+ pid = json_data .get ('pid' )
323+ plugin , new_cmd = self .plugin_controller .\
324+ processCommandInput (pid , cmd , pwd )
325+ if plugin :
326+ return self .pluginAvailable (plugin , new_cmd )
327+ else :
328+ return self .noContent ("no plugin available for cmd" )
329+ else :
330+ return self .badRequest ("pwd parameter not sent" )
331+ else :
332+ return self .badRequest ("pid parameter not sent" )
333+ else :
334+ return self .badRequest ("cmd parameter not sent" )
335+
336+
326337
327338 def postCmdOutput (self ):
328339 json_data = request .get_json ()
329- if 'cmd ' in json_data .keys ():
340+ if 'pid ' in json_data .keys ():
330341 if 'output' in json_data .keys ():
331- cmd = json_data .get ('cmd' )
332- output = base64 .b64decode (json_data .get ('output' ))
333- if self .plugin_controller .onCommandFinished (cmd , output ):
334- return self .ok ("output successfully sent to plugin" )
335- return self .badRequest ("output received but no active plugin" )
342+ if 'exit_code' in json_data .keys ():
343+ pid = json_data .get ('pid' )
344+ output = base64 .b64decode (json_data .get ('output' ))
345+ exit_code = json_data .get ('exit_code' )
346+ if self .plugin_controller .onCommandFinished (
347+ pid , exit_code , output ):
348+ return self .ok ("output successfully sent to plugin" )
349+ return self .badRequest (
350+ "output received but no active plugin" )
351+ return self .badRequest ("exit_code parameter not sent" )
336352 return self .badRequest ("output parameter not sent" )
337- return self .badRequest ("cmd parameter not sent" )
353+ return self .badRequest ("pid parameter not sent" )
338354
339355 def clearActivePlugins (self ):
340356 self .plugin_controller .clearActivePlugins ()
0 commit comments