socks5_impl/server/
mod.rs

1use std::{
2    net::SocketAddr,
3    task::{Context, Poll},
4};
5use tokio::net::TcpListener;
6
7pub mod auth;
8pub mod connection;
9
10pub use crate::{
11    server::auth::{AuthAdaptor, AuthExecutor},
12    server::connection::{
13        ClientConnection, IncomingConnection,
14        associate::{AssociatedUdpSocket, UdpAssociate},
15        bind::Bind,
16        connect::Connect,
17    },
18};
19
20/// The socks5 server itself.
21///
22/// The server can be constructed on a given socket address, or be created on an existing TcpListener.
23///
24/// The authentication method can be configured with the
25/// [`AuthExecutor`] trait.
26pub struct Server<O> {
27    listener: TcpListener,
28    auth: AuthAdaptor<O>,
29}
30
31impl<O: 'static> Server<O> {
32    /// Create a new socks5 server with the given TCP listener and authentication method.
33    #[inline]
34    pub fn new(listener: TcpListener, auth: AuthAdaptor<O>) -> Self {
35        Self { listener, auth }
36    }
37
38    /// Create a new socks5 server on the given socket address and authentication method.
39    #[inline]
40    pub async fn bind(addr: SocketAddr, auth: AuthAdaptor<O>) -> std::io::Result<Self> {
41        Self::bind_with_backlog(addr, auth, 1024).await
42    }
43
44    pub async fn bind_with_backlog(addr: SocketAddr, auth: AuthAdaptor<O>, backlog: u32) -> std::io::Result<Self> {
45        let socket = if addr.is_ipv4() {
46            tokio::net::TcpSocket::new_v4()?
47        } else {
48            tokio::net::TcpSocket::new_v6()?
49        };
50        socket.set_reuseaddr(true)?;
51        socket.bind(addr)?;
52        let listener = socket.listen(backlog)?;
53        Ok(Self::new(listener, auth))
54    }
55
56    /// Accept an [`IncomingConnection`].
57    /// The connection may not be a valid socks5 connection. You need to call
58    /// [`IncomingConnection::authenticate`](crate::server::connection::IncomingConnection::authenticate)
59    /// to hand-shake it into a proper socks5 connection.
60    #[inline]
61    pub async fn accept(&self) -> std::io::Result<(IncomingConnection<O>, SocketAddr)> {
62        let (stream, addr) = self.listener.accept().await?;
63        Ok((IncomingConnection::new(stream, self.auth.clone()), addr))
64    }
65
66    /// Polls to accept an [`IncomingConnection<O>`](crate::server::connection::IncomingConnection).
67    ///
68    /// The connection is only a freshly created TCP connection and may not be a valid SOCKS5 connection.
69    /// You should call
70    /// [`IncomingConnection::authenticate`](crate::server::connection::IncomingConnection::authenticate)
71    /// to perform a SOCKS5 authentication handshake.
72    ///
73    /// If there is no connection to accept, Poll::Pending is returned and the current task will be notified by a waker.
74    /// Note that on multiple calls to poll_accept, only the Waker from the Context passed to the most recent call is scheduled to receive a wakeup.
75    #[inline]
76    pub fn poll_accept(&self, cx: &mut Context<'_>) -> Poll<std::io::Result<(IncomingConnection<O>, SocketAddr)>> {
77        self.listener
78            .poll_accept(cx)
79            .map_ok(|(stream, addr)| (IncomingConnection::new(stream, self.auth.clone()), addr))
80    }
81
82    /// Get the the local socket address binded to this server
83    #[inline]
84    pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
85        self.listener.local_addr()
86    }
87}
88
89impl<O> From<(TcpListener, AuthAdaptor<O>)> for Server<O> {
90    #[inline]
91    fn from((listener, auth): (TcpListener, AuthAdaptor<O>)) -> Self {
92        Self { listener, auth }
93    }
94}
95
96impl<O> From<Server<O>> for (TcpListener, AuthAdaptor<O>) {
97    #[inline]
98    fn from(server: Server<O>) -> Self {
99        (server.listener, server.auth)
100    }
101}