diff --git a/src/constants.py b/src/constants.py index 4ac5110..81b9f1b 100644 --- a/src/constants.py +++ b/src/constants.py @@ -1,12 +1,30 @@ """常量定义""" from pathlib import Path +from httpx import Client LOG_DIR: Path = Path("logs") -IS_CN: bool = True CONFIG_FILE: Path = Path("config.json") +def check_ip(): + try: + with Client(verify=False, timeout=5.0) as client: + req = client.get( + "https://mips.kugou.com/check/iscn", + ) + req.raise_for_status() + body = req.json() + print("已获取IP属地") + return bool(body["flag"]) + except: + print("获取IP属地失败, 默认您位于中国大陆境内") + return True + + +IS_CN: bool = check_ip() + + STEAM_API_BASE: str = "https://steam.ikunshare.com/api" STEAM_CACHE_CDN_LIST: list = ( [ diff --git a/src/main.py b/src/main.py index d62e3a1..ee311c4 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,6 @@ from .logger import Logger from .models import DepotInfo, ManifestInfo, SteamAppInfo, SteamAppManifestInfo from .network.client import HttpClient from .manifest_handler import ManifestHandler -from . import constants class OnekeyApp: @@ -20,14 +19,6 @@ class OnekeyApp: ) self.client = HttpClient() - async def check_ip(self): - req = await self.client.get( - "https://mips.kugou.com/check/iscn", - ) - req.raise_for_status() - body = req.json() - constants.IS_CN = bool(body["flag"]) - async def fetch_key(self): trans = { "week": "周卡", @@ -165,7 +156,6 @@ class OnekeyApp: self.logger.error("Steam路径未配置或无效,无法继续") return False - await self.check_ip() await self.fetch_key() manifests = [] diff --git a/src/network/client.py b/src/network/client.py index afcce63..b91514b 100644 --- a/src/network/client.py +++ b/src/network/client.py @@ -8,7 +8,7 @@ class HttpClient: """HTTP客户端封装""" def __init__(self): - self._client = httpx.AsyncClient(timeout=60) + self._client = httpx.AsyncClient(timeout=60.0, verify=False) async def get(self, url: str, headers: Optional[Dict] = None) -> httpx.Response: """GET请求""" diff --git a/web/app.py b/web/app.py index 10aa078..be53285 100644 --- a/web/app.py +++ b/web/app.py @@ -13,6 +13,7 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, RedirectResponse from fastapi.templating import Jinja2Templates +from src import constants from src.constants import STEAM_API_BASE @@ -205,6 +206,9 @@ templates = Jinja2Templates(directory=f"{base_path}/templates") web_app = WebOnekeyApp(manager) + + + @app.get("/") async def index(request: Request): """主页""" @@ -391,7 +395,7 @@ async def get_key_info(request: Request): if not key: return JSONResponse({"success": False, "message": "卡密不能为空"}) - async with httpx.AsyncClient(timeout=10.0) as client: + async with httpx.AsyncClient(timeout=10.0, verify=False) as client: response = await client.post( f"{STEAM_API_BASE}/getKeyInfo", json={"key": key},