aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2024-04-20 03:40:59 +0000
committermat <git@matdoes.dev>2024-04-20 03:40:59 +0000
commit353eda21ac8280213edcec3823cc3bd77fe17c44 (patch)
treee57201568f99f3a3e9ebf7b8991b22b945bb65bc /azalea-protocol
parentfa96af786b6f549edd8f04f4a19ced01faffe114 (diff)
downloadazalea-drasl-353eda21ac8280213edcec3823cc3bd77fe17c44.tar.xz
socks5 support (#113)
Diffstat (limited to 'azalea-protocol')
-rw-r--r--azalea-protocol/Cargo.toml2
-rwxr-xr-xazalea-protocol/src/connect.rs38
2 files changed, 39 insertions, 1 deletions
diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml
index 93908770..609d2019 100644
--- a/azalea-protocol/Cargo.toml
+++ b/azalea-protocol/Cargo.toml
@@ -48,6 +48,8 @@ trust-dns-resolver = { version = "^0.23.2", default-features = false, features =
uuid = "1.7.0"
log = "0.4.20"
+socks5-impl = "0.5.6"
+
[features]
connecting = []
default = ["packets"]
diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs
index 86b92693..cc1b71c1 100755
--- a/azalea-protocol/src/connect.rs
+++ b/azalea-protocol/src/connect.rs
@@ -20,7 +20,7 @@ use std::io::Cursor;
use std::marker::PhantomData;
use std::net::SocketAddr;
use thiserror::Error;
-use tokio::io::AsyncWriteExt;
+use tokio::io::{AsyncWriteExt, BufStream};
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf, ReuniteError};
use tokio::net::TcpStream;
use tracing::{error, info};
@@ -257,6 +257,20 @@ pub enum ConnectionError {
Io(#[from] std::io::Error),
}
+use socks5_impl::protocol::UserKey;
+
+#[derive(Debug, Clone)]
+pub struct Proxy {
+ pub addr: SocketAddr,
+ pub auth: Option<UserKey>,
+}
+
+impl Proxy {
+ pub fn new(addr: SocketAddr, auth: Option<UserKey>) -> Self {
+ Self { addr, auth }
+ }
+}
+
impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> {
/// Create a new connection to the given address.
pub async fn new(address: &SocketAddr) -> Result<Self, ConnectionError> {
@@ -265,6 +279,28 @@ impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> {
// enable tcp_nodelay
stream.set_nodelay(true)?;
+ Self::new_from_stream(stream).await
+ }
+
+ /// Create a new connection to the given address and Socks5 proxy. If you're
+ /// not using a proxy, use [`Self::new`] instead.
+ pub async fn new_with_proxy(
+ address: &SocketAddr,
+ proxy: Proxy,
+ ) -> Result<Self, ConnectionError> {
+ let proxy_stream = TcpStream::connect(proxy.addr).await?;
+ let mut stream = BufStream::new(proxy_stream);
+
+ let _ = socks5_impl::client::connect(&mut stream, address, proxy.auth)
+ .await
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
+
+ Self::new_from_stream(stream.into_inner()).await
+ }
+
+ /// Create a new connection from an existing stream. Useful if you want to
+ /// set custom options on the stream. Otherwise, just use [`Self::new`].
+ pub async fn new_from_stream(stream: TcpStream) -> Result<Self, ConnectionError> {
let (read_stream, write_stream) = stream.into_split();
Ok(Connection {