diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index ddefc0dd16269d9db7124b534daade7a34f727a7..a164718a3f1e102bd5301272636d8a5ff0430267 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -29,11 +29,15 @@ pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> { } } -/// Import the profile from url +/// import the profile from url /// and save to `profiles.yaml` #[tauri::command] -pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) -> Result<(), String> { - match fetch_profile(&url).await { +pub async fn import_profile( + url: String, + with_proxy: bool, + profiles: State<'_, ProfilesState>, +) -> Result<(), String> { + match fetch_profile(&url, with_proxy).await { Some(result) => { let mut profiles = profiles.0.lock().unwrap(); profiles.import_from_url(url, result) @@ -43,12 +47,10 @@ pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) -> } /// Update the profile -/// and save to `profiles.yaml` -/// http request firstly -/// then acquire the lock of `profiles.yaml` #[tauri::command] pub async fn update_profile( index: usize, + with_proxy: bool, clash: State<'_, ClashState>, profiles: State<'_, ProfilesState>, ) -> Result<(), String> { @@ -69,7 +71,7 @@ pub async fn update_profile( Err(_) => return Err("failed to get profiles lock".into()), }; - match fetch_profile(&url).await { + match fetch_profile(&url, with_proxy).await { Some(result) => match profiles.0.lock() { Ok(mut profiles) => { profiles.update_item(index, result)?; diff --git a/src-tauri/src/utils/fetch.rs b/src-tauri/src/utils/fetch.rs index ab39f59d033808f7df5e6820141031551e31b4b4..26fa1355d6803b1276a672c5575a53c853e91a56 100644 --- a/src-tauri/src/utils/fetch.rs +++ b/src-tauri/src/utils/fetch.rs @@ -23,11 +23,20 @@ fn parse_string<T: FromStr>(target: &str, key: &str) -> Option<T> { } /// fetch and parse the profile -pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> { - let resp = match reqwest::get(url).await { - Ok(res) => res, +pub async fn fetch_profile(url: &str, with_proxy: bool) -> Option<ProfileResponse> { + let builder = reqwest::ClientBuilder::new(); + let client = match with_proxy { + true => builder.build(), + false => builder.no_proxy().build(), + }; + let resp = match client { + Ok(client) => match client.get(url).send().await { + Ok(res) => res, + Err(_) => return None, + }, Err(_) => return None, }; + let header = resp.headers(); // parse the Subscription Userinfo diff --git a/src/components/profile-item.tsx b/src/components/profile-item.tsx index c83dcc1b02683d885d94149db8c1ad9d54fef02c..b3670a6b78b6dbb609cfd38cda4f710b0a334ca2 100644 --- a/src/components/profile-item.tsx +++ b/src/components/profile-item.tsx @@ -59,12 +59,12 @@ const ProfileItem: React.FC<Props> = (props) => { const progress = Math.round(((download + upload) * 100) / (total + 0.1)); const fromnow = updated > 0 ? dayjs(updated * 1000).fromNow() : ""; - const onUpdate = async () => { + const onUpdateWrapper = (withProxy: boolean) => async () => { setAnchorEl(null); if (loading) return; setLoading(true); try { - await updateProfile(index); + await updateProfile(index, withProxy); mutate("getProfiles"); } catch (err: any) { Notice.error(err.toString()); @@ -151,7 +151,7 @@ const ProfileItem: React.FC<Props> = (props) => { disabled={loading} onClick={(e) => { e.stopPropagation(); - onUpdate(); + onUpdateWrapper(false)(); }} > <RefreshRounded /> @@ -202,7 +202,8 @@ const ProfileItem: React.FC<Props> = (props) => { anchorPosition={position} anchorReference="anchorPosition" > - <MenuItem onClick={onUpdate}>Update</MenuItem> + <MenuItem onClick={onUpdateWrapper(false)}>Update</MenuItem> + <MenuItem onClick={onUpdateWrapper(true)}>Update(Proxy)</MenuItem> <MenuItem onClick={onDelete}>Delete</MenuItem> </Menu> </> diff --git a/src/services/cmds.ts b/src/services/cmds.ts index d309c19e69bf45fd4e00d41df2c6e3cc1839c11c..eab9cc3c1a2840126c0efcfff65d2c7de229812a 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -10,11 +10,11 @@ export async function syncProfiles() { } export async function importProfile(url: string) { - return invoke<void>("import_profile", { url }); + return invoke<void>("import_profile", { url, withProxy: true }); } -export async function updateProfile(index: number) { - return invoke<void>("update_profile", { index }); +export async function updateProfile(index: number, withProxy: boolean) { + return invoke<void>("update_profile", { index, withProxy }); } export async function deleteProfile(index: number) {