def tracker_update(tracker_hostname, tracker_port, request, app_name, hostname, port, key, pk): conn = TCPConnection() conn.connect(tracker_hostname, tracker_port) data = { 'request': request, 'params': { 'app': app_name, 'hostname': hostname, 'port': port } } bstr = Wrap.reduceToBytes(Wrap.dictToBen(data)) reqd = { 'request-data': { 'data': data, 'type': "POST", 'key': str(b64encode(host.gen_key_str(key)), encoding='utf-8'), 'sign': str(b64encode(host.sign_str(pk, bstr=bstr)), encoding='utf-8') }, 'request-type': 'new' } req = client.request(reqd) client.request_send(conn, req) b = client.response_recv(conn) d = client.response(b) conn.close() return d
'data-sign': data_sign # 'sign': input(), # 'ring': str(ring) } bstr = Wrap.reduceToBytes(Wrap.dictToBen(data)) reqd = { 'request-data': { 'data': data, 'type': "POST", 'key': str(b64encode(host.gen_key_str(key)), encoding='utf-8'), 'sign': str(b64encode(host.sign_str(pk, bstr=bstr)), encoding='utf-8') }, 'request-type': 'new' } req = client.request(reqd) # udp = UDPConnection() # udp.send(req,('127.0.0.1',1024)) # data,addr = udp.recv() # print(data,addr) client.request_send(conn, req) b = client.response_recv(conn) d = client.response(b) # if reqd['request-data']['type'] == 'POST': # b2 = client.response_recv(conn) conn.close() # print(b) # print(d['response-data']['data']['db'][0][0]['post']['sign']) # if host.verify_str(host.load_key_str(b64decode(d['response-data']['data']['db'][0]['user']['key'])), # Wrap.reduceToBytes(Wrap.dictToBen(d['response-data']['data']['db'][0]['post']['signed-data'])), # b64decode(d['response-data']['data']['db'][0]['post']['sign'])):
def new(req,**kwargs): bstr = Wrap.toBen(req['request-data']['data']) key = host.load_key_str(b64decode( req['request-data']['key'])) sign = b64decode(req['request-data']['sign']) if not host.verify_str(key,bstr,sign): # print("UN VERIFIED") resp = helper.invalid_request_response(pk,key_str,req['request-data']['sign']) server.response_send(kwargs['conn'], server.response(resp)) kwargs['conn'].close() raise Exception # else: # print("VERIFIED") # pass data = dispatch(kwargs['dispatch'],list(req['request-data']['data']['request']),req['request-data']) out_bstr = Wrap.toBen(data) response_data = { 'data': data, 'host':host.b64_str(key_str), 'sign' : host.b64_str(host.sign_str(pk,out_bstr)) } resp = { 'response-data' : response_data, 'response-type' : 'DATA' } server.response_send(kwargs['conn'],server.response(resp)) if req['request-data']['type'] == 'POST' and data['status'] == 'OK': flag = True while True: fw_bstr = Wrap.toBen(req['request-data']) req['request-type'] = 'forward' req['forward-host'] = host.b64_str(key_str) req['forward-sign'] = host.b64_str(host.sign_str(pk,fw_bstr)) trac = helper.tracker_get('127.0.0.1', 1024, ['get'], 'my_app2', '127.0.0.1', kwargs['port'], key, pk) if trac['response-data']['data']['status'] == 'empty-list': server.response_send(kwargs['conn'],server.response({'status':'empty-list'})) kwargs['conn'].close() return fw_conn = TCPConnection() fw_conn.connect(trac['response-data']['data']['db'][0], trac['response-data']['data']['db'][1]) req_b = client.request(req) client.request_send(fw_conn,req_b) fw_resp_b = client.response_recv(fw_conn) fw_resp = client.response(fw_resp_b) #verify_response fw_bstr = Wrap.toBen(fw_resp['response-data']['data']) fw_key = host.load_key_str(b64decode(fw_resp['response-data']['host'])) fw_sign = b64decode(fw_resp['response-data']['sign']) if not host.verify_str(fw_key, fw_bstr, fw_sign): # print("UN VERIFIED") resp = helper.invalid_request_response(pk, key_str, fw_resp['response-data']['host']) server.response_send(fw_conn, server.response(resp)) fw_conn.close() # return raise Exception if flag: server.response_send(kwargs['conn'], server.response(fw_resp)) kwargs['conn'].close() flag = False if fw_resp['response-data']['data']['status'] != 'OK': fw_conn.close() return fw_resp_b2 = client.response_recv(fw_conn) fw_resp2 = client.response(fw_resp_b2) fw_bstr2 = Wrap.toBen(fw_resp2['response-data']['data']) fw_key2 = host.load_key_str(b64decode(fw_resp2['response-data']['host'])) fw_sign2 = b64decode(fw_resp2['response-data']['sign']) if not host.verify_str(fw_key2, fw_bstr2, fw_sign2): # print("UN VERIFIED") resp = helper.invalid_request_response(pk, key_str, fw_resp2['response-data']['host']) server.response_send(fw_conn, server.response(resp)) fw_conn.close() continue # raise Exception #verify_response fw_conn.close()
def xor_replication_post(trac, database): if trac['response-data']['data']['status'] == 'empty': return sesson = database.new_session() respd = None rows = sesson.query(Post).order_by(Post.id).all() r = len(rows) database.sesson_close(sesson) while r > 0: print(r) xor_key = rows[r - 1].xor data = { 'request': ['replicate'], 'params': { 'xor': xor_key, } } bstr = Wrap.reduceToBytes(Wrap.dictToBen(data)) reqd = { 'request-data': { 'data': data, 'type': "GET", 'key': str(b64encode(host.gen_key_str(key)), encoding='utf-8'), 'sign': str(b64encode(host.sign_str(pk, bstr=bstr)), encoding='utf-8') }, 'request-type': 'new' } fw_conn = TCPConnection() try: fw_conn.connect(trac['response-data']['data']['db'][0], trac['response-data']['data']['db'][1]) except ConnectionRefusedError: break req = client.request(reqd) client.request_send(fw_conn, req) resp = client.response_recv(fw_conn) respd = client.response(resp) fw_conn.close() if (respd['response-data']['data']['status'] == 'NOT_FOUND'): r -= 1 # time.sleep(1) else: break if respd is None: return sesson = database.new_session() print(respd['response-data']['data']['response']) for i in range(len(respd['response-data']['data']['response'])): if len( sesson.query(Post).filter_by( sign=respd['response-data']['data']['response'][i] ['sign']).all()) > 0: continue userlst = sesson.query(User).filter_by( key=respd['response-data']['data']['response'][i]['key']).all() if len(userlst) == 0: u = User(key=respd['response-data']['data']['response'][i]['key']) sesson.add(u) user = sesson.query(User).filter_by( key=respd['response-data']['data']['response'][i]['key'])[0] xor = int(sesson.query(Post).all()[-1].xor, base=16) xor ^= int(helper.binToHex( host.hash( bytes(respd['response-data']['data']['response'][i]['sign'], encoding='utf-8'))), base=16) post = Post(text=respd['response-data']['data']['response'][i]['text'], sign=respd['response-data']['data']['response'][i]['sign'], user=user, xor=hex(xor)[2:]) sesson.add(post) sesson.commit() database.sesson_close(sesson)