2025-03-27 10:20:06 +08:00
|
|
|
|
from dataclasses import dataclass
|
2025-04-01 15:43:27 +08:00
|
|
|
|
from typing import Tuple, Optional
|
2025-03-27 10:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class GlobalConfig:
|
|
|
|
|
|
max_concurrency: int
|
|
|
|
|
|
timeout: int
|
|
|
|
|
|
retry_times: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class DatabaseConfig:
|
2025-04-01 15:43:27 +08:00
|
|
|
|
# SQLite的配置字段保留,用于兼容
|
|
|
|
|
|
path: Optional[str] = None
|
|
|
|
|
|
pool_size: int = 10
|
|
|
|
|
|
# MySQL配置
|
|
|
|
|
|
host: str = "localhost"
|
|
|
|
|
|
port: int = 3306
|
|
|
|
|
|
username: str = "auto_cursor_reg"
|
|
|
|
|
|
password: str = "this_password_jiaqiao"
|
|
|
|
|
|
database: str = "auto_cursor_reg"
|
|
|
|
|
|
# Redis配置
|
|
|
|
|
|
use_redis: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class RedisConfig:
|
|
|
|
|
|
host: str
|
|
|
|
|
|
port: int
|
|
|
|
|
|
password: str = ""
|
|
|
|
|
|
db: int = 0
|
2025-03-27 10:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class ProxyConfig:
|
|
|
|
|
|
api_url: str
|
|
|
|
|
|
batch_size: int
|
|
|
|
|
|
check_interval: int
|
2025-04-01 16:25:08 +08:00
|
|
|
|
api_proxy: Optional[str] = None
|
2025-03-27 10:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class RegisterConfig:
|
|
|
|
|
|
delay_range: Tuple[int, int]
|
|
|
|
|
|
batch_size: int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class EmailConfig:
|
|
|
|
|
|
file_path: str
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-07 13:14:29 +08:00
|
|
|
|
@dataclass
|
|
|
|
|
|
class SelfHostedEmailConfig:
|
|
|
|
|
|
"""自建邮箱配置"""
|
|
|
|
|
|
api_base_url: str
|
|
|
|
|
|
api_key: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-01 16:25:08 +08:00
|
|
|
|
@dataclass
|
|
|
|
|
|
class ServerConfig:
|
|
|
|
|
|
hostname: str
|
|
|
|
|
|
description: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class AutoServiceConfig:
|
|
|
|
|
|
check_interval: int = 60
|
|
|
|
|
|
upload_interval: int = 300
|
|
|
|
|
|
email_check_threshold: int = 30
|
|
|
|
|
|
email_fetch_count: int = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-27 10:20:06 +08:00
|
|
|
|
@dataclass
|
|
|
|
|
|
class CapsolverConfig:
|
|
|
|
|
|
api_key: str
|
|
|
|
|
|
website_url: str
|
|
|
|
|
|
website_key: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class YesCaptchaConfig:
|
|
|
|
|
|
client_key: str
|
|
|
|
|
|
website_url: str
|
|
|
|
|
|
website_key: str
|
|
|
|
|
|
use_cn_server: bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class CaptchaConfig:
|
|
|
|
|
|
provider: str
|
|
|
|
|
|
capsolver: CapsolverConfig
|
|
|
|
|
|
yescaptcha: YesCaptchaConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
|
class Config:
|
|
|
|
|
|
global_config: GlobalConfig
|
|
|
|
|
|
database_config: DatabaseConfig
|
2025-04-01 15:43:27 +08:00
|
|
|
|
redis_config: Optional[RedisConfig] = None
|
|
|
|
|
|
proxy_config: ProxyConfig = None
|
|
|
|
|
|
register_config: RegisterConfig = None
|
|
|
|
|
|
email_config: EmailConfig = None
|
|
|
|
|
|
captcha_config: CaptchaConfig = None
|
2025-04-01 16:25:08 +08:00
|
|
|
|
server_config: Optional[ServerConfig] = None
|
|
|
|
|
|
auto_service_config: Optional[AutoServiceConfig] = None
|
2025-04-07 13:14:29 +08:00
|
|
|
|
self_hosted_email_config: Optional[SelfHostedEmailConfig] = None
|
2025-04-01 16:25:08 +08:00
|
|
|
|
hostname: Optional[str] = None # 向后兼容
|
2025-03-27 10:20:06 +08:00
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_yaml(cls, path: str = "config.yaml"):
|
|
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
|
|
|
|
data = yaml.safe_load(f)
|
|
|
|
|
|
|
2025-04-01 15:43:27 +08:00
|
|
|
|
# 创建 database 配置对象
|
|
|
|
|
|
db_config = DatabaseConfig(**data.get('database', {}))
|
|
|
|
|
|
|
|
|
|
|
|
# 创建 redis 配置对象(如果有)
|
|
|
|
|
|
redis_config = None
|
|
|
|
|
|
if 'redis' in data and db_config.use_redis:
|
|
|
|
|
|
redis_config = RedisConfig(**data['redis'])
|
|
|
|
|
|
|
2025-03-27 10:20:06 +08:00
|
|
|
|
# 创建 captcha 配置对象
|
2025-04-01 15:43:27 +08:00
|
|
|
|
captcha_data = data.get('captcha', {})
|
|
|
|
|
|
captcha_config = None
|
|
|
|
|
|
if captcha_data:
|
|
|
|
|
|
captcha_config = CaptchaConfig(
|
|
|
|
|
|
provider=captcha_data.get('provider', 'capsolver'),
|
|
|
|
|
|
capsolver=CapsolverConfig(**captcha_data.get('capsolver', {})),
|
|
|
|
|
|
yescaptcha=YesCaptchaConfig(**captcha_data.get('yescaptcha', {}))
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 创建其他配置对象
|
|
|
|
|
|
global_config = GlobalConfig(**data.get('global', {}))
|
|
|
|
|
|
proxy_config = ProxyConfig(**data.get('proxy', {})) if 'proxy' in data else None
|
|
|
|
|
|
register_config = RegisterConfig(**data.get('register', {})) if 'register' in data else None
|
|
|
|
|
|
email_config = EmailConfig(**data.get('email', {})) if 'email' in data else None
|
2025-04-01 16:25:08 +08:00
|
|
|
|
|
|
|
|
|
|
# 创建服务器配置对象
|
|
|
|
|
|
server_config = ServerConfig(**data.get('server_config', {})) if 'server_config' in data else None
|
|
|
|
|
|
|
|
|
|
|
|
# 创建自动服务配置对象
|
|
|
|
|
|
auto_service_config = AutoServiceConfig(**data.get('auto_service', {})) if 'auto_service' in data else None
|
|
|
|
|
|
|
2025-04-07 13:14:29 +08:00
|
|
|
|
# 创建自建邮箱配置对象
|
|
|
|
|
|
self_hosted_email_config = SelfHostedEmailConfig(**data.get('self_hosted_email', {})) if 'self_hosted_email' in data else None
|
|
|
|
|
|
|
2025-04-01 16:25:08 +08:00
|
|
|
|
# 设置hostname (优先使用server_config中的hostname)
|
|
|
|
|
|
hostname = None
|
|
|
|
|
|
if server_config and hasattr(server_config, 'hostname'):
|
|
|
|
|
|
hostname = server_config.hostname
|
|
|
|
|
|
|
2025-03-27 10:20:06 +08:00
|
|
|
|
return cls(
|
2025-04-01 15:43:27 +08:00
|
|
|
|
global_config=global_config,
|
|
|
|
|
|
database_config=db_config,
|
|
|
|
|
|
redis_config=redis_config,
|
|
|
|
|
|
proxy_config=proxy_config,
|
|
|
|
|
|
register_config=register_config,
|
|
|
|
|
|
email_config=email_config,
|
2025-04-01 16:25:08 +08:00
|
|
|
|
captcha_config=captcha_config,
|
|
|
|
|
|
server_config=server_config,
|
|
|
|
|
|
auto_service_config=auto_service_config,
|
2025-04-07 13:14:29 +08:00
|
|
|
|
self_hosted_email_config=self_hosted_email_config,
|
2025-04-01 16:25:08 +08:00
|
|
|
|
hostname=hostname
|
2025-03-27 10:20:06 +08:00
|
|
|
|
)
|