support proxy protocol in more cases

This commit is contained in:
Σrebe - Romain GERARD 2024-08-10 10:58:05 +02:00
parent 8c4d091b9e
commit dff243369c
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
5 changed files with 28 additions and 22 deletions

View file

@ -520,10 +520,11 @@ fn parse_tunnel_arg(arg: &str) -> Result<LocalToRemote, io::Error> {
format!("cannot parse unix socket path from {}", arg), format!("cannot parse unix socket path from {}", arg),
)); ));
}; };
let (dest_host, dest_port, _options) = parse_tunnel_dest(remote)?; let (dest_host, dest_port, options) = parse_tunnel_dest(remote)?;
Ok(LocalToRemote { Ok(LocalToRemote {
local_protocol: LocalProtocol::Unix { local_protocol: LocalProtocol::Unix {
path: PathBuf::from(path), path: PathBuf::from(path),
proxy_protocol: get_proxy_protocol(&options),
}, },
local: SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0)), local: SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0)),
remote: (dest_host, dest_port), remote: (dest_host, dest_port),
@ -557,9 +558,11 @@ fn parse_tunnel_arg(arg: &str) -> Result<LocalToRemote, io::Error> {
}) })
} }
"stdio" => { "stdio" => {
let (dest_host, dest_port, _options) = parse_tunnel_dest(tunnel_info)?; let (dest_host, dest_port, options) = parse_tunnel_dest(tunnel_info)?;
Ok(LocalToRemote { Ok(LocalToRemote {
local_protocol: LocalProtocol::Stdio, local_protocol: LocalProtocol::Stdio {
proxy_protocol: get_proxy_protocol(&options),
},
local: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from(0), 0)), local: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from(0), 0)),
remote: (dest_host, dest_port), remote: (dest_host, dest_port),
}) })
@ -604,7 +607,7 @@ fn parse_reverse_tunnel_arg(arg: &str) -> Result<LocalToRemote, io::Error> {
credentials, credentials,
proxy_protocol: _proxy_protocol, proxy_protocol: _proxy_protocol,
} => LocalProtocol::ReverseHttpProxy { timeout, credentials }, } => LocalProtocol::ReverseHttpProxy { timeout, credentials },
LocalProtocol::Unix { path } => LocalProtocol::ReverseUnix { path }, LocalProtocol::Unix { path, .. } => LocalProtocol::ReverseUnix { path },
LocalProtocol::ReverseTcp { .. } LocalProtocol::ReverseTcp { .. }
| LocalProtocol::ReverseUdp { .. } | LocalProtocol::ReverseUdp { .. }
| LocalProtocol::ReverseSocks5 { .. } | LocalProtocol::ReverseSocks5 { .. }
@ -612,7 +615,7 @@ fn parse_reverse_tunnel_arg(arg: &str) -> Result<LocalToRemote, io::Error> {
| LocalProtocol::ReverseUnix { .. } | LocalProtocol::ReverseUnix { .. }
| LocalProtocol::TProxyTcp | LocalProtocol::TProxyTcp
| LocalProtocol::TProxyUdp { .. } | LocalProtocol::TProxyUdp { .. }
| LocalProtocol::Stdio => { | LocalProtocol::Stdio { .. } => {
return Err(io::Error::new( return Err(io::Error::new(
ErrorKind::InvalidInput, ErrorKind::InvalidInput,
format!("Cannot use {:?} as reverse tunnels {}", proto.local_protocol, arg), format!("Cannot use {:?} as reverse tunnels {}", proto.local_protocol, arg),
@ -710,7 +713,7 @@ async fn main() -> anyhow::Result<()> {
if args if args
.local_to_remote .local_to_remote
.iter() .iter()
.filter(|x| x.local_protocol == LocalProtocol::Stdio) .filter(|x| matches!(x.local_protocol, LocalProtocol::Stdio { .. }))
.count() .count()
> 0 > 0
{ {
@ -937,7 +940,7 @@ async fn main() -> anyhow::Result<()> {
LocalProtocol::ReverseUnix { .. } => { LocalProtocol::ReverseUnix { .. } => {
panic!("Unix socket is not available for non Unix platform") panic!("Unix socket is not available for non Unix platform")
} }
LocalProtocol::Stdio LocalProtocol::Stdio { .. }
| LocalProtocol::TProxyTcp | LocalProtocol::TProxyTcp
| LocalProtocol::TProxyUdp { .. } | LocalProtocol::TProxyUdp { .. }
| LocalProtocol::Tcp { .. } | LocalProtocol::Tcp { .. }
@ -975,9 +978,9 @@ async fn main() -> anyhow::Result<()> {
}); });
} }
#[cfg(unix)] #[cfg(unix)]
LocalProtocol::Unix { path } => { LocalProtocol::Unix { path, proxy_protocol } => {
use crate::tunnel::listeners::UnixTunnelListener; use crate::tunnel::listeners::UnixTunnelListener;
let server = UnixTunnelListener::new(path, tunnel.remote.clone(), false).await?; // TODO: support proxy protocol let server = UnixTunnelListener::new(path, tunnel.remote.clone(), *proxy_protocol).await?;
tokio::spawn(async move { tokio::spawn(async move {
if let Err(err) = client.run_tunnel(server).await { if let Err(err) = client.run_tunnel(server).await {
error!("{:?}", err); error!("{:?}", err);
@ -1035,8 +1038,8 @@ async fn main() -> anyhow::Result<()> {
}); });
} }
LocalProtocol::Stdio => { LocalProtocol::Stdio { proxy_protocol } => {
let (server, mut handle) = new_stdio_listener(tunnel.remote.clone(), false).await?; // TODO: support proxy protocol let (server, mut handle) = new_stdio_listener(tunnel.remote.clone(), *proxy_protocol).await?;
tokio::spawn(async move { tokio::spawn(async move {
if let Err(err) = client.run_tunnel(server).await { if let Err(err) = client.run_tunnel(server).await {
error!("{:?}", err); error!("{:?}", err);

View file

@ -157,7 +157,7 @@ impl From<&LocalProtocol> for ReverseTunnelConfigProtocol {
match value { match value {
LocalProtocol::Tcp { .. } LocalProtocol::Tcp { .. }
| LocalProtocol::Udp { .. } | LocalProtocol::Udp { .. }
| LocalProtocol::Stdio | LocalProtocol::Stdio { .. }
| LocalProtocol::Socks5 { .. } | LocalProtocol::Socks5 { .. }
| LocalProtocol::TProxyTcp { .. } | LocalProtocol::TProxyTcp { .. }
| LocalProtocol::TProxyUdp { .. } | LocalProtocol::TProxyUdp { .. }
@ -178,7 +178,7 @@ impl From<&LocalProtocol> for TunnelConfigProtocol {
| LocalProtocol::ReverseUdp { .. } | LocalProtocol::ReverseUdp { .. }
| LocalProtocol::ReverseSocks5 { .. } | LocalProtocol::ReverseSocks5 { .. }
| LocalProtocol::ReverseUnix { .. } | LocalProtocol::ReverseUnix { .. }
| LocalProtocol::Stdio | LocalProtocol::Stdio { .. }
| LocalProtocol::Socks5 { .. } | LocalProtocol::Socks5 { .. }
| LocalProtocol::TProxyTcp { .. } | LocalProtocol::TProxyTcp { .. }
| LocalProtocol::TProxyUdp { .. } | LocalProtocol::TProxyUdp { .. }

View file

@ -22,7 +22,9 @@ pub enum LocalProtocol {
Udp { Udp {
timeout: Option<Duration>, timeout: Option<Duration>,
}, },
Stdio, Stdio {
proxy_protocol: bool,
},
Socks5 { Socks5 {
timeout: Option<Duration>, timeout: Option<Duration>,
credentials: Option<(String, String)>, credentials: Option<(String, String)>,
@ -53,6 +55,7 @@ pub enum LocalProtocol {
}, },
Unix { Unix {
path: PathBuf, path: PathBuf,
proxy_protocol: bool,
}, },
} }

View file

@ -266,7 +266,7 @@ impl WsServer {
error!("Received an unsupported target protocol {:?}", remote); error!("Received an unsupported target protocol {:?}", remote);
Err(anyhow::anyhow!("Invalid upgrade request")) Err(anyhow::anyhow!("Invalid upgrade request"))
} }
LocalProtocol::Stdio LocalProtocol::Stdio { .. }
| LocalProtocol::Socks5 { .. } | LocalProtocol::Socks5 { .. }
| LocalProtocol::TProxyTcp | LocalProtocol::TProxyTcp
| LocalProtocol::TProxyUdp { .. } | LocalProtocol::TProxyUdp { .. }

View file

@ -33,17 +33,17 @@ impl JwtTunnelConfig {
p: match dest.protocol { p: match dest.protocol {
LocalProtocol::Tcp { .. } => dest.protocol.clone(), LocalProtocol::Tcp { .. } => dest.protocol.clone(),
LocalProtocol::Udp { .. } => dest.protocol.clone(), LocalProtocol::Udp { .. } => dest.protocol.clone(),
LocalProtocol::Stdio => LocalProtocol::Tcp { proxy_protocol: false }, LocalProtocol::ReverseTcp => dest.protocol.clone(),
LocalProtocol::Socks5 { .. } => LocalProtocol::Tcp { proxy_protocol: false },
LocalProtocol::HttpProxy { .. } => dest.protocol.clone(),
LocalProtocol::ReverseTcp => LocalProtocol::ReverseTcp,
LocalProtocol::ReverseUdp { .. } => dest.protocol.clone(), LocalProtocol::ReverseUdp { .. } => dest.protocol.clone(),
LocalProtocol::ReverseSocks5 { .. } => dest.protocol.clone(), LocalProtocol::ReverseSocks5 { .. } => dest.protocol.clone(),
LocalProtocol::TProxyTcp => LocalProtocol::Tcp { proxy_protocol: false },
LocalProtocol::TProxyUdp { timeout } => LocalProtocol::Udp { timeout },
LocalProtocol::Unix { .. } => LocalProtocol::Tcp { proxy_protocol: false },
LocalProtocol::ReverseUnix { .. } => dest.protocol.clone(), LocalProtocol::ReverseUnix { .. } => dest.protocol.clone(),
LocalProtocol::ReverseHttpProxy { .. } => dest.protocol.clone(), LocalProtocol::ReverseHttpProxy { .. } => dest.protocol.clone(),
LocalProtocol::TProxyTcp => unreachable!("cannot use tproxy tcp as destination protocol"),
LocalProtocol::TProxyUdp { .. } => unreachable!("cannot use tproxy udp as destination protocol"),
LocalProtocol::Stdio { .. } => unreachable!("cannot use stdio as destination protocol"),
LocalProtocol::Unix { .. } => unreachable!("canont use unix as destination protocol"),
LocalProtocol::Socks5 { .. } => unreachable!("cannot use socks5 as destination protocol"),
LocalProtocol::HttpProxy { .. } => unreachable!("cannot use http proxy as destination protocol"),
}, },
r: dest.host.to_string(), r: dest.host.to_string(),
rp: dest.port, rp: dest.port,