From 0245861c4852a3926d19e86e89ec7e698b77b25c Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Mon, 13 Jan 2025 22:01:06 +0000 Subject: [PATCH] [Feature] Can put rqbit Web UI behind proxy server --- crates/librqbit/webui/src/http-api.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/crates/librqbit/webui/src/http-api.ts b/crates/librqbit/webui/src/http-api.ts index 0c350d6a..2e00df30 100644 --- a/crates/librqbit/webui/src/http-api.ts +++ b/crates/librqbit/webui/src/http-api.ts @@ -10,21 +10,26 @@ import { // Define API URL and base path const apiUrl = (() => { - if (window.origin === "null" || window.origin === "http://localhost:3031") { + if (window.origin === "null") { return "http://localhost:3030"; } - let port = /http.*:\/\/.*:(\d+)/.exec(window.origin)?.[1]; - if (port == "3031") { - return window.origin.replace("3031", "3030"); + const url = new URL(window.location.href); + + // assume Vite devserver + if (url.port == "3031") { + return `${url.protocol}//${url.hostname}:3030`; } - return ""; + + // Remove "/web" or "/web/" from the end and also ending slash. + const path = /(.*?)\/?(\/web\/?)?$/.exec(url.pathname)![1] ?? ""; + return path; })(); const makeRequest = async ( method: string, path: string, data?: any, - isJson?: boolean, + isJson?: boolean ): Promise => { console.log(method, path); const url = apiUrl + path; @@ -127,7 +132,7 @@ export const API: RqbitAPI & { getVersion: () => Promise } = { { only_files: files, }, - true, + true ); }, @@ -153,7 +158,7 @@ export const API: RqbitAPI & { getVersion: () => Promise } = { getTorrentStreamUrl: ( index: number, file_id: number, - filename?: string | null, + filename?: string | null ) => { let url = apiUrl + `/torrents/${index}/stream/${file_id}`; if (!!filename) {