Skip to content

Commit

Permalink
fix(proxies): use indexmap instead to correct order
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Feb 24, 2024
1 parent 483335d commit 4d250cc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ backon = "0.4.1"
rust-i18n = "3"
adler = "1.0.2"
rfd = "0.10" # should bump to v0.14 when clarify why the rfd v0.10 from tauri breaks build
indexmap = { version = "2.2.3", features = ["serde"] }

[target.'cfg(windows)'.dependencies]
deelevate = "0.2.0"
Expand Down
9 changes: 6 additions & 3 deletions backend/tauri/src/core/clash/api.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::config::Config;
use anyhow::{bail, Result};
use indexmap::IndexMap;
use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize};
use serde_yaml::Mapping;
use std::{
collections::{BTreeMap, HashMap},
collections::HashMap,
fmt::{self, Display, Formatter},
};

Expand Down Expand Up @@ -43,7 +44,8 @@ pub async fn patch_configs(config: &Mapping) -> Result<()> {
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProxiesRes {
pub proxies: Option<BTreeMap<String, ProxyItem>>,
#[serde(with = "indexmap::map::serde_seq", default)]
pub proxies: IndexMap<String, ProxyItem>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -206,7 +208,8 @@ pub struct ProxyProviderItem {
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProvidersProxiesRes {
pub providers: Option<BTreeMap<String, ProxyProviderItem>>,
#[serde(with = "indexmap::map::serde_seq", default)]
pub providers: IndexMap<String, ProxyProviderItem>,
}

/// GET /providers/proxies
Expand Down
17 changes: 8 additions & 9 deletions backend/tauri/src/core/clash/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ use super::{api, CLASH_API_DEFAULT_BACKOFF_STRATEGY};
use adler::adler32;
use anyhow::Result;
use backon::Retryable;
use indexmap::IndexMap;
use log::warn;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
sync::{Arc, OnceLock},
};
use std::sync::{Arc, OnceLock};
use tokio::{sync::broadcast, try_join};

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -62,7 +60,8 @@ pub struct Proxies {
pub global: ProxyGroupItem,
pub direct: api::ProxyItem,
pub groups: Vec<ProxyGroupItem>,
pub records: BTreeMap<String, api::ProxyItem>,
#[serde(with = "indexmap::map::serde_seq")]
pub records: IndexMap<String, api::ProxyItem>,
pub proxies: Vec<api::ProxyItem>,
}

Expand All @@ -75,10 +74,10 @@ impl Proxies {
let (inner_proxies, providers_proxies) = fetch_proxies
.retry(&*CLASH_API_DEFAULT_BACKOFF_STRATEGY)
.await?;
let inner_proxies = inner_proxies.proxies.unwrap_or_default();
let inner_proxies = inner_proxies.proxies;
// 1. filter out the Http or File type provider proxies
let providers_proxies: BTreeMap<String, api::ProxyProviderItem> = {
let records = providers_proxies.providers.unwrap_or_default();
let providers_proxies: IndexMap<String, api::ProxyProviderItem> = {
let records = providers_proxies.providers;
records
.into_iter()
.filter(|(_k, v)| {
Expand All @@ -91,7 +90,7 @@ impl Proxies {
};

// 2. mapping provider => providerProxiesItem to name => ProxyItem
let mut provider_map = BTreeMap::<String, api::ProxyItem>::new();
let mut provider_map = IndexMap::<String, api::ProxyItem>::new();
for (provider, record) in providers_proxies.iter() {
let name = record.name.clone();
let mut record: api::ProxyItem = record.clone().into();
Expand Down
4 changes: 2 additions & 2 deletions backend/tauri/src/core/tray/proxies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::core::{
clash::proxies::{Proxies, ProxiesGuard, ProxiesGuardExt},
handle::Handle,
};
use indexmap::IndexMap;
use log::{debug, error, warn};
use std::collections::BTreeMap;
use tauri::SystemTrayMenu;

async fn loop_task() {
Expand Down Expand Up @@ -46,7 +46,7 @@ struct TrayProxyItem {
current: Option<String>,
all: Vec<String>,
}
type TrayProxies = BTreeMap<String, TrayProxyItem>;
type TrayProxies = IndexMap<String, TrayProxyItem>;

/// Convert raw proxies to tray proxies
fn to_tray_proxies(mode: &str, raw_proxies: &Proxies) -> TrayProxies {
Expand Down

0 comments on commit 4d250cc

Please sign in to comment.