Skip to content

Commit

Permalink
fix(clash): accpet clash rs status code and handle status error
Browse files Browse the repository at this point in the history
fix: lint
  • Loading branch information
greenhat616 committed Aug 1, 2024
1 parent afb0f1b commit 1ecfd4d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
38 changes: 19 additions & 19 deletions backend/tauri/src/core/clash/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::config::Config;
use anyhow::{bail, Result};
use indexmap::IndexMap;
use reqwest::header::HeaderMap;
use reqwest::{header::HeaderMap, StatusCode};
use serde::{Deserialize, Serialize};
use serde_yaml::Mapping;
use std::{
Expand All @@ -22,12 +22,12 @@ pub async fn put_configs(path: &str) -> Result<()> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.put(&url).headers(headers).json(&data);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

match response.status().as_u16() {
204 => Ok(()),
status => {
bail!("failed to put configs with status \"{status}\"")
match response.status() {
StatusCode::NO_CONTENT | StatusCode::ACCEPTED => Ok(()),
_ => {
bail!("failed to put configs")
}
}
}
Expand All @@ -40,7 +40,7 @@ pub async fn patch_configs(config: &Mapping) -> Result<()> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.patch(&url).headers(headers.clone()).json(config);
builder.send().await?;
builder.send().await?.error_for_status()?;
Ok(())
}

Expand Down Expand Up @@ -125,7 +125,7 @@ pub async fn get_proxies() -> Result<ProxiesRes> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.get(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<ProxiesRes>().await?)
}
Expand All @@ -143,7 +143,7 @@ pub async fn get_proxy(name: String) -> Result<ProxyItem> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.get(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<ProxyItem>().await?)
}
Expand All @@ -162,10 +162,10 @@ pub async fn update_proxy(group: &str, name: &str) -> Result<()> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.put(&url).headers(headers).json(&data);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

match response.status().as_u16() {
204 => Ok(()),
match response.status() {
StatusCode::ACCEPTED | StatusCode::NO_CONTENT => Ok(()),
status => {
bail!("failed to put proxy with status \"{status}\"")
}
Expand Down Expand Up @@ -227,7 +227,7 @@ pub async fn get_providers_proxies() -> Result<ProvidersProxiesRes> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.get(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<ProvidersProxiesRes>().await?)
}
Expand All @@ -243,7 +243,7 @@ pub async fn get_providers_proxies_group(group: String) -> Result<ProxyProviderI

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.get(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<ProxyProviderItem>().await?)
}
Expand All @@ -258,10 +258,10 @@ pub async fn update_providers_proxies_group(name: &str) -> Result<()> {

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.put(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

match response.status().as_u16() {
204 => Ok(()),
match response.status() {
StatusCode::NO_CONTENT | StatusCode::ACCEPTED => Ok(()),
status => {
bail!("failed to put providers proxies name with status \"{status}\"")
}
Expand All @@ -279,7 +279,7 @@ pub async fn get_providers_proxies_healthcheck(name: String) -> Result<Mapping>

let client = reqwest::ClientBuilder::new().no_proxy().build()?;
let builder = client.get(&url).headers(headers);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<Mapping>().await?)
}
Expand All @@ -305,7 +305,7 @@ pub async fn get_proxy_delay(name: String, test_url: Option<String>) -> Result<D
.get(&url)
.headers(headers)
.query(&[("timeout", "10000"), ("url", &test_url)]);
let response = builder.send().await?;
let response = builder.send().await?.error_for_status()?;

Ok(response.json::<DelayRes>().await?)
}
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/core/clash/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl CoreManager {
let config_path = Config::generate_file(ConfigType::Check)?;
let config_path = dirs::path_to_str(&config_path)?;

let clash_core = { Config::verge().latest().clash_core.clone() };
let clash_core = { Config::verge().latest().clash_core };
let clash_core = clash_core.unwrap_or(ClashCore::ClashPremium).to_string();

let app_dir = dirs::app_data_dir()?;
Expand Down
1 change: 0 additions & 1 deletion backend/tauri/src/core/updater/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ impl Updater {
let current_core = crate::config::Config::verge()
.latest()
.clash_core
.clone()
.unwrap_or_default();
if current_core == self.core_type {
CoreManager::global().stop_core().await?;
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/core/updater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl UpdaterManager {
let updater = Arc::new(
instance::UpdaterBuilder::new()
.set_client(self.client.clone())
.set_core_type(core_type.clone())
.set_core_type(*core_type)
.set_mirror(mirror)
.set_artifact(artifact)
.set_tag(tag)
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/enhance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn enhance() -> (Mapping, Vec<String>, IndexMap<String, Logs>) {
let verge = Config::verge();
let verge = verge.latest();
(
verge.clash_core.clone(),
verge.clash_core,
verge.enable_tun_mode.unwrap_or(false),
verge.enable_builtin_enhanced.unwrap_or(true),
verge.enable_clash_fields.unwrap_or(true),
Expand Down

0 comments on commit 1ecfd4d

Please sign in to comment.