|
| 1 | +// Partial implementation of RFC 1928, Socks5 protocol |
| 2 | +// ref: https://datatracker.ietf.org/doc/html/rfc1928#section-1 |
1 | 3 |
|
| 4 | +use std::{ |
| 5 | + net::{IpAddr, SocketAddr}, |
| 6 | + time::Duration, |
| 7 | +}; |
| 8 | + |
| 9 | +use tokio::{ |
| 10 | + io::{AsyncReadExt, AsyncWriteExt}, |
| 11 | + net::TcpStream, |
| 12 | +}; |
| 13 | + |
| 14 | +use crate::network::{StreamReader, StreamWriter}; |
| 15 | + |
| 16 | +use super::error::Socks5Error; |
| 17 | + |
| 18 | +const CONNECTION_TIMEOUT: u64 = 2; |
| 19 | +const VERSION: u8 = 5; |
| 20 | +const NOAUTH: u8 = 0; |
| 21 | +const METHODS: u8 = 1; |
| 22 | +const CMD_CONNECT: u8 = 1; |
| 23 | +const RESPONSE_SUCCESS: u8 = 0; |
| 24 | +const RSV: u8 = 0; |
| 25 | +const ADDR_TYPE_IPV4: u8 = 1; |
| 26 | +const ADDR_TYPE_IPV6: u8 = 4; |
| 27 | + |
| 28 | +pub(crate) async fn create_socks5( |
| 29 | + proxy: SocketAddr, |
| 30 | + ip_addr: IpAddr, |
| 31 | + port: u16, |
| 32 | +) -> Result<(StreamReader, StreamWriter), Socks5Error> { |
| 33 | + // Connect to the proxy, likely a local Tor daemon. |
| 34 | + let timeout = tokio::time::timeout( |
| 35 | + Duration::from_secs(CONNECTION_TIMEOUT), |
| 36 | + TcpStream::connect(proxy), |
| 37 | + ) |
| 38 | + .await |
| 39 | + .map_err(|_| Socks5Error::ConnectionTimeout)?; |
| 40 | + // Format the destination IP address and port according to the Socks5 spec |
| 41 | + let dest_ip_bytes = match ip_addr { |
| 42 | + IpAddr::V4(ipv4) => ipv4.octets().to_vec(), |
| 43 | + IpAddr::V6(ipv6) => ipv6.octets().to_vec(), |
| 44 | + }; |
| 45 | + let dest_port_bytes = port.to_be_bytes(); |
| 46 | + let ip_type_byte = match ip_addr { |
| 47 | + IpAddr::V4(_) => ADDR_TYPE_IPV4, |
| 48 | + IpAddr::V6(_) => ADDR_TYPE_IPV6, |
| 49 | + }; |
| 50 | + // Begin the handshake by requesting a connection to the proxy. |
| 51 | + let mut tcp_stream = timeout.map_err(|_| Socks5Error::ConnectionFailed)?; |
| 52 | + tcp_stream.write_all(&[VERSION, METHODS, NOAUTH]).await?; |
| 53 | + // Read the response from the proxy |
| 54 | + let mut buf = [0_u8; 2]; |
| 55 | + tcp_stream.read_exact(&mut buf).await?; |
| 56 | + if buf[0] != VERSION { |
| 57 | + return Err(Socks5Error::WrongVersion); |
| 58 | + } |
| 59 | + if buf[1] != NOAUTH { |
| 60 | + return Err(Socks5Error::AuthRequired); |
| 61 | + } |
| 62 | + // Write the request to the proxy to connect to our destination |
| 63 | + tcp_stream |
| 64 | + .write_all(&[VERSION, CMD_CONNECT, RSV, ip_type_byte]) |
| 65 | + .await?; |
| 66 | + tcp_stream.write_all(&dest_ip_bytes).await?; |
| 67 | + tcp_stream.write_all(&dest_port_bytes).await?; |
| 68 | + // First 4 bytes of the response: version, success/failure, reserved byte, ip type |
| 69 | + let mut buf = [0_u8; 4]; |
| 70 | + tcp_stream.read_exact(&mut buf).await?; |
| 71 | + if buf[0] != VERSION { |
| 72 | + return Err(Socks5Error::WrongVersion); |
| 73 | + } |
| 74 | + if buf[1] != RESPONSE_SUCCESS { |
| 75 | + return Err(Socks5Error::ConnectionFailed); |
| 76 | + } |
| 77 | + // Read off the destination of our request |
| 78 | + match buf[3] { |
| 79 | + ADDR_TYPE_IPV4 => { |
| 80 | + // Read the IPv4 address and additonal two bytes for the port |
| 81 | + let mut buf = [0_u8; 6]; |
| 82 | + tcp_stream.read_exact(&mut buf).await?; |
| 83 | + } |
| 84 | + ADDR_TYPE_IPV6 => { |
| 85 | + // Read the IPv6 address and additonal two bytes for the port |
| 86 | + let mut buf = [0_u8; 18]; |
| 87 | + tcp_stream.read_exact(&mut buf).await?; |
| 88 | + } |
| 89 | + _ => return Err(Socks5Error::ConnectionFailed), |
| 90 | + } |
| 91 | + // Proxy handshake is complete, the TCP reader/writer can be returned |
| 92 | + let (reader, writer) = tcp_stream.into_split(); |
| 93 | + Ok((Box::new(reader), Box::new(writer))) |
| 94 | +} |
0 commit comments