Add udp reverse tunneling

This commit is contained in:
Σrebe - Romain GERARD 2023-11-26 18:22:28 +01:00
parent b352d5717d
commit f48b156542
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
5 changed files with 70 additions and 20 deletions

View file

@ -62,7 +62,8 @@ struct Client {
/// Listen on remote and forwards traffic from local. Can be specified multiple times. Only tcp is supported
/// examples:
/// 'tcp://1212:google.com:443' => listen on server for incoming tcp cnx on port 1212 and forward to google.com on port 443 from local machine
#[arg(short='R', long, value_name = "{tcp}://[BIND:]PORT:HOST:PORT", value_parser = parse_tunnel_arg, verbatim_doc_comment)]
/// 'udp://1212:1.1.1.1:53' => listen on server for incoming udp on port 1212 and forward to cloudflare dns 1.1.1.1 on port 53 from local machine
#[arg(short='R', long, value_name = "{tcp,udp}://[BIND:]PORT:HOST:PORT", value_parser = parse_tunnel_arg, verbatim_doc_comment)]
remote_to_local: Vec<LocalToRemote>,
/// (linux only) Mark network packet with SO_MARK sockoption with the specified value.
@ -172,6 +173,7 @@ enum LocalProtocol {
Stdio,
Socks5,
ReverseTcp,
ReverseUdp { timeout: Option<Duration> },
}
#[derive(Clone, Debug)]
@ -551,7 +553,31 @@ async fn main() {
LocalProtocol::Tcp => {
tunnel.local_protocol = ReverseTcp;
tokio::spawn(async move {
if let Err(err) = tunnel::client::run_reverse_tunnel(client_config, tunnel).await {
let remote = tunnel.remote.clone();
let cfg = client_config.clone();
let connect_to_dest = || async {
tcp::connect(&remote.0, remote.1, cfg.socket_so_mark, cfg.timeout_connect).await
};
if let Err(err) =
tunnel::client::run_reverse_tunnel(client_config, tunnel, connect_to_dest).await
{
error!("{:?}", err);
}
});
}
LocalProtocol::Udp { timeout } => {
tunnel.local_protocol = LocalProtocol::ReverseUdp { timeout: *timeout };
tokio::spawn(async move {
let cfg = client_config.clone();
let remote = tunnel.remote.clone();
let connect_to_dest =
|| async { udp::connect(&remote.0, remote.1, cfg.timeout_connect).await };
if let Err(err) =
tunnel::client::run_reverse_tunnel(client_config, tunnel, connect_to_dest).await
{
error!("{:?}", err);
}
});
@ -628,6 +654,7 @@ async fn main() {
}
}
LocalProtocol::ReverseTcp => {}
LocalProtocol::ReverseUdp { .. } => {}
}
}
}