socksprox 0.1.1

Simple SOCKS5 Proxy Server in Rust. Probably shouldn't use this, but you can.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::error::Error;
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::Mutex;
use tokio::{
    net::{TcpListener, TcpStream},
    task,
    time::timeout,
};

mod errors;
mod socks_msgs;

use errors::{SocksHandleError, SocksSetupError, SocksUserAuthError};
use socks_msgs::{
    ClientConnection, SocksAuthMethod, SocksCommand, SocksErrorReply, SocksErrorReplyCode,
    SocksReply, SocksRequest, SocksServerAuthResponse,
};

#[derive(Clone, Debug, PartialEq)]
/// Simple wrapper around a SocksUser with a username / password.
pub struct SocksUser {
    pub username: String,
    password: String,
}
// Maybe some serde JSON for the username/password

impl SocksUser {
    /// Create a new SocksUser with the provided username and password.
    pub fn new(username: String, password: String) -> Self {
        Self { username, password }
    }
}

impl TryFrom<&str> for SocksUser {
    /// Decodes a string with format username:password into a SocksUser.
    /// Of note, the username and password cannot contain the character ':'
    type Error = &'static str;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let userpass: Vec<&str> = value.split(':').collect();
        if userpass.len() != 2 {
            return Err("Invalid input string");
        }
        Ok(Self {
            username: userpass[0].to_string(),
            password: userpass[1].to_string(),
        })
    }
}

/// A super simple, feature-lacking async SOCKS5 server.
/// Currently only supports the CONNECT command (TCP) and limited authentication
/// (Username/Password). No authentication is also supported.
///
/// ## But Why
/// Why would you want to use this? You probably don't, better implementations exist. BUT if you
/// want a straightforward, easy-to-use SOCKS5 library, well, you found it. This can be used (along
/// with the provided binary) to quickly and easily deploy a SOCKS5 Server.
///
/// To use in your own binary, all you need is the tokio runtime. Using anyhow for errors is also
/// convienent to easily handle errors provided by the library. The main function for the binary
/// in it's nascent stages looked something like this:
///
/// ```no_run
/// use socksprox::Socks5Server;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///     let mut server = match Socks5Server::new("0.0.0.0", 4444, None, None).await {
///         Ok(s) => s,
///         Err(e) => { return Err(anyhow::anyhow!("Error starting server: {e}")); }
///     };
///
///     server.serve().await;
///
///     Ok(())
/// }
/// ```
pub struct Socks5Server {
    /// The Listener that will accept connections
    listener: TcpListener,

    /// Table of users for User/Pass authentication method; if None, no authentication will be
    /// supported
    user_table: Arc<Option<Vec<SocksUser>>>,

    /// Optional Duration for a connection timeout
    connection_timeout: Arc<Option<Duration>>,
}

impl Socks5Server {
    /// Create a new Socks5Server and start listening for incoming connections on the provided IP
    /// and port. An optional list of SocksUsers can be provided for Username/Password
    /// authentication. If a list is provided and a client requests Username/Password
    /// authentication, this list of users will be used in order to authenticate clients.
    /// Additionally, an optional timeout can be provided for connections to upstream servers. That
    /// is, if the connection is not made in the specified timeout, the connection will drop.
    ///
    /// To build a server with no authentication and no timeout:
    ///
    /// ```no_run
    /// # use socksprox::Socks5Server;
    /// let server = Socks5Server::new("127.0.0.1", 4444, None, None);
    /// ```
    ///
    /// To add user authentication for clients that request it:
    ///
    /// ```no_run
    /// # use socksprox::{Socks5Server, SocksUser};
    /// let users = vec![SocksUser::new("admin".to_string(), "password".to_string())];
    ///
    /// let server = Socks5Server::new("127.0.0.1", 4444, Some(users), None);
    /// ```
    ///
    /// Finally, to add an optional timeout (like 3 seconds):
    ///
    /// ```no_run
    /// # use socksprox::Socks5Server;
    /// # use std::time::Duration;
    /// let server = Socks5Server::new("127.0.0.1", 4444, None, Some(Duration::from_secs(3)));
    /// ```
    pub async fn new(
        ip: &str,
        port: u16,
        user_table: Option<Vec<SocksUser>>,
        connection_timeout: Option<Duration>,
    ) -> Result<Self, Box<dyn Error>> {
        log::info!("Listening on {ip}:{port}");
        Ok(Self {
            listener: TcpListener::bind((ip, port)).await?,
            user_table: Arc::new(user_table),
            connection_timeout: Arc::new(connection_timeout),
        })
    }

    /// Begin serving connections to clients asynchronously. When a client connects, it will be
    /// handled as a separate task. Until the connection is finished.
    ///
    /// Once, you have a server created with `Socks5Server::new()`, serving clients is as easy as:
    ///
    /// ```no_run
    /// # use socksprox::Socks5Server;
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// # let mut server = match Socks5Server::new("127.0.0.1", 4444, None, None).await {
    /// # Ok(s) => s,
    /// # Err(e) => { return Err(anyhow::anyhow!("Error: {e}"));}
    /// # };
    /// server.serve().await;
    /// # Ok(())
    /// # }
    ///
    /// ```
    pub async fn serve(&mut self) {
        log::info!("Serving clients...");

        while let Ok((client_stream, client_addr)) = self.listener.accept().await {
            log::info!("New connection from: {client_addr}");

            let users = self.user_table.clone();
            let timeout = self.connection_timeout.clone();
            let stream = Arc::new(Mutex::new(client_stream));

            // spawn new tokio task to handle the client
            task::spawn(async move {
                let mut client = SocksConnection::new(stream, client_addr, &users, timeout);
                match client.setup().await.map_err(|e| e.to_string()) {
                    Ok(_) => {}
                    Err(e) => {
                        log::error!("Error initializing SOCKS5 with client: {client_addr}: {e:?}");

                        let _ = client.shutdown().await.map_err(|e| e.to_string());
                    }
                }

                // start handling the client once we have authenticated
                if let Err(err) = client.handle().await.map_err(|e| e.to_string()) {
                    log::warn!("Error handling the client...shutting down: {err:?}");
                    if let Err(e) = client.shutdown().await.map_err(|e| e.to_string()) {
                        log::warn!("Error closing TcpStream: {:?}", e);
                    }
                }
                log::info!("Shutting down client connection: {client_addr:?}");
                let _ = client.shutdown().await;
            });
        }
    }
}

struct SocksConnection<'a> {
    /// Stream for this client connection
    stream: Arc<Mutex<TcpStream>>,

    /// Addr of the client
    addr: SocketAddr,

    /// Optional Users (None indicates NO authentication will be supported)
    users: &'a Option<Vec<SocksUser>>,

    /// Connection timeout for this connection
    timeout: Arc<Option<Duration>>,

    /// Authentication Method chosen for this client
    auth: SocksAuthMethod,
}

impl<'a> SocksConnection<'a> {
    const SOCKS_VERSION: u8 = 0x05;

    fn new(
        stream: Arc<Mutex<TcpStream>>,
        addr: SocketAddr,
        users: &'a Option<Vec<SocksUser>>,
        timeout: Arc<Option<Duration>>,
    ) -> Self {
        Self {
            stream,
            addr,
            users,
            timeout,
            auth: SocksAuthMethod::NoAuth,
        }
    }

    async fn setup(&mut self) -> Result<(), Box<dyn Error>> {
        log::debug!("Setting up new connection with {:?}", self.addr);

        let client_init = ClientConnection::read_from(self.stream.clone()).await?;

        // pick the auth method (Server only supports No Authentication or User/Pass)
        self.auth = if self.users.is_some() && client_init.user_pass_supported() {
            // we can support this and the client requested it
            SocksAuthMethod::UserPass
        } else if client_init.no_auth_supported() {
            SocksAuthMethod::NoAuth
        } else {
            SocksAuthMethod::NoAcceptableMethods
        };

        log::info!("Server selecting {:?} for {}", self.auth, self.addr);

        self.authenticate().await?;

        Ok(())
    }

    /// Sends a ServerConnectionResponse and authenticates the client (if needed)
    async fn authenticate(&self) -> Result<(), Box<dyn Error>> {
        match self.auth {
            SocksAuthMethod::NoAuth => {
                let response = SocksServerAuthResponse::create(
                    Self::SOCKS_VERSION,
                    SocksAuthMethod::NoAuth as u8,
                );
                response.send(self.stream.clone()).await?;
                log::info!("Authenticated with NoAuth");
                Ok(())
            }
            SocksAuthMethod::UserPass => {
                // initial response that we accept UserPass auth
                let response = SocksServerAuthResponse::create(
                    Self::SOCKS_VERSION,
                    SocksAuthMethod::UserPass as u8,
                );
                response.send(self.stream.clone()).await?;

                let users = self.users.as_ref().unwrap();
                let auth_engine = UserAuthEngine::create(users, self.stream.clone());
                let user_authed = auth_engine.authenticate().await?;
                log::info!("Authenticated user: {}", user_authed.username);
                Ok(())
            }
            _ => {
                let response = SocksServerAuthResponse::create(
                    Self::SOCKS_VERSION,
                    SocksAuthMethod::NoAcceptableMethods as u8,
                );
                response.send(self.stream.clone()).await?;
                Err(SocksSetupError::UnsupportedAuthMethod)?
            }
        }
    }

    async fn shutdown(&self) -> tokio::io::Result<()> {
        self.stream.lock().await.shutdown().await
    }

    async fn handle(&self) -> Result<usize, Box<dyn Error>> {
        log::info!("Receiving client requests");

        // should match on the errors that this can give. because we need to send a reply no matter
        // what. send the reply here and then return a specific error.
        // just match on what the request gives. if you get an error, then send response and return
        // later if you have a bad socks command, handle that there
        let request = SocksRequest::read_from_stream(self.stream.clone()).await?;

        log::info!(
            "Request; Command: {:?}, Addr/Port: {:?}",
            request.command,
            request.address
        );

        match request.command {
            SocksCommand::Connect => {
                // make a connection to addr:port
                log::info!("Connecting to: {:?}", request.address);

                let to = self.timeout.unwrap_or(Duration::from_millis(500));
                let addr = request
                    .address
                    .get_resolved()
                    .ok_or(SocksHandleError::FailedHostLookup)?;

                let mut conn = match timeout(to, async move { TcpStream::connect(addr).await })
                    .await
                    .map_err(|_| SocksHandleError::FailedConnection)
                {
                    Ok(conn_res) => match conn_res {
                        Ok(conn) => conn,
                        Err(_e) => {
                            SocksErrorReply::send(
                                self.stream.clone(),
                                SocksErrorReplyCode::ConnectionRefused,
                            )
                            .await?;
                            return Err(SocksHandleError::FailedConnection)?;
                        }
                    },
                    Err(_e) => {
                        SocksErrorReply::send(
                            self.stream.clone(),
                            SocksErrorReplyCode::ConnectionRefused,
                        )
                        .await?;
                        return Err(SocksHandleError::FailedConnection)?;
                    }
                };

                log::info!("Connected to upstream");

                // send reply indicating success
                SocksReply::send(self.stream.clone()).await?;

                // copy the data!
                log::info!("Copying data");
                let mut stream_b = self.stream.lock().await;
                match tokio::io::copy_bidirectional(&mut *stream_b, &mut conn).await {
                    Err(_e) => Err(SocksHandleError::FailedRelay)?,
                    Ok((_cli_to_serv, serv_to_up)) => {
                        let _ = conn.shutdown().await;
                        Ok(serv_to_up as usize)
                    }
                }
            }
            SocksCommand::Bind => {
                SocksErrorReply::send(
                    self.stream.clone(),
                    SocksErrorReplyCode::CommandNotSupported,
                )
                .await?;
                Err(SocksHandleError::BindNotSupported)?
            }
            SocksCommand::UdpAssociate => {
                SocksErrorReply::send(
                    self.stream.clone(),
                    SocksErrorReplyCode::CommandNotSupported,
                )
                .await?;
                Err(SocksHandleError::UdpNotSupported)?
            }
        }
    }
}

/// Engine to authenticate using UserPass
struct UserAuthEngine<'a> {
    /// Users and passwords
    users: &'a Vec<SocksUser>,

    /// Stream to use for negotiating the authentication
    stream: Arc<Mutex<TcpStream>>,
}

impl<'a> UserAuthEngine<'a> {
    const USER_PASS_VERSION: u8 = 0x01;
    const USER_PASS_AUTH_SUCCESS: u8 = 0x00;
    const USER_PASS_AUTH_FAILED: u8 = 0x01;

    fn create(users: &'a Vec<SocksUser>, stream: Arc<Mutex<TcpStream>>) -> Self {
        Self { users, stream }
    }

    async fn authenticate(&self) -> Result<&'a SocksUser, Box<dyn Error>> {
        // get initial header of version and username length
        let mut initial_hdr = [0u8; 2];

        self.stream
            .lock()
            .await
            .read_exact(&mut initial_hdr)
            .await?;

        if initial_hdr[0] != Self::USER_PASS_VERSION {
            return Err(SocksUserAuthError::InvalidVersion)?;
        }

        // read username
        let username_len = initial_hdr[1] as usize;

        let mut username = vec![0u8; username_len];
        self.stream.lock().await.read_exact(&mut username).await?;

        // password length (read it all to guard against timing attacks on username finding)
        let mut password_len_buf = [0u8; 1];
        self.stream
            .lock()
            .await
            .read_exact(&mut password_len_buf)
            .await?;
        let password_len = password_len_buf[0] as usize;

        // read password
        let mut password = vec![0u8; password_len];
        self.stream.lock().await.read_exact(&mut password).await?;

        // see if we can find this SocksUser in users
        let client_creds = SocksUser::new(
            String::from_utf8_lossy(&username).to_string(),
            String::from_utf8_lossy(&password).to_string(),
        );

        match self.users.iter().position(|x| *x == client_creds) {
            Some(user) => {
                let response = SocksServerAuthResponse::create(
                    Self::USER_PASS_VERSION,
                    Self::USER_PASS_AUTH_SUCCESS,
                );
                response.send(self.stream.clone()).await?;
                Ok(&self.users[user])
            }
            None => {
                let response = SocksServerAuthResponse::create(
                    Self::USER_PASS_VERSION,
                    Self::USER_PASS_AUTH_FAILED,
                );
                response.send(self.stream.clone()).await?;
                Err(Box::new(SocksUserAuthError::FailedAuthentication))
            }
        }
    }
}