|
|
@@ -8,7 +8,7 @@ from datetime import datetime, timedelta
|
|
|
from pydantic import BaseModel
|
|
|
from typing import Dict, List, Optional
|
|
|
import httpx
|
|
|
-from sqlmodel import Session, select, or_
|
|
|
+from sqlmodel import Session, select, or_,func
|
|
|
import yaml
|
|
|
import signal
|
|
|
from asyncio import subprocess
|
|
|
@@ -16,7 +16,6 @@ import multiprocessing
|
|
|
from multiprocessing import Pipe, Process
|
|
|
from config.logu import logger, get_logger
|
|
|
from config.settings import settings
|
|
|
-from config.app_yaml import app_yaml, Subscription
|
|
|
from routers.subscriptions import list_subscriptions,SubscriptionResponse
|
|
|
from utils.mihomo_service import port_is_using,find_free_port
|
|
|
from utils.sub import update_config
|
|
|
@@ -68,7 +67,7 @@ async def request_select_proxy_name(external_ctl: str, provider_name: str, proxy
|
|
|
@mihomo_router.post("/start")
|
|
|
async def post_start_mihomo(request: MihomoBatchRequest):
|
|
|
db = SubscriptionManager()
|
|
|
-
|
|
|
+ logger.info(f"{request}")
|
|
|
# 获取对应的订阅文件
|
|
|
with Session(db.engine) as session:
|
|
|
# 查找对应的订阅文件
|
|
|
@@ -135,6 +134,37 @@ async def post_start_mihomo(request: MihomoBatchRequest):
|
|
|
logger.exception(f"Failed to start mihomo: {str(e)}")
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
+@mihomo_router.post("/startup")
|
|
|
+async def post_start_each_provider():
|
|
|
+ db = SubscriptionManager()
|
|
|
+ with Session(db.engine) as session:
|
|
|
+ # 子查询:获取每个 provider_name 的最小 id
|
|
|
+ subquery = (
|
|
|
+ select(
|
|
|
+ MihomoMeta.provider_name,
|
|
|
+ func.min(MihomoMeta.id).label("min_id")
|
|
|
+ )
|
|
|
+ .group_by(MihomoMeta.provider_name)
|
|
|
+ .subquery()
|
|
|
+ )
|
|
|
+
|
|
|
+ # 主查询:通过联接到子查询获取每个 provider_name 的第一条记录
|
|
|
+ stmt = (
|
|
|
+ select(MihomoMeta)
|
|
|
+ .join(subquery, MihomoMeta.id == subquery.c.min_id)
|
|
|
+ )
|
|
|
+
|
|
|
+ # 执行查询并获取结果
|
|
|
+ results = session.exec(stmt).all()
|
|
|
+ ret = []
|
|
|
+ for provider_moho in results:
|
|
|
+ try:
|
|
|
+ res = await post_start_mihomo(MihomoBatchRequest(id=int(provider_moho.id)))
|
|
|
+ ret.append(res)
|
|
|
+ except Exception as e:
|
|
|
+ logger.exception(f"Failed to start mihomo: {str(e)}")
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+ return ret
|
|
|
@mihomo_router.post("/stop")
|
|
|
async def post_stop_mihomo(request: MihomoBatchRequest):
|
|
|
db = SubscriptionManager()
|