diff --git a/src/tunnel/connectors/mod.rs b/src/tunnel/connectors/mod.rs index b4162b5..2524d05 100644 --- a/src/tunnel/connectors/mod.rs +++ b/src/tunnel/connectors/mod.rs @@ -1,3 +1,4 @@ +use anyhow::anyhow; use tokio::io::{AsyncRead, AsyncWrite}; use url::Url; @@ -18,7 +19,11 @@ pub trait TunnelConnector { async fn connect(&self, remote: &Option) -> anyhow::Result<(Self::Reader, Self::Writer)>; async fn connect_with_http_proxy( &self, - proxy: &Url, - remote: &Option, - ) -> anyhow::Result<(Self::Reader, Self::Writer)>; + _proxy: &Url, + _remote: &Option, + ) -> anyhow::Result<(Self::Reader, Self::Writer)> { + Err(anyhow!( + "Requested to use HTTP Proxy to connect but it is not supported with this connector" + )) + } } diff --git a/src/tunnel/connectors/sock5.rs b/src/tunnel/connectors/sock5.rs index 66ac590..16aedf6 100644 --- a/src/tunnel/connectors/sock5.rs +++ b/src/tunnel/connectors/sock5.rs @@ -65,10 +65,29 @@ impl TunnelConnector for Socks5TunnelConnector<'_> { async fn connect_with_http_proxy( &self, - _proxy: &Url, - _remote: &Option, + proxy: &Url, + remote: &Option, ) -> anyhow::Result<(Self::Reader, Self::Writer)> { - Err(anyhow!("SOCKS5 tunneling is not supported with HTTP proxy")) + let Some(remote) = remote else { + return Err(anyhow!("Missing remote destination for reverse socks5")); + }; + + match remote.protocol { + LocalProtocol::Tcp { proxy_protocol: _ } => { + let stream = protocols::tcp::connect_with_http_proxy( + proxy, + &remote.host, + remote.port, + self.so_mark, + self.connect_timeout, + self.dns_resolver, + ) + .await?; + let (reader, writer) = stream.into_split(); + Ok((Socks5Reader::Tcp(reader), Socks5Writer::Tcp(writer))) + } + _ => Err(anyhow!("Socks5 UDP cannot use http proxy to connect to destination")), + } } } diff --git a/src/tunnel/connectors/udp.rs b/src/tunnel/connectors/udp.rs index f9c893a..ffdb651 100644 --- a/src/tunnel/connectors/udp.rs +++ b/src/tunnel/connectors/udp.rs @@ -1,7 +1,6 @@ use std::time::Duration; -use anyhow::anyhow; -use url::{Host, Url}; +use url::Host; use crate::protocols; use crate::protocols::dns::DnsResolver; @@ -46,12 +45,4 @@ impl TunnelConnector for UdpTunnelConnector<'_> { Ok((stream.clone(), stream)) } - - async fn connect_with_http_proxy( - &self, - _proxy: &Url, - _remote: &Option, - ) -> anyhow::Result<(Self::Reader, Self::Writer)> { - Err(anyhow!("UDP tunneling is not supported with HTTP proxy")) - } } diff --git a/src/tunnel/server/reverse_tunnel.rs b/src/tunnel/server/reverse_tunnel.rs index c77eaa2..cb1b1f4 100644 --- a/src/tunnel/server/reverse_tunnel.rs +++ b/src/tunnel/server/reverse_tunnel.rs @@ -1,6 +1,6 @@ use crate::tunnel::listeners::TunnelListener; use crate::tunnel::RemoteAddr; -use ahash::{AHashMap}; +use ahash::AHashMap; use anyhow::anyhow; use futures_util::{pin_mut, StreamExt}; use log::warn;