From 5752996f8925043fb5ac3bcf0cf82b92f47026b6 Mon Sep 17 00:00:00 2001 From: jeasonnow Date: Tue, 30 Jul 2024 16:54:36 +0800 Subject: [PATCH] refactor: support proxy option --- bin/cli.ts | 1 + bin/defaults.ts | 1 + bin/helpers/merge.ts | 3 +++ bin/types.ts | 3 +++ src-tauri/Cargo.toml | 2 +- src-tauri/src/app/config.rs | 1 + src-tauri/src/app/window.rs | 9 +++++++-- 7 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bin/cli.ts b/bin/cli.ts index f71ec3d..2054d5f 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -33,6 +33,7 @@ program .option('--multi-arch', 'Only for Mac, supports both Intel and M1', DEFAULT.multiArch) .option('--inject [injects...]', 'Injection of .js or .css Files', DEFAULT.inject) .option('--debug', 'Debug build and more output', DEFAULT.debug) + .option('--proxy-url', "Proxy URL", DEFAULT.proxyUrl) .addOption(new Option('--user-agent ', 'Custom user agent').default(DEFAULT.userAgent).hideHelp()) .addOption( new Option('--targets ', 'Only for Linux, option "deb" or "appimage"').default(DEFAULT.targets).hideHelp(), diff --git a/bin/defaults.ts b/bin/defaults.ts index cc6ecdc..5093b3e 100644 --- a/bin/defaults.ts +++ b/bin/defaults.ts @@ -16,6 +16,7 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = { targets: 'deb', useLocalFile: false, systemTrayIcon: '', + proxyUrl: "", debug: false, inject: [], safeDomain: [], diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts index be8ac91..c334a9e 100644 --- a/bin/helpers/merge.ts +++ b/bin/helpers/merge.ts @@ -24,6 +24,7 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon name, resizable = true, inject, + proxyUrl, } = options; const { platform } = process; @@ -189,6 +190,8 @@ export async function mergeConfig(url: string, options: PakeAppOptions, tauriCon await fsExtra.writeFile(injectFilePath, ''); } + tauriConf.pake.proxy_url = proxyUrl || ""; + // Save config file. const platformConfigPaths: PlatformMap = { win32: 'tauri.windows.conf.json', diff --git a/bin/types.ts b/bin/types.ts index 8a5a7f7..105f71a 100644 --- a/bin/types.ts +++ b/bin/types.ts @@ -59,6 +59,9 @@ export interface PakeCliOptions { /* the domain that can use ipc or tauri javascript sdk */ safeDomain: string[]; + + // Proxy + proxyUrl: string; } export interface PakeAppOptions extends PakeCliOptions { diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 0de751f..0a57725 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -21,7 +21,7 @@ tauri-build = { version = "2.0.0-beta", features = [] } [dependencies] serde_json = "1.0.116" serde = { version = "1.0.200", features = ["derive"] } -tauri = { version = "2.0.0-beta.25", features = ["tray-icon", "image-ico", "image-png"] } +tauri = { version = "2.0.0-beta.25", features = ["tray-icon", "image-ico", "image-png", "macos-proxy"] } tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-plugin-oauth = { git = "https://github.com/FabianLars/tauri-plugin-oauth", branch = "v2" } tauri-plugin-clipboard-manager = "2.1.0-beta.6" diff --git a/src-tauri/src/app/config.rs b/src-tauri/src/app/config.rs index ba141b6..2763ca7 100644 --- a/src-tauri/src/app/config.rs +++ b/src-tauri/src/app/config.rs @@ -52,6 +52,7 @@ pub struct PakeConfig { pub user_agent: UserAgent, pub system_tray: FunctionON, pub system_tray_path: String, + pub proxy_url: String } impl PakeConfig { diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs index eb0027b..4e9fa7b 100644 --- a/src-tauri/src/app/window.rs +++ b/src-tauri/src/app/window.rs @@ -1,6 +1,6 @@ use crate::app::config::PakeConfig; -use std::path::PathBuf; -use tauri::{App, WebviewUrl, WebviewWindow, WebviewWindowBuilder}; +use std::{path::PathBuf, str::FromStr}; +use tauri::{App, Url, WebviewUrl, WebviewWindow, WebviewWindowBuilder}; #[cfg(target_os = "macos")] use tauri::TitleBarStyle; @@ -40,6 +40,11 @@ pub fn get_window(app: &mut App, config: &PakeConfig, _data_dir: PathBuf) -> Web //This is necessary to allow for file injection by external developers for customization purposes. .initialization_script(include_str!("../inject/custom.js")); + if config.proxy_url != "" { + println!("{}", &config.proxy_url); + window_builder = window_builder.proxy_url(Url::from_str(&config.proxy_url.as_str()).unwrap()); + } + #[cfg(target_os = "macos")] { let title_bar_style = if window_config.hide_title_bar {