Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clash-lib/src/app/api/handlers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ pub fn routes(
Router::new()
.route(
"/",
get(get_configs).put(update_configs).patch(patch_configs),
get(get_configs)
.post(get_configs)
.put(update_configs)
.patch(patch_configs),
)
.with_state(ConfigState {
inbound_manager,
Expand Down
25 changes: 25 additions & 0 deletions clash-lib/src/app/api/handlers/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ pub struct GroupState {
pub fn routes(outbound_manager: ThreadSafeOutboundManager) -> Router<Arc<AppState>> {
let state = GroupState { outbound_manager };
Router::new()
.route("/", get(get_groups))
.nest(
"/{name}",
Router::new()
.route("/", get(get_group))
.route("/delay", get(get_group_delay))
.route_layer(middleware::from_fn_with_state(
state.clone(),
Expand All @@ -44,6 +46,29 @@ pub fn routes(outbound_manager: ThreadSafeOutboundManager) -> Router<Arc<AppStat
.with_state(state)
}

async fn get_groups(State(state): State<GroupState>) -> impl IntoResponse {
let outbound_manager = state.outbound_manager.clone();
let mut res = HashMap::new();
let groups = outbound_manager.get_groups().await;
res.insert("proxies".to_owned(), groups);
Json(res)
}

async fn get_group(
Extension(proxy): Extension<AnyOutboundHandler>,
State(state): State<GroupState>,
) -> impl IntoResponse {
if proxy.try_as_group_handler().is_none() {
return (
StatusCode::NOT_FOUND,
format!("proxy {} is not a group", proxy.name()),
)
.into_response();
}
let outbound_manager = state.outbound_manager.clone();
Json(outbound_manager.get_proxy(&proxy).await).into_response()
}

async fn find_group_by_name(
State(state): State<GroupState>,
Path(name): Path<String>,
Expand Down
4 changes: 2 additions & 2 deletions clash-lib/src/app/api/handlers/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use axum::{
http::{Request, StatusCode},
middleware::{self, Next},
response::{IntoResponse, Response},
routing::get,
routing::{any, get},
};
use serde::{Deserialize, Serialize};

Expand All @@ -33,7 +33,7 @@ struct ProviderState {
pub fn routes(outbound_manager: ThreadSafeOutboundManager) -> Router<Arc<AppState>> {
let state = ProviderState { outbound_manager };
Router::new()
.route("/", get(get_providers))
.route("/", any(get_providers))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this any?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems mihomo doesn't restrcit HTTP method. Could also be my misunderstand of tauri ipc mechanism

.nest(
"/{provider_name}",
Router::new()
Expand Down
4 changes: 2 additions & 2 deletions clash-lib/src/app/api/handlers/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use axum::{
http::Request,
middleware::{self, Next},
response::{IntoResponse, Response},
routing::get,
routing::{any, get},
};

use http::{HeaderMap, StatusCode, header};
Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn routes(
cache_store,
};
Router::new()
.route("/", get(get_proxies))
.route("/", any(get_proxies))
.nest(
"/{name}",
Router::new()
Expand Down
24 changes: 24 additions & 0 deletions clash-lib/src/app/outbound/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,30 @@ impl OutboundManager {
r
}

/// Get only group proxies (Selector, URLTest, Fallback, LoadBalance, Relay,
/// Smart).
pub async fn get_groups(&self) -> HashMap<String, Box<dyn Serialize + Send>> {
let mut r = HashMap::new();

let handlers: Vec<(String, AnyOutboundHandler)> = self
.registry
.read()
.await
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();

for (k, v) in handlers {
if let Some(g) = v.try_as_group_handler() {
let mut m = g.as_map().await;
self.apply_common_proxy_fields(&mut m, &v, &k).await;
r.insert(k.clone(), Box::new(m) as _);
}
}

r
}

pub async fn get_proxy(
&self,
proxy: &AnyOutboundHandler,
Expand Down
4 changes: 3 additions & 1 deletion clash-lib/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ pub fn serialize_duration<S>(
where
S: serde::Serializer,
{
serializer.serialize_u128(duration.as_millis())
let millis = duration.as_millis();
let capped = u16::try_from(millis).unwrap_or(u16::MAX);
serializer.serialize_u16(capped)
}

pub async fn download<P>(
Expand Down
2 changes: 1 addition & 1 deletion clash-lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Display for OutboundType {
OutboundType::Tuic => write!(f, "Tuic"),
OutboundType::Socks5 => write!(f, "Socks5"),
OutboundType::Hysteria2 => write!(f, "Hysteria2"),
OutboundType::Ssh => write!(f, "ssh"),
OutboundType::Ssh => write!(f, "Ssh"),
OutboundType::Tailscale => write!(f, "Tailscale"),
OutboundType::ShadowQuic => write!(f, "ShadowQuic"),

Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ test-no-docker:
CLASH_RS_CI=true cargo test --all --all-features

verge-win verge_path='C:\Apps\Clash Verge\verge-mihomo-alpha.exe':
cargo build -p clash-rs --release --features=standard
cargo build -p clash-rs --profile detailed-release --features=standard
rm -f "{{verge_path}}"
cp target/release/clash-rs.exe "{{verge_path}}"

test-api token='test_token' url='http://127.0.0.1:9093/proxies':
test-api token='test_token' url='http://127.0.0.1:9090/group':
curl -H "Authorization: Bearer {{token}}" {{url}}
Loading