feat(server) add dead connection detection

This commit is contained in:
Datong Sun 2021-09-16 14:09:43 -07:00 committed by Datong Sun
parent 7c06c5b08b
commit e869255785
2 changed files with 22 additions and 0 deletions

View File

@ -1,9 +1,12 @@
use clap::{App, Arg}; use clap::{App, Arg};
use log::info;
use phantom::fake_tcp::packet::MAX_PACKET_LEN; use phantom::fake_tcp::packet::MAX_PACKET_LEN;
use phantom::fake_tcp::Stack; use phantom::fake_tcp::Stack;
use std::net::SocketAddrV4; use std::net::SocketAddrV4;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
use tokio::time::{self, Duration};
use tokio_tun::TunBuilder; use tokio_tun::TunBuilder;
const UDP_TTL: Duration = Duration::from_secs(180);
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -56,6 +59,7 @@ async fn main() {
//thread::sleep(time::Duration::from_secs(5)); //thread::sleep(time::Duration::from_secs(5));
let mut stack = Stack::new(tun); let mut stack = Stack::new(tun);
stack.listen(local_port); stack.listen(local_port);
info!("Listening on {}", local_port);
let main_loop = tokio::spawn(async move { let main_loop = tokio::spawn(async move {
let mut buf_udp = [0u8; MAX_PACKET_LEN]; let mut buf_udp = [0u8; MAX_PACKET_LEN];
@ -63,11 +67,15 @@ async fn main() {
loop { loop {
let sock = stack.accept().await; let sock = stack.accept().await;
info!("New connection: {}", sock);
tokio::spawn(async move { tokio::spawn(async move {
let udp_sock = UdpSocket::bind("0.0.0.0:0").await.unwrap(); let udp_sock = UdpSocket::bind("0.0.0.0:0").await.unwrap();
udp_sock.connect(remote_addr).await.unwrap(); udp_sock.connect(remote_addr).await.unwrap();
loop { loop {
let read_timeout = time::sleep(UDP_TTL);
tokio::select! { tokio::select! {
Ok(size) = udp_sock.recv(&mut buf_udp) => { Ok(size) = udp_sock.recv(&mut buf_udp) => {
if let None = sock.send(&buf_udp[..size]).await { if let None = sock.send(&buf_udp[..size]).await {
@ -84,6 +92,10 @@ async fn main() {
None => { return; }, None => { return; },
} }
}, },
_ = read_timeout => {
info!("No traffic seen in the last {:?}, closing connection", UDP_TTL);
return;
}
}; };
} }
}); });

View File

@ -404,6 +404,16 @@ impl Stack {
); );
shared.outgoing.try_send(buf).unwrap(); shared.outgoing.try_send(buf).unwrap();
} }
} else {
let buf = build_tcp_packet(
local_addr,
remote_addr,
0,
tcp_packet.get_acknowledgement() + 1,
tcp::TcpFlags::RST,
None,
);
shared.outgoing.try_send(buf).unwrap();
} }
} }
} }