mirror of https://github.com/rapiz1/rathole.git
fix: improve keepalive
This commit is contained in:
parent
4a1da9be7f
commit
8d2dc65a1d
|
@ -8,17 +8,27 @@ use std::{
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use socket2::{SockRef, TcpKeepalive};
|
use socket2::{SockRef, TcpKeepalive};
|
||||||
use tokio::net::{TcpStream, ToSocketAddrs, UdpSocket};
|
use tokio::net::{TcpStream, ToSocketAddrs, UdpSocket};
|
||||||
|
use tracing::error;
|
||||||
|
|
||||||
// Tokio hesitates to expose this option...So we have to do it on our own :(
|
// Tokio hesitates to expose this option...So we have to do it on our own :(
|
||||||
// The good news is that using socket2 it can be easily done, without losing portablity.
|
// The good news is that using socket2 it can be easily done, without losing portablity.
|
||||||
// See https://github.com/tokio-rs/tokio/issues/3082
|
// See https://github.com/tokio-rs/tokio/issues/3082
|
||||||
pub fn set_tcp_keepalive(conn: &TcpStream) -> Result<()> {
|
pub fn try_set_tcp_keepalive(conn: &TcpStream) -> Result<()> {
|
||||||
let s = SockRef::from(conn);
|
let s = SockRef::from(conn);
|
||||||
let keepalive = TcpKeepalive::new().with_time(Duration::from_secs(60));
|
let keepalive = TcpKeepalive::new().with_time(Duration::from_secs(60));
|
||||||
s.set_tcp_keepalive(&keepalive)
|
s.set_tcp_keepalive(&keepalive)
|
||||||
.with_context(|| "Failed to set keepalive")
|
.with_context(|| "Failed to set keepalive")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_tcp_keepalive(conn: &TcpStream) {
|
||||||
|
if let Err(e) = try_set_tcp_keepalive(conn) {
|
||||||
|
error!(
|
||||||
|
"Failed to set TCP keepalive. The connection maybe unstable: {:?}",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn feature_not_compile(feature: &str) -> ! {
|
pub fn feature_not_compile(feature: &str) -> ! {
|
||||||
panic!(
|
panic!(
|
||||||
|
|
|
@ -9,7 +9,6 @@ use anyhow::{anyhow, Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use snowstorm::{Builder, NoiseParams, NoiseStream};
|
use snowstorm::{Builder, NoiseParams, NoiseStream};
|
||||||
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
pub struct NoiseTransport {
|
pub struct NoiseTransport {
|
||||||
config: NoiseConfig,
|
config: NoiseConfig,
|
||||||
|
@ -74,18 +73,16 @@ impl Transport for NoiseTransport {
|
||||||
|
|
||||||
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
||||||
let (conn, addr) = a.accept().await?;
|
let (conn, addr) = a.accept().await?;
|
||||||
|
set_tcp_keepalive(&conn);
|
||||||
|
|
||||||
let conn = NoiseStream::handshake(conn, self.builder().build_responder()?).await?;
|
let conn = NoiseStream::handshake(conn, self.builder().build_responder()?).await?;
|
||||||
Ok((conn, addr))
|
Ok((conn, addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
||||||
let conn = TcpStream::connect(addr).await?;
|
let conn = TcpStream::connect(addr).await?;
|
||||||
if let Err(e) = set_tcp_keepalive(&conn) {
|
set_tcp_keepalive(&conn);
|
||||||
error!(
|
|
||||||
"Failed to set TCP keepalive. The connection maybe unstable: {:?}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
let conn = NoiseStream::handshake(conn, self.builder().build_initiator()?).await?;
|
let conn = NoiseStream::handshake(conn, self.builder().build_initiator()?).await?;
|
||||||
return Ok(conn);
|
return Ok(conn);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TcpTransport {}
|
pub struct TcpTransport {}
|
||||||
|
@ -26,17 +25,13 @@ impl Transport for TcpTransport {
|
||||||
|
|
||||||
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
||||||
let (s, addr) = a.accept().await?;
|
let (s, addr) = a.accept().await?;
|
||||||
|
set_tcp_keepalive(&s);
|
||||||
Ok((s, addr))
|
Ok((s, addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
||||||
let s = TcpStream::connect(addr).await?;
|
let s = TcpStream::connect(addr).await?;
|
||||||
if let Err(e) = set_tcp_keepalive(&s) {
|
set_tcp_keepalive(&s);
|
||||||
error!(
|
|
||||||
"Failed to set TCP keepalive. The connection maybe unstable: {:?}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
Ok(s)
|
Ok(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ use tokio::fs;
|
||||||
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
|
||||||
use tokio_native_tls::native_tls::{self, Certificate, Identity};
|
use tokio_native_tls::native_tls::{self, Certificate, Identity};
|
||||||
use tokio_native_tls::{TlsAcceptor, TlsConnector, TlsStream};
|
use tokio_native_tls::{TlsAcceptor, TlsConnector, TlsStream};
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct TlsTransport {
|
pub struct TlsTransport {
|
||||||
|
@ -66,6 +65,8 @@ impl Transport for TlsTransport {
|
||||||
|
|
||||||
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
async fn accept(&self, a: &Self::Acceptor) -> Result<(Self::Stream, SocketAddr)> {
|
||||||
let (conn, addr) = a.0.accept().await?;
|
let (conn, addr) = a.0.accept().await?;
|
||||||
|
set_tcp_keepalive(&conn);
|
||||||
|
|
||||||
let conn = a.1.accept(conn).await?;
|
let conn = a.1.accept(conn).await?;
|
||||||
|
|
||||||
Ok((conn, addr))
|
Ok((conn, addr))
|
||||||
|
@ -73,12 +74,8 @@ impl Transport for TlsTransport {
|
||||||
|
|
||||||
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
async fn connect(&self, addr: &str) -> Result<Self::Stream> {
|
||||||
let conn = TcpStream::connect(&addr).await?;
|
let conn = TcpStream::connect(&addr).await?;
|
||||||
if let Err(e) = set_tcp_keepalive(&conn) {
|
set_tcp_keepalive(&conn);
|
||||||
error!(
|
|
||||||
"Failed to set TCP keepalive. The connection maybe unstable: {:?}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
let connector = self.connector.as_ref().unwrap();
|
let connector = self.connector.as_ref().unwrap();
|
||||||
Ok(connector
|
Ok(connector
|
||||||
.connect(
|
.connect(
|
||||||
|
|
Loading…
Reference in New Issue