wireguard_netstack/
lib.rs

1//! Userspace WireGuard tunnel with TCP/IP network stack.
2//!
3//! This crate provides:
4//! - WireGuard tunnel implementation using gotatun
5//! - Userspace TCP/IP stack using smoltcp
6//! - DNS-over-HTTPS resolver for privacy (with configurable DNS servers)
7//! - High-level `ManagedTunnel` for easy integration
8//!
9//! # DNS Configuration
10//!
11//! You can configure different DNS servers for:
12//! - **Pre-connection (direct mode)**: Used before the WireGuard tunnel is established
13//! - **Post-connection (tunnel mode)**: Used after the tunnel is up, queries go through VPN
14//!
15//! ```no_run
16//! use wireguard_netstack::{WgConfigFile, DohServerConfig, DnsConfig};
17//!
18//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
19//! // Use Google DNS for resolving the WireGuard endpoint
20//! let config = WgConfigFile::from_file("wg.conf")?
21//!     .into_wireguard_config_with_dns(DohServerConfig::google())
22//!     .await?;
23//! # Ok(())
24//! # }
25//! ```
26//!
27//! # Example
28//!
29//! ```no_run
30//! use wireguard_netstack::{ManagedTunnel, WgConfigFile, TcpConnection};
31//!
32//! #[tokio::main]
33//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
34//!     // Load WireGuard configuration
35//!     let config = WgConfigFile::from_file("wg.conf")?
36//!         .into_wireguard_config()
37//!         .await?;
38//!     
39//!     // Connect (handles all background tasks automatically)
40//!     let tunnel = ManagedTunnel::connect(config).await?;
41//!     
42//!     // Create a TCP connection through the tunnel
43//!     let addr = "93.184.216.34:80".parse()?;
44//!     let conn = TcpConnection::connect(tunnel.netstack(), addr).await?;
45//!     
46//!     // Use the connection...
47//!     conn.write_all(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n").await?;
48//!     
49//!     // Graceful shutdown
50//!     tunnel.shutdown().await;
51//!     Ok(())
52//! }
53//! ```
54
55pub mod config;
56pub mod dns;
57pub mod error;
58pub mod netstack;
59pub mod tunnel;
60pub mod wireguard;
61
62// Re-export main types
63pub use config::WgConfigFile;
64pub use dns::{DnsConfig, DohResolver, DohServerConfig};
65pub use error::{Error, Result};
66pub use netstack::{NetStack, TcpConnection};
67pub use tunnel::ManagedTunnel;
68pub use wireguard::{WireGuardConfig, WireGuardTunnel};