Persist captcha pool settings in database

This commit is contained in:
genz27
2026-03-30 15:43:15 +08:00
parent a12c02c8ec
commit 0d614870af

View File

@@ -214,6 +214,10 @@ class Database:
remote_browser_base_url = ""
remote_browser_api_key = ""
remote_browser_timeout = 60
browser_count = 1
personal_project_pool_size = 4
personal_max_resident_tabs = 5
personal_idle_tab_ttl_seconds = 600
if config_dict:
captcha_config = config_dict.get("captcha", {})
@@ -223,17 +227,39 @@ class Database:
remote_browser_base_url = captcha_config.get("remote_browser_base_url", "")
remote_browser_api_key = captcha_config.get("remote_browser_api_key", "")
remote_browser_timeout = captcha_config.get("remote_browser_timeout", 60)
browser_count = captcha_config.get("browser_count", 1)
personal_project_pool_size = captcha_config.get("personal_project_pool_size", 4)
personal_max_resident_tabs = captcha_config.get("personal_max_resident_tabs", 5)
personal_idle_tab_ttl_seconds = captcha_config.get("personal_idle_tab_ttl_seconds", 600)
try:
remote_browser_timeout = max(5, int(remote_browser_timeout))
except Exception:
remote_browser_timeout = 60
try:
browser_count = max(1, int(browser_count))
except Exception:
browser_count = 1
try:
personal_project_pool_size = max(1, min(50, int(personal_project_pool_size)))
except Exception:
personal_project_pool_size = 4
try:
personal_max_resident_tabs = max(1, min(50, int(personal_max_resident_tabs)))
except Exception:
personal_max_resident_tabs = 5
try:
personal_idle_tab_ttl_seconds = max(60, int(personal_idle_tab_ttl_seconds))
except Exception:
personal_idle_tab_ttl_seconds = 600
await db.execute("""
INSERT INTO captcha_config (
id, captcha_method, yescaptcha_api_key, yescaptcha_base_url,
remote_browser_base_url, remote_browser_api_key, remote_browser_timeout
remote_browser_base_url, remote_browser_api_key, remote_browser_timeout,
browser_count, personal_project_pool_size,
personal_max_resident_tabs, personal_idle_tab_ttl_seconds
)
VALUES (1, ?, ?, ?, ?, ?, ?)
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (
captcha_method,
yescaptcha_api_key,
@@ -241,6 +267,10 @@ class Database:
remote_browser_base_url,
remote_browser_api_key,
remote_browser_timeout,
browser_count,
personal_project_pool_size,
personal_max_resident_tabs,
personal_idle_tab_ttl_seconds,
))
# Ensure plugin_config has a row
@@ -462,6 +492,7 @@ class Database:
# Check and add missing columns to captcha_config table
if await self._table_exists(db, "captcha_config"):
captcha_columns_to_add = [
("personal_project_pool_size", "INTEGER DEFAULT 4"),
("personal_max_resident_tabs", "INTEGER DEFAULT 5"),
("personal_idle_tab_ttl_seconds", "INTEGER DEFAULT 600"),
]
@@ -677,6 +708,7 @@ class Database:
browser_proxy_enabled BOOLEAN DEFAULT 0,
browser_proxy_url TEXT,
browser_count INTEGER DEFAULT 1,
personal_project_pool_size INTEGER DEFAULT 4,
personal_max_resident_tabs INTEGER DEFAULT 5,
personal_idle_tab_ttl_seconds INTEGER DEFAULT 600,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
@@ -1523,6 +1555,7 @@ class Database:
config.set_remote_browser_base_url(captcha_config.remote_browser_base_url)
config.set_remote_browser_api_key(captcha_config.remote_browser_api_key)
config.set_remote_browser_timeout(captcha_config.remote_browser_timeout)
config.set_personal_project_pool_size(captcha_config.personal_project_pool_size)
config.set_personal_max_resident_tabs(captcha_config.personal_max_resident_tabs)
config.set_personal_idle_tab_ttl_seconds(captcha_config.personal_idle_tab_ttl_seconds)
@@ -1657,6 +1690,7 @@ class Database:
browser_proxy_enabled: bool = None,
browser_proxy_url: str = None,
browser_count: int = None,
personal_project_pool_size: int = None,
personal_max_resident_tabs: int = None,
personal_idle_tab_ttl_seconds: int = None
):
@@ -1683,9 +1717,11 @@ class Database:
new_proxy_enabled = browser_proxy_enabled if browser_proxy_enabled is not None else current.get("browser_proxy_enabled", False)
new_proxy_url = browser_proxy_url if browser_proxy_url is not None else current.get("browser_proxy_url")
new_browser_count = browser_count if browser_count is not None else current.get("browser_count", 1)
new_personal_project_pool_size = personal_project_pool_size if personal_project_pool_size is not None else current.get("personal_project_pool_size", 4)
new_personal_max_tabs = personal_max_resident_tabs if personal_max_resident_tabs is not None else current.get("personal_max_resident_tabs", 5)
new_personal_idle_ttl = personal_idle_tab_ttl_seconds if personal_idle_tab_ttl_seconds is not None else current.get("personal_idle_tab_ttl_seconds", 600)
new_remote_timeout = max(5, int(new_remote_timeout)) if new_remote_timeout is not None else 60
new_personal_project_pool_size = max(1, min(50, int(new_personal_project_pool_size)))
new_personal_max_tabs = max(1, min(50, int(new_personal_max_tabs))) # 限制1-50
new_personal_idle_ttl = max(60, int(new_personal_idle_ttl)) # 最少60秒
@@ -1697,13 +1733,14 @@ class Database:
capsolver_api_key = ?, capsolver_base_url = ?,
remote_browser_base_url = ?, remote_browser_api_key = ?, remote_browser_timeout = ?,
browser_proxy_enabled = ?, browser_proxy_url = ?, browser_count = ?,
personal_project_pool_size = ?,
personal_max_resident_tabs = ?, personal_idle_tab_ttl_seconds = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = 1
""", (new_method, new_yes_key, new_yes_url, new_cap_key, new_cap_url,
new_ez_key, new_ez_url, new_cs_key, new_cs_url,
(new_remote_base_url or "").strip(), (new_remote_api_key or "").strip(), new_remote_timeout,
new_proxy_enabled, new_proxy_url, new_browser_count,
new_proxy_enabled, new_proxy_url, new_browser_count, new_personal_project_pool_size,
new_personal_max_tabs, new_personal_idle_ttl))
else:
new_method = captcha_method if captcha_method is not None else "yescaptcha"
@@ -1721,9 +1758,11 @@ class Database:
new_proxy_enabled = browser_proxy_enabled if browser_proxy_enabled is not None else False
new_proxy_url = browser_proxy_url
new_browser_count = browser_count if browser_count is not None else 1
new_personal_project_pool_size = personal_project_pool_size if personal_project_pool_size is not None else 4
new_personal_max_tabs = personal_max_resident_tabs if personal_max_resident_tabs is not None else 5
new_personal_idle_ttl = personal_idle_tab_ttl_seconds if personal_idle_tab_ttl_seconds is not None else 600
new_remote_timeout = max(5, int(new_remote_timeout))
new_personal_project_pool_size = max(1, min(50, int(new_personal_project_pool_size)))
new_personal_max_tabs = max(1, min(50, int(new_personal_max_tabs)))
new_personal_idle_ttl = max(60, int(new_personal_idle_ttl))
@@ -1733,12 +1772,13 @@ class Database:
capsolver_api_key, capsolver_base_url,
remote_browser_base_url, remote_browser_api_key, remote_browser_timeout,
browser_proxy_enabled, browser_proxy_url, browser_count,
personal_project_pool_size,
personal_max_resident_tabs, personal_idle_tab_ttl_seconds)
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", (new_method, new_yes_key, new_yes_url, new_cap_key, new_cap_url,
new_ez_key, new_ez_url, new_cs_key, new_cs_url,
(new_remote_base_url or "").strip(), (new_remote_api_key or "").strip(), new_remote_timeout,
new_proxy_enabled, new_proxy_url, new_browser_count,
new_proxy_enabled, new_proxy_url, new_browser_count, new_personal_project_pool_size,
new_personal_max_tabs, new_personal_idle_ttl))
await db.commit()