forked from nsacyber/WALKOFF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartServer.py
94 lines (80 loc) · 2.77 KB
/
startServer.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
import logging.config
import sys, traceback
import ssl
import os
from os.path import isfile
from core.config import config, paths
from apps import *
from gevent.wsgi import WSGIServer
from gevent import monkey
logger = logging.getLogger('startserver')
def get_ssl_context():
if config.https:
# Sets up HTTPS
if config.tls_version == "1.2":
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
elif config.tls_version == "1.1":
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_1)
else:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
if isfile(paths.certificate_path) and isfile(paths.private_key_path):
context.load_cert_chain(paths.certificate_path, paths.private_key_path)
return context
else:
print('Certificates not found')
return None
def setup_logger():
log_config = None
if isfile(paths.logging_config_path):
try:
with open(paths.logging_config_path, 'rt') as log_config_file:
log_config = json.loads(log_config_file.read())
except:
print('Invalid JSON in logging config file')
pass
else:
print('No logging config found')
if log_config is not None:
logging.config.dictConfig(log_config)
else:
logging.basicConfig()
logger.info("Basic logging is being used")
def run():
setup_logger()
monkey.patch_all()
# The order of these imports matter for initialization (should probably be fixed)
from compose_api import compose_api
compose_api()
from server import flaskserver
import core.case.database as case_database
case_database.initialize()
ssl_context = get_ssl_context()
flaskserver.running_context.controller.initialize_threading()
try:
port = int(config.port)
except ValueError:
print('Invalid port {0}. Port must be an integer'.format(config.port))
else:
host = config.host
if ssl_context:
server = WSGIServer((host, port), application=flaskserver.app, ssl_context=ssl_context)
proto = 'https'
else:
server = WSGIServer((host, port), application=flaskserver.app)
proto = 'http'
logger.info('Listening on host {0}://{1}:{2}'.format(proto, host, port))
server.serve_forever()
# app.run()
if __name__ == "__main__":
try:
run()
except KeyboardInterrupt:
logger.info('Caught KeyboardInterrupt!')
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exc()
finally:
from server import flaskserver
flaskserver.running_context.controller.shutdown_pool()
logger.info('Shutting down server')
os._exit(0)