Skip to content

Commit

Permalink
Ping local server to avoid connection interruptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
reu committed Apr 28, 2023
1 parent 7009d1a commit 5cd9d58
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tinytun-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
io::{BufRead, Cursor},
net::SocketAddr,
sync::Arc,
time::Duration,
};

use clap::Parser;
Expand All @@ -14,6 +15,7 @@ use hyper::{
};
use tokio::{
net::{TcpListener, TcpStream},
time::sleep,
try_join,
};

Expand Down Expand Up @@ -99,10 +101,26 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
if let Ok(conn) = hyper::upgrade::on(&mut req).await {
trace!("Tunnel opened");

if let Ok((client, h2)) = h2::client::handshake(conn).await
if let Ok((client, mut h2)) =
h2::client::handshake(conn).await
{
tun_entry.add_tunnel(client.into()).await;

let mut ping_pong = h2.ping_pong().unwrap();
tokio::spawn(
#[allow(unreachable_code)]
async move {
loop {
sleep(Duration::from_secs(10)).await;
trace!("Sending ping");
ping_pong.ping(h2::Ping::opaque()).await?;
trace!("Received pong");
}
Ok::<_, Box<dyn Error + Send + Sync>>(())
}
.in_current_span(),
);

if let Err(err) = h2.await {
debug!(error = %err, "Error");
}
Expand Down

0 comments on commit 5cd9d58

Please sign in to comment.