diff --git a/README.md b/README.md index 39bd8a2..25da90f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ PlexPlaylistSync 是一个用于同步 Plex 播放列表和本地 `.m3u`/`.m3u8` 首次登录时使用用户名和密码连接 Plex 服务器,成功后程序会将获得的 `token` 保存在配置文件中,后续通信仅使用该 `token`,不再保存明文密码。 默认情况下 Plex 服务器使用 `32400` 端口,可在未修改服务器端口时直接使用该默认值。 -服务器地址字段支持包含 `http://` 或 `https://` 前缀,当提供 `https://` 前缀时将使用 HTTPS 连接。 +登录页面提供选择 `http` 或 `https` 的下拉框,服务器地址输入框只需填写域名或 IP,默认值会从 `config.json` 读取。 ## 安装 diff --git a/app/main.py b/app/main.py index 0b5dce7..d330b87 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ import os -from app.utils.config import load_config, save_config +from app.utils.config import load_config, save_config, get_server_settings from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.templating import Jinja2Templates @@ -18,27 +18,54 @@ app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__ async def home(request: Request): config = load_config() theme = config.get("theme", "auto") - return templates.TemplateResponse("login.html", {"request": request, "theme": theme, "path": "/login"}) + scheme, host, port = get_server_settings(config) + return templates.TemplateResponse( + "login.html", + { + "request": request, + "theme": theme, + "path": "/login", + "scheme": scheme, + "server_url": host, + "port": port, + }, + ) @app.get("/login", response_class=HTMLResponse) async def login_page(request: Request): config = load_config() theme = config.get("theme", "auto") - return templates.TemplateResponse("login.html", {"request": request, "theme": theme, "path": "/login"}) + scheme, host, port = get_server_settings(config) + return templates.TemplateResponse( + "login.html", + { + "request": request, + "theme": theme, + "path": "/login", + "scheme": scheme, + "server_url": host, + "port": port, + }, + ) @app.post("/login", response_class=HTMLResponse) async def login( request: Request, user: str = Form(...), pw: str = Form(...), + scheme: str = Form("https"), url: str = Form(...), port: str = Form("32400") ): config = load_config() theme = config.get("theme", "auto") try: - connect_plex(config, user, pw, url, port) + full_url = url + if not full_url.startswith("http://") and not full_url.startswith("https://"): + full_url = f"{scheme}://{url}" + connect_plex(config, user, pw, full_url, port) save_config(config) + scheme, host, port = get_server_settings(config) return templates.TemplateResponse( "login.html", { @@ -47,6 +74,9 @@ async def login( "success": True, "theme": theme, "path": "/login", + "scheme": scheme, + "server_url": host, + "port": port, }, ) except Exception as e: @@ -58,6 +88,9 @@ async def login( "success": False, "theme": theme, "path": "/login", + "scheme": scheme, + "server_url": url, + "port": port, }, ) diff --git a/app/templates/login.html b/app/templates/login.html index 0c380cd..1103cab 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -13,13 +13,20 @@ +