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 tokio::io::{AsyncRead, AsyncWrite};
use url::Url; 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(&self, remote: &Option<RemoteAddr>) -> anyhow::Result<(Self::Reader, Self::Writer)>;
async fn connect_with_http_proxy( async fn connect_with_http_proxy(
&self, &self,
proxy: &Url, _proxy: &Url,
remote: &Option<RemoteAddr>, _remote: &Option<RemoteAddr>,
) -> anyhow::Result<(Self::Reader, Self::Writer)>; ) -> 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( async fn connect_with_http_proxy(
&self, &self,
_proxy: &Url, proxy: &Url,
_remote: &Option<RemoteAddr>, remote: &Option<RemoteAddr>,
) -> anyhow::Result<(Self::Reader, Self::Writer)> { ) -> 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 std::time::Duration;
use anyhow::anyhow; use url::Host;
use url::{Host, Url};
use crate::protocols; use crate::protocols;
use crate::protocols::dns::DnsResolver; use crate::protocols::dns::DnsResolver;
@ -46,12 +45,4 @@ impl TunnelConnector for UdpTunnelConnector<'_> {
Ok((stream.clone(), stream)) 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::listeners::TunnelListener;
use crate::tunnel::RemoteAddr; use crate::tunnel::RemoteAddr;
use ahash::{AHashMap}; use ahash::AHashMap;
use anyhow::anyhow; use anyhow::anyhow;
use futures_util::{pin_mut, StreamExt}; use futures_util::{pin_mut, StreamExt};
use log::warn; use log::warn;