class SimpleHttpServer: def __init__(self): plugin_cfg_path = os.getenv('RECCE7_PLUGIN_CONFIG') or 'config/plugins.cfg' global_cfg_path = os.getenv('RECCE7_GLOBAL_CONFIG') or 'config/global.cfg' self.g_config = GlobalConfig(plugin_cfg_path, global_cfg_path) self.g_config.read_plugin_config() self.g_config.read_global_config() self.host = self.g_config.get_report_server_host() self.port = self.g_config.get_report_server_port() log_path = self.g_config['ReportServer']['reportserver.logName'] log_level = self.g_config['ReportServer']['reportserver.logLevel'] self.log = Logger(log_path, log_level).get('reportserver.server.SimpleHTTPServer.SimpleHTTPServer') def setupAndStart(self): server_address = (self.host, self.port) request_handler = RestRequestHandler # instantiate a server object httpd = HTTPServer (server_address, request_handler) print(time.asctime(), "Server Starting - %s:%s" % (self.host, self.port)) try: # start serving pages httpd.serve_forever () except KeyboardInterrupt: pass httpd.server_close() print(time.asctime(), "Server Stopped - %s:%s" % (self.host, self.port))
class GlobalConfig_Test(unittest.TestCase): def setUp(self): self.gconfig = GlobalConfig(test_cfg_path, test_global_cfg_path, refresh=True) self.gconfig.read_plugin_config() self.gconfig.read_global_config() def test_getInstance(self): gconfig2 = GlobalConfig() self.assertEqual(str(self.gconfig),str(gconfig2),"these 2 objects should equal") gconfig3 = GlobalConfig() self.assertEqual(str(self.gconfig), str(gconfig3), "these 2 objects should equal") self.assertEqual(str(gconfig2), str(gconfig3), "these 2 objects should equal") def test_getPorts(self): ports = self.gconfig.get_ports() self.assertEqual(len(ports), 2, "expected 2 ports in test.cfg found: " + str(len(ports))) for port in ports: print("found: " + str(port)) def test_getReportServerConfig(self): host = self.gconfig.get_report_server_host() port = self.gconfig.get_report_server_port() self.assertEqual(host, "", "expected host to be ''") self.assertEqual(port, 8080, "expected port to be '8080' ") def test_getReportServerHost(self): self.assertEqual("", self.gconfig.get_report_server_host()) def test_getReportServerPort(self): self.assertEqual(8080, self.gconfig.get_report_server_port()) def test_refresh_instance(self): gconfig2 = GlobalConfig(test_cfg_path, test_global_cfg_path, refresh=True) self.assertNotEqual(str(self.gconfig), str(gconfig2), "these 2 objects should NOT equal when refresh set to True") def test_refresh_instance_same(self): gconfig2 = GlobalConfig() self.assertEqual(str(self.gconfig), str(gconfig2), "these 2 objects should equal when False is set for Refresh") gconfig2 = GlobalConfig() self.assertEqual(str(self.gconfig), str(gconfig2), "these 2 objects should equal with default of False") def test_get_date_time_name(self): self.assertEqual("eventDateTime", self.gconfig['Database']['datetime.name']) def test_get_db_peerAddress_nameself(self): self.assertEqual("peerAddress", self.gconfig['Database']['peerAddress.name']) def test_get_db_localAddress_name(self): self.assertEqual("localAddress", self.gconfig['Database']['localAddress.name'])
class RestRequestHandler (BaseHTTPRequestHandler): def do_GET(self) : path_query_tuple = utilities.get_path_query_tokens(self.path) path_tokens = path_query_tuple[0] query_tokens = path_query_tuple[1] if self.path.startswith("/v1/analytics"): if len(path_tokens) >= 4: if str(path_tokens[3]) == "ports": PortsServiceHandler().process(self, path_tokens, query_tokens) elif str(path_tokens[3]) == "ipaddresses": IpsServiceHandler().process(self, path_tokens, query_tokens) elif str(path_tokens[3]) == "worldmap": WorldmapServiceHandler().process(self, path_tokens, query_tokens) elif str(path_tokens[3] == ""): self.showIndex() else: self.badRequest() else: self.showIndex() else: self.notFound() def get_full_url_path(self): self.g_config = GlobalConfig() self.host = self.g_config.get_report_server_host() self.port = self.g_config.get_report_server_port() full_path = 'http://%s:%s/v1/analytics' % (str(self.host), str(self.port)) return full_path def getIndexPayload(self): return {'links': ['rel: ports, href: ' + self.get_full_url_path() + '/ports', 'rel: ipaddresses, href:' + self.get_full_url_path() + '/ipaddresses']} def showIndex(self): # send response code: self.sendJsonResponse(self.getIndexPayload(), 200) def notFound(self): # send response code: self.sendJsonResponse(notFoundPayload,404) def badRequest(self, rqstPayload=badRequestPayload): # send response code: self.sendJsonResponse(rqstPayload,400) def sendJsonResponse(self, payload, responseCode): # Note: responseCode must be set before headers in python3!! # see this post: # http://stackoverflow.com/questions/23321887/python-3-http-server-sends-headers-as-output/35634827#35634827 json_result = json.dumps(payload) self.send_response(responseCode) #todo make this configurable for allow-origin self.send_header("Access-Control-Allow-Origin","http://localhost:8000") self.send_header('Content-Type', 'application/json') self.send_header('Content-Length', len(json_result)) self.end_headers() self.flush_headers() self.wfile.write(bytes(json_result, "utf-8")) self.wfile.flush() return def sendPngResponse(self, filepath, responseCode): # Note: responseCode must be set before headers in python3!! # see this post: # http://stackoverflow.com/questions/23321887/python-3-http-server-sends-headers-as-output/35634827#35634827 f=open(filepath, 'rb') self.send_response(responseCode) # todo make this configurable for allow-origin self.send_header("Access-Control-Allow-Origin", "http://localhost:8000") self.send_header('Content-Type', 'image/png') #self.send_header('Content-Length', len(json_result)) self.end_headers() self.flush_headers() self.wfile.write(f.read()) self.wfile.flush() f.close() return