diff --git a/.gitignore b/.gitignore index 0a684e6ac..b8fb9f8ea 100644 --- a/.gitignore +++ b/.gitignore @@ -158,4 +158,7 @@ my_wakeup_words.mp3 main/manager-api/.vscode # Ignore webpack cache directory -main/manager-web/.webpack_cache/ \ No newline at end of file +main/manager-web/.webpack_cache/ + +# SSL +main/xiaozhi-server/ssl/ \ No newline at end of file diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 60ab7ab43..6851f069d 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -8,6 +8,10 @@ server: # 服务器监听地址和端口(Server listening address and port) ip: 0.0.0.0 port: 8000 + # SSL + use_ssl: false + ssl_cert: "ssl/cert.pem" + ssl_key: "ssl/key.pem" # 认证配置 auth: # 是否启用认证 diff --git a/main/xiaozhi-server/core/websocket_server.py b/main/xiaozhi-server/core/websocket_server.py index 98b601339..cfe06d43a 100644 --- a/main/xiaozhi-server/core/websocket_server.py +++ b/main/xiaozhi-server/core/websocket_server.py @@ -1,5 +1,7 @@ import asyncio import websockets +import ssl +import os from config.logger import setup_logging from core.connection import ConnectionHandler from core.utils.util import get_local_ip @@ -86,9 +88,36 @@ class WebSocketServer: host = server_config["ip"] port = server_config["port"] - self.logger.bind(tag=TAG).info( - "Server is running at ws://{}:{}/xiaozhi/v1/", get_local_ip(), port - ) + # 检查是否启用SSL + use_ssl = server_config.get("use_ssl", False) + ssl_context = None + + if use_ssl: + # 创建SSL上下文 + ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + + # 配置证书和密钥路径 + cert_path = server_config.get("ssl_cert", "cert.pem") + key_path = server_config.get("ssl_key", "key.pem") + + # 确保证书文件存在 + if not os.path.exists(cert_path) or not os.path.exists(key_path): + self.logger.bind(tag=TAG).error( + "SSL证书文件不存在: {} 或 {}", cert_path, key_path + ) + self.logger.bind(tag=TAG).info("使用非加密WebSocket连接继续...") + else: + # 加载证书和密钥 + ssl_context.load_cert_chain(cert_path, key_path) + + self.logger.bind(tag=TAG).info( + "Server is running at wss://{}:{}/xiaozhi/v1/", get_local_ip(), port + ) + else: + self.logger.bind(tag=TAG).info( + "Server is running at ws://{}:{}/xiaozhi/v1/", get_local_ip(), port + ) + self.logger.bind(tag=TAG).info( "=======上面的地址是websocket协议地址,请勿用浏览器访问=======" ) @@ -98,7 +127,9 @@ class WebSocketServer: self.logger.bind(tag=TAG).info( "=============================================================\n" ) - async with websockets.serve(self._handle_connection, host, port): + + # 使用SSL上下文启动服务器 + async with websockets.serve(self._handle_connection, host, port, ssl=ssl_context): await asyncio.Future() async def _handle_connection(self, websocket):