mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-25 08:16:03 +00:00
connection_managers renamed to connection_manager
This commit is contained in:
parent
d00a18c865
commit
46ca342aba
4 changed files with 20 additions and 56 deletions
|
@ -391,10 +391,6 @@ pub(crate) struct HttpManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConnectionManager for HttpManager {
|
impl ConnectionManager for HttpManager {
|
||||||
fn handles_connection(&self, info: &ConnectionInfo) -> bool {
|
|
||||||
info.protocol == IpProtocol::Tcp
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_tcp_proxy(&self, info: &ConnectionInfo, _: bool) -> Result<Box<dyn TcpProxy>, Error> {
|
fn new_tcp_proxy(&self, info: &ConnectionInfo, _: bool) -> Result<Box<dyn TcpProxy>, Error> {
|
||||||
if info.protocol != IpProtocol::Tcp {
|
if info.protocol != IpProtocol::Tcp {
|
||||||
return Err("Invalid protocol".into());
|
return Err("Invalid protocol".into());
|
||||||
|
|
|
@ -130,7 +130,7 @@ pub fn tun_to_proxy<'a>(
|
||||||
ProxyType::Socks5 => Rc::new(SocksProxyManager::new(server, Version::V5, credentials)) as Rc<dyn ConnectionManager>,
|
ProxyType::Socks5 => Rc::new(SocksProxyManager::new(server, Version::V5, credentials)) as Rc<dyn ConnectionManager>,
|
||||||
ProxyType::Http => Rc::new(HttpManager::new(server, credentials)) as Rc<dyn ConnectionManager>,
|
ProxyType::Http => Rc::new(HttpManager::new(server, credentials)) as Rc<dyn ConnectionManager>,
|
||||||
};
|
};
|
||||||
ttp.add_connection_manager(mgr);
|
ttp.set_connection_manager(Some(mgr));
|
||||||
Ok(ttp)
|
Ok(ttp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ use crate::{
|
||||||
OutgoingDirection, TcpProxy,
|
OutgoingDirection, TcpProxy,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use smoltcp::wire::IpProtocol;
|
|
||||||
use socks5_impl::protocol::{self, handshake, password_method, Address, AuthMethod, StreamOperation, UserKey, Version};
|
use socks5_impl::protocol::{self, handshake, password_method, Address, AuthMethod, StreamOperation, UserKey, Version};
|
||||||
use std::{collections::VecDeque, convert::TryFrom, net::SocketAddr};
|
use std::{collections::VecDeque, convert::TryFrom, net::SocketAddr};
|
||||||
|
|
||||||
|
@ -336,14 +335,7 @@ pub(crate) struct SocksProxyManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConnectionManager for SocksProxyManager {
|
impl ConnectionManager for SocksProxyManager {
|
||||||
fn handles_connection(&self, info: &ConnectionInfo) -> bool {
|
|
||||||
info.protocol == IpProtocol::Tcp
|
|
||||||
}
|
|
||||||
|
|
||||||
fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>> {
|
fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>> {
|
||||||
if info.protocol != IpProtocol::Tcp {
|
|
||||||
return Err("Invalid protocol".into());
|
|
||||||
}
|
|
||||||
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 };
|
||||||
Ok(Box::new(SocksProxyImpl::new(
|
Ok(Box::new(SocksProxyImpl::new(
|
||||||
|
|
|
@ -192,7 +192,6 @@ pub(crate) trait UdpProxy {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait ConnectionManager {
|
pub(crate) trait ConnectionManager {
|
||||||
fn handles_connection(&self, info: &ConnectionInfo) -> bool;
|
|
||||||
fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>>;
|
fn new_tcp_proxy(&self, info: &ConnectionInfo, udp_associate: bool) -> Result<Box<dyn TcpProxy>>;
|
||||||
fn close_connection(&self, info: &ConnectionInfo);
|
fn close_connection(&self, info: &ConnectionInfo);
|
||||||
fn get_server_addr(&self) -> SocketAddr;
|
fn get_server_addr(&self) -> SocketAddr;
|
||||||
|
@ -207,7 +206,7 @@ pub struct TunToProxy<'a> {
|
||||||
poll: Poll,
|
poll: Poll,
|
||||||
iface: Interface,
|
iface: Interface,
|
||||||
connection_map: HashMap<ConnectionInfo, TcpConnectState>,
|
connection_map: HashMap<ConnectionInfo, TcpConnectState>,
|
||||||
connection_managers: Vec<Rc<dyn ConnectionManager>>,
|
connection_manager: Option<Rc<dyn ConnectionManager>>,
|
||||||
next_token: usize,
|
next_token: usize,
|
||||||
sockets: SocketSet<'a>,
|
sockets: SocketSet<'a>,
|
||||||
device: VirtualTunDevice,
|
device: VirtualTunDevice,
|
||||||
|
@ -255,7 +254,7 @@ impl<'a> TunToProxy<'a> {
|
||||||
iface,
|
iface,
|
||||||
connection_map: HashMap::default(),
|
connection_map: HashMap::default(),
|
||||||
next_token: usize::from(EXIT_TOKEN) + 1,
|
next_token: usize::from(EXIT_TOKEN) + 1,
|
||||||
connection_managers: Vec::default(),
|
connection_manager: None,
|
||||||
sockets: SocketSet::new([]),
|
sockets: SocketSet::new([]),
|
||||||
device,
|
device,
|
||||||
options,
|
options,
|
||||||
|
@ -272,8 +271,8 @@ impl<'a> TunToProxy<'a> {
|
||||||
token
|
token
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_connection_manager(&mut self, manager: Rc<dyn ConnectionManager>) {
|
pub(crate) fn set_connection_manager(&mut self, manager: Option<Rc<dyn ConnectionManager>>) {
|
||||||
self.connection_managers.push(manager);
|
self.connection_manager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read data from virtual device (remote server) and inject it into tun interface.
|
/// Read data from virtual device (remote server) and inject it into tun interface.
|
||||||
|
@ -320,13 +319,8 @@ impl<'a> TunToProxy<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_connection_manager(&self, info: &ConnectionInfo) -> Option<Rc<dyn ConnectionManager>> {
|
fn get_connection_manager(&self) -> Option<Rc<dyn ConnectionManager>> {
|
||||||
for manager in self.connection_managers.iter() {
|
self.connection_manager.clone()
|
||||||
if manager.handles_connection(info) {
|
|
||||||
return Some(manager.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scan connection state machine and check if any connection should be closed.
|
/// Scan connection state machine and check if any connection should be closed.
|
||||||
|
@ -451,29 +445,17 @@ impl<'a> TunToProxy<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if connection_info.protocol == IpProtocol::Tcp {
|
|
||||||
let server_addr = self
|
|
||||||
.get_connection_manager(&connection_info)
|
|
||||||
.ok_or("get_connection_manager")?
|
|
||||||
.get_server_addr();
|
|
||||||
if first_packet {
|
|
||||||
let mut done = false;
|
|
||||||
for manager in self.connection_managers.iter_mut() {
|
|
||||||
let tcp_proxy_handler = manager.new_tcp_proxy(&connection_info, false);
|
|
||||||
if tcp_proxy_handler.is_err() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let tcp_proxy_handler = tcp_proxy_handler?;
|
|
||||||
let state = self.create_new_tcp_connection_state(server_addr, dst, tcp_proxy_handler)?;
|
|
||||||
self.connection_map.insert(connection_info.clone(), state);
|
|
||||||
|
|
||||||
log::info!("Connect done {} ({})", connection_info, dst);
|
let manager = self.get_connection_manager().ok_or("get connection manager")?;
|
||||||
done = true;
|
let server_addr = manager.get_server_addr();
|
||||||
break;
|
|
||||||
}
|
if connection_info.protocol == IpProtocol::Tcp {
|
||||||
if !done {
|
if first_packet {
|
||||||
log::debug!("No connection manager for {} ({})", connection_info, dst);
|
let tcp_proxy_handler = manager.new_tcp_proxy(&connection_info, false)?;
|
||||||
}
|
let state = self.create_new_tcp_connection_state(server_addr, dst, tcp_proxy_handler)?;
|
||||||
|
self.connection_map.insert(connection_info.clone(), state);
|
||||||
|
|
||||||
|
log::info!("Connect done {} ({})", connection_info, dst);
|
||||||
} else if !self.connection_map.contains_key(&connection_info) {
|
} else if !self.connection_map.contains_key(&connection_info) {
|
||||||
log::debug!("Not found {} ({})", connection_info, dst);
|
log::debug!("Not found {} ({})", connection_info, dst);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -514,12 +496,6 @@ impl<'a> TunToProxy<'a> {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Another UDP packet
|
// Another UDP packet
|
||||||
let manager = self.get_connection_manager(&connection_info);
|
|
||||||
if manager.is_none() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
let manager = manager.ok_or("")?;
|
|
||||||
let server_addr = manager.get_server_addr();
|
|
||||||
let tcp_proxy_handler = manager.new_tcp_proxy(&connection_info, true)?;
|
let tcp_proxy_handler = manager.new_tcp_proxy(&connection_info, true)?;
|
||||||
let state = self.create_new_tcp_connection_state(server_addr, dst, tcp_proxy_handler)?;
|
let state = self.create_new_tcp_connection_state(server_addr, dst, tcp_proxy_handler)?;
|
||||||
self.connection_map.insert(connection_info.clone(), state);
|
self.connection_map.insert(connection_info.clone(), state);
|
||||||
|
@ -664,19 +640,19 @@ impl<'a> TunToProxy<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mio_socket_event(&mut self, event: &Event) -> Result<(), Error> {
|
fn mio_socket_event(&mut self, event: &Event) -> Result<(), Error> {
|
||||||
let e = "connection not found";
|
|
||||||
let conn_info = match self.find_info_by_token(event.token()) {
|
let conn_info = match self.find_info_by_token(event.token()) {
|
||||||
Some(conn_info) => conn_info.clone(),
|
Some(conn_info) => conn_info.clone(),
|
||||||
None => {
|
None => {
|
||||||
// We may have closed the connection in an earlier iteration over the poll events,
|
// We may have closed the connection in an earlier iteration over the poll events,
|
||||||
// e.g. because an event through the tunnel interface indicated that the connection
|
// e.g. because an event through the tunnel interface indicated that the connection
|
||||||
// should be closed.
|
// should be closed.
|
||||||
log::trace!("{e}");
|
log::trace!("Connection info not found");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let server = self.get_connection_manager(&conn_info).ok_or(e)?.get_server_addr();
|
let e = "connection manager not found";
|
||||||
|
let server = self.get_connection_manager().ok_or(e)?.get_server_addr();
|
||||||
|
|
||||||
let mut block = || -> Result<(), Error> {
|
let mut block = || -> Result<(), Error> {
|
||||||
if event.is_readable() || event.is_read_closed() {
|
if event.is_readable() || event.is_read_closed() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue