Use HTTP proxy if configured for socks5

This commit is contained in:
Σrebe - Romain GERARD 2024-08-10 11:32:38 +02:00
parent 0f33feecfc
commit 21c4f7ffc6
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
4 changed files with 32 additions and 17 deletions

View file

@ -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<RemoteAddr>) -> anyhow::Result<(Self::Reader, Self::Writer)>;
async fn connect_with_http_proxy(
&self,
proxy: &Url,
remote: &Option<RemoteAddr>,
) -> anyhow::Result<(Self::Reader, Self::Writer)>;
_proxy: &Url,
_remote: &Option<RemoteAddr>,
) -> anyhow::Result<(Self::Reader, Self::Writer)> {
Err(anyhow!(
"Requested to use HTTP Proxy to connect but it is not supported with this connector"
))
}
}

View file

@ -65,10 +65,29 @@ impl TunnelConnector for Socks5TunnelConnector<'_> {
async fn connect_with_http_proxy(
&self,
_proxy: &Url,
_remote: &Option<RemoteAddr>,
proxy: &Url,
remote: &Option<RemoteAddr>,
) -> 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")),
}
}
}

View file

@ -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<RemoteAddr>,
) -> anyhow::Result<(Self::Reader, Self::Writer)> {
Err(anyhow!("UDP tunneling is not supported with HTTP proxy"))
}
}

View file

@ -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;