Skip to content

Commit

Permalink
fix(proxies): use indexmap instead to correct order
Browse files Browse the repository at this point in the history
chore: rm useless debug response

fix: issues
  • Loading branch information
greenhat616 committed Feb 24, 2024
1 parent 483335d commit e0db001
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 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(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(default)]
pub providers: IndexMap<String, ProxyProviderItem>,
}

/// GET /providers/proxies
Expand Down
16 changes: 7 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,7 @@ pub struct Proxies {
pub global: ProxyGroupItem,
pub direct: api::ProxyItem,
pub groups: Vec<ProxyGroupItem>,
pub records: BTreeMap<String, api::ProxyItem>,
pub records: IndexMap<String, api::ProxyItem>,
pub proxies: Vec<api::ProxyItem>,
}

Expand All @@ -75,10 +73,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 +89,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
20 changes: 13 additions & 7 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 All @@ -21,11 +21,13 @@ async fn loop_task() {
if guard.updated_at() == 0 {
error!(target: "tray", "proxies not updated yet!!!!");
// TODO: add a error dialog or notification, and panic?
} else {
let proxies = guard.inner();
let str = simd_json::to_string_pretty(proxies).unwrap();
debug!(target: "tray", "proxies info: {:?}", str);
}

// else {
// let proxies = guard.inner();
// let str = simd_json::to_string_pretty(proxies).unwrap();
// debug!(target: "tray", "proxies info: {:?}", str);
// }
}
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; // TODO: add a config to control the interval
}
Expand All @@ -46,7 +48,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 Expand Up @@ -156,6 +158,7 @@ pub async fn proxies_updated_receiver() {

match diff_proxies(&tray_proxies_holder, &current_tray_proxies) {
TrayUpdateType::Full => {
debug!(target: "tray::proxies", "should do full update");
tray_proxies_holder = current_tray_proxies;
match Handle::update_systray() {
Ok(_) => {
Expand All @@ -167,6 +170,7 @@ pub async fn proxies_updated_receiver() {
}
}
TrayUpdateType::Part(action_list) => {
debug!(target: "tray::proxies", "should do partial update, op list: {:?}", action_list);
tray_proxies_holder = current_tray_proxies;
platform_impl::update_selected_proxies(&action_list);
debug!(target: "tray::proxies", "update selected proxies success");
Expand Down Expand Up @@ -239,7 +243,9 @@ mod platform_impl {
for action in actions {
let from = format!("select_proxy_{}_{}", action.0, action.1);
let to = format!("select_proxy_{}_{}", action.0, action.2);
let _ = tray.get_item(&from).set_selected(false);
if let Some(item) = tray.try_get_item(&from) {
let _ = item.set_selected(false);
}
let _ = tray.get_item(&to).set_selected(true);
}
}
Expand Down

0 comments on commit e0db001

Please sign in to comment.