use tokio::io::AsyncReadExt;
use tokio_util::sync::CancellationToken;
use tun::{AbstractDevice, BoxError};
#[tokio::main]
async fn main() -> Result<(), BoxError> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
let token = CancellationToken::new();
let token_clone = token.clone();
let ctrlc = ctrlc2::AsyncCtrlC::new(move || {
token_clone.cancel();
true
})?;
main_entry(token).await?;
ctrlc.await?;
Ok(())
}
async fn main_entry(token: CancellationToken) -> Result<(), BoxError> {
let mut config = tun::Configuration::default();
config
.address((10, 0, 0, 9))
.netmask((255, 255, 255, 0))
.destination((10, 0, 0, 1))
.mtu(tun::DEFAULT_MTU)
.up();
#[cfg(target_os = "linux")]
config.platform_config(|config| {
config.ensure_root_privileges(true);
});
let mut dev = tun::create_as_async(&config)?;
let size = dev.mtu()? as usize + tun::PACKET_INFORMATION_LENGTH;
let mut buf = vec![0; size];
loop {
tokio::select! {
_ = token.cancelled() => {
println!("Quit...");
break;
}
len = dev.read(&mut buf) => {
println!("pkt: {:?}", &buf[..len?]);
}
};
}
Ok(())
}