mirror of
https://mirror.suhoan.cn/https://github.com/EasyTier/EasyTier.git
synced 2025-12-12 12:47:25 +08:00
cli: put the local IP at the front (#1256)
This commit is contained in:
@@ -25,6 +25,7 @@ use easytier::{
|
|||||||
constants::EASYTIER_VERSION,
|
constants::EASYTIER_VERSION,
|
||||||
stun::{StunInfoCollector, StunInfoCollectorTrait},
|
stun::{StunInfoCollector, StunInfoCollectorTrait},
|
||||||
},
|
},
|
||||||
|
peers,
|
||||||
proto::{
|
proto::{
|
||||||
cli::{
|
cli::{
|
||||||
list_peer_route_pair, AclManageRpc, AclManageRpcClientFactory, AddPortForwardRequest,
|
list_peer_route_pair, AclManageRpc, AclManageRpcClientFactory, AddPortForwardRequest,
|
||||||
@@ -573,10 +574,34 @@ impl CommandHandler<'_> {
|
|||||||
items.push(p.into());
|
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| {
|
items.sort_by(|a, b| {
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr};
|
||||||
use std::str::FromStr;
|
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 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));
|
let b_ip = IpAddr::from_str(&b.ipv4).unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
|
||||||
match a_ip.cmp(&b_ip) {
|
match a_ip.cmp(&b_ip) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
foreign_network_manager is used to forward packets of other networks. currently
|
foreign_network_manager is used to forward packets of other networks. currently
|
||||||
only forward packets of peers that directly connected to this node.
|
only forward packets of peers that directly connected to this node.
|
||||||
|
|
||||||
in future, with the help wo peer center we can forward packets of peers that
|
in the future, with the help wo peer center we can forward packets of peers that
|
||||||
connected to any node in the local network.
|
connected to any node in the local network.
|
||||||
*/
|
*/
|
||||||
use std::{
|
use std::{
|
||||||
@@ -49,7 +49,7 @@ use super::{
|
|||||||
peer_rpc_service::DirectConnectorManagerRpcServer,
|
peer_rpc_service::DirectConnectorManagerRpcServer,
|
||||||
recv_packet_from_chan,
|
recv_packet_from_chan,
|
||||||
route_trait::NextHopPolicy,
|
route_trait::NextHopPolicy,
|
||||||
PacketRecvChan, PacketRecvChanReceiver,
|
PacketRecvChan, PacketRecvChanReceiver, PUBLIC_SERVER_HOSTNAME_PREFIX,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
@@ -161,7 +161,11 @@ impl ForeignNetworkEntry {
|
|||||||
) -> ArcGlobalCtx {
|
) -> ArcGlobalCtx {
|
||||||
let config = TomlConfigLoader::default();
|
let config = TomlConfigLoader::default();
|
||||||
config.set_network_identity(network.clone());
|
config.set_network_identity(network.clone());
|
||||||
config.set_hostname(Some(format!("PublicServer_{}", global_ctx.get_hostname())));
|
config.set_hostname(Some(format!(
|
||||||
|
"{}{}",
|
||||||
|
PUBLIC_SERVER_HOSTNAME_PREFIX,
|
||||||
|
global_ctx.get_hostname()
|
||||||
|
)));
|
||||||
|
|
||||||
let mut flags = config.get_flags();
|
let mut flags = config.get_flags();
|
||||||
flags.disable_relay_kcp = !global_ctx.get_flags().enable_relay_foreign_network_kcp;
|
flags.disable_relay_kcp = !global_ctx.get_flags().enable_relay_foreign_network_kcp;
|
||||||
|
|||||||
@@ -64,3 +64,5 @@ pub async fn recv_packet_from_chan(
|
|||||||
.await
|
.await
|
||||||
.ok_or(anyhow::anyhow!("recv_packet_from_chan failed"))
|
.ok_or(anyhow::anyhow!("recv_packet_from_chan failed"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const PUBLIC_SERVER_HOSTNAME_PREFIX: &str = "PublicServer_";
|
||||||
|
|||||||
Reference in New Issue
Block a user