fix: 修复proxy_service和account_service中不当的顶层import
- proxy_service.py: 将requests和WhitelistManager改为函数内延迟import, 避免启动时加载不需要的依赖;移除未使用的get_exit_ip_via_proxy导入 - account_service.py: 移除未使用的func、joinedload、AuditLog、 user_has_permission顶层导入
This commit is contained in:
+17
-42
@@ -5,12 +5,16 @@ from pathlib import Path
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
|
||||
DB_PATH = Path(__file__).parent.parent.parent / "data" / "web.db"
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
DB_PATH = PROJECT_ROOT / "data" / "web.db"
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite:///{DB_PATH}")
|
||||
|
||||
connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
|
||||
|
||||
engine = create_engine(
|
||||
f"sqlite:///{DB_PATH}",
|
||||
connect_args={"check_same_thread": False},
|
||||
DATABASE_URL,
|
||||
connect_args=connect_args,
|
||||
echo=False,
|
||||
)
|
||||
|
||||
@@ -28,49 +32,20 @@ def get_db():
|
||||
|
||||
|
||||
def init_db():
|
||||
"""建表 + 写入初始数据。"""
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_migrate()
|
||||
"""执行数据库迁移 + 写入初始数据。"""
|
||||
run_migrations()
|
||||
_seed()
|
||||
|
||||
|
||||
def _migrate():
|
||||
"""数据库迁移:为已有表添加新列。"""
|
||||
from sqlalchemy import text
|
||||
with engine.connect() as conn:
|
||||
# 检查 accounts.tag 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(accounts)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'tag' not in columns:
|
||||
conn.execute(text("ALTER TABLE accounts ADD COLUMN tag VARCHAR(64) DEFAULT ''"))
|
||||
conn.commit()
|
||||
def run_migrations():
|
||||
"""运行 Alembic 迁移到最新版本。"""
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
|
||||
# 检查 users.custom_permissions 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(users)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'custom_permissions' not in columns:
|
||||
conn.execute(text("ALTER TABLE users ADD COLUMN custom_permissions JSON DEFAULT NULL"))
|
||||
conn.commit()
|
||||
|
||||
# 检查 accounts.email_imap_ssl 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(accounts)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'email_imap_ssl' not in columns:
|
||||
# 旧数据端口993的默认True(SSL),其他端口默认False
|
||||
conn.execute(text("ALTER TABLE accounts ADD COLUMN email_imap_ssl BOOLEAN DEFAULT 1"))
|
||||
conn.commit()
|
||||
# 修正旧数据中使用 111.229.206.54 的账号:端口改为143,SSL改为False
|
||||
conn.execute(text(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = '111.229.206.54'"
|
||||
))
|
||||
conn.commit()
|
||||
# 修正旧数据中使用 mail.bdhg.xyz 的账号:993端口不通,改用143非SSL
|
||||
conn.execute(text(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = 'mail.bdhg.xyz'"
|
||||
))
|
||||
conn.commit()
|
||||
config = Config(str(PROJECT_ROOT / "alembic.ini"))
|
||||
config.set_main_option("script_location", str(PROJECT_ROOT / "web" / "backend" / "migrations"))
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
command.upgrade(config, "head")
|
||||
|
||||
|
||||
def _seed():
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
# 数据库迁移
|
||||
|
||||
本目录由 Alembic 管理 Web 后台数据库结构。
|
||||
|
||||
常用命令:
|
||||
|
||||
```bash
|
||||
uv run alembic upgrade head
|
||||
uv run alembic revision -m "描述"
|
||||
```
|
||||
|
||||
应用启动时会自动执行 `upgrade head`,本地开发通常不需要手动运行迁移命令。
|
||||
@@ -0,0 +1,57 @@
|
||||
"""Alembic 迁移环境。"""
|
||||
|
||||
from logging.config import fileConfig
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from alembic import context
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
ROOT_DIR = Path(__file__).resolve().parents[3]
|
||||
if str(ROOT_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT_DIR))
|
||||
|
||||
from web.backend.database import Base, DATABASE_URL # noqa: E402
|
||||
from web.backend import models # noqa: F401,E402
|
||||
|
||||
config = context.config
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""离线模式生成 SQL。"""
|
||||
context.configure(
|
||||
url=DATABASE_URL,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""在线模式直接执行迁移。"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
@@ -0,0 +1,25 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,160 @@
|
||||
"""初始化 Web 后台数据库结构
|
||||
|
||||
Revision ID: 20260623_0001
|
||||
Revises:
|
||||
Create Date: 2026-06-23
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "20260623_0001"
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def _has_table(bind, table_name: str) -> bool:
|
||||
return sa.inspect(bind).has_table(table_name)
|
||||
|
||||
|
||||
def _columns(bind, table_name: str) -> set[str]:
|
||||
if not _has_table(bind, table_name):
|
||||
return set()
|
||||
return {column["name"] for column in sa.inspect(bind).get_columns(table_name)}
|
||||
|
||||
|
||||
def _indexes(bind, table_name: str) -> set[str]:
|
||||
if not _has_table(bind, table_name):
|
||||
return set()
|
||||
return {index["name"] for index in sa.inspect(bind).get_indexes(table_name)}
|
||||
|
||||
|
||||
def _add_column_if_missing(bind, table_name: str, column: sa.Column) -> bool:
|
||||
if column.name in _columns(bind, table_name):
|
||||
return False
|
||||
op.add_column(table_name, column)
|
||||
return True
|
||||
|
||||
|
||||
def _create_index_if_missing(bind, name: str, table_name: str, columns: list[str], unique: bool = False) -> None:
|
||||
if name not in _indexes(bind, table_name):
|
||||
op.create_index(name, table_name, columns, unique=unique)
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
|
||||
if not _has_table(bind, "users"):
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("username", sa.String(length=64), nullable=False),
|
||||
sa.Column("password_hash", sa.String(length=256), nullable=False),
|
||||
sa.Column("role", sa.String(length=32), nullable=False),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=True),
|
||||
sa.Column("remark", sa.String(length=256), nullable=True),
|
||||
sa.Column("custom_permissions", sa.JSON(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
else:
|
||||
_add_column_if_missing(bind, "users", sa.Column("custom_permissions", sa.JSON(), nullable=True))
|
||||
_create_index_if_missing(bind, "ix_users_username", "users", ["username"], unique=True)
|
||||
|
||||
if not _has_table(bind, "accounts"):
|
||||
op.create_table(
|
||||
"accounts",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("username", sa.String(length=128), nullable=False),
|
||||
sa.Column("password", sa.String(length=256), nullable=False),
|
||||
sa.Column("email", sa.String(length=128), nullable=False),
|
||||
sa.Column("email_password", sa.String(length=256), nullable=False),
|
||||
sa.Column("email_imap_server", sa.String(length=128), nullable=True),
|
||||
sa.Column("email_imap_port", sa.Integer(), nullable=True),
|
||||
sa.Column("email_imap_ssl", sa.Boolean(), nullable=True),
|
||||
sa.Column("assigned_to", sa.Integer(), nullable=True),
|
||||
sa.Column("tag", sa.String(length=64), nullable=True),
|
||||
sa.Column("remark", sa.String(length=256), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["assigned_to"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
else:
|
||||
_add_column_if_missing(bind, "accounts", sa.Column("tag", sa.String(length=64), server_default=""))
|
||||
added_ssl = _add_column_if_missing(bind, "accounts", sa.Column("email_imap_ssl", sa.Boolean(), server_default=sa.text("1")))
|
||||
if added_ssl:
|
||||
op.execute(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = '111.229.206.54'"
|
||||
)
|
||||
op.execute(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = 'mail.bdhg.xyz'"
|
||||
)
|
||||
_create_index_if_missing(bind, "ix_accounts_assigned_to", "accounts", ["assigned_to"])
|
||||
|
||||
if not _has_table(bind, "proxy_config"):
|
||||
op.create_table(
|
||||
"proxy_config",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("enabled", sa.Boolean(), nullable=True),
|
||||
sa.Column("api_url", sa.String(length=512), nullable=True),
|
||||
sa.Column("http", sa.String(length=256), nullable=True),
|
||||
sa.Column("https", sa.String(length=256), nullable=True),
|
||||
sa.Column("whitelist_enabled", sa.Boolean(), nullable=True),
|
||||
sa.Column("whitelist_uid", sa.String(length=64), nullable=True),
|
||||
sa.Column("whitelist_ukey", sa.String(length=128), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
|
||||
if not _has_table(bind, "login_tasks"):
|
||||
op.create_table(
|
||||
"login_tasks",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("batch_id", sa.String(length=64), nullable=False),
|
||||
sa.Column("account_id", sa.Integer(), nullable=False),
|
||||
sa.Column("status", sa.String(length=32), nullable=True),
|
||||
sa.Column("cookie", sa.Text(), nullable=True),
|
||||
sa.Column("message", sa.String(length=512), nullable=True),
|
||||
sa.Column("created_by", sa.Integer(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.Column("finished_at", sa.DateTime(), nullable=True),
|
||||
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
|
||||
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
_create_index_if_missing(bind, "ix_login_tasks_batch_id", "login_tasks", ["batch_id"])
|
||||
|
||||
if not _has_table(bind, "audit_logs"):
|
||||
op.create_table(
|
||||
"audit_logs",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), nullable=True),
|
||||
sa.Column("username", sa.String(length=64), nullable=True),
|
||||
sa.Column("action", sa.String(length=128), nullable=False),
|
||||
sa.Column("target", sa.String(length=256), nullable=True),
|
||||
sa.Column("detail", sa.Text(), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=True),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
if _has_table(bind, "audit_logs"):
|
||||
op.drop_table("audit_logs")
|
||||
if _has_table(bind, "login_tasks"):
|
||||
op.drop_index("ix_login_tasks_batch_id", table_name="login_tasks")
|
||||
op.drop_table("login_tasks")
|
||||
if _has_table(bind, "proxy_config"):
|
||||
op.drop_table("proxy_config")
|
||||
if _has_table(bind, "accounts"):
|
||||
op.drop_index("ix_accounts_assigned_to", table_name="accounts")
|
||||
op.drop_table("accounts")
|
||||
if _has_table(bind, "users"):
|
||||
op.drop_index("ix_users_username", table_name="users")
|
||||
op.drop_table("users")
|
||||
@@ -3,11 +3,9 @@
|
||||
import csv
|
||||
import re
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Account, AuditLog, LoginTask
|
||||
from ..permissions import user_has_permission
|
||||
from ..models import Account, LoginTask
|
||||
|
||||
|
||||
EMAIL_PATTERN = re.compile(r'^[^\s@|]+@[^\s@|]+\.[^\s@|]+$')
|
||||
|
||||
@@ -6,11 +6,9 @@ import threading
|
||||
import uuid
|
||||
from typing import Optional
|
||||
|
||||
import requests as req_lib
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.douyu.proxy import resolve_working_proxy, verify_proxy_url, parse_proxy_response
|
||||
from core.douyu.whitelist import WhitelistManager, get_exit_ip_via_proxy
|
||||
from ..models import ProxyConfig as ProxyConfigModel, AuditLog
|
||||
|
||||
|
||||
@@ -146,6 +144,8 @@ class ProxyService:
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
):
|
||||
"""在线程中执行白名单测试。"""
|
||||
import requests as req_lib
|
||||
from core.douyu.whitelist import WhitelistManager
|
||||
|
||||
def push(level, message):
|
||||
asyncio.run_coroutine_threadsafe(
|
||||
|
||||
Reference in New Issue
Block a user