You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

# P2P Network Communication - Configuration Module
"""
配置模块,包含服务器和客户端的配置参数
"""
import os
from dataclasses import dataclass
from typing import Optional
@dataclass
class ServerConfig:
"""服务器配置"""
host: str = "0.0.0.0"
port: int = 8888
max_connections: int = 1000
heartbeat_interval: int = 30 # seconds
offline_message_ttl: int = 7 * 24 * 3600 # 7 days in seconds
# Database configuration
db_host: str = "localhost"
db_port: int = 3306
db_name: str = "p2p_chat"
db_user: str = "root"
db_password: str = ""
db_pool_size: int = 10
@dataclass
class ClientConfig:
"""客户端配置"""
server_host: str = "113.45.148.222"
server_port: int = 8888
# LAN discovery
lan_broadcast_port: int = 8889
lan_discovery_timeout: float = 2.0 # seconds
# Connection settings
connection_timeout: float = 10.0 # seconds
reconnect_attempts: int = 3
reconnect_delay: float = 1.0 # seconds (will be multiplied: 1s, 2s, 4s)
heartbeat_interval: int = 30 # seconds
# File transfer
chunk_size: int = 64 * 1024 # 64KB per chunk
max_file_size: int = 2 * 1024 * 1024 * 1024 # 2GB max
# Voice chat
audio_sample_rate: int = 16000
audio_channels: int = 1
audio_chunk_duration: float = 0.02 # 20ms per chunk
max_voice_latency: float = 0.3 # 300ms max latency
# UI settings
window_width: int = 1024
window_height: int = 768
thumbnail_size: tuple = (200, 200)
# Local storage
data_dir: str = "data"
cache_dir: str = "cache"
@dataclass
class SecurityConfig:
"""安全配置"""
use_tls: bool = True
cert_file: Optional[str] = None
key_file: Optional[str] = None
# Encryption
encryption_algorithm: str = "AES-256-GCM"
key_derivation: str = "PBKDF2"
key_iterations: int = 100000
def load_server_config() -> ServerConfig:
"""从环境变量加载服务器配置"""
return ServerConfig(
host=os.getenv("P2P_SERVER_HOST", "0.0.0.0"),
port=int(os.getenv("P2P_SERVER_PORT", "8888")),
max_connections=int(os.getenv("P2P_MAX_CONNECTIONS", "1000")),
db_host=os.getenv("P2P_DB_HOST", "localhost"),
db_port=int(os.getenv("P2P_DB_PORT", "3306")),
db_name=os.getenv("P2P_DB_NAME", "p2p_chat"),
db_user=os.getenv("P2P_DB_USER", "root"),
db_password=os.getenv("P2P_DB_PASSWORD", ""),
)
def load_client_config() -> ClientConfig:
"""从环境变量加载客户端配置"""
return ClientConfig(
server_host=os.getenv("P2P_SERVER_HOST", "127.0.0.1"),
server_port=int(os.getenv("P2P_SERVER_PORT", "8888")),
)
def load_security_config() -> SecurityConfig:
"""从环境变量加载安全配置"""
return SecurityConfig(
use_tls=os.getenv("P2P_USE_TLS", "true").lower() == "true",
cert_file=os.getenv("P2P_CERT_FILE"),
key_file=os.getenv("P2P_KEY_FILE"),
)