mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-05-21 05:52:14 +00:00
Fixed possibility of opening external links in system browser
This commit is contained in:
parent
5ba5d9e103
commit
bcf15e9188
@ -2,12 +2,18 @@
|
|||||||
"$schema": "../gen/schemas/desktop-schema.json",
|
"$schema": "../gen/schemas/desktop-schema.json",
|
||||||
"identifier": "default",
|
"identifier": "default",
|
||||||
"description": "Default capability for MindWork AI Studio",
|
"description": "Default capability for MindWork AI Studio",
|
||||||
|
"remote": {
|
||||||
|
"urls": [
|
||||||
|
"http://localhost:*"
|
||||||
|
]
|
||||||
|
},
|
||||||
"windows": [
|
"windows": [
|
||||||
"main"
|
"main"
|
||||||
],
|
],
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"core:default",
|
"core:default",
|
||||||
"updater:default",
|
"updater:default",
|
||||||
|
"opener:default",
|
||||||
"shell:allow-open",
|
"shell:allow-open",
|
||||||
{
|
{
|
||||||
"identifier": "shell:allow-spawn",
|
"identifier": "shell:allow-spawn",
|
||||||
|
|||||||
@ -38,6 +38,9 @@ static EVENT_BROADCAST: Lazy<Mutex<Option<broadcast::Sender<Event>>>> = Lazy::ne
|
|||||||
/// Stores the currently registered global shortcuts (name -> shortcut string).
|
/// Stores the currently registered global shortcuts (name -> shortcut string).
|
||||||
static REGISTERED_SHORTCUTS: Lazy<Mutex<HashMap<Shortcut, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
|
static REGISTERED_SHORTCUTS: Lazy<Mutex<HashMap<Shortcut, String>>> = Lazy::new(|| Mutex::new(HashMap::new()));
|
||||||
|
|
||||||
|
/// Stores the localhost origin of the Blazor app after the .NET server is ready.
|
||||||
|
static APPROVED_APP_URL: Lazy<Mutex<Option<tauri::Url>>> = Lazy::new(|| Mutex::new(None));
|
||||||
|
|
||||||
/// Enum identifying global keyboard shortcuts.
|
/// Enum identifying global keyboard shortcuts.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Display)]
|
||||||
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
|
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
|
||||||
@ -81,6 +84,7 @@ pub fn start_tauri() {
|
|||||||
let app = tauri::Builder::default()
|
let app = tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_dialog::init())
|
.plugin(tauri_plugin_dialog::init())
|
||||||
.plugin(tauri_plugin_shell::init())
|
.plugin(tauri_plugin_shell::init())
|
||||||
|
.plugin(tauri_plugin_opener::init())
|
||||||
.plugin(
|
.plugin(
|
||||||
tauri::plugin::Builder::<tauri::Wry, ()>::new("external-link-handler")
|
tauri::plugin::Builder::<tauri::Wry, ()>::new("external-link-handler")
|
||||||
.on_navigation(|webview, url| {
|
.on_navigation(|webview, url| {
|
||||||
@ -199,16 +203,35 @@ fn is_local_host(host: Option<&str>) -> bool {
|
|||||||
matches!(host, Some("localhost") | Some("127.0.0.1") | Some("::1") | Some("[::1]"))
|
matches!(host, Some("localhost") | Some("127.0.0.1") | Some("::1") | Some("[::1]"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_local_http_url(url: &tauri::Url) -> bool {
|
||||||
|
matches!(url.scheme(), "http" | "https") && is_local_host(url.host_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn same_origin(left: &tauri::Url, right: &tauri::Url) -> bool {
|
||||||
|
left.scheme() == right.scheme()
|
||||||
|
&& left.host_str() == right.host_str()
|
||||||
|
&& left.port_or_known_default() == right.port_or_known_default()
|
||||||
|
}
|
||||||
|
|
||||||
fn should_open_in_system_browser<R: tauri::Runtime>(webview: &tauri::Webview<R>, url: &tauri::Url) -> bool {
|
fn should_open_in_system_browser<R: tauri::Runtime>(webview: &tauri::Webview<R>, url: &tauri::Url) -> bool {
|
||||||
if !matches!(url.scheme(), "http" | "https") {
|
match url.scheme() {
|
||||||
return false;
|
"mailto" | "tel" => return true,
|
||||||
|
"http" | "https" => {},
|
||||||
|
_ => return false,
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(approved_app_url) = APPROVED_APP_URL.lock().unwrap().as_ref() {
|
||||||
|
if same_origin(approved_app_url, url) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_local_http_url(url) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(current_url) = webview.url() {
|
if let Ok(current_url) = webview.url() {
|
||||||
let same_origin = current_url.scheme() == url.scheme()
|
if same_origin(¤t_url, url) {
|
||||||
&& current_url.host_str() == url.host_str()
|
|
||||||
&& current_url.port_or_known_default() == url.port_or_known_default();
|
|
||||||
if same_origin {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -377,6 +400,12 @@ pub async fn change_location_to(url: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Ok(parsed_url) = tauri::Url::parse(url) {
|
||||||
|
if is_local_http_url(&parsed_url) {
|
||||||
|
*APPROVED_APP_URL.lock().unwrap() = Some(parsed_url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let js_location_change = format!("window.location = '{url}';");
|
let js_location_change = format!("window.location = '{url}';");
|
||||||
let main_window = main_window_spawn_clone.lock().unwrap();
|
let main_window = main_window_spawn_clone.lock().unwrap();
|
||||||
let location_change_result = main_window.as_ref().unwrap().eval(js_location_change.as_str());
|
let location_change_result = main_window.as_ref().unwrap().eval(js_location_change.as_str());
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user