Skip to main content

layer_client/
socks5.rs

1//! SOCKS5 proxy connector.
2//!
3//! Provides [`Socks5Config`] that can be attached to a [`crate::Config`]
4//! so every Telegram connection is routed through a SOCKS5 proxy.
5//!
6//! # Example
7//! ```rust,no_run
8//! use layer_client::{Config, proxy::Socks5Config};
9//! use std::sync::Arc;
10//! use layer_client::retry::AutoSleep;
11//!
12//! let cfg = Config {
13//! socks5: Some(Socks5Config::new("127.0.0.1:1080")),
14//! ..Default::default()
15//! };
16//! ```
17
18use crate::InvocationError;
19use tokio::net::TcpStream;
20use tokio_socks::tcp::Socks5Stream;
21
22/// SOCKS5 proxy configuration.
23#[derive(Clone, Debug)]
24pub struct Socks5Config {
25    /// Host:port of the SOCKS5 proxy server.
26    pub proxy_addr: String,
27    /// Optional username and password for proxy authentication.
28    pub auth: Option<(String, String)>,
29}
30
31impl Socks5Config {
32    /// Create an unauthenticated SOCKS5 config.
33    pub fn new(proxy_addr: impl Into<String>) -> Self {
34        Self {
35            proxy_addr: proxy_addr.into(),
36            auth: None,
37        }
38    }
39
40    /// Create a SOCKS5 config with username/password authentication.
41    pub fn with_auth(
42        proxy_addr: impl Into<String>,
43        username: impl Into<String>,
44        password: impl Into<String>,
45    ) -> Self {
46        Self {
47            proxy_addr: proxy_addr.into(),
48            auth: Some((username.into(), password.into())),
49        }
50    }
51
52    /// Establish a TCP connection through this SOCKS5 proxy.
53    ///
54    /// Returns a [`TcpStream`] tunnelled through the proxy to `target`.
55    pub async fn connect(&self, target: &str) -> Result<TcpStream, InvocationError> {
56        tracing::info!("[socks5] Connecting via {} → {target}", self.proxy_addr);
57        let stream = match &self.auth {
58            None => Socks5Stream::connect(self.proxy_addr.as_str(), target)
59                .await
60                .map_err(|e| InvocationError::Io(std::io::Error::other(e)))?,
61            Some((user, pass)) => Socks5Stream::connect_with_password(
62                self.proxy_addr.as_str(),
63                target,
64                user.as_str(),
65                pass.as_str(),
66            )
67            .await
68            .map_err(|e| InvocationError::Io(std::io::Error::other(e)))?,
69        };
70        tracing::info!("[socks5] Connected ✓");
71        Ok(stream.into_inner())
72    }
73}