-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathconfig.py
More file actions
85 lines (68 loc) · 3.1 KB
/
config.py
File metadata and controls
85 lines (68 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
代理服务器配置文件
可以通过环境变量或配置文件覆盖默认设置
"""
import os
from typing import Optional
class Config:
"""配置类"""
# 服务器配置
HOST: str = os.getenv("PROXY_HOST", "0.0.0.0")
PORT: int = int(os.getenv("PROXY_PORT", "8000"))
WORKERS: int = int(os.getenv("PROXY_WORKERS", "1"))
# 连接池配置
MAX_CONNECTIONS: int = int(os.getenv("PROXY_MAX_CONNECTIONS", "100"))
MAX_KEEPALIVE_CONNECTIONS: int = int(os.getenv("PROXY_MAX_KEEPALIVE_CONNECTIONS", "20"))
KEEPALIVE_EXPIRY: float = float(os.getenv("PROXY_KEEPALIVE_EXPIRY", "30.0"))
# 超时配置
CONNECT_TIMEOUT: float = float(os.getenv("PROXY_CONNECT_TIMEOUT", "10.0"))
READ_TIMEOUT: float = float(os.getenv("PROXY_READ_TIMEOUT", "60.0"))
WRITE_TIMEOUT: float = float(os.getenv("PROXY_WRITE_TIMEOUT", "10.0"))
POOL_TIMEOUT: float = float(os.getenv("PROXY_POOL_TIMEOUT", "5.0"))
# 内存监控配置
MEMORY_CHECK_INTERVAL: int = int(os.getenv("PROXY_MEMORY_CHECK_INTERVAL", "30"))
MAX_MEMORY_USAGE: int = int(os.getenv("PROXY_MAX_MEMORY_USAGE", "500"))
CLEANUP_THRESHOLD: int = int(os.getenv("PROXY_CLEANUP_THRESHOLD", "400"))
# 日志配置
LOG_LEVEL: str = os.getenv("PROXY_LOG_LEVEL", "INFO")
LOG_FILE: Optional[str] = os.getenv("PROXY_LOG_FILE", "proxy.log")
# 安全配置
VERIFY_SSL: bool = os.getenv("PROXY_VERIFY_SSL", "false").lower() == "true"
ALLOWED_HOSTS: Optional[str] = os.getenv("PROXY_ALLOWED_HOSTS") # 逗号分隔的主机列表
# 性能配置
CHUNK_SIZE: int = int(os.getenv("PROXY_CHUNK_SIZE", "8192"))
ENABLE_COMPRESSION: bool = os.getenv("PROXY_ENABLE_COMPRESSION", "true").lower() == "true"
@classmethod
def get_allowed_hosts(cls) -> list:
"""获取允许的主机列表"""
if cls.ALLOWED_HOSTS:
return [host.strip() for host in cls.ALLOWED_HOSTS.split(",")]
return ["*"] # 默认允许所有主机
@classmethod
def load_from_file(cls, config_file: str):
"""从配置文件加载配置"""
try:
import json
with open(config_file, 'r', encoding='utf-8') as f:
config_data = json.load(f)
for key, value in config_data.items():
if hasattr(cls, key.upper()):
setattr(cls, key.upper(), value)
except FileNotFoundError:
pass # 配置文件不存在时使用默认值
except Exception as e:
print(f"加载配置文件失败: {e}")
@classmethod
def save_to_file(cls, config_file: str):
"""保存配置到文件"""
import json
config_data = {}
for attr in dir(cls):
if attr.isupper() and not attr.startswith('_'):
config_data[attr.lower()] = getattr(cls, attr)
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(config_data, f, indent=2, ensure_ascii=False)
# 默认配置实例
config = Config()
# 尝试加载配置文件
config.load_from_file("proxy_config.json")