mirror of
https://github.com/aramperes/onetun.git
synced 2025-09-09 06:38:32 -04:00
Virtual interface task
This commit is contained in:
parent
492875c392
commit
7b5aefa623
1 changed files with 39 additions and 2 deletions
41
src/main.rs
41
src/main.rs
|
@ -2,7 +2,9 @@
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use tokio::io::Interest;
|
use tokio::io::Interest;
|
||||||
|
@ -110,12 +112,24 @@ async fn handle_tcp_proxy_connection(
|
||||||
virtual_port: u16,
|
virtual_port: u16,
|
||||||
wg: Arc<WireGuardTunnel>,
|
wg: Arc<WireGuardTunnel>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
|
let abort = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
|
// Spawn virtual interface
|
||||||
|
{
|
||||||
|
let abort = abort.clone();
|
||||||
|
tokio::spawn(async move { virtual_tcp_interface(virtual_port, wg, abort).await });
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let ready = socket
|
let ready = socket
|
||||||
.ready(Interest::READABLE | Interest::WRITABLE)
|
.ready(Interest::READABLE | Interest::WRITABLE)
|
||||||
.await
|
.await
|
||||||
.with_context(|| "Failed to wait for TCP proxy socket readiness")?;
|
.with_context(|| "Failed to wait for TCP proxy socket readiness")?;
|
||||||
|
|
||||||
|
if abort.load(Ordering::Relaxed) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if ready.is_readable() {
|
if ready.is_readable() {
|
||||||
let mut buffer = [0u8; MAX_PACKET];
|
let mut buffer = [0u8; MAX_PACKET];
|
||||||
|
|
||||||
|
@ -132,7 +146,11 @@ async fn handle_tcp_proxy_connection(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(e).with_context(|| "Failed to read from real client TCP socket");
|
error!(
|
||||||
|
"[{}] Failed to read from client TCP socket: {:?}",
|
||||||
|
virtual_port, e
|
||||||
|
);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +159,26 @@ async fn handle_tcp_proxy_connection(
|
||||||
if ready.is_writable() {}
|
if ready.is_writable() {}
|
||||||
|
|
||||||
if ready.is_read_closed() || ready.is_write_closed() {
|
if ready.is_read_closed() || ready.is_write_closed() {
|
||||||
return Ok(());
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trace!("[{}] TCP socket handler task terminated", virtual_port);
|
||||||
|
abort.store(true, Ordering::Relaxed);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn virtual_tcp_interface(
|
||||||
|
virtual_port: u16,
|
||||||
|
wg: Arc<WireGuardTunnel>,
|
||||||
|
abort: Arc<AtomicBool>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
loop {
|
||||||
|
if abort.load(Ordering::Relaxed) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
}
|
||||||
|
trace!("[{}] Virtual interface task terminated", virtual_port);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue