Commit 005ad490 by Luciano Barletta

added cleaner for unsended messages

1 parent 5fe66ea1
......@@ -8,9 +8,11 @@ import time, threading
from python_arptable import get_arp_table
import random
from enums import States, Table
import datetime
app = Flask(__name__)
retry_timer = 10
clean_timer = 20
prefix_lenght = 16
filename = {}
......@@ -31,7 +33,9 @@ def data():
key = request.files.get('key')
if key != None:
key.save(prefix + "_key.enc")
request.files['data'].save(prefix + ".enc")
request.files['data'].save(prefix + ".enc")
else:
request.files['data'].save(prefix)
filename[prefix] = request.files['data'].filename
return prefix
......@@ -94,10 +98,46 @@ def attempt():
process.send()
threading.Timer(retry_timer, attempt).start()
def clean():
process = Process('messages.db')
paths = process.paths()
now = datetime.datetime.now()
# in database (after /msg)
for file in paths:
mtime = os.path.getmtime(file)
# if the file exists for more than a 23 hs, erase it
if int(now.strftime("%Y%m%d%H")) - int(time.strftime("%Y%m%d%H")) > 23:
os.system("rm " + file)
# in prefixes dictionary (after /data)
for file in filename:
# not encrypted
if os.path.exists(file):
mtime = os.path.getmtime(file)
# if the file exists for more than a 23 hs, erase it
if int(now.strftime("%Y%m%d%H")) - int(time.strftime("%Y%m%d%H")) > 23:
os.system("rm " + file)
filename.pop(file)
# encrypted
elif os.path.exists(file + ".enc"):
mtime = os.path.getmtime(file + ".enc")
# if the file exists for more than a 23 hs, erase it
if int(now.strftime("%Y%m%d%H")) - int(time.strftime("%Y%m%d%H")) > 23:
os.system("rm " + file + ".enc")
os.system("rm " + file + "_key")
filename.pop(file)
threading.Timer(clean_timer, clean).start()
if __name__ == "__main__":
# generate keys
os.system("openssl genrsa -out rsa_key.pri 4096") # private key
os.system("openssl rsa -in rsa_key.pri -out rsa_key.pub -outform PEM -pubout") # public key
# starts attempt daemon
attempt()
# starts cleaning daemon
clean()
app.run("0.0.0.0")
\ No newline at end of file
......@@ -62,4 +62,11 @@ class Process:
Table.state : row[Table.state],
}
self.conn.insert("history",entities)
return row
\ No newline at end of file
return row
def paths(self):
rows = self.conn.query("SELECT path FROM msg")
paths = []
for row in rows:
paths.append(row[0])
return paths
\ No newline at end of file
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!