deploy.py
4 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#-*- coding: utf-8 -*-
from flask import Flask, render_template, json, request, url_for
from werkzeug.datastructures import FileStorage
import os
import ipdb
from process import Process
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 = {}
@app.route('/')
def main():
return render_template('index.html')
@app.route('/key', methods = ['GET', 'POST'])
def key():
f = open("rsa_key.pub", "r")
key = f.read()
f.close()
return key
@app.route('/data', methods = ['POST'])
def data():
prefix = newprefix()
key = request.files.get('key')
if key != None:
key.save(prefix + "_key.enc")
request.files['data'].save(prefix + ".enc")
else:
request.files['data'].save(prefix)
filename[prefix] = request.files['data'].filename
return prefix
def newprefix():
prefix = ""
i = 0
while i < prefix_lenght:
range = random.randrange(3)
if range == 0:
prefix += chr(random.randrange(48,58))
if range == 1:
prefix += chr(random.randrange(65,91))
if range == 2:
prefix += chr(random.randrange(97,123))
i += 1
return prefix
@app.route('/msg', methods = ['POST'])
def msg():
process = Process('messages.db')
prefix = request.values['id']
if prefix not in filename:
return "El id de la data es invalido"
# symetric key was sent, decrypt data
if os.path.exists(prefix + "_key.enc"):
# decrypt random key with stored private key and store in host folder
os.system("openssl rsautl -decrypt -inkey rsa_key.pri -in " + prefix + "_key.enc -out " + prefix + "_key")
# decrypt JSON with decrypted random key and store in dir folder
os.system("openssl enc -d -aes-256-cbc -in " + prefix + ".enc -out " + prefix + " -pass file:" + prefix + "_key")
# delete garbage
os.system("rm " + prefix + ".enc")
os.system("rm " + prefix + "_key.enc")
os.system("rm " + prefix + "_key")
query = {
'path' : prefix,
'file' : filename.pop(prefix),
'serv' : request.values['serv'],
'dest' : request.values['dest'],
'type' : request.values['type']
}
id = process.store(query)
return str(id)
@app.route('/cons', methods = ['POST'])
def cons():
process = Process('messages.db')
id_query = request.form['id']
row = process.lookup(id_query)
if type(row) == str: # error message
return row
if row[Table.state] == States.delivered:
os.system("rm " + row[Table.path])
return str(row[Table.state])
def attempt():
process = Process('messages.db')
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")