Compare commits
10 Commits
v0.1.0-bet
...
v0.1.0-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11d5305d8c | ||
|
|
c2d7f8d8c6 | ||
|
|
678ed158a6 | ||
|
|
56201f6b72 | ||
|
|
b160652737 | ||
|
|
7df5aa4317 | ||
|
|
99a2993ace | ||
|
|
fb7ad7ec40 | ||
|
|
87bb99c10b | ||
|
|
ba8e2b0aa1 |
13
README.md
13
README.md
@@ -35,7 +35,7 @@ Most VPNs treat TCP as a fallback. Konduit is designed for TCP from the ground u
|
|||||||
- **QR code provisioning** — scan once, connect instantly
|
- **QR code provisioning** — scan once, connect instantly
|
||||||
- **Cross-platform** — Linux, Windows, Android (macOS and iOS coming soon)
|
- **Cross-platform** — Linux, Windows, Android (macOS and iOS coming soon)
|
||||||
- **Modern cryptography** — X25519 key exchange, ChaCha20-Poly1305 data channel
|
- **Modern cryptography** — X25519 key exchange, ChaCha20-Poly1305 data channel
|
||||||
- **Stealth mode** — port 443 deployment with decoy proxy for hostile network environments
|
- **SOCKS5 chaining** — egress-dial (`use_proxy`) to reach the server through your own proxy, or a post-tunnel listener (`listen_socks`) for per-app routing — see [SOCKS Modes](docs/socks-modes.md)
|
||||||
- **Memory safe** — written entirely in Rust
|
- **Memory safe** — written entirely in Rust
|
||||||
|
|
||||||
## Download
|
## Download
|
||||||
@@ -95,7 +95,8 @@ Coming soon.
|
|||||||
**CLI & server**
|
**CLI & server**
|
||||||
- [Client Quickstart](docs/client-quickstart.md) — download, configure, connect, run as a systemd service
|
- [Client Quickstart](docs/client-quickstart.md) — download, configure, connect, run as a systemd service
|
||||||
- [Server Quickstart](docs/server-quickstart.md) — install, provision, NAT setup for iptables and firewalld
|
- [Server Quickstart](docs/server-quickstart.md) — install, provision, NAT setup for iptables and firewalld
|
||||||
- [Stealth Mode Setup](docs/stealth-setup.md) — HAProxy TCP passthrough + camouflage configuration
|
- [SOCKS5 Modes](docs/socks-modes.md) — reach the server through a proxy, or expose a local SOCKS5 listener
|
||||||
|
- [Connection Tuning](docs/connection-tuning.md) — opt-in connection pooling for unstable links
|
||||||
- [systemd units](docs/systemd/) — service files for konduit-server, konduit (client), and konduit-admin-ui
|
- [systemd units](docs/systemd/) — service files for konduit-server, konduit (client), and konduit-admin-ui
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
@@ -124,7 +125,7 @@ Konduit engine (Rust)
|
|||||||
|
|
||||||
The [`konduit-platform`](./konduit-platform) crate is published here for transparency and security audit. It contains the cryptographic primitives, connection statistics, and platform networking layer (TUN device, DNS, routes) — everything an auditor needs to verify what runs on your machine. It is licensed under the [PolyForm Noncommercial License 1.0.0](LICENSE) — free to read, study, and use for noncommercial purposes.
|
The [`konduit-platform`](./konduit-platform) crate is published here for transparency and security audit. It contains the cryptographic primitives, connection statistics, and platform networking layer (TUN device, DNS, routes) — everything an auditor needs to verify what runs on your machine. It is licensed under the [PolyForm Noncommercial License 1.0.0](LICENSE) — free to read, study, and use for noncommercial purposes.
|
||||||
|
|
||||||
The VPN server, management UI, and stealth-mode protocol are proprietary. Keeping stealth mechanisms private makes automated DPI fingerprinting significantly harder. Source review under NDA is available for enterprise partners.
|
The VPN server and management UI are proprietary. Source review under NDA is available for enterprise partners.
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
@@ -132,13 +133,9 @@ The VPN server, management UI, and stealth-mode protocol are proprietary. Keepin
|
|||||||
|
|
||||||
**Key storage:** Private keys are stored in the OS secure enclave on every platform (iOS Keychain, macOS Keychain, Android Keystore). They are never written to disk in plaintext.
|
**Key storage:** Private keys are stored in the OS secure enclave on every platform (iOS Keychain, macOS Keychain, Android Keystore). They are never written to disk in plaintext.
|
||||||
|
|
||||||
**Stealth mode:** On port 443, failed or unrecognized handshakes are proxied transparently to a configurable decoy service. From the outside, the server is indistinguishable from a standard HTTPS endpoint.
|
|
||||||
|
|
||||||
## Support
|
## Support
|
||||||
|
|
||||||
**Bug reports:** Use the in-app reporting feature or open an issue in this repository.
|
Bug reports, security vulnerabilities, and any other inquiries: use the contact form at [www.k-ops.eu](https://www.k-ops.eu).
|
||||||
|
|
||||||
**Security vulnerabilities:** Do not open a public issue. Contact the maintainer directly at the address shown in the application's About screen.
|
|
||||||
|
|
||||||
**Contributing:** Core development is handled internally. We do not currently accept external pull requests.
|
**Contributing:** Core development is handled internally. We do not currently accept external pull requests.
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ sudo cp client.toml /opt/konduit/client.toml
|
|||||||
sudo chmod 600 /opt/konduit/client.toml
|
sudo chmod 600 /opt/konduit/client.toml
|
||||||
```
|
```
|
||||||
|
|
||||||
If your server runs in stealth mode, the config already points to port 443. No additional client-side configuration is needed.
|
|
||||||
|
|
||||||
## 3. Connect
|
## 3. Connect
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
20
docs/connection-tuning.md
Normal file
20
docs/connection-tuning.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Connection Tuning
|
||||||
|
|
||||||
|
Konduit is a TCP-native VPN. On stable networks, a single TCP connection works fine. On unstable links (spotty Wi-Fi, mobile data, satellite), you can opt into a small pool of pre-warmed connections so a failing connection can be replaced without a full reconnect cascade.
|
||||||
|
|
||||||
|
## Config
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[connection]
|
||||||
|
pool_enabled = false # default off; opt in for unstable links
|
||||||
|
pool_size = 3 # pre-warmed idle connections when enabled
|
||||||
|
carry_duration = 1.0 # seconds an active connection carries traffic before handoff
|
||||||
|
```
|
||||||
|
|
||||||
|
- `pool_enabled` — when `true`, the client keeps `pool_size` extra pre-authenticated connections warm, so an in-use connection can fail over instantly instead of paying a fresh TCP-plus-handshake cost.
|
||||||
|
- `pool_size` — number of pre-warmed idle connections to keep, in addition to the active one. Only used when `pool_enabled = true`.
|
||||||
|
- `carry_duration` — how long (in seconds) an active connection carries traffic before handoff to a pre-warmed one. Only used when `pool_enabled = true`.
|
||||||
|
|
||||||
|
This is a resilience feature, not a stealth feature — it exists purely to make konduit more tolerant of flaky links, and has no effect on how konduit's traffic looks to a network observer.
|
||||||
|
|
||||||
|
Server-side, sticky DL-target routing keeps your active connection selected until it actually fails (detected via a write timeout), rather than rotating on a fixed timer. This needs no client-side configuration.
|
||||||
@@ -51,7 +51,6 @@ Server-side connection logs are retained for a limited period for operational pu
|
|||||||
|
|
||||||
- All traffic between client and server is encrypted using **X25519** key exchange and **ChaCha20-Poly1305** AEAD.
|
- All traffic between client and server is encrypted using **X25519** key exchange and **ChaCha20-Poly1305** AEAD.
|
||||||
- The pre-shared key (PSK) is stored in the local config file with permissions restricted to the current user.
|
- The pre-shared key (PSK) is stored in the local config file with permissions restricted to the current user.
|
||||||
- Stealth mode wraps the tunnel in a protocol that is indistinguishable from HTTPS, preventing deep-packet inspection from identifying Konduit traffic.
|
|
||||||
|
|
||||||
## Children
|
## Children
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ echo "your secret mantra phrase here" | ./konduit-ctl bootstrap -l vpn.example.c
|
|||||||
echo "your secret mantra phrase here" | ./konduit-ctl bootstrap -l vpn.example.com:8443 --public-port 443 -p -
|
echo "your secret mantra phrase here" | ./konduit-ctl bootstrap -l vpn.example.com:8443 --public-port 443 -p -
|
||||||
```
|
```
|
||||||
|
|
||||||
`--public-port` sets the port written into client configs, so they connect to 443 even though konduit listens on 8443. See [stealth-setup.md](stealth-setup.md) for the full HAProxy configuration.
|
`--public-port` sets the port written into client configs, so they connect to 443 even though konduit listens on 8443 — useful when running behind any TCP-passthrough reverse proxy (e.g. HAProxy, nginx `stream` module).
|
||||||
|
|
||||||
## 3. Add a Client
|
## 3. Add a Client
|
||||||
|
|
||||||
@@ -102,8 +102,8 @@ sudo apt install iptables-persistent && sudo netfilter-persistent save
|
|||||||
## 7. Verify
|
## 7. Verify
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Should return your website (camouflage) not an error
|
# Connect a client and confirm the tunnel comes up
|
||||||
curl -sk https://your-server/
|
./konduit --config client.toml
|
||||||
|
|
||||||
# Check konduit logs
|
# Check konduit logs
|
||||||
journalctl -u konduit-server -f
|
journalctl -u konduit-server -f
|
||||||
|
|||||||
50
docs/socks-modes.md
Normal file
50
docs/socks-modes.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# SOCKS5 Modes
|
||||||
|
|
||||||
|
Konduit is a VPN for reaching your own resources over the public internet — it is not a censorship-circumvention tool, and it doesn't try to disguise its traffic. If you need to get through a network that's actively blocking or fingerprinting VPN traffic, that's a job for a dedicated, purpose-built tool — an SSH SOCKS proxy (`ssh -D`), Xray, sing-box, or similar — not something konduit reimplements.
|
||||||
|
|
||||||
|
What konduit does provide is standard SOCKS5 support on both sides of the connection, so you can chain it with whichever of those tools you already trust.
|
||||||
|
|
||||||
|
## Egress-dial: reaching the server through a proxy (`use_proxy`)
|
||||||
|
|
||||||
|
If you already have a SOCKS5 proxy that gets you out of a restrictive network (for example `ssh -D 1080 jumphost`), point konduit at it instead of dialing the server directly:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[client]
|
||||||
|
server_endpoint = "vpn.example.com:443"
|
||||||
|
use_proxy = "socks5://127.0.0.1:1080"
|
||||||
|
```
|
||||||
|
|
||||||
|
Or via the CLI:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./konduit connect --config client.toml --use-proxy socks5://127.0.0.1:1080
|
||||||
|
```
|
||||||
|
|
||||||
|
Konduit dials the SOCKS5 proxy, asks it to `CONNECT` to your server, and then runs its normal protocol over that connection — the proxy is otherwise transparent to it. If the proxy is unreachable, or requires authentication konduit doesn't support, the connection attempt fails immediately; there is no silent fallback to a direct connection.
|
||||||
|
|
||||||
|
## Listener: exposing a SOCKS5 proxy after the tunnel is up (`listen_socks`)
|
||||||
|
|
||||||
|
Once connected, konduit can also expose a local SOCKS5 listener so other tools (browsers, curl, Xray outbounds) can route traffic through the tunnel without needing their own TUN-level integration:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[client]
|
||||||
|
listen_socks = "127.0.0.1:1080"
|
||||||
|
```
|
||||||
|
|
||||||
|
Or via the CLI:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./konduit connect --config client.toml --listen-socks 127.0.0.1:1080
|
||||||
|
```
|
||||||
|
|
||||||
|
Connections accepted by the listener are plain outbound TCP connections, routed through the tunnel by the kernel's routing table — the same routes any other tunnelled traffic uses. The SOCKS code itself has no VPN-specific logic.
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- SOCKS5 only, `CONNECT` command only — no `BIND`, no `UDP ASSOCIATE`.
|
||||||
|
- No authentication — bind it to localhost (the default) unless you understand the exposure of doing otherwise.
|
||||||
|
- If no address is given, konduit falls back to `127.0.0.1:1080`. Override the fallback itself with the `KONDUIT_SOCKS_LISTEN_ADDR` environment variable.
|
||||||
|
|
||||||
|
## Using both together
|
||||||
|
|
||||||
|
`use_proxy` and `listen_socks` are independent and can be combined — for example, dial out through an SSH SOCKS proxy to reach your server, and also expose a local SOCKS5 listener once connected for other apps to use.
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
# Stealth Mode: Real TLS Bypass via HAProxy
|
|
||||||
|
|
||||||
Stealth mode makes VPN traffic indistinguishable from normal HTTPS by wrapping it in
|
|
||||||
real TLS with a valid certificate. Deep packet inspection sees a standard TLS 1.3
|
|
||||||
connection to a legitimate domain — not a VPN.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## How It Works
|
|
||||||
|
|
||||||
1. **Client** opens a TLS 1.3 connection to port 443 using the system's trusted
|
|
||||||
certificate store — the same TLS stack as any browser.
|
|
||||||
2. **HAProxy** terminates TLS using a real certbot certificate. It then inspects the
|
|
||||||
first byte of the decrypted payload to route traffic:
|
|
||||||
- HTTP method bytes (`GET`, `POST`, `HEAD`, `PUT`, `DELETE`, `OPTIONS`) → web backend
|
|
||||||
- Any other byte → konduit VPN backend
|
|
||||||
3. **Konduit** receives a plain TCP connection and runs its normal handshake.
|
|
||||||
Stealth mode must be **disabled** on the server side — HAProxy already handled TLS.
|
|
||||||
|
|
||||||
The `stealth = true` and `tls = true` config flags are equivalent. Both activate TLS
|
|
||||||
wrapping on the client. Existing configs with `stealth = true` continue to work without
|
|
||||||
modification.
|
|
||||||
|
|
||||||
## Server Requirements
|
|
||||||
|
|
||||||
- A domain with a valid TLS certificate (Let's Encrypt / certbot)
|
|
||||||
- HAProxy 2.4+
|
|
||||||
- Konduit server on a non-public port (e.g. `8443`)
|
|
||||||
- Web server on a local port (e.g. `8080`) — for non-VPN HTTP requests
|
|
||||||
|
|
||||||
## HAProxy Configuration
|
|
||||||
|
|
||||||
Combine the certificate and private key into a single PEM file for HAProxy:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cat /etc/letsencrypt/live/your-domain.com/fullchain.pem \
|
|
||||||
/etc/letsencrypt/live/your-domain.com/privkey.pem \
|
|
||||||
> /etc/haproxy/ssl/your-domain.pem
|
|
||||||
chmod 600 /etc/haproxy/ssl/your-domain.pem
|
|
||||||
```
|
|
||||||
|
|
||||||
```haproxy
|
|
||||||
frontend https-ingress
|
|
||||||
bind *:443 ssl crt /etc/haproxy/ssl/your-domain.pem
|
|
||||||
mode tcp
|
|
||||||
option tcplog
|
|
||||||
tcp-request inspect-delay 3s
|
|
||||||
|
|
||||||
# Accept as soon as the first byte arrives — VPN connections never send a FIN,
|
|
||||||
# so WAIT_END would stall every connection for the full inspect-delay.
|
|
||||||
tcp-request content accept if { req.payload(0,1) -m found }
|
|
||||||
|
|
||||||
use_backend web if { req.payload(0,3) -m str GET }
|
|
||||||
use_backend web if { req.payload(0,4) -m str POST }
|
|
||||||
use_backend web if { req.payload(0,4) -m str HEAD }
|
|
||||||
use_backend web if { req.payload(0,3) -m str PUT }
|
|
||||||
use_backend web if { req.payload(0,6) -m str DELETE }
|
|
||||||
use_backend web if { req.payload(0,7) -m str OPTIONS }
|
|
||||||
|
|
||||||
default_backend konduit-vpn
|
|
||||||
|
|
||||||
backend konduit-vpn
|
|
||||||
mode tcp
|
|
||||||
server konduit 127.0.0.1:8443
|
|
||||||
|
|
||||||
backend web
|
|
||||||
mode http
|
|
||||||
server web 127.0.0.1:8080
|
|
||||||
```
|
|
||||||
|
|
||||||
Reload after changes:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
haproxy -c -f /etc/haproxy/haproxy.cfg # validate first
|
|
||||||
systemctl reload haproxy
|
|
||||||
```
|
|
||||||
|
|
||||||
## Konduit Server (`server.toml`)
|
|
||||||
|
|
||||||
Disable stealth on the server — HAProxy terminated TLS before the connection arrives:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[server]
|
|
||||||
listen_addr = "0.0.0.0"
|
|
||||||
listen_port = 8443
|
|
||||||
public_addr = "your-domain.com"
|
|
||||||
public_port = 443
|
|
||||||
|
|
||||||
[stealth]
|
|
||||||
enabled = false
|
|
||||||
```
|
|
||||||
|
|
||||||
Bootstrap peers with `--public-port` so generated client configs reference port 443:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
echo "your-mantra" | ./konduit-ctl bootstrap \
|
|
||||||
-l your-domain.com:8443 --public-port 443 -p -
|
|
||||||
```
|
|
||||||
|
|
||||||
## Client Configuration (`client.toml`)
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[client]
|
|
||||||
server_endpoint = "your-domain.com:443"
|
|
||||||
tls = true
|
|
||||||
peer_id = "..."
|
|
||||||
identity_key = "..."
|
|
||||||
server_public_key = "..."
|
|
||||||
```
|
|
||||||
|
|
||||||
The legacy flag is identical:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[stealth]
|
|
||||||
enabled = true
|
|
||||||
```
|
|
||||||
|
|
||||||
## Flutter / Mobile App
|
|
||||||
|
|
||||||
Enable **Stealth Mode** in the app settings. The toggle maps to `stealth = true` in
|
|
||||||
the connection config and activates TLS wrapping on all platforms (Linux, Android,
|
|
||||||
Windows).
|
|
||||||
|
|
||||||
For QR-code-based provisioning, the `t: true` field in the QR payload enables stealth.
|
|
||||||
|
|
||||||
## Verify
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# A browser must see your real website — not a TLS error or empty response
|
|
||||||
curl -s https://your-domain.com/ | head -5
|
|
||||||
|
|
||||||
# Connect the VPN client — should stay connected without 20-second drops
|
|
||||||
./konduit --config client.toml
|
|
||||||
|
|
||||||
# HAProxy serves both roles: check access log
|
|
||||||
journalctl -u haproxy -f
|
|
||||||
```
|
|
||||||
|
|
||||||
## Common Mistakes
|
|
||||||
|
|
||||||
| Mistake | Effect | Fix |
|
|
||||||
|---------|--------|-----|
|
|
||||||
| `WAIT_END` in inspect rule | 5-second stall on every connect | Use `req.payload(0,1) -m found` |
|
|
||||||
| Stealth enabled on server | Handshake mismatch after HAProxy strips TLS | Set `[stealth] enabled = false` |
|
|
||||||
| Port 8443 exposed to internet | Bypass HAProxy, no DPI camouflage | Firewall port 8443 to localhost only |
|
|
||||||
| Expired/self-signed cert | TLS error on client | Use Let's Encrypt; renew via certbot |
|
|
||||||
@@ -31,6 +31,31 @@ trait Resolve1Manager {
|
|||||||
fn revert_link(&self, ifindex: i32) -> zbus::Result<()>;
|
fn revert_link(&self, ifindex: i32) -> zbus::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Per-link object (/org/freedesktop/resolve1/link/_<ifindex>), used only to
|
||||||
|
// read back the physical interface's current DNS servers before we override
|
||||||
|
// them, so they can be restored byte-for-byte on disconnect.
|
||||||
|
//
|
||||||
|
// SetLinkDefaultRoute (the "correct" API for demoting a link as a default DNS
|
||||||
|
// route) requires interactive polkit auth (auth_admin_keep) — unlike
|
||||||
|
// SetLinkDNS/SetLinkDomains, it does NOT get a CAP_NET_ADMIN bypass in
|
||||||
|
// systemd-resolved, so it silently fails for this capability-only (non-root)
|
||||||
|
// process. Reusing SetLinkDNS on the physical link's own object instead stays
|
||||||
|
// on the bypass path.
|
||||||
|
#[zbus::proxy(interface = "org.freedesktop.resolve1.Link", default_service = "org.freedesktop.resolve1")]
|
||||||
|
trait Resolve1Link {
|
||||||
|
#[zbus(property, name = "DNS")]
|
||||||
|
fn dns(&self) -> zbus::Result<Vec<(i32, Vec<u8>)>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn link_object_path(if_index: u32) -> String {
|
||||||
|
let mut path = String::from("/org/freedesktop/resolve1/link/");
|
||||||
|
for c in if_index.to_string().chars() {
|
||||||
|
path.push_str("_3");
|
||||||
|
path.push(c);
|
||||||
|
}
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Public API
|
// Public API
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -38,6 +63,7 @@ trait Resolve1Manager {
|
|||||||
pub async fn set_dns(
|
pub async fn set_dns(
|
||||||
interface: &str,
|
interface: &str,
|
||||||
if_index: Option<u32>,
|
if_index: Option<u32>,
|
||||||
|
physical_if_index: Option<u32>,
|
||||||
dns_servers: &[IpAddr],
|
dns_servers: &[IpAddr],
|
||||||
_assigned_ip: IpAddr,
|
_assigned_ip: IpAddr,
|
||||||
state: &mut DnsState,
|
state: &mut DnsState,
|
||||||
@@ -59,6 +85,31 @@ pub async fn set_dns(
|
|||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
state.configured = true;
|
state.configured = true;
|
||||||
|
// Both the physical link and the tunnel link end up with
|
||||||
|
// Default Route: yes in systemd-resolved (the physical link
|
||||||
|
// never loses that flag just because another link gains it).
|
||||||
|
// With two "default route" resolvers, systemd-resolved can
|
||||||
|
// race queries between the VPN's DNS server and the ISP's,
|
||||||
|
// leaking lookups outside the tunnel and returning
|
||||||
|
// inconsistent/partial answers (e.g. AAAA-only responses).
|
||||||
|
// Blank out the physical link's own DNS server list so it has
|
||||||
|
// nothing to answer with, leaving the tunnel as the sole
|
||||||
|
// practical resolver; its original servers are saved and
|
||||||
|
// restored on disconnect.
|
||||||
|
if let Some(phys_idx) = physical_if_index {
|
||||||
|
match clear_physical_link_dns(phys_idx).await {
|
||||||
|
Ok(original) => {
|
||||||
|
state.demoted_physical_if_index = Some(phys_idx);
|
||||||
|
state.physical_dns_backup = original;
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(
|
||||||
|
"Failed to clear physical interface {} DNS servers: {}",
|
||||||
|
phys_idx, e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!(
|
warn!(
|
||||||
@@ -100,14 +151,28 @@ pub async fn restore(
|
|||||||
|
|
||||||
info!("Restoring DNS settings...");
|
info!("Restoring DNS settings...");
|
||||||
|
|
||||||
let result = match if_index {
|
// Always attempt both paths: D-Bus RevertLink clears per-link config in
|
||||||
Some(idx) => revert_link_dbus(idx).await,
|
// systemd-resolved's in-memory state; resolvectl revert is a belt-and-suspenders
|
||||||
None => revert_resolvectl(interface).await,
|
// pass that ensures the +DefaultRoute domain is removed even if D-Bus races
|
||||||
};
|
// with interface teardown.
|
||||||
|
if let Some(idx) = if_index {
|
||||||
|
if let Err(e) = revert_link_dbus(idx).await {
|
||||||
|
warn!("D-Bus RevertLink({}) failed: {}", idx, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Always run resolvectl revert as a second pass.
|
||||||
|
revert_resolvectl(interface).await.ok();
|
||||||
|
|
||||||
if let Err(e) = result {
|
// Restore the physical interface's original DNS servers we blanked out
|
||||||
warn!("D-Bus DNS revert failed ({}), trying resolvectl", e);
|
// in set_dns, or it's left with no resolver after the VPN disconnects.
|
||||||
revert_resolvectl(interface).await.ok();
|
if let Some(phys_idx) = state.demoted_physical_if_index.take() {
|
||||||
|
let backup = std::mem::take(&mut state.physical_dns_backup);
|
||||||
|
if let Err(e) = restore_physical_link_dns(phys_idx, backup).await {
|
||||||
|
warn!(
|
||||||
|
"Failed to restore physical interface {} DNS servers: {}",
|
||||||
|
phys_idx, e
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state.configured = false;
|
state.configured = false;
|
||||||
@@ -163,6 +228,62 @@ async fn set_dns_dbus(if_index: u32, dns_servers: &[IpAddr]) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads the physical link's current DNS servers, then blanks them out so it
|
||||||
|
/// stops acting as a competing resolver. Returns the original list to restore
|
||||||
|
/// later. Both the read and the clear go through the same CAP_NET_ADMIN
|
||||||
|
/// bypass path as the tunnel's own SetLinkDNS call.
|
||||||
|
async fn clear_physical_link_dns(if_index: u32) -> Result<Vec<(i32, Vec<u8>)>> {
|
||||||
|
let conn = zbus::Connection::system()
|
||||||
|
.await
|
||||||
|
.context("Failed to connect to D-Bus system bus")?;
|
||||||
|
|
||||||
|
let link_proxy = Resolve1LinkProxy::builder(&conn)
|
||||||
|
.path(link_object_path(if_index))
|
||||||
|
.context("Invalid link object path")?
|
||||||
|
.build()
|
||||||
|
.await
|
||||||
|
.context("Failed to create resolve1 link proxy")?;
|
||||||
|
|
||||||
|
let original = link_proxy
|
||||||
|
.dns()
|
||||||
|
.await
|
||||||
|
.context("Failed to read physical link DNS servers")?;
|
||||||
|
|
||||||
|
let manager_proxy = Resolve1ManagerProxy::new(&conn)
|
||||||
|
.await
|
||||||
|
.context("Failed to create resolve1 proxy")?;
|
||||||
|
|
||||||
|
manager_proxy
|
||||||
|
.set_link_dns(if_index as i32, vec![])
|
||||||
|
.await
|
||||||
|
.context("SetLinkDNS (clear) failed")?;
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Cleared physical interface {} DNS servers ({} saved for restore)",
|
||||||
|
if_index,
|
||||||
|
original.len()
|
||||||
|
);
|
||||||
|
Ok(original)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn restore_physical_link_dns(if_index: u32, original: Vec<(i32, Vec<u8>)>) -> Result<()> {
|
||||||
|
let conn = zbus::Connection::system()
|
||||||
|
.await
|
||||||
|
.context("Failed to connect to D-Bus system bus")?;
|
||||||
|
|
||||||
|
let proxy = Resolve1ManagerProxy::new(&conn)
|
||||||
|
.await
|
||||||
|
.context("Failed to create resolve1 proxy")?;
|
||||||
|
|
||||||
|
proxy
|
||||||
|
.set_link_dns(if_index as i32, original)
|
||||||
|
.await
|
||||||
|
.context("SetLinkDNS (restore) failed")?;
|
||||||
|
|
||||||
|
info!("Restored physical interface {} DNS servers", if_index);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn revert_link_dbus(if_index: u32) -> Result<()> {
|
async fn revert_link_dbus(if_index: u32) -> Result<()> {
|
||||||
let conn = zbus::Connection::system()
|
let conn = zbus::Connection::system()
|
||||||
.await
|
.await
|
||||||
@@ -172,6 +293,14 @@ async fn revert_link_dbus(if_index: u32) -> Result<()> {
|
|||||||
.await
|
.await
|
||||||
.context("Failed to create resolve1 proxy")?;
|
.context("Failed to create resolve1 proxy")?;
|
||||||
|
|
||||||
|
// Clear DNS servers and routing domains explicitly before the full revert.
|
||||||
|
// RevertLink alone can race with interface teardown in systemd-resolved;
|
||||||
|
// zeroing each setting first ensures they are gone even if RevertLink races.
|
||||||
|
let _ = proxy.set_link_dns(if_index as i32, vec![]).await;
|
||||||
|
let _ = proxy
|
||||||
|
.set_link_domains(if_index as i32, vec![])
|
||||||
|
.await;
|
||||||
|
|
||||||
proxy
|
proxy
|
||||||
.revert_link(if_index as i32)
|
.revert_link(if_index as i32)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ pub struct DnsManager {
|
|||||||
interface: String,
|
interface: String,
|
||||||
/// Interface index (if available/relevant)
|
/// Interface index (if available/relevant)
|
||||||
if_index: Option<u32>,
|
if_index: Option<u32>,
|
||||||
|
/// Pre-VPN default-gateway interface index, demoted as DNS default route
|
||||||
|
/// while connected (Linux only)
|
||||||
|
physical_if_index: Option<u32>,
|
||||||
/// IP address assigned to the TUN interface
|
/// IP address assigned to the TUN interface
|
||||||
assigned_ip: IpAddr,
|
assigned_ip: IpAddr,
|
||||||
/// State to track if we modified DNS
|
/// State to track if we modified DNS
|
||||||
@@ -26,14 +29,29 @@ pub struct DnsManager {
|
|||||||
struct DnsState {
|
struct DnsState {
|
||||||
configured: bool,
|
configured: bool,
|
||||||
_original_dns: Vec<IpAddr>,
|
_original_dns: Vec<IpAddr>,
|
||||||
|
// Physical interface demoted from systemd-resolved's default DNS route
|
||||||
|
// (Linux only) while the tunnel is the default; restored on disconnect.
|
||||||
|
demoted_physical_if_index: Option<u32>,
|
||||||
|
// Physical interface's original DNS servers (address_family, raw_bytes),
|
||||||
|
// saved before blanking them out; restored verbatim on disconnect.
|
||||||
|
physical_dns_backup: Vec<(i32, Vec<u8>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DnsManager {
|
impl DnsManager {
|
||||||
/// Create a new DNS manager for a specific interface
|
/// Create a new DNS manager for a specific interface.
|
||||||
pub fn new(interface: String, if_index: Option<u32>, assigned_ip: IpAddr) -> Self {
|
/// `physical_if_index` is the pre-VPN default-gateway interface (Linux
|
||||||
|
/// only) — needed so the tunnel can become the sole default DNS
|
||||||
|
/// resolver instead of racing systemd-resolved queries against it.
|
||||||
|
pub fn new(
|
||||||
|
interface: String,
|
||||||
|
if_index: Option<u32>,
|
||||||
|
physical_if_index: Option<u32>,
|
||||||
|
assigned_ip: IpAddr,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
interface,
|
interface,
|
||||||
if_index,
|
if_index,
|
||||||
|
physical_if_index,
|
||||||
assigned_ip,
|
assigned_ip,
|
||||||
state: Arc::new(Mutex::new(DnsState::default())),
|
state: Arc::new(Mutex::new(DnsState::default())),
|
||||||
}
|
}
|
||||||
@@ -51,7 +69,7 @@ impl DnsManager {
|
|||||||
// Platform specific implementation
|
// Platform specific implementation
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
{
|
{
|
||||||
linux::set_dns(&self.interface, self.if_index, dns_servers, self.assigned_ip, &mut state).await?;
|
linux::set_dns(&self.interface, self.if_index, self.physical_if_index, dns_servers, self.assigned_ip, &mut state).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use futures::stream::TryStreamExt;
|
use futures::stream::TryStreamExt;
|
||||||
use netlink_packet_route::route::RouteProtocol;
|
use netlink_packet_route::route::{RouteProtocol, RouteType};
|
||||||
use rtnetlink::{new_connection, Handle, IpVersion};
|
use rtnetlink::{new_connection, Handle, IpVersion};
|
||||||
use std::net::{IpAddr, Ipv4Addr};
|
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
// Added with priority lower than typical policy-routing stacks (e.g., xray uses 9001).
|
||||||
|
// Ensures konduit's main-table routes (tun0 default, server host-route) win in
|
||||||
|
// full-tunnel mode without modifying any third-party routing table.
|
||||||
|
const VPN_RULE_PRIORITY: u32 = 8000;
|
||||||
|
|
||||||
|
|
||||||
const VPN_ROUTE_METRIC: u32 = 50;
|
const VPN_ROUTE_METRIC: u32 = 50;
|
||||||
|
|
||||||
/// Manages routing table for VPN connection using netlink.
|
/// Manages routing table for VPN connection using netlink.
|
||||||
@@ -19,8 +25,21 @@ pub struct RouteManager {
|
|||||||
// Saved during setup for re-adding the server host route after resume.
|
// Saved during setup for re-adding the server host route after resume.
|
||||||
server_ip: Option<Ipv4Addr>,
|
server_ip: Option<Ipv4Addr>,
|
||||||
wifi_gateway: Option<Ipv4Addr>,
|
wifi_gateway: Option<Ipv4Addr>,
|
||||||
|
// Physical interface the server host route must be pinned to. Without this,
|
||||||
|
// if the VPN's assigned tun subnet happens to contain the physical gateway
|
||||||
|
// IP (e.g. both are 10.x.0.1), the kernel resolves the gateway via tun0's
|
||||||
|
// own directly-connected route instead of the physical NIC, looping the
|
||||||
|
// server route back into the tunnel it's meant to bypass.
|
||||||
|
wifi_iface_index: Option<u32>,
|
||||||
// Saved for re-adding the VPN default route after reconnect.
|
// Saved for re-adding the VPN default route after reconnect.
|
||||||
vpn_gateway: Option<Ipv4Addr>,
|
vpn_gateway: Option<Ipv4Addr>,
|
||||||
|
// True when we installed an ip rule to override policy-routing stacks.
|
||||||
|
added_policy_rule: bool,
|
||||||
|
// True when we installed an IPv6 "unreachable" default route to block
|
||||||
|
// IPv6 leaks (full-tunnel mode only pushes an IPv4 default route via
|
||||||
|
// tun0, so without this, any AAAA-resolved destination would route out
|
||||||
|
// the physical NIC directly, bypassing the tunnel entirely).
|
||||||
|
added_ipv6_blackhole: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RouteManager {
|
impl RouteManager {
|
||||||
@@ -35,10 +54,13 @@ impl RouteManager {
|
|||||||
added_routes: Vec::new(),
|
added_routes: Vec::new(),
|
||||||
tun_interface: tun_interface.clone(),
|
tun_interface: tun_interface.clone(),
|
||||||
tun_index: None,
|
tun_index: None,
|
||||||
dns_manager: crate::dns::DnsManager::new(tun_interface, None, IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
|
dns_manager: crate::dns::DnsManager::new(tun_interface, None, None, IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
|
||||||
server_ip: None,
|
server_ip: None,
|
||||||
wifi_gateway: None,
|
wifi_gateway: None,
|
||||||
|
wifi_iface_index: None,
|
||||||
vpn_gateway: None,
|
vpn_gateway: None,
|
||||||
|
added_policy_rule: false,
|
||||||
|
added_ipv6_blackhole: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,23 +79,58 @@ impl RouteManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_default_gateway(&self) -> Result<Ipv4Addr> {
|
/// Returns the physical default gateway and the interface it's reachable on.
|
||||||
|
/// The interface index must be used to pin any route meant to bypass the
|
||||||
|
/// tunnel — relying on gateway-only resolution lets the kernel route it via
|
||||||
|
/// tun0 instead if the VPN's assigned subnet happens to contain this gateway IP.
|
||||||
|
async fn get_default_gateway(&self) -> Result<(Ipv4Addr, u32)> {
|
||||||
|
use netlink_packet_route::route::RouteAddress;
|
||||||
|
|
||||||
let mut routes = self.handle.route().get(IpVersion::V4).execute();
|
let mut routes = self.handle.route().get(IpVersion::V4).execute();
|
||||||
|
// Collect all default-route gateways with their metrics.
|
||||||
|
// VPN default routes have low metric (50); physical/DHCP routes have high metric (600+).
|
||||||
|
// We want the physical gateway, so take the one with the highest metric.
|
||||||
|
//
|
||||||
|
// The metric comparison alone is not reliable: the kernel does not always
|
||||||
|
// round-trip the Priority attribute on route dumps (see the Oif-based match
|
||||||
|
// in suspend_default_route() below, which exists for the same reason), and a
|
||||||
|
// leftover default route on a reused tun ifindex from an imperfectly torn
|
||||||
|
// down previous session can win the metric comparison by accident. Since we
|
||||||
|
// already know our own tun interface's index at this point, exclude it
|
||||||
|
// outright rather than trusting metric alone to steer around it.
|
||||||
|
let mut candidates: Vec<(u32, Ipv4Addr, u32)> = Vec::new();
|
||||||
|
|
||||||
while let Some(route) = routes.try_next().await? {
|
while let Some(route) = routes.try_next().await? {
|
||||||
if route.header.destination_prefix_length == 0 {
|
if route.header.destination_prefix_length != 0 {
|
||||||
for nla in route.attributes.iter() {
|
continue;
|
||||||
if let netlink_packet_route::route::RouteAttribute::Gateway(addr) = nla {
|
}
|
||||||
use netlink_packet_route::route::RouteAddress;
|
let mut metric = 0u32;
|
||||||
if let RouteAddress::Inet(ipv4_addr) = addr {
|
let mut gateway: Option<Ipv4Addr> = None;
|
||||||
return Ok(*ipv4_addr);
|
let mut oif: Option<u32> = None;
|
||||||
}
|
for nla in route.attributes.iter() {
|
||||||
}
|
match nla {
|
||||||
|
netlink_packet_route::route::RouteAttribute::Gateway(
|
||||||
|
RouteAddress::Inet(ip),
|
||||||
|
) => gateway = Some(*ip),
|
||||||
|
netlink_packet_route::route::RouteAttribute::Priority(m) => metric = *m,
|
||||||
|
netlink_packet_route::route::RouteAttribute::Oif(idx) => oif = Some(*idx),
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let (Some(gw), Some(idx)) = (gateway, oif) {
|
||||||
|
if Some(idx) == self.tun_index {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
candidates.push((metric, gw, idx));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
anyhow::bail!("No default gateway found")
|
// Highest metric = physical/DHCP interface (not VPN).
|
||||||
|
candidates
|
||||||
|
.into_iter()
|
||||||
|
.max_by_key(|(m, _, _)| *m)
|
||||||
|
.map(|(_, gw, idx)| (gw, idx))
|
||||||
|
.ok_or_else(|| anyhow::anyhow!("No default gateway found"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Install routes for the VPN connection.
|
/// Install routes for the VPN connection.
|
||||||
@@ -99,11 +156,15 @@ impl RouteManager {
|
|||||||
self.tun_index.unwrap()
|
self.tun_index.unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
let current_gateway = self
|
let (current_gateway, wifi_iface_index) = self
|
||||||
.get_default_gateway()
|
.get_default_gateway()
|
||||||
.await
|
.await
|
||||||
.context("Failed to get current gateway")?;
|
.context("Failed to get current gateway")?;
|
||||||
info!("Current gateway: {}", current_gateway);
|
self.wifi_iface_index = Some(wifi_iface_index);
|
||||||
|
info!(
|
||||||
|
"Current gateway: {} (interface index {})",
|
||||||
|
current_gateway, wifi_iface_index
|
||||||
|
);
|
||||||
|
|
||||||
// Resolve server address to IP
|
// Resolve server address to IP
|
||||||
let server_ip = if let Ok(ip) = server_addr.parse::<IpAddr>() {
|
let server_ip = if let Ok(ip) = server_addr.parse::<IpAddr>() {
|
||||||
@@ -133,6 +194,7 @@ impl RouteManager {
|
|||||||
.v4()
|
.v4()
|
||||||
.destination_prefix(server_v4, 32)
|
.destination_prefix(server_v4, 32)
|
||||||
.gateway(current_gateway)
|
.gateway(current_gateway)
|
||||||
|
.output_interface(wifi_iface_index)
|
||||||
.priority(VPN_ROUTE_METRIC)
|
.priority(VPN_ROUTE_METRIC)
|
||||||
.execute()
|
.execute()
|
||||||
.await;
|
.await;
|
||||||
@@ -178,6 +240,9 @@ impl RouteManager {
|
|||||||
.map_err(|e| anyhow::anyhow!("Failed to add VPN default route: {:?}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to add VPN default route: {:?}", e))?;
|
||||||
|
|
||||||
self.added_routes.push((Ipv4Addr::new(0, 0, 0, 0), 0));
|
self.added_routes.push((Ipv4Addr::new(0, 0, 0, 0), 0));
|
||||||
|
|
||||||
|
self.install_policy_rule().await;
|
||||||
|
self.block_ipv6_leak().await;
|
||||||
} else {
|
} else {
|
||||||
info!(
|
info!(
|
||||||
"Split-tunnel mode: adding {} routes via VPN...",
|
"Split-tunnel mode: adding {} routes via VPN...",
|
||||||
@@ -239,14 +304,105 @@ impl RouteManager {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a high-priority ip rule so konduit's main-table routes override any
|
||||||
|
/// policy-routing stack (e.g., xray, sing-box) that intercepts traffic via a
|
||||||
|
/// separate routing table at priority ~9000.
|
||||||
|
///
|
||||||
|
/// Uses the same netlink handle as every other route change here, rather
|
||||||
|
/// than shelling out to `ip rule add` -- a child process spawned via
|
||||||
|
/// std::process::Command does NOT inherit capabilities granted to this
|
||||||
|
/// process via `setcap`/file capabilities (they'd need to be raised into
|
||||||
|
/// the ambient set first, which nothing here does), so the subprocess
|
||||||
|
/// silently failed with EPERM ("RTNETLINK answers: Operation not
|
||||||
|
/// permitted") every time, regardless of environment. That meant this
|
||||||
|
/// rule never actually got installed, so a competing policy-routing
|
||||||
|
/// stack (e.g. Xray) could keep overriding the VPN's own routes.
|
||||||
|
async fn install_policy_rule(&mut self) {
|
||||||
|
match self
|
||||||
|
.handle
|
||||||
|
.rule()
|
||||||
|
.add()
|
||||||
|
.v4()
|
||||||
|
.priority(VPN_RULE_PRIORITY)
|
||||||
|
// RuleAddRequest::new() defaults the rule's action to Unspec, not
|
||||||
|
// ToTable. A "from all" rule (matches every packet) with an
|
||||||
|
// Unspec action is not a valid "jump to main table" rule and the
|
||||||
|
// kernel does not fall through past it — it broke route lookups
|
||||||
|
// for ALL traffic, not just VPN traffic, until reboot.
|
||||||
|
.action(netlink_packet_route::rule::RuleAction::ToTable)
|
||||||
|
.execute()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(()) => {
|
||||||
|
self.added_policy_rule = true;
|
||||||
|
info!("Added ip rule priority {} → main table", VPN_RULE_PRIORITY);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("{:?}", e);
|
||||||
|
if err_msg.contains("File exists") || err_msg.contains("EEXIST") || err_msg.contains("code: Some(-17)")
|
||||||
|
{
|
||||||
|
self.added_policy_rule = true;
|
||||||
|
warn!("ip rule priority {} already exists, continuing...", VPN_RULE_PRIORITY);
|
||||||
|
} else {
|
||||||
|
warn!("Failed to add ip rule priority {}: {:?}", VPN_RULE_PRIORITY, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Block outbound IPv6 in full-tunnel mode by installing an "unreachable"
|
||||||
|
/// default route (::/0). Full-tunnel only pushes an IPv4 default route
|
||||||
|
/// via the VPN gateway (VPN servers here don't provide IPv6 transport),
|
||||||
|
/// so without this, any destination that resolves to an AAAA record
|
||||||
|
/// leaks straight out the physical interface instead of the tunnel —
|
||||||
|
/// bypassing it entirely rather than merely failing closed. Failing the
|
||||||
|
/// IPv6 attempt fast also lets Happy-Eyeballs clients fall back to IPv4
|
||||||
|
/// (through the tunnel) quickly instead of hanging on an IPv6 path that
|
||||||
|
/// silently goes nowhere.
|
||||||
|
async fn block_ipv6_leak(&mut self) {
|
||||||
|
let result = self
|
||||||
|
.handle
|
||||||
|
.route()
|
||||||
|
.add()
|
||||||
|
.v6()
|
||||||
|
.destination_prefix(Ipv6Addr::UNSPECIFIED, 0)
|
||||||
|
.kind(RouteType::Unreachable)
|
||||||
|
.priority(VPN_ROUTE_METRIC)
|
||||||
|
.execute()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(_) => {
|
||||||
|
self.added_ipv6_blackhole = true;
|
||||||
|
info!("✅ IPv6 default route blocked (prevents leaking outside tunnel)");
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("{:?}", e);
|
||||||
|
if err_msg.contains("File exists")
|
||||||
|
|| err_msg.contains("EEXIST")
|
||||||
|
|| err_msg.contains("code: Some(-17)")
|
||||||
|
{
|
||||||
|
self.added_ipv6_blackhole = true;
|
||||||
|
warn!("IPv6 blackhole route already exists, continuing...");
|
||||||
|
} else {
|
||||||
|
warn!("Failed to block IPv6 default route: {:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Setup DNS configuration
|
/// Setup DNS configuration
|
||||||
pub async fn setup_dns(&mut self, dns_servers: &[IpAddr], assigned_ip: IpAddr) -> Result<()> {
|
pub async fn setup_dns(&mut self, dns_servers: &[IpAddr], assigned_ip: IpAddr) -> Result<()> {
|
||||||
if dns_servers.is_empty() {
|
if dns_servers.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
self.dns_manager =
|
self.dns_manager = crate::dns::DnsManager::new(
|
||||||
crate::dns::DnsManager::new(self.tun_interface.clone(), self.tun_index, assigned_ip);
|
self.tun_interface.clone(),
|
||||||
|
self.tun_index,
|
||||||
|
self.wifi_iface_index,
|
||||||
|
assigned_ip,
|
||||||
|
);
|
||||||
|
|
||||||
self.dns_manager.set_dns(dns_servers).await
|
self.dns_manager.set_dns(dns_servers).await
|
||||||
}
|
}
|
||||||
@@ -269,16 +425,18 @@ impl RouteManager {
|
|||||||
_ => return Ok(()),
|
_ => return Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = self
|
let mut req = self
|
||||||
.handle
|
.handle
|
||||||
.route()
|
.route()
|
||||||
.add()
|
.add()
|
||||||
.v4()
|
.v4()
|
||||||
.destination_prefix(server_ip, 32)
|
.destination_prefix(server_ip, 32)
|
||||||
.gateway(gateway)
|
.gateway(gateway)
|
||||||
.priority(VPN_ROUTE_METRIC)
|
.priority(VPN_ROUTE_METRIC);
|
||||||
.execute()
|
if let Some(idx) = self.wifi_iface_index {
|
||||||
.await;
|
req = req.output_interface(idx);
|
||||||
|
}
|
||||||
|
let result = req.execute().await;
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
@@ -312,11 +470,13 @@ impl RouteManager {
|
|||||||
let mut to_delete = None;
|
let mut to_delete = None;
|
||||||
while let Some(route) = routes.try_next().await? {
|
while let Some(route) = routes.try_next().await? {
|
||||||
if route.header.destination_prefix_length == 0 {
|
if route.header.destination_prefix_length == 0 {
|
||||||
|
// Identify our default route by its output interface (tun0), not metric,
|
||||||
|
// since the kernel may not round-trip the Priority attribute reliably.
|
||||||
let is_ours = route.attributes.iter().any(|nla| {
|
let is_ours = route.attributes.iter().any(|nla| {
|
||||||
matches!(
|
matches!(
|
||||||
nla,
|
nla,
|
||||||
netlink_packet_route::route::RouteAttribute::Priority(p)
|
netlink_packet_route::route::RouteAttribute::Oif(idx)
|
||||||
if *p == VPN_ROUTE_METRIC
|
if Some(*idx) == self.tun_index
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
if is_ours {
|
if is_ours {
|
||||||
@@ -380,6 +540,55 @@ impl RouteManager {
|
|||||||
self.added_routes.len()
|
self.added_routes.len()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Remove the policy rule first so traffic stops using the VPN immediately.
|
||||||
|
// Same netlink-handle approach as the add side above -- `handle.rule().del()`
|
||||||
|
// needs the exact existing RuleMessage, so look it up by priority first.
|
||||||
|
if self.added_policy_rule {
|
||||||
|
let mut rules = self.handle.rule().get(IpVersion::V4).execute();
|
||||||
|
let mut to_delete = None;
|
||||||
|
while let Some(rule) = rules.try_next().await? {
|
||||||
|
let matches_priority = rule
|
||||||
|
.attributes
|
||||||
|
.iter()
|
||||||
|
.any(|attr| matches!(attr, netlink_packet_route::rule::RuleAttribute::Priority(p) if *p == VPN_RULE_PRIORITY));
|
||||||
|
if matches_priority {
|
||||||
|
to_delete = Some(rule);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match to_delete {
|
||||||
|
Some(rule) => match self.handle.rule().del(rule).execute().await {
|
||||||
|
Ok(()) => info!("Removed ip rule priority {}", VPN_RULE_PRIORITY),
|
||||||
|
Err(e) => warn!("Failed to delete ip rule priority {}: {:?}", VPN_RULE_PRIORITY, e),
|
||||||
|
},
|
||||||
|
None => warn!("ip rule priority {} not found, nothing to remove", VPN_RULE_PRIORITY),
|
||||||
|
}
|
||||||
|
self.added_policy_rule = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.added_ipv6_blackhole {
|
||||||
|
let mut routes = self.handle.route().get(IpVersion::V6).execute();
|
||||||
|
let mut to_delete = None;
|
||||||
|
while let Some(route) = routes.try_next().await? {
|
||||||
|
if route.header.destination_prefix_length == 0
|
||||||
|
&& route.header.kind == RouteType::Unreachable
|
||||||
|
{
|
||||||
|
to_delete = Some(route);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match to_delete {
|
||||||
|
Some(route) => match self.handle.route().del(route).execute().await {
|
||||||
|
Ok(()) => info!("Removed IPv6 blackhole route"),
|
||||||
|
Err(e) => warn!("Failed to delete IPv6 blackhole route: {:?}", e),
|
||||||
|
},
|
||||||
|
None => warn!("IPv6 blackhole route not found, nothing to remove"),
|
||||||
|
}
|
||||||
|
self.added_ipv6_blackhole = false;
|
||||||
|
}
|
||||||
|
|
||||||
if self.added_routes.is_empty() {
|
if self.added_routes.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -406,14 +615,21 @@ impl RouteManager {
|
|||||||
|
|
||||||
if let Some(ip) = dest_ip {
|
if let Some(ip) = dest_ip {
|
||||||
if self.added_routes.contains(&(ip, prefix_len)) {
|
if self.added_routes.contains(&(ip, prefix_len)) {
|
||||||
let is_ours = route.attributes.iter().any(|nla| {
|
// For the default route (0.0.0.0/0), match by output interface
|
||||||
matches!(
|
// (tun0), not just destination prefix — otherwise we also delete
|
||||||
nla,
|
// the WiFi default route (same prefix, different OIF/metric).
|
||||||
netlink_packet_route::route::RouteAttribute::Priority(p)
|
if prefix_len == 0 {
|
||||||
if *p == VPN_ROUTE_METRIC
|
let is_ours = route.attributes.iter().any(|nla| {
|
||||||
)
|
matches!(
|
||||||
});
|
nla,
|
||||||
if is_ours {
|
netlink_packet_route::route::RouteAttribute::Oif(idx)
|
||||||
|
if Some(*idx) == self.tun_index
|
||||||
|
)
|
||||||
|
});
|
||||||
|
if is_ours {
|
||||||
|
routes_to_delete.push(route);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
routes_to_delete.push(route);
|
routes_to_delete.push(route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -442,3 +658,69 @@ impl Drop for RouteManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use netlink_packet_route::rule::RuleAction;
|
||||||
|
|
||||||
|
const REEXEC_ENV_VAR: &str = "KORIDOR_ROUTE_TEST_IN_NETNS";
|
||||||
|
|
||||||
|
// `unshare(CLONE_NEWUSER)` fails with EINVAL if the calling process is
|
||||||
|
// multithreaded (which `cargo test`'s harness always is). So instead of
|
||||||
|
// calling the syscall in-process, re-exec this same test binary under
|
||||||
|
// the external `unshare --user --net --map-root-user` command, which
|
||||||
|
// starts a fresh, single-threaded process already inside an isolated
|
||||||
|
// user+net namespace -- no real root needed, and the host's actual
|
||||||
|
// routing rules are never touched.
|
||||||
|
fn run_in_isolated_netns(test_name: &str) {
|
||||||
|
let exe = std::env::current_exe().expect("current_exe");
|
||||||
|
let status = std::process::Command::new("unshare")
|
||||||
|
.args(["--user", "--net", "--map-root-user", "--"])
|
||||||
|
.arg(&exe)
|
||||||
|
.args(["--exact", test_name, "--nocapture", "--test-threads=1"])
|
||||||
|
.env(REEXEC_ENV_VAR, "1")
|
||||||
|
.status()
|
||||||
|
.expect("failed to spawn unshare (is util-linux's `unshare` installed?)");
|
||||||
|
assert!(status.success(), "test failed inside isolated netns (see output above)");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Regression test for the bug where connecting broke ALL host networking
|
||||||
|
// (not just VPN traffic) until a reboot: the policy rule installed to
|
||||||
|
// make konduit's routes win over other policy-routing stacks was built
|
||||||
|
// via `RuleAddRequest::new()`, which defaults `header.action` to
|
||||||
|
// `RuleAction::Unspec` — a "from all" rule (matches every packet) with
|
||||||
|
// no action is not a valid "jump to main table" rule, and the kernel
|
||||||
|
// does not fall through to the next rule for it, breaking route lookups
|
||||||
|
// system-wide. The fix must set the action explicitly to `ToTable`.
|
||||||
|
#[tokio::test(flavor = "current_thread")]
|
||||||
|
async fn policy_rule_uses_to_table_action() {
|
||||||
|
if std::env::var(REEXEC_ENV_VAR).is_err() {
|
||||||
|
run_in_isolated_netns("routes::linux::tests::policy_rule_uses_to_table_action");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut mgr = RouteManager::new("lo".to_string())
|
||||||
|
.await
|
||||||
|
.expect("failed to create RouteManager in isolated netns");
|
||||||
|
|
||||||
|
mgr.install_policy_rule().await;
|
||||||
|
|
||||||
|
let mut rules = mgr.handle.rule().get(IpVersion::V4).execute();
|
||||||
|
let mut found = false;
|
||||||
|
while let Some(rule) = rules.try_next().await.unwrap() {
|
||||||
|
let is_ours = rule.attributes.iter().any(|attr| {
|
||||||
|
matches!(attr, netlink_packet_route::rule::RuleAttribute::Priority(p) if *p == VPN_RULE_PRIORITY)
|
||||||
|
});
|
||||||
|
if is_ours {
|
||||||
|
assert_eq!(
|
||||||
|
rule.header.action,
|
||||||
|
RuleAction::ToTable,
|
||||||
|
"policy rule must use the ToTable action, or the kernel treats it as a black hole for every packet it matches (i.e. everything)"
|
||||||
|
);
|
||||||
|
found = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(found, "installed policy rule not found via rule().get()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ pub struct MacOsNetManager {
|
|||||||
server_host: String,
|
server_host: String,
|
||||||
original_gateway: String,
|
original_gateway: String,
|
||||||
primary_service: String,
|
primary_service: String,
|
||||||
|
// Physical interface the server host route must be pinned to via -ifscope.
|
||||||
|
// Without this, if the VPN's assigned tun subnet happens to contain the
|
||||||
|
// physical gateway IP, the OS can resolve the gateway via the tun
|
||||||
|
// interface's own directly-connected route instead of the physical NIC,
|
||||||
|
// looping the server route back into the tunnel it's meant to bypass.
|
||||||
|
wifi_iface: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacOsNetManager {
|
impl MacOsNetManager {
|
||||||
@@ -42,13 +48,18 @@ impl MacOsNetManager {
|
|||||||
server_host,
|
server_host,
|
||||||
original_gateway,
|
original_gateway,
|
||||||
primary_service,
|
primary_service,
|
||||||
|
wifi_iface: iface,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always adds split-default (0/1 + 128/1) for full-tunnel mode.
|
// Always adds split-default (0/1 + 128/1) for full-tunnel mode.
|
||||||
// Split-tunnel (per-route from ServerConfig) is not yet supported on macOS.
|
// Split-tunnel (per-route from ServerConfig) is not yet supported on macOS.
|
||||||
pub async fn setup_vpn_routes(&self) -> Result<()> {
|
pub async fn setup_vpn_routes(&self) -> Result<()> {
|
||||||
run("route", &["add", "-host", &self.server_host, &self.original_gateway]).await;
|
run(
|
||||||
|
"route",
|
||||||
|
&["add", "-host", &self.server_host, &self.original_gateway, "-ifscope", &self.wifi_iface],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
run("route", &["add", "-net", "0.0.0.0/1", "-interface", &self.tun_name]).await;
|
run("route", &["add", "-net", "0.0.0.0/1", "-interface", &self.tun_name]).await;
|
||||||
run("route", &["add", "-net", "128.0.0.0/1", "-interface", &self.tun_name]).await;
|
run("route", &["add", "-net", "128.0.0.0/1", "-interface", &self.tun_name]).await;
|
||||||
info!("macOS VPN routes added via {}", self.tun_name);
|
info!("macOS VPN routes added via {}", self.tun_name);
|
||||||
@@ -89,7 +100,11 @@ impl MacOsNetManager {
|
|||||||
// the wrong gateway until the next reconnect that creates a new MacOsNetManager.
|
// the wrong gateway until the next reconnect that creates a new MacOsNetManager.
|
||||||
pub async fn ensure_server_route(&self) -> Result<()> {
|
pub async fn ensure_server_route(&self) -> Result<()> {
|
||||||
run("route", &["delete", "-host", &self.server_host]).await;
|
run("route", &["delete", "-host", &self.server_host]).await;
|
||||||
run("route", &["add", "-host", &self.server_host, &self.original_gateway]).await;
|
run(
|
||||||
|
"route",
|
||||||
|
&["add", "-host", &self.server_host, &self.original_gateway, "-ifscope", &self.wifi_iface],
|
||||||
|
)
|
||||||
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ pub struct RouteManager {
|
|||||||
tun_routes: Vec<(Ipv4Addr, u8)>,
|
tun_routes: Vec<(Ipv4Addr, u8)>,
|
||||||
server_ip: Option<Ipv4Addr>,
|
server_ip: Option<Ipv4Addr>,
|
||||||
wifi_gateway: Option<Ipv4Addr>,
|
wifi_gateway: Option<Ipv4Addr>,
|
||||||
|
// Physical interface name the server host route must be pinned to. Without
|
||||||
|
// this, if the VPN's assigned tun subnet happens to contain the physical
|
||||||
|
// gateway IP, `route add`'s gateway-only resolution can pick the tun
|
||||||
|
// interface instead of the physical NIC, looping the server route back
|
||||||
|
// into the tunnel it's meant to bypass.
|
||||||
|
wifi_iface: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RouteManager {
|
impl RouteManager {
|
||||||
@@ -21,23 +27,30 @@ impl RouteManager {
|
|||||||
dns_manager: DnsManager::new(
|
dns_manager: DnsManager::new(
|
||||||
tun_interface.clone(),
|
tun_interface.clone(),
|
||||||
None,
|
None,
|
||||||
|
None,
|
||||||
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
|
IpAddr::V4(Ipv4Addr::UNSPECIFIED),
|
||||||
),
|
),
|
||||||
tun_interface,
|
tun_interface,
|
||||||
tun_routes: Vec::new(),
|
tun_routes: Vec::new(),
|
||||||
server_ip: None,
|
server_ip: None,
|
||||||
wifi_gateway: None,
|
wifi_gateway: None,
|
||||||
|
wifi_iface: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_default_gateway() -> Result<Ipv4Addr> {
|
/// Returns the physical default gateway and the friendly name of the
|
||||||
|
/// adapter it's reachable on. The interface name must be used to pin any
|
||||||
|
/// route meant to bypass the tunnel — relying on gateway-only resolution
|
||||||
|
/// lets `route add` route it via the TUN adapter instead if the VPN's
|
||||||
|
/// assigned subnet happens to contain this gateway IP.
|
||||||
|
fn get_default_gateway() -> Result<(Ipv4Addr, String)> {
|
||||||
let adapters = ipconfig::get_adapters()
|
let adapters = ipconfig::get_adapters()
|
||||||
.map_err(|e| anyhow::anyhow!("Failed to enumerate adapters: {}", e))?;
|
.map_err(|e| anyhow::anyhow!("Failed to enumerate adapters: {}", e))?;
|
||||||
for adapter in &adapters {
|
for adapter in &adapters {
|
||||||
for gw in adapter.gateways() {
|
for gw in adapter.gateways() {
|
||||||
if let IpAddr::V4(gw4) = gw {
|
if let IpAddr::V4(gw4) = gw {
|
||||||
if !gw4.is_loopback() && !gw4.is_unspecified() {
|
if !gw4.is_loopback() && !gw4.is_unspecified() {
|
||||||
return Ok(*gw4);
|
return Ok((*gw4, adapter.friendly_name().to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,29 +58,26 @@ impl RouteManager {
|
|||||||
anyhow::bail!("No default gateway found")
|
anyhow::bail!("No default gateway found")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prefix_to_mask(prefix: u8) -> Ipv4Addr {
|
// Add/delete the server host route through the physical WiFi/Ethernet
|
||||||
let bits = if prefix == 0 {
|
// interface, pinned explicitly by adapter name via `netsh` rather than
|
||||||
0u32
|
// `route add`'s gateway-only resolution — same rationale and technique as
|
||||||
} else {
|
// `tun_route_add` below: without an explicit interface, an IP collision
|
||||||
!0u32 << (32 - prefix)
|
// between the VPN's assigned tun subnet and the physical gateway can make
|
||||||
};
|
// this route resolve onto the TUN adapter instead, looping the server
|
||||||
let [a, b, c, d] = bits.to_be_bytes();
|
// route back into the tunnel it's meant to bypass.
|
||||||
Ipv4Addr::new(a, b, c, d)
|
fn wifi_route_add(iface: &str, dest: Ipv4Addr, prefix: u8, gateway: Ipv4Addr) {
|
||||||
}
|
let prefix_str = format!("{}/{}", dest, prefix);
|
||||||
|
let out = std::process::Command::new("netsh")
|
||||||
// Add/delete server host route through WiFi gateway via `route` command.
|
|
||||||
// Specifying gateway on delete ensures we only remove our entry, not any other path.
|
|
||||||
fn wifi_route_add(dest: Ipv4Addr, prefix: u8, gateway: Ipv4Addr) {
|
|
||||||
let mask = Self::prefix_to_mask(prefix);
|
|
||||||
let out = std::process::Command::new("route")
|
|
||||||
.args([
|
.args([
|
||||||
|
"interface",
|
||||||
|
"ipv4",
|
||||||
"add",
|
"add",
|
||||||
&dest.to_string(),
|
"route",
|
||||||
"MASK",
|
&prefix_str,
|
||||||
&mask.to_string(),
|
iface,
|
||||||
&gateway.to_string(),
|
&format!("nexthop={}", gateway),
|
||||||
"METRIC",
|
"metric=1",
|
||||||
"1",
|
"store=active",
|
||||||
])
|
])
|
||||||
.creation_flags(CREATE_NO_WINDOW)
|
.creation_flags(CREATE_NO_WINDOW)
|
||||||
.output();
|
.output();
|
||||||
@@ -76,25 +86,19 @@ impl RouteManager {
|
|||||||
let stdout = String::from_utf8_lossy(&o.stdout);
|
let stdout = String::from_utf8_lossy(&o.stdout);
|
||||||
if !o.status.success() {
|
if !o.status.success() {
|
||||||
// "already exists" is acceptable — a previous run may have left it
|
// "already exists" is acceptable — a previous run may have left it
|
||||||
warn!("route add {}/{} via {}: {}", dest, prefix, gateway, stdout.trim());
|
warn!("netsh add route {}/{} via {} on {}: {}", dest, prefix, gateway, iface, stdout.trim());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => warn!("route add {}/{} via {}: {}", dest, prefix, gateway, e),
|
Err(e) => warn!("netsh add route {}/{} via {} on {}: {}", dest, prefix, gateway, iface, e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wifi_route_delete(dest: Ipv4Addr, prefix: u8, gateway: Ipv4Addr) {
|
fn wifi_route_delete(iface: &str, dest: Ipv4Addr, prefix: u8) {
|
||||||
let mask = Self::prefix_to_mask(prefix);
|
let prefix_str = format!("{}/{}", dest, prefix);
|
||||||
let _ = std::process::Command::new("route")
|
let _ = std::process::Command::new("netsh")
|
||||||
.args([
|
.args(["interface", "ipv4", "delete", "route", &prefix_str, iface])
|
||||||
"delete",
|
|
||||||
&dest.to_string(),
|
|
||||||
"MASK",
|
|
||||||
&mask.to_string(),
|
|
||||||
&gateway.to_string(),
|
|
||||||
])
|
|
||||||
.creation_flags(CREATE_NO_WINDOW)
|
.creation_flags(CREATE_NO_WINDOW)
|
||||||
.status();
|
.output();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add/delete routes through the TUN interface using netsh.
|
// Add/delete routes through the TUN interface using netsh.
|
||||||
@@ -169,8 +173,9 @@ impl RouteManager {
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
info!("Setting up Windows VPN routes...");
|
info!("Setting up Windows VPN routes...");
|
||||||
|
|
||||||
let current_gateway = Self::get_default_gateway()?;
|
let (current_gateway, wifi_iface) = Self::get_default_gateway()?;
|
||||||
info!("Current gateway: {}", current_gateway);
|
info!("Current gateway: {} (interface {})", current_gateway, wifi_iface);
|
||||||
|
self.wifi_iface = Some(wifi_iface.clone());
|
||||||
|
|
||||||
let server_ip: Ipv4Addr = {
|
let server_ip: Ipv4Addr = {
|
||||||
let host = server_addr.split(':').next().unwrap_or(server_addr);
|
let host = server_addr.split(':').next().unwrap_or(server_addr);
|
||||||
@@ -189,7 +194,7 @@ impl RouteManager {
|
|||||||
// even if the add below fails because the route already exists.
|
// even if the add below fails because the route already exists.
|
||||||
self.server_ip = Some(server_ip);
|
self.server_ip = Some(server_ip);
|
||||||
self.wifi_gateway = Some(current_gateway);
|
self.wifi_gateway = Some(current_gateway);
|
||||||
Self::wifi_route_add(server_ip, 32, current_gateway);
|
Self::wifi_route_add(&wifi_iface, server_ip, 32, current_gateway);
|
||||||
|
|
||||||
let is_full_tunnel = routes.iter().any(|r| r == "0.0.0.0/0");
|
let is_full_tunnel = routes.iter().any(|r| r == "0.0.0.0/0");
|
||||||
|
|
||||||
@@ -233,7 +238,7 @@ impl RouteManager {
|
|||||||
if dns_servers.is_empty() {
|
if dns_servers.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
self.dns_manager = DnsManager::new(self.tun_interface.clone(), None, assigned_ip);
|
self.dns_manager = DnsManager::new(self.tun_interface.clone(), None, None, assigned_ip);
|
||||||
self.dns_manager.set_dns(dns_servers).await
|
self.dns_manager.set_dns(dns_servers).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,8 +247,10 @@ impl RouteManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ensure_server_route(&mut self) -> Result<()> {
|
pub async fn ensure_server_route(&mut self) -> Result<()> {
|
||||||
if let (Some(server_ip), Some(gateway)) = (self.server_ip, self.wifi_gateway) {
|
if let (Some(server_ip), Some(gateway), Some(iface)) =
|
||||||
Self::wifi_route_add(server_ip, 32, gateway);
|
(self.server_ip, self.wifi_gateway, self.wifi_iface.as_deref())
|
||||||
|
{
|
||||||
|
Self::wifi_route_add(iface, server_ip, 32, gateway);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -280,8 +287,9 @@ impl RouteManager {
|
|||||||
}
|
}
|
||||||
// Always clean up the server host route — a previous unclean exit may have
|
// Always clean up the server host route — a previous unclean exit may have
|
||||||
// left it behind even if our add was skipped.
|
// left it behind even if our add was skipped.
|
||||||
if let (Some(server_ip), Some(gateway)) = (self.server_ip.take(), self.wifi_gateway.take()) {
|
self.wifi_gateway.take();
|
||||||
Self::wifi_route_delete(server_ip, 32, gateway);
|
if let (Some(server_ip), Some(iface)) = (self.server_ip.take(), self.wifi_iface.take()) {
|
||||||
|
Self::wifi_route_delete(&iface, server_ip, 32);
|
||||||
}
|
}
|
||||||
info!("Windows VPN routes removed");
|
info!("Windows VPN routes removed");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -247,35 +247,6 @@ mod tests {
|
|||||||
assert_eq!(stats.download_bytes, 2048);
|
assert_eq!(stats.download_bytes, 2048);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_speed_calculation() {
|
|
||||||
let tracker = StatsTracker::new("utilbox.eu:8443".to_string(), "test".to_string());
|
|
||||||
|
|
||||||
// Record some traffic
|
|
||||||
tracker.record_upload(1024);
|
|
||||||
tracker.record_download(2048);
|
|
||||||
|
|
||||||
// Update to calculate speed
|
|
||||||
tracker.update();
|
|
||||||
|
|
||||||
let stats = tracker.get_stats(false);
|
|
||||||
assert_eq!(stats.upload_speed, 1024);
|
|
||||||
assert_eq!(stats.download_speed, 2048);
|
|
||||||
|
|
||||||
// Record more traffic
|
|
||||||
tracker.record_upload(512);
|
|
||||||
tracker.record_download(1024);
|
|
||||||
|
|
||||||
// Update again
|
|
||||||
tracker.update();
|
|
||||||
|
|
||||||
let stats = tracker.get_stats(false);
|
|
||||||
assert_eq!(stats.upload_bytes, 1536);
|
|
||||||
assert_eq!(stats.download_bytes, 3072);
|
|
||||||
assert_eq!(stats.upload_speed, 512);
|
|
||||||
assert_eq!(stats.download_speed, 1024);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ring_buffer() {
|
fn test_ring_buffer() {
|
||||||
let mut buffer = RingBuffer::new();
|
let mut buffer = RingBuffer::new();
|
||||||
|
|||||||
@@ -200,6 +200,35 @@ impl TunDevice {
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
|
biased;
|
||||||
|
|
||||||
|
// Server→TUN first: deliver incoming VPN traffic immediately.
|
||||||
|
//
|
||||||
|
// Deliberately NOT write_all(): a TUN character device requires each
|
||||||
|
// write() syscall to contain exactly one complete packet. write_all()
|
||||||
|
// retries a short write by sending the *remaining* bytes in a follow-up
|
||||||
|
// write() call -- correct for stream sockets, but for a TUN device that
|
||||||
|
// turns one packet into two malformed ones (a truncated first packet,
|
||||||
|
// plus a bogus "packet" made of leftover bytes with no valid IP header).
|
||||||
|
// A single write() either succeeds atomically or it doesn't; there is no
|
||||||
|
// safe way to "continue" a partial packet write, so treat anything short
|
||||||
|
// of a full write as a dropped packet, not a retry target.
|
||||||
|
Some(packet) = from_tcp_rx.recv() => {
|
||||||
|
match writer.write(&packet).await {
|
||||||
|
Ok(n) if n == packet.len() => {}
|
||||||
|
Ok(n) => {
|
||||||
|
error!(
|
||||||
|
"TUN short write: wrote {} of {} bytes -- packet dropped (TUN writes must be atomic per-packet)",
|
||||||
|
n, packet.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("TUN write error: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TUN→TCP: forward outgoing packets from the kernel.
|
||||||
packet = tun_pkt_rx.recv() => {
|
packet = tun_pkt_rx.recv() => {
|
||||||
match packet {
|
match packet {
|
||||||
Some(pkt) => {
|
Some(pkt) => {
|
||||||
@@ -213,11 +242,6 @@ impl TunDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(packet) = from_tcp_rx.recv() => {
|
|
||||||
if let Err(e) = writer.write_all(&packet).await {
|
|
||||||
error!("TUN write error: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user