reading code

This commit is contained in:
ssrlive 2023-09-02 20:23:32 +08:00
parent 5bd62d3101
commit 538e40d05b
3 changed files with 20 additions and 31 deletions

View file

@ -401,7 +401,7 @@ impl ConnectionManager for HttpManager {
} }
Ok(Box::new(HttpConnection::new( Ok(Box::new(HttpConnection::new(
info, info,
self.credentials.clone(), self.get_credentials().clone(),
self.digest_state.clone(), self.digest_state.clone(),
)?)) )?))
} }

View file

@ -274,8 +274,7 @@ impl ProxyHandler for SocksProxyImpl {
} }
fn push_data(&mut self, event: IncomingDataEvent<'_>) -> Result<(), Error> { fn push_data(&mut self, event: IncomingDataEvent<'_>) -> Result<(), Error> {
let direction = event.direction; let IncomingDataEvent { direction, buffer } = event;
let buffer = event.buffer;
match direction { match direction {
IncomingDirection::FromServer => { IncomingDirection::FromServer => {
self.server_inbuf.extend(buffer.iter()); self.server_inbuf.extend(buffer.iter());
@ -293,19 +292,17 @@ impl ProxyHandler for SocksProxyImpl {
} }
fn consume_data(&mut self, dir: OutgoingDirection, size: usize) { fn consume_data(&mut self, dir: OutgoingDirection, size: usize) {
let buffer = if dir == OutgoingDirection::ToServer { let buffer = match dir {
&mut self.server_outbuf OutgoingDirection::ToServer => &mut self.server_outbuf,
} else { OutgoingDirection::ToClient => &mut self.client_outbuf,
&mut self.client_outbuf
}; };
buffer.drain(0..size); buffer.drain(0..size);
} }
fn peek_data(&mut self, dir: OutgoingDirection) -> OutgoingDataEvent { fn peek_data(&mut self, dir: OutgoingDirection) -> OutgoingDataEvent {
let buffer = if dir == OutgoingDirection::ToServer { let buffer = match dir {
&mut self.server_outbuf OutgoingDirection::ToServer => &mut self.server_outbuf,
} else { OutgoingDirection::ToClient => &mut self.client_outbuf,
&mut self.client_outbuf
}; };
OutgoingDataEvent { OutgoingDataEvent {
direction: dir, direction: dir,
@ -349,7 +346,7 @@ impl ConnectionManager for SocksProxyManager {
fn new_proxy_handler(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn ProxyHandler>> { fn new_proxy_handler(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn ProxyHandler>> {
use socks5_impl::protocol::Command::{Connect, UdpAssociate}; use socks5_impl::protocol::Command::{Connect, UdpAssociate};
let command = if udp_associate { UdpAssociate } else { Connect }; let command = if udp_associate { UdpAssociate } else { Connect };
let credentials = self.credentials.clone(); let credentials = self.get_credentials().clone();
Ok(Box::new(SocksProxyImpl::new(info, credentials, self.version, command)?)) Ok(Box::new(SocksProxyImpl::new(info, credentials, self.version, command)?))
} }

View file

@ -56,8 +56,7 @@ impl ConnectionInfo {
fn to_named(&self, name: String) -> Self { fn to_named(&self, name: String) -> Self {
let mut result = self.clone(); let mut result = self.clone();
result.dst = Address::from((name, result.dst.port())); result.dst = Address::from((name, result.dst.port()));
let p = self.protocol; log::trace!("{} replace dst \"{}\" -> \"{}\"", self.protocol, self.dst, result.dst);
log::trace!("{p} replace dst \"{}\" -> \"{}\"", self.dst, result.dst);
result result
} }
} }
@ -68,31 +67,25 @@ impl std::fmt::Display for ConnectionInfo {
} }
} }
#[derive(Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub(crate) enum IncomingDirection { pub(crate) enum IncomingDirection {
FromServer, FromServer,
FromClient, FromClient,
} }
#[derive(Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub(crate) enum OutgoingDirection { pub(crate) enum OutgoingDirection {
ToServer, ToServer,
ToClient, ToClient,
} }
#[derive(Eq, PartialEq, Debug)] #[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub(crate) enum Direction { pub(crate) enum Direction {
Incoming(IncomingDirection), Incoming(IncomingDirection),
Outgoing(OutgoingDirection), Outgoing(OutgoingDirection),
} }
#[allow(dead_code)] #[derive(Clone, Eq, PartialEq, Debug)]
pub(crate) enum ConnectionEvent<'a> {
NewConnection(&'a ConnectionInfo),
ConnectionClosed(&'a ConnectionInfo),
}
#[derive(Debug)]
pub(crate) struct DataEvent<'a, T> { pub(crate) struct DataEvent<'a, T> {
pub(crate) direction: T, pub(crate) direction: T,
pub(crate) buffer: &'a [u8], pub(crate) buffer: &'a [u8],
@ -190,10 +183,10 @@ struct ConnectionState {
close_state: u8, close_state: u8,
wait_read: bool, wait_read: bool,
wait_write: bool, wait_write: bool,
origin_dst: SocketAddr,
udp_acco_expiry: Option<::std::time::Instant>, udp_acco_expiry: Option<::std::time::Instant>,
udp_socket: Option<UdpSocket>, udp_socket: Option<UdpSocket>,
udp_token: Option<Token>, udp_token: Option<Token>,
origin_dst: SocketAddr,
udp_data_cache: LinkedList<Vec<u8>>, udp_data_cache: LinkedList<Vec<u8>>,
dns_over_tcp_expiry: Option<::std::time::Instant>, dns_over_tcp_expiry: Option<::std::time::Instant>,
} }
@ -217,7 +210,7 @@ pub(crate) trait ConnectionManager {
} }
const TUN_TOKEN: Token = Token(0); const TUN_TOKEN: Token = Token(0);
const EXIT_TOKEN: Token = Token(2); const EXIT_TOKEN: Token = Token(1);
pub struct TunToProxy<'a> { pub struct TunToProxy<'a> {
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
@ -228,7 +221,7 @@ pub struct TunToProxy<'a> {
iface: Interface, iface: Interface,
connection_map: HashMap<ConnectionInfo, ConnectionState>, connection_map: HashMap<ConnectionInfo, ConnectionState>,
connection_manager: Option<Rc<dyn ConnectionManager>>, connection_manager: Option<Rc<dyn ConnectionManager>>,
next_token: usize, next_token_seed: usize,
sockets: SocketSet<'a>, sockets: SocketSet<'a>,
device: VirtualTunDevice, device: VirtualTunDevice,
options: Options, options: Options,
@ -297,7 +290,7 @@ impl<'a> TunToProxy<'a> {
poll, poll,
iface, iface,
connection_map: HashMap::default(), connection_map: HashMap::default(),
next_token: usize::from(EXIT_TOKEN) + 1, next_token_seed: usize::from(EXIT_TOKEN),
connection_manager: None, connection_manager: None,
sockets: SocketSet::new([]), sockets: SocketSet::new([]),
device, device,
@ -312,9 +305,8 @@ impl<'a> TunToProxy<'a> {
} }
fn new_token(&mut self) -> Token { fn new_token(&mut self) -> Token {
let token = Token(self.next_token); self.next_token_seed += 1;
self.next_token += 1; Token(self.next_token_seed)
token
} }
pub(crate) fn set_connection_manager(&mut self, manager: Option<Rc<dyn ConnectionManager>>) { pub(crate) fn set_connection_manager(&mut self, manager: Option<Rc<dyn ConnectionManager>>) {