From 7ab55e3ae491cf811cb559736dd5cd663ec0caa2 Mon Sep 17 00:00:00 2001
From: Koha9 <36852125+Koha9@users.noreply.github.com>
Date: Sat, 21 Jun 2025 19:20:48 +0900
Subject: [PATCH 1/2] Add HTTPS support for server URL
---
README.md | 1 +
app/templates/login.html | 2 +-
app/utils/plex_client.py | 11 ++++++++++-
3 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index 503ffaf..39bd8a2 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,7 @@ PlexPlaylistSync 是一个用于同步 Plex 播放列表和本地 `.m3u`/`.m3u8`
首次登录时使用用户名和密码连接 Plex 服务器,成功后程序会将获得的 `token` 保存在配置文件中,后续通信仅使用该 `token`,不再保存明文密码。
默认情况下 Plex 服务器使用 `32400` 端口,可在未修改服务器端口时直接使用该默认值。
+服务器地址字段支持包含 `http://` 或 `https://` 前缀,当提供 `https://` 前缀时将使用 HTTPS 连接。
## 安装
diff --git a/app/templates/login.html b/app/templates/login.html
index 7c80bd9..0c380cd 100644
--- a/app/templates/login.html
+++ b/app/templates/login.html
@@ -15,7 +15,7 @@
-
+
diff --git a/app/utils/plex_client.py b/app/utils/plex_client.py
index d67659a..6dab047 100644
--- a/app/utils/plex_client.py
+++ b/app/utils/plex_client.py
@@ -1,5 +1,6 @@
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
+from urllib.parse import urlparse
def connect_plex(config, username, password, url, port="32400"):
@@ -10,7 +11,15 @@ def connect_plex(config, username, password, url, port="32400"):
token = account.authenticationToken
config["token"] = token
- base_url = f"http://{url}:{port}"
+ parsed = urlparse(url)
+
+ if parsed.scheme in ("http", "https"):
+ netloc = parsed.netloc or parsed.path
+ if ":" not in netloc and port:
+ netloc = f"{netloc}:{port}"
+ base_url = f"{parsed.scheme}://{netloc}"
+ else:
+ base_url = f"http://{url}:{port}"
server = PlexServer(base_url, token)
config.update({"server_url": url, "server_port": port})
return server
From d4ed9d22f5e0e285467b54c032f0e9ca2ddcf7d7 Mon Sep 17 00:00:00 2001
From: Koha9 <36852125+Koha9@users.noreply.github.com>
Date: Sat, 21 Jun 2025 19:34:07 +0900
Subject: [PATCH 2/2] Add scheme dropdown and load defaults from config
---
README.md | 2 +-
app/main.py | 41 ++++++++++++++++++++++++++++++++++++----
app/templates/login.html | 11 +++++++++--
app/utils/config.py | 21 ++++++++++++++++++++
4 files changed, 68 insertions(+), 7 deletions(-)
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 @@
+
+
+
+
-
+
-
+
diff --git a/app/utils/config.py b/app/utils/config.py
index e1b4668..f67e190 100644
--- a/app/utils/config.py
+++ b/app/utils/config.py
@@ -1,5 +1,6 @@
import json
import os
+from urllib.parse import urlparse
CONFIG_PATH = os.path.join(os.path.dirname(__file__), "..", "config.json")
@@ -12,3 +13,23 @@ def load_config():
def save_config(new_config):
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(new_config, f, indent=4, ensure_ascii=False)
+
+
+def get_server_settings(config):
+ """Return (scheme, host, port) using defaults when not configured."""
+ scheme = "https"
+ host = ""
+ port = config.get("server_port", "32400") or "32400"
+
+ url = config.get("server_url", "")
+ if url:
+ parsed = urlparse(url)
+ if parsed.scheme:
+ scheme = parsed.scheme
+ host = parsed.hostname or parsed.netloc
+ if parsed.port:
+ port = str(parsed.port)
+ else:
+ host = url
+
+ return scheme, host, port