This commit is contained in:
parent
09fa3e589e
commit
31324756df
@ -5,6 +5,7 @@ from contextlib import asynccontextmanager
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import asyncio
|
import asyncio
|
||||||
import time
|
import time
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
@ -54,6 +55,14 @@ async def show_nodes() -> list:
|
|||||||
return nodes_list
|
return nodes_list
|
||||||
|
|
||||||
|
|
||||||
|
def validate_ip(ip: str) -> bool:
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(ip)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
@app.get("/server/get_node")
|
@app.get("/server/get_node")
|
||||||
async def get_node(ip: str) -> int:
|
async def get_node(ip: str) -> int:
|
||||||
"""
|
"""
|
||||||
@ -63,6 +72,10 @@ async def get_node(ip: str) -> int:
|
|||||||
return:
|
return:
|
||||||
id: ip按点分割成四部分, 每部分转二进制后拼接再转十进制作为节点id
|
id: ip按点分割成四部分, 每部分转二进制后拼接再转十进制作为节点id
|
||||||
"""
|
"""
|
||||||
|
if not validate_ip(ip):
|
||||||
|
content = {"message": "invalid ip "}
|
||||||
|
return JSONResponse(content, status_code=400) # type: ignore
|
||||||
|
|
||||||
ip_parts = ip.split(".")
|
ip_parts = ip.split(".")
|
||||||
ip_int = 0
|
ip_int = 0
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
@ -92,6 +105,7 @@ async def delete_node(ip: str) -> None:
|
|||||||
return:
|
return:
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with sqlite3.connect("server.db") as db:
|
with sqlite3.connect("server.db") as db:
|
||||||
# 查询要删除的节点
|
# 查询要删除的节点
|
||||||
cursor = db.execute("SELECT * FROM nodes WHERE ip=?", (ip,))
|
cursor = db.execute("SELECT * FROM nodes WHERE ip=?", (ip,))
|
||||||
@ -109,6 +123,9 @@ async def delete_node(ip: str) -> None:
|
|||||||
# 接收节点心跳包
|
# 接收节点心跳包
|
||||||
@app.get("/server/heartbeat")
|
@app.get("/server/heartbeat")
|
||||||
async def receive_heartbeat(ip: str):
|
async def receive_heartbeat(ip: str):
|
||||||
|
if not validate_ip(ip):
|
||||||
|
content = {"message": "invalid ip "}
|
||||||
|
return JSONResponse(content, status_code=400)
|
||||||
print("收到来自", ip, "的心跳包")
|
print("收到来自", ip, "的心跳包")
|
||||||
with sqlite3.connect("server.db") as db:
|
with sqlite3.connect("server.db") as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user