Skip to content

SemaServer ¤

SemaServer ¤

SemaServer()
Source code in sema_toolchain/sema_web_app/application/SemaServer.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def __init__(self):
    self.log = SemaServer.app.logger

    # List that contains a dictionary containing all the arguments, it is then used
    # to generate dynamically the UI
    # Each element of the list is a HTML row that contains as element the associated dictionary

    # Init actions_scdg with current arguments available in ArgParser
    SemaServer.actions_scdg = self.init_scdg_args()
    self.log.info("SCDG arguments retreived")

    # Init actions_classifier with current arguments available in ArgParser
    SemaServer.actions_classifier = self.init_classifier_args()
    self.log.info("Classifier arguments retreived")

    SemaServer.exps = []
    SemaServer.download_thread = None
    SemaServer.malware_to_download = 0
    SemaServer.malware_to_downloaded = 0
    SemaServer.sema_res_dir = "database/runs/" # TODO dynamic
    SemaServer.current_exp = 0

add_header ¤

add_header(r)

It sets the cache control headers to prevent caching

:param r: The response object :return: the response object with the headers added.

Source code in sema_toolchain/sema_web_app/application/SemaServer.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
@app.after_request
def add_header(r):
    """
    It sets the cache control headers to prevent caching

    :param r: The response object
    :return: the response object with the headers added.
    """
    r.headers["Cache-Control"] =  "no-cache, no-store, must-revalidate"
    r.headers["Pragma"] = "no-cache"
    r.headers["Expires"] = "0"
    r.headers['Cache-Control'] = 'public, max-age=0'
    r.headers.add("Access-Control-Allow-Headers", "authorization,content-type")
    r.headers.add("Access-Control-Allow-Methods", "DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT")
    r.headers.add("Access-Control-Allow-Origin", "*")
    return r

init_classifier_args ¤

init_classifier_args()

Do an API call to the sema-classifier container to get the arguments to put on the index page

Source code in sema_toolchain/sema_web_app/application/SemaServer.py
74
75
76
77
def init_classifier_args(self):
    """Do an API call to the sema-classifier container to get the arguments to put on the index page"""
    response = requests.get('http://sema-classifier:5002/classifier_args')
    return response.json()

init_scdg_args ¤

init_scdg_args()

Do an API call to the sema-scdg container to get the arguments to put on the index page

Source code in sema_toolchain/sema_web_app/application/SemaServer.py
80
81
82
83
def init_scdg_args(self):
    """Do an API call to the sema-scdg container to get the arguments to put on the index page"""
    response = requests.get('http://sema-scdg:5001/scdg_args')
    return response.json()

redirection ¤

redirection()

It redirects the user to the index.html page :return: a redirect to the index.html page.

Source code in sema_toolchain/sema_web_app/application/SemaServer.py
125
126
127
128
129
130
131
@app.route('/')
def redirection():
    """
    It redirects the user to the index.html page
    :return: a redirect to the index.html page.
    """
    return redirect('index.html', code =302)

serve_index ¤

serve_index()

It creates a folder for the project, and then calls the upload function :return: the upload function.

Source code in sema_toolchain/sema_web_app/application/SemaServer.py
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
@app.route('/index.html', methods = ['GET', 'POST'])
def serve_index():
    """
    It creates a folder for the project, and then calls the upload function
    :return: the upload function.
    """

    if request.method == 'POST':
        scdg_args = {}
        class_args = {}
        fl_args = {}

        #Separate the different arguments of the different part of the toolchain
        exp_number = 1
        arguments = dict(request.form)
        for key,value in arguments.items():
            if (key,value) == ('boundary', 'experiment separation'):
                exp_number += 1
            elif exp_number == 1:
                scdg_args[key] = value
            elif exp_number == 2:
                class_args[key] = value

        ##
        # Here we start the experiments
        ##
        if "scdg_enable" in request.form:
            #Send request to SCDG microservices to start an SCDG with the parameters specified in scdg_args
            response = requests.post('http://sema-scdg:5001/run_scdg', json=scdg_args)
            SemaServer.app.logger.info(str(response.content))

        if "class_enable" in request.form:
            response = requests.post('http://sema-classifier:5002/run_classifier', json=class_args)
            SemaServer.app.logger.info(str(response.content))

    return render_template('index.html',
                        actions_scdg=SemaServer.actions_scdg,
                        actions_classifier=SemaServer.actions_classifier,
                        progress=0)