Skip to content

Commit

Permalink
Merge pull request #26 from ITCraftDevelopmentTeam/access-token-warning
Browse files Browse the repository at this point in the history
在部分连接添加部分情况下未配置 Access Token 的警告
  • Loading branch information
This-is-XiaoDeng authored Jan 30, 2024
2 parents 5622e14 + 20f3c27 commit 147f875
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions network/v11/http.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
from network.authentication import verify_access_token
import utils.uvicorn_server as uvicorn_server
import fastapi
from utils.logger import get_logger
import call_action

BASE_CONFIG = {
"host": "0.0.0.0",
"port": 5700,
"access_token": None
}
logger = get_logger()

class HttpServer:

def __init__(self, config: dict) -> None:
self.config = BASE_CONFIG | config
self.app = fastapi.FastAPI()
self.app.add_route("/{action}", self.handle_request, ["GET", "POST"])
self.check_access_token()

def check_access_token(self) -> None:
if self.config["host"] == "0.0.0.0" or self.config["access_token"]:
logger.warning(f'[{self.config["host"]}:{self.config["port"]}] 未配置 Access Token !')

async def start_server(self) -> None:
await uvicorn_server.run(self.app, host=self.config["host"], port=self.config["port"])
Expand Down
5 changes: 5 additions & 0 deletions network/v11/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def __init__(self, config: dict) -> None:
self.app.add_websocket_route("/", self.handle_root_route)
self.app.add_websocket_route("/event", self.handle_event_route)
self.app.add_websocket_route("/api", self.handle_api_route)
self.check_access_token()

def check_access_token(self) -> None:
if self.config["host"] == "0.0.0.0" or self.config["access_token"]:
logger.warning(f'[{self.config["host"]}:{self.config["port"]}] 未配置 Access Token !')

async def start(self) -> None:
await uvicorn_server.run(
Expand Down
7 changes: 6 additions & 1 deletion network/v12/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ def __init__(self, config: dict) -> None:
self.event_list = []

if self.config["event_enabled"] and self.config["event_buffer_size"] <= 0:
logger.warning("警告: 事件缓冲区大小配置不正确,可能导致内存泄露!")
logger.warning(f"[{self.config['host']}:{self.config['port']}] 事件缓冲区大小配置不正确,可能导致内存泄露!")

self.app = fastapi.FastAPI()
self.app.add_route("/", self.handle_http_connection, ["post"])
self.check_access_token()

def check_access_token(self) -> None:
if self.config["host"] == "0.0.0.0" or self.config["access_token"]:
logger.warning(f'[HTTP {self.config["host"]}:{self.config["port"]}] 未配置 Access Token !')

async def start_server(self):
await uvicorn_server.run(self.app, self.config["port"], self.config["host"])
Expand Down
5 changes: 5 additions & 0 deletions network/v12/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def __init__(self, config: dict) -> None:
self.clients: list[fastapi.WebSocket] = []
self.app = fastapi.FastAPI()
self.app.add_websocket_route("/", self.handle_ws_connect)
self.check_access_token()

def check_access_token(self) -> None:
if self.config["host"] == "0.0.0.0" or self.config["access_token"]:
logger.warning(f'[{self.config["host"]}:{self.config["port"]}] 未配置 Access Token !')

async def start_server(self) -> None:
await uvicorn_server.run(self.app, self.config["port"], self.config["host"])
Expand Down

0 comments on commit 147f875

Please sign in to comment.