From 8d559f94ad8c0a9f7cb8f4b3aac731bdc79e49de Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 22:26:53 +0800 Subject: [PATCH 1/7] fix: update git ignore --- .gitignore | 1 + test.py | 6 ------ 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 test.py diff --git a/.gitignore b/.gitignore index 1e6c292..cf2b89c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ example.py ReEncrypt.py src/temp_message_file src/temp_key_file +src/client.db diff --git a/test.py b/test.py deleted file mode 100644 index 3b4e363..0000000 --- a/test.py +++ /dev/null @@ -1,6 +0,0 @@ -from gmssl import * #pylint: disable = e0401 - -sm3 = Sm3() #pylint: disable = e0602 -sm3.update(b'abc') -dgst = sm3.digest() -print("sm3('abc') : " + dgst.hex()) \ No newline at end of file From 7b6e45690e833dce9773804d19a89a8d363bf7fa Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 22:27:14 +0800 Subject: [PATCH 2/7] feat: init client --- src/client.py | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) diff --git a/src/client.py b/src/client.py index e69de29..6138ede 100644 --- a/src/client.py +++ b/src/client.py @@ -0,0 +1,185 @@ +from fastapi import FastAPI, HTTPException +import requests +import os +from typing import Tuple +from tpre import * +import sqlite3 +from contextlib import asynccontextmanager + + +@asynccontextmanager +async def lifespan(app: FastAPI): + init() + yield + clean_env() + + +app = FastAPI(lifespan=lifespan) + +pk = point +sk = int + + +def init(): + global pk, sk + init_db() + pk, sk = GenerateKeyPair() + get_node_list(6) + + +def init_db(): + with sqlite3.connect("client.db") as db: + # message table + db.execute( + """ + CREATE TABLE IF NOT EXISTS message ( + id INTEGER PRIMARY KEY, + capsule TEXT, + ct TEXT, + senderip TEXT + ); + """ + ) + + # node ip table + db.execute( + """ + CREATE TABLE IF NOT EXISTS node ( + id INTEGER PRIMARY KEY, + nodeip TEXT + ); + """ + ) + + # sender info table + db.execute( + """ + CREATE TABLE IF NOT EXISTS senderinfo ( + id INTEGER PRIMARY KEY, + ip TEXT, + publickey TEXT, + threshold INTEGER + ) + """ + ) + db.commit() + print("Init Database Successful") + + +# execute on exit +def clean_env(): + print("Exit app") + + +# main page +@app.get("/") +async def read_root(): + return {"message": "Hello, World!"} + + +# receive messages from node +@app.post("/receive_messages") +async def receive_messages(C: Tuple[capsule, int], ip: str): + """ + receive capsule and ip from nodes + params: + C: capsule and ct + ip: sender ip + return: + + """ + if not C or not ip: + raise HTTPException(status_code=400, detail="Invalid input data") + + capsule, ct = C + if not Checkcapsule(capsule): + raise HTTPException(status_code=400, detail="Invalid capsule") + + # insert record into database + with sqlite3.connect("message.db") as db: + try: + db.execute( + "INSERT INTO message (capsule_column, ct_column, ip_column) VALUES (?, ?, ?)", + (capsule, ct, ip), + ) + db.commit() + await check_merge(db, ct, ip) + return HTTPException(status_code=200, detail="Message received") + except Exception as e: + print(f"Error occurred: {e}") + db.rollback() + return HTTPException(status_code=400, detail="Database error") + + +# check record count +async def check_merge(db, ct: int, ip: str): + global sk, pk + # Check if the combination of ct_column and ip_column appears more than once. + cursor = db.execute( + """ + SELECT capsule, ct + FROM message + WHERE ct = ? AND senderip = ? + """, + (ct, ip), + ) + # [(capsule, ct), ...] + cfrag_cts = cursor.fetchall() + + # get N + cursor = db.execute( + """ + SELECT publickey, threshold + FROM senderinfo + WHERE senderip = ? + """, + (ip), + ) + result = cursor.fetchall() + pk_sender, T = result[0] + if len(cfrag_cts) >= T: + cfrags = mergecfrag(cfrag_cts) + m = DecryptFrags(sk, pk, pk_sender, cfrags) # type: ignore + + +# send message to node +@app.post("/send_message") +async def send_message(ip: tuple[str, ...]): + return 0 + + +# request message from others +@app.post("/request_message") +async def request_message(ip): + return 0 + +# get node list from central server +def get_node_list(count: int): + server_addr = "" + url = "http://" + server_addr + "/server/send_nodes_list" + payload = {"count": count} + response = requests.post(url, json=payload) + # Checking the response + if response.status_code == 200: + print("Success get node list") + node_ip = response.text + # insert node ip to database + with sqlite3.connect("client.db") as db: + db.executemany( + """ + INSERT INTO node + nodeip + VALUE (?) + """, + node_ip, + ) + db.commit() + print("Success add node ip") + else: + print("Failed:", response.status_code, response.text) + + +if __name__ == "__main__": + import uvicorn # pylint: disable=e0401 + + uvicorn.run("client:app", host="0.0.0.0", port=8000) From 459b03c8728107a617f8cedac0ea17e96f46bb31 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 21 Oct 2023 14:39:37 +0800 Subject: [PATCH 3/7] feat: add init config --- src/client.ini | 3 +++ src/client.py | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 src/client.ini diff --git a/src/client.ini b/src/client.ini new file mode 100644 index 0000000..04c5027 --- /dev/null +++ b/src/client.ini @@ -0,0 +1,3 @@ +[settings] +server_address = "127.0.0.1:8000" +version = 1.0 diff --git a/src/client.py b/src/client.py index 6138ede..28e4f69 100644 --- a/src/client.py +++ b/src/client.py @@ -18,13 +18,15 @@ app = FastAPI(lifespan=lifespan) pk = point sk = int +server_address = str def init(): - global pk, sk + global pk, sk, server_address init_db() pk, sk = GenerateKeyPair() - get_node_list(6) + init_config() + get_node_list(6, server_address) # type: ignore def init_db(): @@ -66,6 +68,16 @@ def init_db(): print("Init Database Successful") +def init_config(): + import configparser + + global server_address + config = configparser.ConfigParser() + config.read("client.ini") + + server_address = config["settings"]["server_address"] + + # execute on exit def clean_env(): print("Exit app") @@ -86,7 +98,7 @@ async def receive_messages(C: Tuple[capsule, int], ip: str): C: capsule and ct ip: sender ip return: - + status_code """ if not C or not ip: raise HTTPException(status_code=400, detail="Invalid input data") @@ -153,9 +165,9 @@ async def send_message(ip: tuple[str, ...]): async def request_message(ip): return 0 + # get node list from central server -def get_node_list(count: int): - server_addr = "" +def get_node_list(count: int, server_addr: str): url = "http://" + server_addr + "/server/send_nodes_list" payload = {"count": count} response = requests.post(url, json=payload) @@ -182,4 +194,4 @@ def get_node_list(count: int): if __name__ == "__main__": import uvicorn # pylint: disable=e0401 - uvicorn.run("client:app", host="0.0.0.0", port=8000) + uvicorn.run("client:app", host="0.0.0.0", port=8003) From 40c375bf98b761afc2a30e3d2de13887d01b2e70 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 21 Oct 2023 20:25:18 +0800 Subject: [PATCH 4/7] feat: update feature --- src/client.py | 108 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 93 insertions(+), 15 deletions(-) diff --git a/src/client.py b/src/client.py index 28e4f69..7044b5f 100644 --- a/src/client.py +++ b/src/client.py @@ -5,6 +5,10 @@ from typing import Tuple from tpre import * import sqlite3 from contextlib import asynccontextmanager +from pydantic import BaseModel +import socket +import random +import time @asynccontextmanager @@ -19,6 +23,8 @@ app = FastAPI(lifespan=lifespan) pk = point sk = int server_address = str +node_response = False +message = bytes def init(): @@ -26,7 +32,7 @@ def init(): init_db() pk, sk = GenerateKeyPair() init_config() - get_node_list(6, server_address) # type: ignore + # get_node_list(6, server_address) # type: ignore def init_db(): @@ -68,6 +74,7 @@ def init_db(): print("Init Database Successful") +# load config from config file def init_config(): import configparser @@ -89,9 +96,14 @@ async def read_root(): return {"message": "Hello, World!"} +class C(BaseModel): + Tuple: Tuple[capsule, int] + ip: str + + # receive messages from node @app.post("/receive_messages") -async def receive_messages(C: Tuple[capsule, int], ip: str): +async def receive_messages(message: C): """ receive capsule and ip from nodes params: @@ -100,22 +112,32 @@ async def receive_messages(C: Tuple[capsule, int], ip: str): return: status_code """ - if not C or not ip: + C_tuple = message.Tuple + + ip = message.ip + if not C_tuple or not ip: raise HTTPException(status_code=400, detail="Invalid input data") - capsule, ct = C - if not Checkcapsule(capsule): + C_capsule = C_tuple[0] + C_ct = C_tuple[1] + + if not Checkcapsule(C_capsule): raise HTTPException(status_code=400, detail="Invalid capsule") # insert record into database with sqlite3.connect("message.db") as db: try: db.execute( - "INSERT INTO message (capsule_column, ct_column, ip_column) VALUES (?, ?, ?)", - (capsule, ct, ip), + """ + INSERT INTO message + (capsule_column, ct_column, ip_column) + VALUES + (?, ?, ?) + """, + (C_capsule, C_ct, ip), ) db.commit() - await check_merge(db, ct, ip) + await check_merge(db, C_ct, ip) return HTTPException(status_code=200, detail="Message received") except Exception as e: print(f"Error occurred: {e}") @@ -125,7 +147,7 @@ async def receive_messages(C: Tuple[capsule, int], ip: str): # check record count async def check_merge(db, ct: int, ip: str): - global sk, pk + global sk, pk, node_response, message # Check if the combination of ct_column and ip_column appears more than once. cursor = db.execute( """ @@ -151,19 +173,75 @@ async def check_merge(db, ct: int, ip: str): pk_sender, T = result[0] if len(cfrag_cts) >= T: cfrags = mergecfrag(cfrag_cts) - m = DecryptFrags(sk, pk, pk_sender, cfrags) # type: ignore + message = DecryptFrags(sk, pk, pk_sender, cfrags) # type: ignore + node_response = True # send message to node -@app.post("/send_message") -async def send_message(ip: tuple[str, ...]): +def send_message(ip: tuple[str, ...]): return 0 +class IP_Message(BaseModel): + dest_ip: str + message_name: str + source_ip: str + + # request message from others @app.post("/request_message") -async def request_message(ip): - return 0 +async def request_message(i_m: IP_Message): + global message, node_response + dest_ip = i_m.dest_ip + message_name = i_m.message_name + source_ip = get_own_ip() + dest_port = "8003" + url = "http://" + dest_ip + dest_port + "/recieve_request" + payload = {"dest_ip": dest_ip, "message_name": message_name, "source_ip": source_ip} + response = requests.post(url, json=payload) + if response.status_code == 200: + data = response.json() + public_key = int(data["public_key"]) + threshold = int(data["threshold"]) + with sqlite3.connect("client.db") as db: + db.execute( + """ + INSERT INTO senderinfo + (public_key, threshold) + VALUES + (?, ?) + """, + (public_key, threshold), + ) + + # wait to recieve message from nodes + for _ in range(10): + if node_response: + data = message + message = b"" + # return message to frontend + return {"message": data} + time.sleep(1) + + +# recieve request from others +@app.post("/recieve_request") +async def recieve_request(i_m: IP_Message): + global pk + source_ip = get_own_ip() + if source_ip != i_m.dest_ip: + return HTTPException(status_code=400, detail="Wrong ip") + dest_ip = i_m.source_ip + threshold = random.randrange(1, 6) + public_key = pk + response = {"threshold": threshold,"public_key": public_key} + return response + + +def get_own_ip() -> str: + hostname = socket.gethostname() + ip = socket.gethostbyname(hostname) + return ip # get node list from central server @@ -194,4 +272,4 @@ def get_node_list(count: int, server_addr: str): if __name__ == "__main__": import uvicorn # pylint: disable=e0401 - uvicorn.run("client:app", host="0.0.0.0", port=8003) + uvicorn.run("client:app", host="0.0.0.0", port=8003, reload="True") From 6efb7179bb5da51282ca0a56d3d9c3c1b7e79676 Mon Sep 17 00:00:00 2001 From: dqy <1016751306@qq.com> Date: Sat, 21 Oct 2023 20:25:40 +0800 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=E6=8F=90=E4=BA=A4server.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/server.py | 131 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index cf2b89c..ed0d000 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ ReEncrypt.py src/temp_message_file src/temp_key_file src/client.db +src/server.db diff --git a/src/server.py b/src/server.py index 5a25696..30481fe 100644 --- a/src/server.py +++ b/src/server.py @@ -1,44 +1,137 @@ from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse +from contextlib import asynccontextmanager from typing import Tuple, Callable +import sqlite3 +import asyncio +import time +import random -app = FastAPI() +@asynccontextmanager +async def lifespan(app: FastAPI): + init() + yield + clean_env() + +app = FastAPI(lifespan = lifespan) + +# 连接到数据库(如果数据库不存在,则会自动创建) +conn = sqlite3.connect('server.db') +# 创建游标对象,用于执行SQL语句 +cursor = conn.cursor() +# 创建表: id: int; ip: TEXT +cursor.execute('''CREATE TABLE IF NOT EXISTS nodes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + ip TEXT NOT NULL, + last_heartbeat INTEGER + )''') + +def init(): + nothing = receive_heartbeat_internal() + +def clean_env(): + # 关闭游标和连接 + cursor.close() + conn.close() + +@app.get("/server/show_nodes") +async def show_nodes() -> list: + nodes_list = [] + # 查询数据 + cursor.execute("SELECT * FROM nodes") + rows = cursor.fetchall() + for row in rows: + nodes_list.append(row) + return nodes_list @app.get("/server/get_node") async def get_node(ip: str) -> int: ''' - 中心服务器与节点交互, 节点发送ip, 中心服务器接收ip存入数据库并将ip转换为int作为节点id返回给节点 - params: - ip: node ip - return: - id: ip按点分割成四部分, 每部分转二进制后拼接再转十进制作为节点id + 中心服务器与节点交互, 节点发送ip, 中心服务器接收ip存入数据库并将ip转换为int作为节点id返回给节点 + params: + ip: node ip + return: + id: ip按点分割成四部分, 每部分转二进制后拼接再转十进制作为节点id ''' - # ip存入数据库, id = hash(int(ip)) - ip_parts = ip.split(".") ip_int = 0 for i in range(4): ip_int += int(ip_parts[i]) << (24 - (8 * i)) + + # 获取当前时间 + current_time = int(time.time()) + + # 插入数据 + cursor.execute("INSERT INTO nodes (id, ip, last_heartbeat) VALUES (?, ?, ?)", (ip_int, ip, current_time)) + conn.commit() + return ip_int @app.get("/server/delete_node") async def delete_node(ip: str) -> None: - # 按照节点ip遍历数据库, 删除该行数据 + ''' + param: + ip: 待删除节点的ip地址 + return: + None + ''' + # 查询要删除的节点 + cursor.execute("SELECT * FROM nodes WHERE ip=?", (ip,)) + row = cursor.fetchone() + if row is not None: + # 执行删除操作 + cursor.execute("DELETE FROM nodes WHERE ip=?", (ip,)) + conn.commit() + print(f"Node with IP {ip} deleted successfully.") + else: + print(f"Node with IP {ip} not found.") -@app.post("/server/send_nodes_list") +# 接收节点心跳包 +@app.post("/server/heartbeat") +async def receive_heartbeat(ip: str): + cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)) + return {"status": "received"} + +async def receive_heartbeat_internal() -> int: + while 1: + print('successful delete1') + timeout = 10 + # 删除超时的节点 + cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) + conn.commit() + print('successful delete') + time.sleep(timeout) + return 1 + +@app.get("/server/send_nodes_list") async def send_nodes_list(count: int) -> JSONResponse: ''' 中心服务器与客户端交互, 客户端发送所需节点个数, 中心服务器从数据库中顺序取出节点封装成json格式返回给客户端 - params: - count: 所需节点个数 - return: - JSONResponse: {id: ip,...} + params: + count: 所需节点个数 + return: + JSONResponse: {id: ip,...} ''' nodes_list = {} - for i in range(count): - # 访问数据库取出节点数据 - node = (id, ip) - nodes_list[node[0]] = node[1] + + # 查询数据库中的节点数据 + cursor.execute("SELECT * FROM nodes LIMIT ?", (count,)) + rows = cursor.fetchall() + + for row in rows: + id, ip, last_heartbeat = row + nodes_list[id] = ip + json_result = jsonable_encoder(nodes_list) - return JSONResponse(content=json_result) \ No newline at end of file + return JSONResponse(content=json_result) + +@app.get("/server/clear_database") +async def clear_database() -> None: + cursor.execute("DELETE FROM nodes") + conn.commit() + +if __name__ == "__main__": + import uvicorn # pylint: disable=e0401 + + uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True) \ No newline at end of file From 8a049fbe6f10ef59948cff48e1b478aabcda7860 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 21 Oct 2023 20:31:23 +0800 Subject: [PATCH 6/7] feat: finish timer --- src/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.py b/src/server.py index 30481fe..d030e2c 100644 --- a/src/server.py +++ b/src/server.py @@ -28,7 +28,7 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS nodes ( )''') def init(): - nothing = receive_heartbeat_internal() + task = asyncio.create_task(receive_heartbeat_internal()) def clean_env(): # 关闭游标和连接 @@ -101,7 +101,7 @@ async def receive_heartbeat_internal() -> int: cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) conn.commit() print('successful delete') - time.sleep(timeout) + await asyncio.sleep(timeout) return 1 @app.get("/server/send_nodes_list") From 4f2dd9727e5eeef260767409e25fffc39776ed4e Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Sat, 21 Oct 2023 20:39:43 +0800 Subject: [PATCH 7/7] feat: add heartbeat package --- src/node.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/node.py b/src/node.py index 1f08167..c69be47 100644 --- a/src/node.py +++ b/src/node.py @@ -68,3 +68,52 @@ async def send_message(message: str): data = {"message": processed_message} response = requests.post(url, data=data) return response.json() + + +import requests + +def send_heartbeat(url: str) -> bool: + try: + response = requests.get(url, timeout=5) # 使用 GET 方法作为心跳请求 + response.raise_for_status() # 检查响应是否为 200 OK + + # 可选:根据响应内容进行进一步验证 + # if response.json() != expected_response: + # return False + + return True + except requests.RequestException: + return False + +# 使用方式 +url = "https://your-service-url.com/heartbeat" +if send_heartbeat(url): + print("Service is alive!") +else: + print("Service might be down or unreachable.") + + +import asyncio +from contextlib import asynccontextmanager +from fastapi import FastAPI + +async def receive_heartbeat_internal() -> int: + while True: + print('successful delete1') + timeout = 10 + # 删除超时的节点(假设你有一个异步的数据库操作函数) + await async_cursor_execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) + await async_conn_commit() + print('successful delete') + await asyncio.sleep(timeout) + + return 1 + +@asynccontextmanager +async def lifespan(app: FastAPI): + task = asyncio.create_task(receive_heartbeat_internal()) + yield + task.cancel() # 取消我们之前创建的任务 + await clean_env() # 假设这是一个异步函数 + +# 其他FastAPI应用的代码...