Commit 5c55c648 authored by Julien David's avatar Julien David
Browse files

Merge branch '005-editing-server' into 'master'

WIP : 005 editing server

See merge request !7
parents b14cf2b0 b3cdcd31
Pipeline #1426 passed with stage
in 13 seconds
...@@ -368,3 +368,37 @@ ENTRYPOINT ["python3","rdos_cleaner.py"] ...@@ -368,3 +368,37 @@ ENTRYPOINT ["python3","rdos_cleaner.py"]
# RUN make # RUN make
# RUN mv bin/arbogen.byte . # RUN mv bin/arbogen.byte .
# ENTRYPOINT ["python3","launcher.py"] # ENTRYPOINT ["python3","launcher.py"]
#######################################################################################
# Server #
#######################################################################################
# # base image
# FROM alpine:latest
# # maintainer
# LABEL maintainer="david@lipn.fr"
# # variable
# ENV AUTHOR Docker
# ADD database/ /usr/local/server/
# ADD server/ /usr/local/server
# WORKDIR /usr/local/server/
# # Dépendances
# RUN apk update
# RUN apk add python3
# RUN apk add py-pip
# RUN pip install mysql-connector-python
# RUN pip install get_docker_secret
# RUN pip install docker
# CMD ["python3", "server.py"]
# # TO BUILD the image from parent directory "Softwares" Command : docker build --tag "tag" -f ./server/Dockerfile .
# # TO run the build in DOCKER : docker run -d -p 9393:9393 --name "name" ser-app
...@@ -114,12 +114,25 @@ def get_generators(): ...@@ -114,12 +114,25 @@ def get_generators():
for result in myresult: for result in myresult:
generators[result[0]] = [result] generators[result[0]] = [result]
# Returns all existing generators from database in a list
# Insert in database query send from client and returns insert status def get_generator_list():
def db_insert(query: dict): database = database_connection()
generators = {}
sql_get_generators = "SELECT id, tool from generators"
mycur = database.cursor(buffered=False)
mycur.execute(sql_get_generators)
res = mycur.fetchall()
mycur.close()
for a, b in res:
generators[b] = a
return generators
# Insert in database query send from client to server and returns insert status
def add_job(query: dict):
if(query is not None): if(query is not None):
database = database_connection() database = database_connection()
if(db_job_check(query) != 0): if(job_check(query) != 0):
query['id'] = str(random.getrandbits(64)) query['id'] = str(random.getrandbits(64))
try: try:
values = query.values() values = query.values()
...@@ -127,16 +140,18 @@ def db_insert(query: dict): ...@@ -127,16 +140,18 @@ def db_insert(query: dict):
sql_new_job_row = "INSERT INTO jobs (%s) VALUES ('%s');" % (", ".join(cols), "','".join(values)) sql_new_job_row = "INSERT INTO jobs (%s) VALUES ('%s');" % (", ".join(cols), "','".join(values))
mycursor = database.cursor(buffered=True) mycursor = database.cursor(buffered=True)
mycursor.execute(sql_new_job_row) mycursor.execute(sql_new_job_row)
print("Record inserteds in DB :", query.values)
database.commit() database.commit()
mycursor.close() mycursor.close()
database.close() database.close()
return (mycursor.rowcount, "Record inserted successfully into job table")
except mysql.connector.Error as err: except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err)) return ("MYSQL Error :{}".format(err))
# Checks existance of query ID in database and returns the status # Checks existance of query ID in database and returns the status
def db_job_check(query: dict): # job_check returns 1 if query exist in database else returns 0
def job_check(query: dict):
if (query is not None): if (query is not None):
db = database_connection() db = database_connection()
try: try:
......
#!/usr/bin/env python3
# authors: Julien DAVID & Ismail MOUMNI # authors: Julien DAVID & Ismail MOUMNI
import socket import socket
import json import json
import datetime
import os import os
import sys import sys
import database.database as db
sys.path.append(os.path.realpath('../softwares/')) sys.path.append(os.path.realpath('../softwares/'))
import database as db # noqa E402
__RDOS_Dict__ = {"parameters": "request"}
__RDOS_Dict__ = {}
# tools List # tools List
__RDOS_Tool__ = {} __RDOS_Tool__ = {}
...@@ -23,26 +24,67 @@ class RdosServer: ...@@ -23,26 +24,67 @@ class RdosServer:
serv.bind((address, PORT)) serv.bind((address, PORT))
# Listening to 5 CLients # Listening to 5 CLients
serv.listen(5) serv.listen(5)
# accepting the connection from client and getting client IP while True:
conn, addr = serv.accept() # accepting the connection from client and getting client IP
while True: conn, addr = serv.accept()
if conn: if conn:
print('Connexion acceptee depuis l IP : ', addr) print('Connexion acceptee depuis l IP : ', addr)
# Receiving Data from Client # Receiving Data from Client
data = conn.recv(4096) data = conn.recv(4096)
query = (json.loads(data.decode('utf-8'))) query = (json.loads(data.decode('utf-8')))
print("Requete reçu : ", query) print("Requete reçu : ", query)
match = verif_param(query) if (query is not None):
if match != query: print(query)
print('Erreur query : ', match) query_process(conn, query)
conn.send(bytes(json.dumps(match), "utf-8")) conn.close()
else:
print("ok")
conn.send(bytes(json.dumps(match), "utf-8")) # Function query_process checks query to get generators or to insert query
# Function takes 2 parameters socket and query (get generator query or to insert in database a new job)
# Function returns generators list to client or returns insert result to client
# Function query_valwid matches the query send from client side def query_process(sock: socket, query: dict):
# with the the parameter dict # Sending GENERATORS
if (match_query_dict(query, __RDOS_Dict__)):
response = db.get_generators()
sock.send(bytes(json.dumps(response), "utf-8"))
print("database generators ", response)
# Inserting client query in DATABASE
else:
print("check", check_and_complete_parameters(query))
res = db_send_job(sock, query_fields(sock, check_and_complete_parameters(query)))
print(res)
# Function query_fields generates default fields to insert query send from client to database
# Function takes 2 parameters addr : IP address of client and data : Generators values
# Function return a valid insert query
def query_fields(addr, data):
__RDOS_Tool__ = db.get_generator_list()
sa = __RDOS_Tool__.get(list(data.keys())[0])
lm = list((data.values()))
s = {"id": "", "idGenerator": "", "IP": "", "timeSubmitted": "", "timeExecuted": "",
"timeFinished": "", "status": "", "parametersJSON": "", "directory": "", "url": "", "message": "", "email": ""}
s["id"] = str(os.getuid())
s["idGenerator"] = str(sa)
s["IP"] = addr.getpeername()[0]
s["timeSubmitted"] = (str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
s["parametersJSON"] = json.dumps(lm[0])
return s
# Function db_send_job send a query to insert in database by socket
# Function takes a socket and dictionary for input
# Function returns a string if query send
def db_send_job(s: socket, req: dict):
if(req is not None):
ins = db.add_job(req)
s.send(bytes(json.dumps(ins), "utf-8"))
print("Query send")
else:
raise Exception("Empty Query")
# Function query_valid matches the query send from client side with the the parameter dict
# Function input dictionary from client # Function input dictionary from client
# Function output boolean true if match else False # Function output boolean true if match else False
def query_valid(data: dict): def query_valid(data: dict):
...@@ -53,22 +95,28 @@ def query_valid(data: dict): ...@@ -53,22 +95,28 @@ def query_valid(data: dict):
return False return False
# Function json_loads changes a json file into a Dict # Function json_to_dict changes a json file into a Dict
# Function json_loads returns a dictionary # Function json_to_dict returns a dictionary
def json_to_dict(js): def json_to_dict(data):
data = json.loads(js) if data is not None:
return data result = json.loads(data)
return result
else:
raise Exception("Dictionnaire Vide!!")
# Function db_req sends a request to get parameters of tools from database # Function db_req sends a request to get parameters of tools from database
# Function takes a dictionary for input # Function takes a dictionary for input
# Function return a Dict containing tool parameters and default parameters # Function return a Dict containing tool parameters and default parameters
def db_req(tool: dict): def db_req(tool: dict):
tool_dict = db.getParameters(tool) if dict is not None:
return tool_dict tool_dict = db.getParameters(tool)
return tool_dict
else:
raise Exception("Dictionnaire Vide!!")
# Function match_dict returns non existing fields in a dict # Function missing_keys returns non existing fields in a dict
# Function input biblio and dict # Function input biblio and dict
# Function returns a list containing missing values # Function returns a list containing missing values
def missing_keys(biblio: dict, gen: dict): def missing_keys(biblio: dict, gen: dict):
...@@ -84,60 +132,65 @@ def missing_keys(biblio: dict, gen: dict): ...@@ -84,60 +132,65 @@ def missing_keys(biblio: dict, gen: dict):
# Function match_query_dict matches two dictionaries # Function match_query_dict matches two dictionaries
# Function input takes 2 dictionaries to match # Function input takes 2 dictionaries to match
# Function output return trur if dictionaries matches else raise a value error exception # Function output return trur if dictionaries matches
def match_query_dict(biblio: dict, data: dict): def match_query_dict(biblio: dict, data: dict):
if biblio is not None and data is not None: if biblio is not None and data is not None:
return biblio.keys() == data.keys() return biblio.keys() == data.keys()
else: else:
raise ValueError("Error Dict Values ") raise ValueError("Dictionnaire Vide ")
# Function verif_param verifies json send from client to server by matching it with database query # Function check_and_complete_parameters verifies json send from client to server by matching it with database query
# Function input takes a dictionary # Function input takes a dictionary
# Function output returns query if it matches with database query keys if not it raises an error # Function output returns query if it matches with database query keys
def verif_param(data: dict): def check_and_complete_parameters(data: dict):
tool = data.keys().strip('[]') if data is not None:
s = db.getParameters(data['tool']) tool = list(data.keys())[0]
parameter = {} s = db.getParameters({tool})
query = {} parameter = {}
if s is not None: if s is not None:
gen = s[0] # gen = s[0]
gen_default = s[1] parameter = s[1]
parameter = gen_default q = data.get(tool)
query = data.get(tool) if match_query_dict(parameter.get(tool), q):
if match_query_dict(gen.get(tool), query): check_and_replace(data.get(tool), parameter.get(tool))
for a in query: return data
if query[a] == "": else:
print(query[a]) raise Exception("Query Doesn't match")
query[a] = parameter.get(a)
return query
else: else:
raise Exception("Query Doesn't match") raise Exception("Error Database")
else: else:
raise Exception("Error Database") return Exception("Dictionnaire Vide!!", tool)
# check_and_replace checks query with default_parameters of generator and replaces empty values with default_parameters
# Function input takes 2 dictionaries
# Function output returns matched query
def check_and_replace(query: dict, default_parameters: dict):
if query is not None and default_parameters is not None:
for a, b in query.items():
if b == '':
query[a] = default_parameters.get(a)
return query
else:
raise Exception("Dictionnaire Vide!!")
# Function db_generators returns database generators # Function db_generators returns all database generators
# Function takes a dictionary request from client side # Function takes a dictionary request from client side
# Function returns all database generators or raises an error if request doesn't match # Function returns all database generators or raises an error if request doesn't match
def db_generators(req: dict): def db_generators(req: dict):
match = {"request": "generators"} if req is not None:
if(req == match): match = {"request": "generators"}
s = db.get_generators() if(req == match):
return s s = db.get_generators()
return s
else:
raise Exception("Dictionnaire non Valid")
else: else:
raise Exception("Error Database") return Exception("Dictionnaire Vide!!", req)
# Function db_insert send a query to insert in database by socket if __name__ == '__main__':
# Function db_insert takes a socket and dictionary for input # TEST FOR Docker
# Function db_insert returns a string if query send RdosServer.server_conn('127.0.0.1', '9393')
def db_insert(s: socket, req: dict):
if(req is not None):
if (verif_param(req) == req):
ins = db.insert_query(req)
s.send(bytes(json.dumps(ins), "utf-8"))
print("Query send")
else:
raise Exception("Query Doesn't match")
raise Exception("Invalid Query")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment