From 893ee0ebb88a078f5e17130f653a65f6b0210d51 Mon Sep 17 00:00:00 2001 From: Xiaotong <55253936+zixixr@users.noreply.github.com> Date: Wed, 26 Mar 2025 00:56:49 +0800 Subject: [PATCH] WebSocket log info fix add: implement get_public_ip function and log public IP in WebSocket server For those who implement the service on the cloud, this should show the public IP in the log. --- main/xiaozhi-server/core/utils/util.py | 6 ++++++ main/xiaozhi-server/core/websocket_server.py | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index 63cb5b9c9..bc487d914 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -23,6 +23,12 @@ def get_local_ip(): return local_ip except Exception as e: return "127.0.0.1" + +def get_public_ip(): + try: + return requests.get("https://api.ipify.org", timeout=2).text + except Exception as e: + return "unknown" def is_private_ip(ip_addr): """ diff --git a/main/xiaozhi-server/core/websocket_server.py b/main/xiaozhi-server/core/websocket_server.py index 41f145157..1751c7b2c 100644 --- a/main/xiaozhi-server/core/websocket_server.py +++ b/main/xiaozhi-server/core/websocket_server.py @@ -3,6 +3,7 @@ import websockets from config.logger import setup_logging from core.connection import ConnectionHandler from core.utils.util import get_local_ip +from core.utils.util import get_public_ip from core.utils import asr, vad, llm, tts, memory, intent TAG = __name__ @@ -66,7 +67,8 @@ class WebSocketServer: selected_module = self.config.get("selected_module") self.logger.bind(tag=TAG).info(f"selected_module values: {', '.join(selected_module.values())}") - self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port) + self.logger.bind(tag=TAG).info("Server is running locally at ws://{}:{}",get_local_ip(), port) + self.logger.bind(tag=TAG).info("Server is running publicly at ws://{}:{}", get_public_ip(), port) self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======") async with websockets.serve( self._handle_connection,