2023-07-23 02:03:15 +08:00
|
|
|
use crate::{
|
|
|
|
error::Error,
|
|
|
|
tun2proxy::{
|
|
|
|
Connection, ConnectionManager, Direction, IncomingDataEvent, IncomingDirection,
|
|
|
|
OutgoingDataEvent, OutgoingDirection, TcpProxy,
|
|
|
|
},
|
2022-08-01 14:36:58 +00:00
|
|
|
};
|
2023-07-23 02:03:15 +08:00
|
|
|
use smoltcp::wire::IpProtocol;
|
|
|
|
use socks5_impl::protocol::{self, Address, AddressType, UserKey};
|
|
|
|
use std::{collections::VecDeque, convert::TryFrom, net::SocketAddr, rc::Rc};
|
2021-09-02 11:30:23 +02:00
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
enum SocksState {
|
|
|
|
ClientHello,
|
|
|
|
ServerHello,
|
2023-03-22 01:02:27 +01:00
|
|
|
SendAuthData,
|
|
|
|
ReceiveAuthResponse,
|
2021-09-02 11:30:23 +02:00
|
|
|
SendRequest,
|
|
|
|
ReceiveResponse,
|
2022-08-01 14:36:58 +00:00
|
|
|
Established,
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 12:08:20 +02:00
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2023-03-25 01:39:46 +01:00
|
|
|
pub enum SocksVersion {
|
|
|
|
V4 = 4,
|
|
|
|
V5 = 5,
|
|
|
|
}
|
|
|
|
|
2021-09-02 11:30:23 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
enum SocksAuthentication {
|
|
|
|
None = 0,
|
2023-05-31 18:25:24 +02:00
|
|
|
GssApi = 1,
|
2022-08-01 14:36:58 +00:00
|
|
|
Password = 2,
|
2023-05-31 18:25:24 +02:00
|
|
|
ChallengeHandshake = 3,
|
|
|
|
Unassigned = 4,
|
|
|
|
Unassigned100 = 100,
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 21:02:17 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
enum SocksReplies {
|
|
|
|
Succeeded,
|
|
|
|
GeneralFailure,
|
|
|
|
ConnectionDisallowed,
|
|
|
|
NetworkUnreachable,
|
|
|
|
ConnectionRefused,
|
|
|
|
TtlExpired,
|
|
|
|
CommandUnsupported,
|
2022-08-01 14:36:58 +00:00
|
|
|
AddressUnsupported,
|
2021-09-02 21:02:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for SocksReplies {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 01:02:27 +01:00
|
|
|
pub(crate) struct SocksConnection {
|
2021-09-02 11:30:23 +02:00
|
|
|
connection: Connection,
|
|
|
|
state: SocksState,
|
|
|
|
client_inbuf: VecDeque<u8>,
|
|
|
|
server_inbuf: VecDeque<u8>,
|
|
|
|
client_outbuf: VecDeque<u8>,
|
|
|
|
server_outbuf: VecDeque<u8>,
|
|
|
|
data_buf: VecDeque<u8>,
|
2023-03-25 01:39:46 +01:00
|
|
|
version: SocksVersion,
|
2023-07-23 02:03:15 +08:00
|
|
|
credentials: Option<UserKey>,
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SocksConnection {
|
2023-03-25 01:39:46 +01:00
|
|
|
pub fn new(
|
|
|
|
connection: &Connection,
|
|
|
|
manager: Rc<dyn ConnectionManager>,
|
|
|
|
version: SocksVersion,
|
|
|
|
) -> Result<Self, Error> {
|
2021-09-02 11:30:23 +02:00
|
|
|
let mut result = Self {
|
2023-03-22 19:11:28 +01:00
|
|
|
connection: connection.clone(),
|
2021-09-02 11:30:23 +02:00
|
|
|
state: SocksState::ServerHello,
|
2023-04-10 18:26:32 +08:00
|
|
|
client_inbuf: VecDeque::default(),
|
|
|
|
server_inbuf: VecDeque::default(),
|
|
|
|
client_outbuf: VecDeque::default(),
|
|
|
|
server_outbuf: VecDeque::default(),
|
|
|
|
data_buf: VecDeque::default(),
|
2023-03-25 01:39:46 +01:00
|
|
|
version,
|
2023-04-27 22:42:34 +02:00
|
|
|
credentials: manager.get_credentials().clone(),
|
2021-09-02 11:30:23 +02:00
|
|
|
};
|
2023-03-25 01:39:46 +01:00
|
|
|
result.send_client_hello()?;
|
|
|
|
Ok(result)
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
fn send_client_hello(&mut self) -> Result<(), Error> {
|
2023-04-27 22:42:34 +02:00
|
|
|
let credentials = &self.credentials;
|
2023-03-25 01:39:46 +01:00
|
|
|
match self.version {
|
|
|
|
SocksVersion::V4 => {
|
2023-07-23 02:03:15 +08:00
|
|
|
self.server_outbuf
|
|
|
|
.extend(&[self.version as u8, protocol::Command::Connect.into()]);
|
|
|
|
self.server_outbuf
|
|
|
|
.extend(self.connection.dst.port().to_be_bytes());
|
2023-03-25 01:39:46 +01:00
|
|
|
let mut ip_vec = Vec::<u8>::new();
|
|
|
|
let mut name_vec = Vec::<u8>::new();
|
2023-07-23 02:03:15 +08:00
|
|
|
match &self.connection.dst {
|
|
|
|
Address::SocketAddress(SocketAddr::V4(addr)) => {
|
|
|
|
ip_vec.extend(addr.ip().octets().as_ref());
|
|
|
|
}
|
|
|
|
Address::SocketAddress(SocketAddr::V6(_)) => {
|
|
|
|
return Err("SOCKS4 does not support IPv6".into());
|
2023-03-25 01:39:46 +01:00
|
|
|
}
|
2023-07-23 02:03:15 +08:00
|
|
|
Address::DomainAddress(host, _) => {
|
2023-03-25 01:39:46 +01:00
|
|
|
ip_vec.extend(&[0, 0, 0, host.len() as u8]);
|
|
|
|
name_vec.extend(host.as_bytes());
|
|
|
|
name_vec.push(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.server_outbuf.extend(ip_vec);
|
|
|
|
if let Some(credentials) = credentials {
|
2023-06-22 13:09:36 -04:00
|
|
|
self.server_outbuf.extend(credentials.username.as_bytes());
|
2023-03-25 01:39:46 +01:00
|
|
|
if !credentials.password.is_empty() {
|
|
|
|
self.server_outbuf.push_back(b':');
|
2023-06-22 13:09:36 -04:00
|
|
|
self.server_outbuf.extend(credentials.password.as_bytes());
|
2023-03-25 01:39:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.server_outbuf.push_back(0);
|
|
|
|
self.server_outbuf.extend(name_vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
SocksVersion::V5 => {
|
2023-05-31 18:25:24 +02:00
|
|
|
// Providing unassigned methods is supposed to bypass China's GFW.
|
|
|
|
// For details, refer to https://github.com/blechschmidt/tun2proxy/issues/35.
|
2023-03-25 01:39:46 +01:00
|
|
|
if credentials.is_some() {
|
2023-04-15 12:08:20 +02:00
|
|
|
self.server_outbuf.extend(&[
|
|
|
|
self.version as u8,
|
2023-05-31 18:25:24 +02:00
|
|
|
4u8,
|
2023-05-27 10:28:35 +02:00
|
|
|
SocksAuthentication::None as u8,
|
2023-04-15 12:08:20 +02:00
|
|
|
SocksAuthentication::Password as u8,
|
2023-05-31 18:25:24 +02:00
|
|
|
SocksAuthentication::Unassigned as u8,
|
|
|
|
SocksAuthentication::Unassigned100 as u8,
|
2023-04-15 12:08:20 +02:00
|
|
|
]);
|
2023-03-25 01:39:46 +01:00
|
|
|
} else {
|
2023-04-15 12:08:20 +02:00
|
|
|
self.server_outbuf.extend(&[
|
|
|
|
self.version as u8,
|
2023-05-31 18:25:24 +02:00
|
|
|
3u8,
|
2023-04-15 12:08:20 +02:00
|
|
|
SocksAuthentication::None as u8,
|
2023-05-31 18:25:24 +02:00
|
|
|
SocksAuthentication::Unassigned as u8,
|
|
|
|
SocksAuthentication::Unassigned100 as u8,
|
2023-04-15 12:08:20 +02:00
|
|
|
]);
|
2023-03-25 01:39:46 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
|
|
|
self.state = SocksState::ServerHello;
|
2023-03-25 01:39:46 +01:00
|
|
|
Ok(())
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
fn receive_server_hello_socks4(&mut self) -> Result<(), Error> {
|
|
|
|
if self.server_inbuf.len() < 8 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.server_inbuf[1] != 0x5a {
|
|
|
|
return Err("SOCKS4 server replied with an unexpected reply code.".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.server_inbuf.drain(0..8);
|
|
|
|
self.server_outbuf.append(&mut self.data_buf);
|
|
|
|
self.data_buf.clear();
|
|
|
|
|
|
|
|
self.state = SocksState::Established;
|
|
|
|
self.state_change()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn receive_server_hello_socks5(&mut self) -> Result<(), Error> {
|
2023-03-22 01:02:27 +01:00
|
|
|
if self.server_inbuf.len() < 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if self.server_inbuf[0] != 5 {
|
2023-03-25 01:39:46 +01:00
|
|
|
return Err("SOCKS5 server replied with an unexpected version.".into());
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
2021-09-02 21:02:17 +02:00
|
|
|
|
2023-05-27 10:28:35 +02:00
|
|
|
let auth_method = self.server_inbuf[1];
|
|
|
|
|
|
|
|
if auth_method != SocksAuthentication::None as u8 && self.credentials.is_none()
|
|
|
|
|| (auth_method != SocksAuthentication::None as u8
|
|
|
|
&& auth_method != SocksAuthentication::Password as u8)
|
|
|
|
&& self.credentials.is_some()
|
2023-03-22 01:02:27 +01:00
|
|
|
{
|
2023-03-25 01:39:46 +01:00
|
|
|
return Err("SOCKS5 server requires an unsupported authentication method.".into());
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
2021-09-02 21:02:17 +02:00
|
|
|
|
2023-03-22 01:02:27 +01:00
|
|
|
self.server_inbuf.drain(0..2);
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-05-27 10:28:35 +02:00
|
|
|
if auth_method == SocksAuthentication::Password as u8 {
|
2023-03-22 01:02:27 +01:00
|
|
|
self.state = SocksState::SendAuthData;
|
|
|
|
} else {
|
|
|
|
self.state = SocksState::SendRequest;
|
|
|
|
}
|
|
|
|
self.state_change()
|
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
fn receive_server_hello(&mut self) -> Result<(), Error> {
|
|
|
|
match self.version {
|
|
|
|
SocksVersion::V4 => self.receive_server_hello_socks4(),
|
|
|
|
SocksVersion::V5 => self.receive_server_hello_socks5(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
fn send_auth_data(&mut self) -> Result<(), Error> {
|
2023-07-23 02:03:15 +08:00
|
|
|
let tmp = UserKey::default();
|
2023-04-27 22:42:34 +02:00
|
|
|
let credentials = self.credentials.as_ref().unwrap_or(&tmp);
|
2023-03-22 01:02:27 +01:00
|
|
|
self.server_outbuf
|
|
|
|
.extend(&[1u8, credentials.username.len() as u8]);
|
2023-06-22 13:09:36 -04:00
|
|
|
self.server_outbuf.extend(credentials.username.as_bytes());
|
2023-03-22 01:02:27 +01:00
|
|
|
self.server_outbuf
|
|
|
|
.extend(&[credentials.password.len() as u8]);
|
2023-06-22 13:09:36 -04:00
|
|
|
self.server_outbuf.extend(credentials.password.as_bytes());
|
2023-03-22 01:02:27 +01:00
|
|
|
self.state = SocksState::ReceiveAuthResponse;
|
|
|
|
self.state_change()
|
|
|
|
}
|
2021-09-02 21:02:17 +02:00
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
fn receive_auth_data(&mut self) -> Result<(), Error> {
|
2023-03-22 01:02:27 +01:00
|
|
|
if self.server_inbuf.len() < 2 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if self.server_inbuf[0] != 1 || self.server_inbuf[1] != 0 {
|
2023-03-22 22:19:00 +08:00
|
|
|
return Err("SOCKS authentication failed.".into());
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
|
|
|
self.server_inbuf.drain(0..2);
|
|
|
|
self.state = SocksState::SendRequest;
|
|
|
|
self.state_change()
|
|
|
|
}
|
2021-09-02 21:02:17 +02:00
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
fn receive_connection_status(&mut self) -> Result<(), Error> {
|
2023-03-22 01:02:27 +01:00
|
|
|
if self.server_inbuf.len() < 4 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let ver = self.server_inbuf[0];
|
|
|
|
let rep = self.server_inbuf[1];
|
|
|
|
let _rsv = self.server_inbuf[2];
|
|
|
|
let atyp = self.server_inbuf[3];
|
|
|
|
|
|
|
|
if ver != 5 {
|
2023-03-25 01:39:46 +01:00
|
|
|
return Err("SOCKS5 server replied with an unexpected version.".into());
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-03-22 01:02:27 +01:00
|
|
|
if rep != 0 {
|
2023-03-25 01:39:46 +01:00
|
|
|
return Err("SOCKS5 connection unsuccessful.".into());
|
2023-03-22 01:02:27 +01:00
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-07-23 02:03:15 +08:00
|
|
|
let message_length = match AddressType::try_from(atyp)? {
|
|
|
|
AddressType::Domain => {
|
2023-04-10 18:26:32 +08:00
|
|
|
if self.server_inbuf.len() < 5 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if self.server_inbuf.len() < 7 + (self.server_inbuf[4] as usize) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
7 + (self.server_inbuf[4] as usize)
|
|
|
|
}
|
2023-07-23 02:03:15 +08:00
|
|
|
AddressType::IPv4 => 10,
|
|
|
|
AddressType::IPv6 => 22,
|
2023-03-22 01:02:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.server_inbuf.drain(0..message_length);
|
|
|
|
self.server_outbuf.append(&mut self.data_buf);
|
|
|
|
self.data_buf.clear();
|
|
|
|
|
|
|
|
self.state = SocksState::Established;
|
|
|
|
self.state_change()
|
|
|
|
}
|
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
fn send_request(&mut self) -> Result<(), Error> {
|
2023-07-23 02:03:15 +08:00
|
|
|
protocol::Request::new(protocol::Command::Connect, self.connection.dst.clone())
|
|
|
|
.write_to_stream(&mut self.server_outbuf)?;
|
2023-03-22 01:02:27 +01:00
|
|
|
self.state = SocksState::ReceiveResponse;
|
|
|
|
self.state_change()
|
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
fn relay_traffic(&mut self) -> Result<(), Error> {
|
|
|
|
self.client_outbuf.extend(self.server_inbuf.iter());
|
|
|
|
self.server_outbuf.extend(self.client_inbuf.iter());
|
|
|
|
self.server_inbuf.clear();
|
|
|
|
self.client_inbuf.clear();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-22 22:19:00 +08:00
|
|
|
pub fn state_change(&mut self) -> Result<(), Error> {
|
2023-03-22 01:02:27 +01:00
|
|
|
match self.state {
|
|
|
|
SocksState::ServerHello => self.receive_server_hello(),
|
|
|
|
|
|
|
|
SocksState::SendAuthData => self.send_auth_data(),
|
|
|
|
|
|
|
|
SocksState::ReceiveAuthResponse => self.receive_auth_data(),
|
|
|
|
|
|
|
|
SocksState::SendRequest => self.send_request(),
|
|
|
|
|
|
|
|
SocksState::ReceiveResponse => self.receive_connection_status(),
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
SocksState::Established => self.relay_traffic(),
|
2021-09-02 11:30:23 +02:00
|
|
|
|
2023-03-22 01:02:27 +01:00
|
|
|
_ => Ok(()),
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TcpProxy for SocksConnection {
|
2023-03-22 22:19:00 +08:00
|
|
|
fn push_data(&mut self, event: IncomingDataEvent<'_>) -> Result<(), Error> {
|
2021-09-02 11:30:23 +02:00
|
|
|
let direction = event.direction;
|
|
|
|
let buffer = event.buffer;
|
|
|
|
match direction {
|
|
|
|
IncomingDirection::FromServer => {
|
|
|
|
self.server_inbuf.extend(buffer.iter());
|
2022-08-01 14:36:58 +00:00
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
IncomingDirection::FromClient => {
|
|
|
|
if self.state == SocksState::Established {
|
|
|
|
self.client_inbuf.extend(buffer.iter());
|
|
|
|
} else {
|
|
|
|
self.data_buf.extend(buffer.iter());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:02:17 +02:00
|
|
|
self.state_change()
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn consume_data(&mut self, dir: OutgoingDirection, size: usize) {
|
2022-08-01 14:36:58 +00:00
|
|
|
let buffer = if dir == OutgoingDirection::ToServer {
|
2021-09-02 11:30:23 +02:00
|
|
|
&mut self.server_outbuf
|
|
|
|
} else {
|
|
|
|
&mut self.client_outbuf
|
|
|
|
};
|
|
|
|
buffer.drain(0..size);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek_data(&mut self, dir: OutgoingDirection) -> OutgoingDataEvent {
|
|
|
|
let buffer = if dir == OutgoingDirection::ToServer {
|
|
|
|
&mut self.server_outbuf
|
|
|
|
} else {
|
|
|
|
&mut self.client_outbuf
|
|
|
|
};
|
2022-08-01 14:36:58 +00:00
|
|
|
OutgoingDataEvent {
|
2021-09-02 11:30:23 +02:00
|
|
|
direction: dir,
|
2022-08-01 14:36:58 +00:00
|
|
|
buffer: buffer.make_contiguous(),
|
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
2021-09-02 21:02:17 +02:00
|
|
|
|
|
|
|
fn connection_established(&self) -> bool {
|
2022-08-01 14:36:58 +00:00
|
|
|
self.state == SocksState::Established
|
2021-09-02 21:02:17 +02:00
|
|
|
}
|
2023-04-04 00:18:50 +02:00
|
|
|
|
|
|
|
fn have_data(&mut self, dir: Direction) -> bool {
|
|
|
|
match dir {
|
|
|
|
Direction::Incoming(incoming) => match incoming {
|
2023-04-04 00:19:41 +02:00
|
|
|
IncomingDirection::FromServer => !self.server_inbuf.is_empty(),
|
2023-04-04 00:18:50 +02:00
|
|
|
IncomingDirection::FromClient => {
|
2023-04-04 00:19:41 +02:00
|
|
|
!self.client_inbuf.is_empty() || !self.data_buf.is_empty()
|
2023-04-04 00:18:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Direction::Outgoing(outgoing) => match outgoing {
|
2023-04-04 00:19:41 +02:00
|
|
|
OutgoingDirection::ToServer => !self.server_outbuf.is_empty(),
|
|
|
|
OutgoingDirection::ToClient => !self.client_outbuf.is_empty(),
|
2023-04-04 00:18:50 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 13:09:36 -04:00
|
|
|
|
|
|
|
fn reset_connection(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
pub struct SocksManager {
|
2023-03-23 20:00:59 +08:00
|
|
|
server: SocketAddr,
|
2023-07-23 02:03:15 +08:00
|
|
|
credentials: Option<UserKey>,
|
2023-03-25 01:39:46 +01:00
|
|
|
version: SocksVersion,
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
impl ConnectionManager for SocksManager {
|
2021-09-02 11:30:23 +02:00
|
|
|
fn handles_connection(&self, connection: &Connection) -> bool {
|
2023-03-25 00:24:02 +08:00
|
|
|
connection.proto == IpProtocol::Tcp
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 01:02:27 +01:00
|
|
|
fn new_connection(
|
|
|
|
&self,
|
|
|
|
connection: &Connection,
|
2023-03-23 16:31:33 +08:00
|
|
|
manager: Rc<dyn ConnectionManager>,
|
2023-03-25 01:39:46 +01:00
|
|
|
) -> Result<Option<Box<dyn TcpProxy>>, Error> {
|
2023-03-25 00:24:02 +08:00
|
|
|
if connection.proto != IpProtocol::Tcp {
|
2023-03-25 01:39:46 +01:00
|
|
|
return Ok(None);
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
2023-03-25 01:39:46 +01:00
|
|
|
Ok(Some(Box::new(SocksConnection::new(
|
|
|
|
connection,
|
|
|
|
manager,
|
|
|
|
self.version,
|
|
|
|
)?)))
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 01:08:44 +01:00
|
|
|
fn close_connection(&self, _: &Connection) {}
|
2021-09-02 11:30:23 +02:00
|
|
|
|
|
|
|
fn get_server(&self) -> SocketAddr {
|
|
|
|
self.server
|
|
|
|
}
|
2023-03-22 01:02:27 +01:00
|
|
|
|
2023-07-23 02:03:15 +08:00
|
|
|
fn get_credentials(&self) -> &Option<UserKey> {
|
2023-03-22 01:02:27 +01:00
|
|
|
&self.credentials
|
|
|
|
}
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
|
|
|
|
2023-03-25 01:39:46 +01:00
|
|
|
impl SocksManager {
|
|
|
|
pub fn new(
|
|
|
|
server: SocketAddr,
|
|
|
|
version: SocksVersion,
|
2023-07-23 02:03:15 +08:00
|
|
|
credentials: Option<UserKey>,
|
2023-03-25 01:39:46 +01:00
|
|
|
) -> Rc<Self> {
|
2023-03-23 16:31:33 +08:00
|
|
|
Rc::new(Self {
|
2021-09-02 11:30:23 +02:00
|
|
|
server,
|
2023-03-22 01:02:27 +01:00
|
|
|
credentials,
|
2023-03-25 01:39:46 +01:00
|
|
|
version,
|
2023-03-22 01:02:27 +01:00
|
|
|
})
|
2021-09-02 11:30:23 +02:00
|
|
|
}
|
2022-08-01 14:36:58 +00:00
|
|
|
}
|