From 83c4aadbb4a3dfa6c6627027cfe9bc79f1eea547 Mon Sep 17 00:00:00 2001 From: Simon Eklundh Date: Sun, 31 Aug 2025 11:33:44 +0100 Subject: [PATCH] feat: Add https/http testing for removing the need for adding them manually (#1005) --- app/login.tsx | 73 +++++++++++++++++++++++++++++--------------- translations/en.json | 4 ++- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/app/login.tsx b/app/login.tsx index 6a54cc35..d64516b6 100644 --- a/app/login.tsx +++ b/app/login.tsx @@ -133,27 +133,52 @@ const Login: React.FC = () => { */ const checkUrl = useCallback(async (url: string) => { setLoadingServerCheck(true); - + const baseUrl = url.replace(/^https?:\/\//i, ""); + const protocols = ["https", "http"]; try { - const response = await fetch(`${url}/System/Info/Public`, { - mode: "cors", - }); - - if (response.ok) { - const data = (await response.json()) as PublicSystemInfo; - - setServerName(data.ServerName || ""); - return url; + return checkHttp(baseUrl, protocols); + } catch (e) { + if (e instanceof Error && e.message === "Server too old") { + throw e; } - - return undefined; - } catch { return undefined; } finally { setLoadingServerCheck(false); } }, []); + async function checkHttp(baseUrl: string, protocols: string[]) { + for (const protocol of protocols) { + try { + const response = await fetch( + `${protocol}://${baseUrl}/System/Info/Public`, + { + mode: "cors", + }, + ); + if (response.ok) { + const data = (await response.json()) as PublicSystemInfo; + const serverVersion = data.Version?.split("."); + if (serverVersion && +serverVersion[0] <= 10) { + if (+serverVersion[1] < 10) { + Alert.alert( + t("login.too_old_server_text"), + t("login.too_old_server_description"), + ); + throw new Error("Server too old"); + } + } + setServerName(data.ServerName || ""); + return `${protocol}://${baseUrl}`; + } + } catch (e) { + if (e instanceof Error && e.message === "Server too old") { + throw e; + } + } + } + return undefined; + } /** * Handles the connection attempt to a Jellyfin server. * @@ -172,17 +197,17 @@ const Login: React.FC = () => { */ const handleConnect = useCallback(async (url: string) => { url = url.trim().replace(/\/$/, ""); - const result = await checkUrl(url); - - if (result === undefined) { - Alert.alert( - t("login.connection_failed"), - t("login.could_not_connect_to_server"), - ); - return; - } - - await setServer({ address: url }); + try { + const result = await checkUrl(url); + if (result === undefined) { + Alert.alert( + t("login.connection_failed"), + t("login.could_not_connect_to_server"), + ); + return; + } + await setServer({ address: result }); + } catch {} }, []); const handleQuickConnect = async () => { diff --git a/translations/en.json b/translations/en.json index ade93f3c..b1ccbb80 100644 --- a/translations/en.json +++ b/translations/en.json @@ -20,7 +20,9 @@ "server_is_taking_too_long_to_respond_try_again_later": "Server is taking too long to respond, try again later", "server_received_too_many_requests_try_again_later": "Server received too many requests, try again later.", "there_is_a_server_error": "There is a server error", - "an_unexpected_error_occured_did_you_enter_the_correct_url": "An unexpected error occurred. Did you enter the server URL correctly?" + "an_unexpected_error_occured_did_you_enter_the_correct_url": "An unexpected error occurred. Did you enter the server URL correctly?", + "too_old_server_text": "Unsupported jellyfin server discovered", + "too_old_server_description": "Please update jellyfin to the latest version" }, "server": { "enter_url_to_jellyfin_server": "Enter the URL to your Jellyfin server",