Fix VPN internet connectivity and simplify client setup
Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
This commit is contained in:
@@ -77,6 +77,119 @@ impl Drop for Proxy {
|
||||
struct DnsGuard {
|
||||
child: Child,
|
||||
}
|
||||
|
||||
// Darwin IP_BOUND_IF sockets need an interface-scoped default when the TUN's
|
||||
// more-specific split defaults become the global route. Own only our new route.
|
||||
struct PhysicalRouteGuard {
|
||||
interface: String,
|
||||
gateway: std::net::Ipv4Addr,
|
||||
owned: bool,
|
||||
}
|
||||
fn route_field<'a>(text: &'a str, field: &str) -> Option<&'a str> {
|
||||
text.lines()
|
||||
.find_map(|line| line.trim().strip_prefix(field).map(str::trim))
|
||||
}
|
||||
fn scoped_route_matches(text: &str, interface: &str, gateway: std::net::Ipv4Addr) -> bool {
|
||||
route_field(text, "interface:") == Some(interface)
|
||||
&& route_field(text, "gateway:").and_then(|value| value.parse().ok()) == Some(gateway)
|
||||
&& route_field(text, "flags:").is_some_and(|flags| flags.contains("IFSCOPE"))
|
||||
}
|
||||
impl PhysicalRouteGuard {
|
||||
fn install(interface: &str) -> Result<Self> {
|
||||
let before = Command::new("/sbin/route")
|
||||
.args(["-n", "get", "-ifscope", interface, "default"])
|
||||
.output()?;
|
||||
ensure!(before.status.success(), "physical IPv4 gateway unavailable");
|
||||
let text = String::from_utf8(before.stdout)?;
|
||||
ensure!(
|
||||
route_field(&text, "interface:") == Some(interface),
|
||||
"physical gateway interface changed"
|
||||
);
|
||||
let gateway: std::net::Ipv4Addr = route_field(&text, "gateway:")
|
||||
.context("physical IPv4 gateway missing")?
|
||||
.parse()?;
|
||||
let mut guard = Self {
|
||||
interface: interface.into(),
|
||||
gateway,
|
||||
owned: false,
|
||||
};
|
||||
if scoped_route_matches(&text, interface, gateway) {
|
||||
return Ok(guard);
|
||||
}
|
||||
let installed = Command::new("/sbin/route")
|
||||
.args([
|
||||
"-n",
|
||||
"add",
|
||||
"-inet",
|
||||
"-ifscope",
|
||||
interface,
|
||||
"default",
|
||||
&gateway.to_string(),
|
||||
])
|
||||
.output()?;
|
||||
if !installed.status.success() {
|
||||
// Another owner may have installed it meanwhile. Reuse, never remove.
|
||||
let existing = Command::new("/sbin/route")
|
||||
.args(["-n", "get", "-ifscope", interface, "default"])
|
||||
.output()?;
|
||||
ensure!(
|
||||
existing.status.success()
|
||||
&& scoped_route_matches(
|
||||
&String::from_utf8_lossy(&existing.stdout),
|
||||
interface,
|
||||
gateway
|
||||
),
|
||||
"cannot protect physical IPv4 default route"
|
||||
);
|
||||
return Ok(guard);
|
||||
}
|
||||
guard.owned = true;
|
||||
let verified = Command::new("/sbin/route")
|
||||
.args(["-n", "get", "-ifscope", interface, "default"])
|
||||
.output()?;
|
||||
ensure!(
|
||||
verified.status.success()
|
||||
&& scoped_route_matches(
|
||||
&String::from_utf8_lossy(&verified.stdout),
|
||||
interface,
|
||||
gateway
|
||||
),
|
||||
"physical scoped route verification failed"
|
||||
);
|
||||
Ok(guard)
|
||||
}
|
||||
}
|
||||
impl Drop for PhysicalRouteGuard {
|
||||
fn drop(&mut self) {
|
||||
if !self.owned {
|
||||
return;
|
||||
}
|
||||
if let Ok(current) = Command::new("/sbin/route")
|
||||
.args(["-n", "get", "-ifscope", &self.interface, "default"])
|
||||
.output()
|
||||
{
|
||||
if current.status.success()
|
||||
&& scoped_route_matches(
|
||||
&String::from_utf8_lossy(¤t.stdout),
|
||||
&self.interface,
|
||||
self.gateway,
|
||||
)
|
||||
{
|
||||
let _ = Command::new("/sbin/route")
|
||||
.args([
|
||||
"-n",
|
||||
"delete",
|
||||
"-inet",
|
||||
"-ifscope",
|
||||
&self.interface,
|
||||
"default",
|
||||
&self.gateway.to_string(),
|
||||
])
|
||||
.output();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl DnsGuard {
|
||||
fn install() -> Result<Self> {
|
||||
let mut child = Command::new("/usr/sbin/scutil")
|
||||
@@ -122,6 +235,7 @@ impl Drop for DnsGuard {
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub(super) struct Unified {
|
||||
physical_route: Option<PhysicalRouteGuard>,
|
||||
dns: Option<DnsGuard>,
|
||||
proxy: Option<Proxy>,
|
||||
profile: Value,
|
||||
@@ -237,7 +351,13 @@ impl Unified {
|
||||
);
|
||||
}
|
||||
self.mesh = self.profile["feature"] != "proxy";
|
||||
let physical_route = if self.profile["feature"] != "mesh" {
|
||||
Some(PhysicalRouteGuard::install(&self.physical)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.proxy = Some(Proxy::open()?);
|
||||
self.physical_route = physical_route;
|
||||
self.proxy
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
@@ -326,6 +446,7 @@ impl Unified {
|
||||
.take()
|
||||
.and_then(|proxy| proxy.request(json!({"op":"stop"})).err());
|
||||
let network = super::call(id, &json!({"op":"stop"}));
|
||||
self.physical_route.take();
|
||||
self.protected.clear();
|
||||
self.active.clear();
|
||||
if let Some(error) = proxy_error {
|
||||
@@ -335,3 +456,22 @@ impl Unified {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod route_guard_tests {
|
||||
use super::scoped_route_matches;
|
||||
#[test]
|
||||
fn only_matching_scoped_route_is_owned() {
|
||||
let gateway = "192.168.21.1".parse().unwrap();
|
||||
let original = "gateway: 192.168.21.1\ninterface: en0\nflags: <UP,GATEWAY,GLOBAL>";
|
||||
assert!(!scoped_route_matches(original, "en0", gateway));
|
||||
let scoped = original.replace("GLOBAL", "IFSCOPE");
|
||||
assert!(scoped_route_matches(&scoped, "en0", gateway));
|
||||
assert!(!scoped_route_matches(&scoped, "en1", gateway));
|
||||
assert!(!scoped_route_matches(
|
||||
&scoped,
|
||||
"en0",
|
||||
"192.168.21.2".parse().unwrap()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user