|
| 1 | +from flask import Flask, render_template, request |
| 2 | +import requests |
| 3 | +import json |
| 4 | +import os |
| 5 | +from secretsharing import SecretSharer as Shamir |
| 6 | +import numpy as np |
| 7 | +import time |
| 8 | +from werkzeug.contrib.cache import SimpleCache |
| 9 | +import atexit |
| 10 | +from apscheduler.schedulers.background import BackgroundScheduler |
| 11 | +from apscheduler.triggers.interval import IntervalTrigger |
| 12 | +import logging |
| 13 | +logging.basicConfig() |
| 14 | + |
| 15 | +cache = SimpleCache() |
| 16 | +app = Flask(__name__) |
| 17 | + |
| 18 | +scheduler = BackgroundScheduler() |
| 19 | +scheduler.start() |
| 20 | +# log = logging.getLogger('apscheduler.executors.default') |
| 21 | +# log.setLevel(logging.INFO) # DEBUG |
| 22 | + |
| 23 | +# fmt = logging.Formatter('%(levelname)s:%(name)s:%(message)s') |
| 24 | +# h = logging.StreamHandler() |
| 25 | +# h.setFormatter(fmt) |
| 26 | +# log.addHandler(h) |
| 27 | + |
| 28 | + |
| 29 | +@app.route('/') |
| 30 | +def hello_world(): |
| 31 | + return 'Welcome to test' |
| 32 | + |
| 33 | +#request function that receive clients' vectors with jason form |
| 34 | +@app.route('/server_0_uploader', methods = ['POST']) |
| 35 | +def upload_request_(): |
| 36 | + if request.method == 'POST': |
| 37 | + request_json = request.get_json() |
| 38 | + #print request_json |
| 39 | + u_0 = np.array(request_json.get('u_0')) |
| 40 | + u_1 = np.array(request_json.get('u_1')) |
| 41 | + u_2 = np.array(request_json.get('u_2')) |
| 42 | + v_0 = np.array(request_json.get('v_0')) |
| 43 | + v_1 = np.array(request_json.get('v_1')) |
| 44 | + v_2 = np.array(request_json.get('v_2')) |
| 45 | + # v1_square_0 = np.array(request_json.get('v_0_square')) |
| 46 | + # v1_square_1 = np.array(request_json.get('v_1_square')) |
| 47 | + # v1_square_2 = np.array(request_json.get('v_2_square')) |
| 48 | + # use reshape to perform 1-d array transpose |
| 49 | + u_0_v_0 = u_0.reshape([-1,1]).dot(v_0.reshape([1,-1])) |
| 50 | + u_1_v_1 = u_1.reshape([-1,1]).dot(v_1.reshape([1,-1])) |
| 51 | + u_2_v_2 = u_2.reshape([-1,1]).dot(v_2.reshape([1,-1])) |
| 52 | + |
| 53 | + # u_0_v_0_square = u_0.reshape([-1,1]).dot(v1_square_0.reshape([1,-1])) |
| 54 | + # u_1_v_1_square = u_1.reshape([-1,1]).dot(v1_square_1.reshape([1,-1])) |
| 55 | + # u_2_v_2_square = u_2.reshape([-1,1]).dot(v1_square_2.reshape([1,-1])) |
| 56 | + |
| 57 | + batch_list = np.array([u_0_v_0,u_1_v_1,u_2_v_2]) |
| 58 | + # batch_square_list = [u_0_v_0_square,u_1_v_1_square,u_2_v_2_square] |
| 59 | + # batch_list = np.array([batch_list, batch_square_list]) |
| 60 | + batch_result_save(batch_list) |
| 61 | + |
| 62 | + return 'message sent' |
| 63 | + |
| 64 | +#batch function that used to save client's results |
| 65 | +def batch_result_save(batch_list): |
| 66 | + batch_result = get_batch_result() |
| 67 | + if (type(batch_result) is not np.ndarray): |
| 68 | + set_batch_result(batch_list) |
| 69 | + else: |
| 70 | + for i in range(0,3): |
| 71 | + batch_result[i] = batch_result[i] + batch_list[i] |
| 72 | + set_batch_result(batch_result) |
| 73 | + |
| 74 | +#used to print result for simple demo, will be replaced if database is implemented |
| 75 | +def batch_result(): |
| 76 | + batch_result = get_batch_result() |
| 77 | + if (type(batch_result) is not np.ndarray): |
| 78 | + print "message is empty" |
| 79 | + else: |
| 80 | + print recover(batch_result) |
| 81 | + set_batch_result(None) |
| 82 | + |
| 83 | + |
| 84 | +#recover function perform database reconstruction |
| 85 | +def recover(s): |
| 86 | + db = [] |
| 87 | + |
| 88 | + for i in range(np.shape(s)[1]): |
| 89 | + for j in range(np.shape(s)[2]): |
| 90 | + tmp1 = [] |
| 91 | + for k in range(np.shape(s)[0]): |
| 92 | + tmp1.append(str(k+1)+"-"+str(hex(s[k][i][j]))[2:-1]) |
| 93 | + answer = Shamir.recover_secret(tmp1) |
| 94 | + if len(answer) % 2 == 1: |
| 95 | + answer = '0' + answer |
| 96 | + db.append(answer.decode('hex')) |
| 97 | + return db |
| 98 | + |
| 99 | +def get_batch_result(): |
| 100 | + return cache.get('batch_result') |
| 101 | + |
| 102 | +def set_batch_result(batch_input): |
| 103 | + cache.set('batch_result', batch_input, timeout=5 * 60) |
| 104 | + |
| 105 | +scheduler.add_job( |
| 106 | + func=batch_result, |
| 107 | + trigger=IntervalTrigger(seconds=8), |
| 108 | + id='batch_result', |
| 109 | + name='Print datebase with 5s batch', |
| 110 | + replace_existing=True) |
| 111 | +# Shut down the scheduler when exiting the app |
| 112 | +atexit.register(lambda: scheduler.shutdown()) |
| 113 | + |
| 114 | +if __name__ == '__main__': |
| 115 | + app.run(host='0.0.0.0', port=80, debug=True) |
| 116 | + |
| 117 | + |
0 commit comments