cli: put the local IP at the front (#1256)

This commit is contained in:
fanyang
2025-08-22 20:40:28 +08:00
committed by GitHub
parent 2e7e0088dd
commit 34560af141
3 changed files with 35 additions and 4 deletions
+26 -1
View File
@@ -25,6 +25,7 @@ use easytier::{
constants::EASYTIER_VERSION,
stun::{StunInfoCollector, StunInfoCollectorTrait},
},
peers,
proto::{
cli::{
list_peer_route_pair, AclManageRpc, AclManageRpcClientFactory, AddPortForwardRequest,
@@ -573,10 +574,34 @@ impl CommandHandler<'_> {
items.push(p.into());
}
// Sort items by ipv4 (using IpAddr for proper numeric comparison) first, then by hostname
// Sort items: local IP first, then public servers, then other servers by IP
items.sort_by(|a, b| {
use std::net::{IpAddr, Ipv4Addr};
use std::str::FromStr;
// Priority 1: Local IP (cost is "Local")
let a_is_local = a.cost == "Local";
let b_is_local = b.cost == "Local";
if a_is_local != b_is_local {
return if a_is_local {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
};
}
// Priority 2: Public servers
let a_is_public = a.hostname.starts_with(peers::PUBLIC_SERVER_HOSTNAME_PREFIX);
let b_is_public = b.hostname.starts_with(peers::PUBLIC_SERVER_HOSTNAME_PREFIX);
if a_is_public != b_is_public {
return if a_is_public {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Greater
};
}
// Priority 3: Sort by IP address
let a_ip = IpAddr::from_str(&a.ipv4).unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
let b_ip = IpAddr::from_str(&b.ipv4).unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
match a_ip.cmp(&b_ip) {