feat: update profile supports noproxy

This commit is contained in:
GyDi 2022-01-16 22:57:42 +08:00
parent 9c43b31fc0
commit 43af55252d
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
4 changed files with 29 additions and 17 deletions

View File

@ -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` /// and save to `profiles.yaml`
#[tauri::command] #[tauri::command]
pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) -> Result<(), String> { pub async fn import_profile(
match fetch_profile(&url).await { url: String,
with_proxy: bool,
profiles: State<'_, ProfilesState>,
) -> Result<(), String> {
match fetch_profile(&url, with_proxy).await {
Some(result) => { Some(result) => {
let mut profiles = profiles.0.lock().unwrap(); let mut profiles = profiles.0.lock().unwrap();
profiles.import_from_url(url, result) profiles.import_from_url(url, result)
@ -43,12 +47,10 @@ pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) ->
} }
/// Update the profile /// Update the profile
/// and save to `profiles.yaml`
/// http request firstly
/// then acquire the lock of `profiles.yaml`
#[tauri::command] #[tauri::command]
pub async fn update_profile( pub async fn update_profile(
index: usize, index: usize,
with_proxy: bool,
clash: State<'_, ClashState>, clash: State<'_, ClashState>,
profiles: State<'_, ProfilesState>, profiles: State<'_, ProfilesState>,
) -> Result<(), String> { ) -> Result<(), String> {
@ -69,7 +71,7 @@ pub async fn update_profile(
Err(_) => return Err("failed to get profiles lock".into()), 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() { Some(result) => match profiles.0.lock() {
Ok(mut profiles) => { Ok(mut profiles) => {
profiles.update_item(index, result)?; profiles.update_item(index, result)?;

View File

@ -23,11 +23,20 @@ fn parse_string<T: FromStr>(target: &str, key: &str) -> Option<T> {
} }
/// fetch and parse the profile /// fetch and parse the profile
pub async fn fetch_profile(url: &str) -> Option<ProfileResponse> { pub async fn fetch_profile(url: &str, with_proxy: bool) -> Option<ProfileResponse> {
let resp = match reqwest::get(url).await { let builder = reqwest::ClientBuilder::new();
Ok(res) => res, 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, Err(_) => return None,
}; };
let header = resp.headers(); let header = resp.headers();
// parse the Subscription Userinfo // parse the Subscription Userinfo

View File

@ -59,12 +59,12 @@ const ProfileItem: React.FC<Props> = (props) => {
const progress = Math.round(((download + upload) * 100) / (total + 0.1)); const progress = Math.round(((download + upload) * 100) / (total + 0.1));
const fromnow = updated > 0 ? dayjs(updated * 1000).fromNow() : ""; const fromnow = updated > 0 ? dayjs(updated * 1000).fromNow() : "";
const onUpdate = async () => { const onUpdateWrapper = (withProxy: boolean) => async () => {
setAnchorEl(null); setAnchorEl(null);
if (loading) return; if (loading) return;
setLoading(true); setLoading(true);
try { try {
await updateProfile(index); await updateProfile(index, withProxy);
mutate("getProfiles"); mutate("getProfiles");
} catch (err: any) { } catch (err: any) {
Notice.error(err.toString()); Notice.error(err.toString());
@ -151,7 +151,7 @@ const ProfileItem: React.FC<Props> = (props) => {
disabled={loading} disabled={loading}
onClick={(e) => { onClick={(e) => {
e.stopPropagation(); e.stopPropagation();
onUpdate(); onUpdateWrapper(false)();
}} }}
> >
<RefreshRounded /> <RefreshRounded />
@ -202,7 +202,8 @@ const ProfileItem: React.FC<Props> = (props) => {
anchorPosition={position} anchorPosition={position}
anchorReference="anchorPosition" 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> <MenuItem onClick={onDelete}>Delete</MenuItem>
</Menu> </Menu>
</> </>

View File

@ -10,11 +10,11 @@ export async function syncProfiles() {
} }
export async function importProfile(url: string) { 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) { export async function updateProfile(index: number, withProxy: boolean) {
return invoke<void>("update_profile", { index }); return invoke<void>("update_profile", { index, withProxy });
} }
export async function deleteProfile(index: number) { export async function deleteProfile(index: number) {