From 469bb4f5bf6053bb587a7cfb86ba5694511b2d63 Mon Sep 17 00:00:00 2001 From: "Timothy Z." Date: Mon, 11 May 2026 02:02:19 +0300 Subject: [PATCH] Pass streaming server URL to WebUI --- src/stremio_app/app.rs | 44 ++++++++++++++++++++++++++++++++++-- src/stremio_app/constants.rs | 9 ++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/stremio_app/app.rs b/src/stremio_app/app.rs index 024272e..e4f6dec 100644 --- a/src/stremio_app/app.rs +++ b/src/stremio_app/app.rs @@ -16,7 +16,10 @@ use url::Url; use winapi::um::{winbase::CREATE_BREAKAWAY_FROM_JOB, winuser::WS_EX_TOPMOST}; use crate::stremio_app::{ - constants::{APP_NAME, UPDATE_ENDPOINT, UPDATE_INTERVAL, WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH}, + constants::{ + web_endpoint_with_streaming_server, APP_NAME, UPDATE_ENDPOINT, UPDATE_INTERVAL, + WEB_ENDPOINT, WINDOW_MIN_HEIGHT, WINDOW_MIN_WIDTH, + }, ipc::{RPCRequest, RPCResponse}, splash::SplashImage, stremio_player::Player, @@ -29,6 +32,40 @@ use crate::stremio_app::{ use super::stremio_server::StremioServer; +fn webui_url_with_streaming_server(webui_url: &str) -> String { + if webui_url.trim_end_matches('/') != WEB_ENDPOINT.trim_end_matches('/') { + return webui_url.to_string(); + } + + find_streaming_server_url() + .map(|server_url| web_endpoint_with_streaming_server(&server_url)) + .unwrap_or_else(|| webui_url.to_string()) +} + +fn find_streaming_server_url() -> Option { + let client = reqwest::blocking::Client::builder() + .timeout(time::Duration::from_millis(500)) + .build() + .ok()?; + + (11470..=11474).find_map(|port| { + let server_url = format!("http://127.0.0.1:{port}"); + let settings_url = format!("{server_url}/settings"); + let response = client.get(settings_url).send().ok()?; + + if !response.status().is_success() { + return None; + } + + let body = response.text().ok()?; + if body.contains("\"baseUrl\"") && body.contains("\"values\"") { + Some(server_url) + } else { + None + } + }) +} + #[derive(Default, NwgUi)] pub struct MainWindow { pub command: String, @@ -126,7 +163,10 @@ impl MainWindow { } } fn on_init(&self) { - self.webview.endpoint.set(self.webui_url.clone()).ok(); + self.webview + .endpoint + .set(webui_url_with_streaming_server(&self.webui_url)) + .ok(); self.webview.dev_tools.set(self.dev_tools).ok(); if let Some(hwnd) = self.window.handle.hwnd() { if let Ok(mut saved_style) = self.saved_window_style.try_borrow_mut() { diff --git a/src/stremio_app/constants.rs b/src/stremio_app/constants.rs index 27f8317..4cd6e9d 100644 --- a/src/stremio_app/constants.rs +++ b/src/stremio_app/constants.rs @@ -15,3 +15,12 @@ pub const STREMIO_SERVER_DEV_MODE: &str = "STREMIO_SERVER_DEV_MODE"; pub const SRV_BUFFER_SIZE: usize = 1024; pub const SERVER_IPC_KEY: &str = "SERVER_IPC_KEY"; pub const SRV_LOG_SIZE: usize = 20; + +pub fn web_endpoint_with_streaming_server(server_url: &str) -> String { + let server_url = server_url.trim_end_matches('/'); + let streaming_server_url = + url::form_urlencoded::byte_serialize(server_url.as_bytes()).collect::(); + let web_endpoint = WEB_ENDPOINT.trim_end_matches('/'); + + format!("{web_endpoint}/#/?streamingServerUrl={streaming_server_url}") +}