forked from sangge/tpre-python
modified: src/client.ini
modified: src/client.py modified: src/node.py
This commit is contained in:
parent
55add91467
commit
a45cd66e9d
@ -1,3 +1,3 @@
|
|||||||
[settings]
|
[settings]
|
||||||
server_address = 10.20.127.226:8000
|
server_address = 10.20.14.232:8000
|
||||||
version = 1.0
|
version = 1.0
|
||||||
|
@ -17,6 +17,7 @@ async def lifespan(app: FastAPI):
|
|||||||
yield
|
yield
|
||||||
clean_env()
|
clean_env()
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ def init():
|
|||||||
init_db()
|
init_db()
|
||||||
pk, sk = GenerateKeyPair()
|
pk, sk = GenerateKeyPair()
|
||||||
init_config()
|
init_config()
|
||||||
get_node_list(6, server_address) # type: ignore
|
get_node_list(2, server_address) # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
@ -93,6 +94,7 @@ class C(BaseModel):
|
|||||||
Tuple: Tuple[capsule, int]
|
Tuple: Tuple[capsule, int]
|
||||||
ip: str
|
ip: str
|
||||||
|
|
||||||
|
|
||||||
# receive messages from node
|
# receive messages from node
|
||||||
@app.post("/receive_messages")
|
@app.post("/receive_messages")
|
||||||
async def receive_messages(message: C):
|
async def receive_messages(message: C):
|
||||||
@ -170,12 +172,9 @@ async def check_merge(db, ct: int, ip: str):
|
|||||||
|
|
||||||
|
|
||||||
# send message to node
|
# send message to node
|
||||||
async def send_messages(node_ips: tuple[str, ...],
|
async def send_messages(
|
||||||
message: bytes,
|
node_ips: tuple[str, ...], message: bytes, dest_ip: str, pk_B: point, shreshold: int
|
||||||
dest_ip: str,
|
):
|
||||||
pk_B: point,
|
|
||||||
shreshold: int
|
|
||||||
):
|
|
||||||
global pk, sk
|
global pk, sk
|
||||||
id_list = []
|
id_list = []
|
||||||
for node_ip in node_ips:
|
for node_ip in node_ips:
|
||||||
@ -184,14 +183,14 @@ async def send_messages(node_ips: tuple[str, ...],
|
|||||||
for i in range(4):
|
for i in range(4):
|
||||||
id += int(ip_parts[i]) << (24 - (8 * i))
|
id += int(ip_parts[i]) << (24 - (8 * i))
|
||||||
id_list.append(id)
|
id_list.append(id)
|
||||||
rk_list = GenerateReKey(sk, pk_B, len(node_ips), shreshold, tuple(id_list)) # type: ignore
|
rk_list = GenerateReKey(sk, pk_B, len(node_ips), shreshold, tuple(id_list)) # type: ignore
|
||||||
for i in range(len(node_ips)):
|
for i in range(len(node_ips)):
|
||||||
url = "http://" + node_ips[i] + ":8001" + "/recieve_message"
|
url = "http://" + node_ips[i] + ":8001" + "/recieve_message"
|
||||||
payload = {
|
payload = {
|
||||||
"source_ip": local_ip,
|
"source_ip": local_ip,
|
||||||
"dest_ip": dest_ip,
|
"dest_ip": dest_ip,
|
||||||
"message": message,
|
"message": message,
|
||||||
"rk": rk_list[i]
|
"rk": rk_list[i],
|
||||||
}
|
}
|
||||||
response = requests.post(url, json=payload)
|
response = requests.post(url, json=payload)
|
||||||
return 0
|
return 0
|
||||||
@ -203,6 +202,7 @@ class IP_Message(BaseModel):
|
|||||||
source_ip: str
|
source_ip: str
|
||||||
pk: int
|
pk: int
|
||||||
|
|
||||||
|
|
||||||
# request message from others
|
# request message from others
|
||||||
@app.post("/request_message")
|
@app.post("/request_message")
|
||||||
async def request_message(i_m: IP_Message):
|
async def request_message(i_m: IP_Message):
|
||||||
@ -212,11 +212,12 @@ async def request_message(i_m: IP_Message):
|
|||||||
source_ip = get_own_ip()
|
source_ip = get_own_ip()
|
||||||
dest_port = "8003"
|
dest_port = "8003"
|
||||||
url = "http://" + dest_ip + dest_port + "/recieve_request"
|
url = "http://" + dest_ip + dest_port + "/recieve_request"
|
||||||
payload = {"dest_ip": dest_ip,
|
payload = {
|
||||||
"message_name": message_name,
|
"dest_ip": dest_ip,
|
||||||
"source_ip": source_ip,
|
"message_name": message_name,
|
||||||
"pk": pk
|
"source_ip": source_ip,
|
||||||
}
|
"pk": pk,
|
||||||
|
}
|
||||||
response = requests.post(url, json=payload)
|
response = requests.post(url, json=payload)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
@ -224,7 +225,7 @@ async def request_message(i_m: IP_Message):
|
|||||||
threshold = int(data["threshold"])
|
threshold = int(data["threshold"])
|
||||||
with sqlite3.connect("client.db") as db:
|
with sqlite3.connect("client.db") as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO senderinfo
|
INSERT INTO senderinfo
|
||||||
(public_key, threshold)
|
(public_key, threshold)
|
||||||
VALUES
|
VALUES
|
||||||
@ -255,17 +256,20 @@ async def recieve_request(i_m: IP_Message):
|
|||||||
threshold = random.randrange(1, 6)
|
threshold = random.randrange(1, 6)
|
||||||
own_public_key = pk
|
own_public_key = pk
|
||||||
pk_B = i_m.pk
|
pk_B = i_m.pk
|
||||||
|
|
||||||
with sqlite3.connect("client.db") as db:
|
with sqlite3.connect("client.db") as db:
|
||||||
cursor = db.execute("""
|
cursor = db.execute(
|
||||||
|
"""
|
||||||
SELECT nodeip
|
SELECT nodeip
|
||||||
FROM node
|
FROM node
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
""",(threshold,))
|
""",
|
||||||
|
(threshold,),
|
||||||
|
)
|
||||||
node_ips = cursor.fetchall()
|
node_ips = cursor.fetchall()
|
||||||
message = b"hello world" + random.randbytes(8)
|
message = b"hello world" + random.randbytes(8)
|
||||||
await send_messages(node_ips, message, dest_ip, pk_B, threshold) # type: ignore
|
await send_messages(node_ips, message, dest_ip, pk_B, threshold) # type: ignore
|
||||||
response = {"threshold": threshold,"public_key": own_public_key}
|
response = {"threshold": threshold, "public_key": own_public_key}
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
@ -303,7 +307,6 @@ def get_node_list(count: int, server_addr: str):
|
|||||||
print("Failed:", response.status_code, response.text)
|
print("Failed:", response.status_code, response.text)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pk = point
|
pk = point
|
||||||
sk = int
|
sk = int
|
||||||
server_address = str
|
server_address = str
|
||||||
@ -314,4 +317,4 @@ local_ip = get_own_ip()
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn # pylint: disable=e0401
|
import uvicorn # pylint: disable=e0401
|
||||||
|
|
||||||
uvicorn.run("client:app", host="0.0.0.0", port=8003, reload=True)
|
uvicorn.run("client:app", host="0.0.0.0", port=8002, reload=True)
|
||||||
|
56
src/node.py
56
src/node.py
@ -1,4 +1,4 @@
|
|||||||
from fastapi import FastAPI,Request
|
from fastapi import FastAPI, Request
|
||||||
import requests
|
import requests
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
import socket
|
import socket
|
||||||
@ -6,6 +6,7 @@ import asyncio
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from tpre import *
|
from tpre import *
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
# Load the ML model
|
# Load the ML model
|
||||||
@ -14,25 +15,28 @@ async def lifespan(app: FastAPI):
|
|||||||
# Clean up the ML models and release the resources
|
# Clean up the ML models and release the resources
|
||||||
clear()
|
clear()
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(lifespan=lifespan)
|
app = FastAPI(lifespan=lifespan)
|
||||||
server_address ="http://中心服务器IP地址/server"
|
server_address = "http://中心服务器IP地址/server"
|
||||||
id = 0
|
id = 0
|
||||||
ip = ''
|
ip = ""
|
||||||
client_ip_src = '' # 发送信息用户的ip
|
client_ip_src = "" # 发送信息用户的ip
|
||||||
client_ip_des = '' # 接收信息用户的ip
|
client_ip_des = "" # 接收信息用户的ip
|
||||||
processed_message = () # 重加密后的数据
|
processed_message = () # 重加密后的数据
|
||||||
|
|
||||||
# class C(BaseModel):
|
# class C(BaseModel):
|
||||||
# Tuple: Tuple[capsule, int]
|
# Tuple: Tuple[capsule, int]
|
||||||
# ip_src: str
|
# ip_src: str
|
||||||
|
|
||||||
|
|
||||||
# 向中心服务器发送自己的IP地址,并获取自己的id
|
# 向中心服务器发送自己的IP地址,并获取自己的id
|
||||||
def send_ip():
|
def send_ip():
|
||||||
url = server_address + '/get_node?ip = ' + ip
|
url = server_address + "/get_node?ip = " + ip
|
||||||
# ip = get_local_ip # type: ignore
|
# ip = get_local_ip # type: ignore
|
||||||
global id
|
global id
|
||||||
id = requests.get(url)
|
id = requests.get(url)
|
||||||
|
|
||||||
|
|
||||||
# 用socket获取本机ip
|
# 用socket获取本机ip
|
||||||
def get_local_ip():
|
def get_local_ip():
|
||||||
# 创建一个套接字对象
|
# 创建一个套接字对象
|
||||||
@ -42,7 +46,7 @@ def get_local_ip():
|
|||||||
# 获取本地IP地址
|
# 获取本地IP地址
|
||||||
local_ip = s.getsockname()[0]
|
local_ip = s.getsockname()[0]
|
||||||
s.close()
|
s.close()
|
||||||
global ip
|
global ip
|
||||||
ip = local_ip
|
ip = local_ip
|
||||||
|
|
||||||
|
|
||||||
@ -51,52 +55,46 @@ def init():
|
|||||||
global id
|
global id
|
||||||
send_ip()
|
send_ip()
|
||||||
task = asyncio.create_task(send_heartbeat_internal())
|
task = asyncio.create_task(send_heartbeat_internal())
|
||||||
def clear():
|
|
||||||
|
|
||||||
|
|
||||||
|
def clear():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# 接收用户发来的消息,经过处理之后,再将消息发送给其他用户
|
# 接收用户发来的消息,经过处理之后,再将消息发送给其他用户
|
||||||
|
|
||||||
|
|
||||||
async def send_heartbeat_internal() -> None:
|
async def send_heartbeat_internal() -> None:
|
||||||
while True:
|
while True:
|
||||||
# print('successful send my_heart')
|
# print('successful send my_heart')
|
||||||
global ip
|
global ip
|
||||||
url = server_address + '/get_node?ip = ' + ip
|
url = server_address + "/get_node?ip = " + ip
|
||||||
folderol = requests.get(url)
|
folderol = requests.get(url)
|
||||||
timeout = 30
|
timeout = 30
|
||||||
# 删除超时的节点(假设你有一个异步的数据库操作函数)
|
# 删除超时的节点(假设你有一个异步的数据库操作函数)
|
||||||
await asyncio.sleep(timeout)
|
await asyncio.sleep(timeout)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/user_src") # 接收用户1发送的信息
|
||||||
@app.post("/user_src") # 接收用户1发送的信息
|
|
||||||
async def receive_user_src_message(message: Request):
|
async def receive_user_src_message(message: Request):
|
||||||
json_data = await message.json()
|
json_data = await message.json()
|
||||||
global client_ip_src,client_ip_des
|
global client_ip_src, client_ip_des
|
||||||
# kfrag , capsule_ct ,client_ip_src , client_ip_des = json_data[] # 看梁俊勇
|
# kfrag , capsule_ct ,client_ip_src , client_ip_des = json_data[] # 看梁俊勇
|
||||||
global processed_message
|
global processed_message
|
||||||
processed_message = ReEncrypt(kfrag, capsule_ct)
|
processed_message = ReEncrypt(kfrag, capsule_ct)
|
||||||
|
|
||||||
|
|
||||||
|
def send_user_des_message(): # 发送消息给用户2
|
||||||
|
global processed_message, client_ip_src, client_ip_des
|
||||||
|
|
||||||
def send_user_des_message(): # 发送消息给用户2
|
data = {"Tuple": processed_message, "ip": client_ip_src} # 类型不匹配
|
||||||
global processed_message,client_ip_src,client_ip_des
|
|
||||||
|
|
||||||
data = {
|
# 发送 HTTP POST 请求
|
||||||
"Tuple": processed_message, # 类型不匹配
|
response = requests.post("http://" + client_ip_des + "/receive_messages", json=data)
|
||||||
"ip": client_ip_src
|
|
||||||
}
|
|
||||||
|
|
||||||
# 发送 HTTP POST 请求
|
|
||||||
response = requests.post("http://"+ client_ip_des + "/receive_messages", json=data)
|
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn # pylint: disable=e0401
|
import uvicorn # pylint: disable=e0401
|
||||||
|
|
||||||
uvicorn.run("node:app", host="0.0.0.0", port=8000, reload=True)
|
uvicorn.run("node:app", host="0.0.0.0", port=8001, reload=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user