Skip to content

Commit

Permalink
Use HTTP proxy if configured for socks5
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed Aug 10, 2024
1 parent 0f33fee commit 21c4f7f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
11 changes: 8 additions & 3 deletions src/tunnel/connectors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use tokio::io::{AsyncRead, AsyncWrite};
use url::Url;

Expand All @@ -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"
))
}
}
25 changes: 22 additions & 3 deletions src/tunnel/connectors/sock5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
}
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/tunnel/connectors/udp.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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"))
}
}
2 changes: 1 addition & 1 deletion src/tunnel/server/reverse_tunnel.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 21c4f7f

Please sign in to comment.