mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-19 02:12:10 +08:00
fix(core): only fail the macOS listener guard on real bind conflicts
Guard addresses can vanish or fail to bind for reasons unrelated to the port, and probe_claims turned that into a persisted mixed-port fallback.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use anyhow::{Context as _, Result, anyhow, bail};
|
||||
#[cfg(target_os = "macos")]
|
||||
use clash_verge_logging::{Type as LogType, logging};
|
||||
#[cfg(target_os = "macos")]
|
||||
use network_interface::{NetworkInterface, NetworkInterfaceConfig as _};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml_ng::{Mapping, Value};
|
||||
@@ -380,14 +382,25 @@ fn bind_claim(claim: &BindClaim) -> io::Result<Vec<Socket>> {
|
||||
// Hold every overlapping form while probing so they still report a conflict.
|
||||
let mut guard_addresses = if claim.address.is_unspecified() {
|
||||
NetworkInterface::show()
|
||||
.unwrap_or_default()
|
||||
.unwrap_or_else(|error| {
|
||||
// Losing the interface list shrinks the guard to loopback — say so.
|
||||
logging!(
|
||||
warn,
|
||||
LogType::Network,
|
||||
"unable to enumerate interfaces while probing {}; \
|
||||
only loopback guards the wildcard claim: {error}",
|
||||
claim.socket_addr()
|
||||
);
|
||||
Vec::new()
|
||||
})
|
||||
.into_iter()
|
||||
.flat_map(|interface| interface.addr)
|
||||
.map(|address| address.ip())
|
||||
.filter(|candidate| {
|
||||
// Link-local addresses are the likeliest to vanish mid-probe.
|
||||
candidate.is_ipv4() == claim.address.is_ipv4()
|
||||
&& match candidate {
|
||||
IpAddr::V4(address) => !address.is_unspecified(),
|
||||
IpAddr::V4(address) => !address.is_unspecified() && !address.is_link_local(),
|
||||
IpAddr::V6(address) => !address.is_unspecified() && !address.is_unicast_link_local(),
|
||||
}
|
||||
})
|
||||
@@ -409,12 +422,12 @@ fn bind_claim(claim: &BindClaim) -> io::Result<Vec<Socket>> {
|
||||
guard_addresses.sort_unstable();
|
||||
guard_addresses.dedup();
|
||||
for address in guard_addresses {
|
||||
sockets.push(bind_socket(&BindClaim::new(
|
||||
claim.name,
|
||||
address,
|
||||
claim.port,
|
||||
claim.transport,
|
||||
))?);
|
||||
match bind_socket(&BindClaim::new(claim.name, address, claim.port, claim.transport)) {
|
||||
Ok(socket) => sockets.push(socket),
|
||||
// Only a conflict proves the port is taken; a vanished guard address proves nothing.
|
||||
Err(error) if is_bind_conflict(&error) => return Err(error),
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,6 +539,41 @@ mod tests {
|
||||
Ok(serde_yaml_ng::from_str(yaml)?)
|
||||
}
|
||||
|
||||
/// The wildcard claim only reaches conflicts through the enumerated guards.
|
||||
#[cfg(target_os = "macos")]
|
||||
#[test]
|
||||
fn probe_reports_interface_tcp_listener_conflicts_for_the_wildcard_address() -> anyhow::Result<()> {
|
||||
use network_interface::{NetworkInterface, NetworkInterfaceConfig as _};
|
||||
use std::net::IpAddr;
|
||||
|
||||
let Some(address) = NetworkInterface::show()?
|
||||
.into_iter()
|
||||
.flat_map(|interface| interface.addr)
|
||||
.map(|address| address.ip())
|
||||
.find(|address| match address {
|
||||
IpAddr::V4(address) => !address.is_loopback() && !address.is_unspecified() && !address.is_link_local(),
|
||||
IpAddr::V6(_) => false,
|
||||
})
|
||||
else {
|
||||
// A host with no routable IPv4 address cannot host this conflict.
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let listener = TcpListener::bind((address, 0))?;
|
||||
let port = listener.local_addr()?.port();
|
||||
assert_eq!(
|
||||
probe_listener(&ListenerProbe {
|
||||
address: format!("0.0.0.0:{port}"),
|
||||
transports: vec![ListenerTransport::Tcp],
|
||||
}),
|
||||
ListenerProbeOutcome::Conflict {
|
||||
port,
|
||||
transport: ListenerTransport::Tcp
|
||||
}
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn probe_reports_tcp_conflicts_on_the_requested_address() -> anyhow::Result<()> {
|
||||
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?;
|
||||
|
||||
Reference in New Issue
Block a user