tun2proxy/src/tun2proxy.rs

772 lines
29 KiB
Rust
Raw Normal View History

2023-08-05 15:52:32 +08:00
use crate::{error::Error, error::Result, virtdevice::VirtualTunDevice, NetworkInterface, Options};
2023-07-23 02:03:15 +08:00
use mio::{event::Event, net::TcpStream, unix::SourceFd, Events, Interest, Poll, Token};
use smoltcp::{
iface::{Config, Interface, SocketHandle, SocketSet},
phy::{Device, Medium, RxToken, TunTapInterface, TxToken},
socket::{tcp, tcp::State, udp, udp::UdpMetadata},
time::Instant,
2023-08-05 15:52:32 +08:00
wire::{IpCidr, IpProtocol, Ipv4Packet, Ipv6Packet, TcpPacket, UdpPacket, UDP_HEADER_LEN},
2023-07-23 02:03:15 +08:00
};
use socks5_impl::protocol::{Address, UserKey};
use std::{
collections::{HashMap, HashSet},
convert::{From, TryFrom},
io::{Read, Write},
2023-08-09 16:50:59 +08:00
net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr},
2023-07-23 02:03:15 +08:00
os::unix::io::AsRawFd,
rc::Rc,
str::FromStr,
};
2023-03-22 19:11:28 +01:00
#[derive(Hash, Clone, Eq, PartialEq, Debug)]
2023-08-05 15:52:32 +08:00
pub(crate) struct ConnectionInfo {
2023-03-23 20:00:59 +08:00
pub(crate) src: SocketAddr,
2023-07-23 02:03:15 +08:00
pub(crate) dst: Address,
2023-08-05 15:52:32 +08:00
pub(crate) protocol: IpProtocol,
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
impl Default for ConnectionInfo {
fn default() -> Self {
Self {
src: SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
dst: Address::unspecified(),
protocol: IpProtocol::Tcp,
}
}
}
impl ConnectionInfo {
#[allow(dead_code)]
pub fn new(src: SocketAddr, dst: Address, protocol: IpProtocol) -> Self {
Self { src, dst, protocol }
}
fn to_named(&self, name: String) -> Self {
let mut result = self.clone();
2023-07-23 02:03:15 +08:00
result.dst = Address::from((name, result.dst.port()));
2023-08-05 15:52:32 +08:00
// let p = self.protocol;
// log::trace!("{p} replace dst \"{}\" -> \"{}\"", self.dst, result.dst);
result
}
}
2023-08-05 15:52:32 +08:00
impl std::fmt::Display for ConnectionInfo {
2023-03-25 16:44:33 +08:00
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2023-08-05 15:52:32 +08:00
write!(f, "{} {} -> {}", self.protocol, self.src, self.dst)
2021-09-02 21:02:17 +02:00
}
}
2021-09-02 11:30:23 +02:00
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum IncomingDirection {
FromServer,
2022-08-01 14:36:58 +00:00
FromClient,
2021-09-02 11:30:23 +02:00
}
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum OutgoingDirection {
ToServer,
2022-08-01 14:36:58 +00:00
ToClient,
2021-09-02 11:30:23 +02:00
}
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum Direction {
Incoming(IncomingDirection),
Outgoing(OutgoingDirection),
}
2021-09-02 11:30:23 +02:00
#[allow(dead_code)]
pub(crate) enum ConnectionEvent<'a> {
2023-08-05 15:52:32 +08:00
NewConnection(&'a ConnectionInfo),
ConnectionClosed(&'a ConnectionInfo),
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
#[derive(Debug)]
2021-09-02 11:30:23 +02:00
pub(crate) struct DataEvent<'a, T> {
pub(crate) direction: T,
2022-08-01 14:36:58 +00:00
pub(crate) buffer: &'a [u8],
2021-09-02 11:30:23 +02:00
}
pub(crate) type IncomingDataEvent<'a> = DataEvent<'a, IncomingDirection>;
pub(crate) type OutgoingDataEvent<'a> = DataEvent<'a, OutgoingDirection>;
2022-08-01 14:36:58 +00:00
fn get_transport_info(
2023-08-05 15:52:32 +08:00
protocol: IpProtocol,
2022-08-01 14:36:58 +00:00
transport_offset: usize,
packet: &[u8],
2023-08-05 15:52:32 +08:00
) -> Result<((u16, u16), bool, usize, usize)> {
match protocol {
IpProtocol::Udp => UdpPacket::new_checked(packet)
.map(|result| {
(
(result.src_port(), result.dst_port()),
false,
transport_offset + UDP_HEADER_LEN,
packet.len() - UDP_HEADER_LEN,
)
})
.map_err(|e| e.into()),
IpProtocol::Tcp => TcpPacket::new_checked(packet)
.map(|result| {
2023-08-08 09:16:57 +08:00
let header_len = result.header_len() as usize;
2023-08-05 15:52:32 +08:00
(
(result.src_port(), result.dst_port()),
result.syn() && !result.ack(),
2023-08-08 09:16:57 +08:00
transport_offset + header_len,
packet.len() - header_len,
2023-08-05 15:52:32 +08:00
)
})
.map_err(|e| e.into()),
2023-08-08 09:16:57 +08:00
_ => Err(format!("Unsupported protocol {protocol} in IP packet").into()),
2021-09-02 11:30:23 +02:00
}
}
2023-08-05 15:52:32 +08:00
fn connection_tuple(frame: &[u8]) -> Result<(ConnectionInfo, bool, usize, usize)> {
2022-08-01 14:36:58 +00:00
if let Ok(packet) = Ipv4Packet::new_checked(frame) {
2023-08-05 15:52:32 +08:00
let protocol = packet.next_header();
2022-08-01 14:36:58 +00:00
2023-07-23 02:03:15 +08:00
let mut a = [0_u8; 4];
2022-08-01 14:36:58 +00:00
a.copy_from_slice(packet.src_addr().as_bytes());
let src_addr = IpAddr::from(a);
a.copy_from_slice(packet.dst_addr().as_bytes());
let dst_addr = IpAddr::from(a);
2023-08-05 15:52:32 +08:00
let header_len = packet.header_len().into();
let (ports, first_packet, payload_offset, payload_size) =
get_transport_info(protocol, header_len, &frame[header_len..])?;
let info = ConnectionInfo {
src: SocketAddr::new(src_addr, ports.0),
dst: SocketAddr::new(dst_addr, ports.1).into(),
protocol,
2023-03-23 13:24:33 +01:00
};
2023-08-05 15:52:32 +08:00
return Ok((info, first_packet, payload_offset, payload_size));
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
if let Ok(packet) = Ipv6Packet::new_checked(frame) {
// TODO: Support extension headers.
let protocol = packet.next_header();
2021-09-02 11:30:23 +02:00
2023-08-05 15:52:32 +08:00
let mut a = [0_u8; 16];
a.copy_from_slice(packet.src_addr().as_bytes());
let src_addr = IpAddr::from(a);
a.copy_from_slice(packet.dst_addr().as_bytes());
let dst_addr = IpAddr::from(a);
let header_len = packet.header_len();
let (ports, first_packet, payload_offset, payload_size) =
get_transport_info(protocol, header_len, &frame[header_len..])?;
let info = ConnectionInfo {
src: SocketAddr::new(src_addr, ports.0),
dst: SocketAddr::new(dst_addr, ports.1).into(),
protocol,
};
return Ok((info, first_packet, payload_offset, payload_size));
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
Err("Neither IPv6 nor IPv4 packet".into())
2021-09-02 11:30:23 +02:00
}
const SERVER_WRITE_CLOSED: u8 = 1;
const CLIENT_WRITE_CLOSED: u8 = 2;
2023-08-05 15:52:32 +08:00
struct TcpConnectState {
smoltcp_handle: Option<SocketHandle>,
2021-09-02 11:30:23 +02:00
mio_stream: TcpStream,
2021-09-02 22:36:47 +02:00
token: Token,
2023-08-05 15:52:32 +08:00
tcp_proxy_handler: Box<dyn TcpProxy>,
close_state: u8,
wait_read: bool,
wait_write: bool,
2021-09-02 11:30:23 +02:00
}
pub(crate) trait TcpProxy {
fn push_data(&mut self, event: IncomingDataEvent<'_>) -> Result<(), Error>;
2021-09-02 11:30:23 +02:00
fn consume_data(&mut self, dir: OutgoingDirection, size: usize);
fn peek_data(&mut self, dir: OutgoingDirection) -> OutgoingDataEvent;
2021-09-02 21:02:17 +02:00
fn connection_established(&self) -> bool;
fn have_data(&mut self, dir: Direction) -> bool;
2023-06-22 13:09:36 -04:00
fn reset_connection(&self) -> bool;
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
pub(crate) trait UdpProxy {
fn send_frame(&mut self, destination: &Address, frame: &[u8]) -> Result<(), Error>;
fn receive_frame(&mut self, source: &SocketAddr, frame: &[u8]) -> Result<(), Error>;
}
2021-09-02 11:30:23 +02:00
pub(crate) trait ConnectionManager {
2023-08-05 15:52:32 +08:00
fn handles_connection(&self, info: &ConnectionInfo) -> bool;
fn new_tcp_proxy(&self, info: &ConnectionInfo) -> Result<Box<dyn TcpProxy>, Error>;
fn close_connection(&self, info: &ConnectionInfo);
fn get_server_addr(&self) -> SocketAddr;
2023-07-23 02:03:15 +08:00
fn get_credentials(&self) -> &Option<UserKey>;
2021-09-02 11:30:23 +02:00
}
2023-04-27 22:41:54 +02:00
const TUN_TOKEN: Token = Token(0);
2023-04-15 11:46:54 +08:00
const EXIT_TOKEN: Token = Token(2);
2023-04-14 17:27:37 +08:00
2023-04-27 22:41:54 +02:00
pub struct TunToProxy<'a> {
2021-09-02 11:30:23 +02:00
tun: TunTapInterface,
poll: Poll,
2023-03-20 15:34:41 +01:00
iface: Interface,
2023-08-05 15:52:32 +08:00
connection_map: HashMap<ConnectionInfo, TcpConnectState>,
2023-03-23 16:31:33 +08:00
connection_managers: Vec<Rc<dyn ConnectionManager>>,
2021-09-02 11:30:23 +02:00
next_token: usize,
2023-08-05 15:52:32 +08:00
token_to_info: HashMap<Token, ConnectionInfo>,
2023-03-20 15:34:41 +01:00
sockets: SocketSet<'a>,
device: VirtualTunDevice,
options: Options,
write_sockets: HashSet<Token>,
2023-04-27 22:41:54 +02:00
_exit_receiver: mio::unix::pipe::Receiver,
exit_sender: mio::unix::pipe::Sender,
2021-09-02 11:30:23 +02:00
}
impl<'a> TunToProxy<'a> {
2023-04-27 22:41:54 +02:00
pub fn new(interface: &NetworkInterface, options: Options) -> Result<Self, Error> {
2023-04-10 23:24:53 +02:00
let tun = match interface {
NetworkInterface::Named(name) => TunTapInterface::new(name.as_str(), Medium::Ip)?,
2023-08-08 23:45:16 +08:00
NetworkInterface::Fd(fd) => TunTapInterface::from_fd(*fd, Medium::Ip, options.mtu.unwrap_or(1500))?,
2023-04-10 23:24:53 +02:00
};
2023-03-24 09:27:31 +08:00
let poll = Poll::new()?;
2023-08-08 23:45:16 +08:00
poll.registry()
.register(&mut SourceFd(&tun.as_raw_fd()), TUN_TOKEN, Interest::READABLE)?;
2021-09-02 11:30:23 +02:00
2023-04-27 22:41:54 +02:00
let (exit_sender, mut exit_receiver) = mio::unix::pipe::new()?;
2023-04-14 17:27:37 +08:00
poll.registry()
2023-04-27 22:41:54 +02:00
.register(&mut exit_receiver, EXIT_TOKEN, Interest::READABLE)?;
2023-04-14 17:27:37 +08:00
2023-08-08 23:45:16 +08:00
#[rustfmt::skip]
2023-04-10 09:58:17 +08:00
let config = match tun.capabilities().medium {
2023-08-08 23:45:16 +08:00
Medium::Ethernet => Config::new(smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]).into()),
2023-04-10 09:58:17 +08:00
Medium::Ip => Config::new(smoltcp::wire::HardwareAddress::Ip),
Medium::Ieee802154 => todo!(),
};
2023-08-05 15:52:32 +08:00
let mut device = VirtualTunDevice::new(tun.capabilities());
let gateway4: Ipv4Addr = Ipv4Addr::from_str("0.0.0.1")?;
let gateway6: Ipv6Addr = Ipv6Addr::from_str("::1")?;
2023-08-05 15:52:32 +08:00
let mut iface = Interface::new(config, &mut device, Instant::now());
2023-03-20 15:34:41 +01:00
iface.update_ip_addrs(|ip_addrs| {
2023-03-24 09:27:31 +08:00
ip_addrs.push(IpCidr::new(gateway4.into(), 0)).unwrap();
ip_addrs.push(IpCidr::new(gateway6.into(), 0)).unwrap()
2023-03-20 15:34:41 +01:00
});
2023-03-24 09:27:31 +08:00
iface.routes_mut().add_default_ipv4_route(gateway4.into())?;
iface.routes_mut().add_default_ipv6_route(gateway6.into())?;
2023-03-20 15:34:41 +01:00
iface.set_any_ip(true);
2021-09-02 11:30:23 +02:00
2023-03-24 09:27:31 +08:00
let tun = Self {
2021-09-02 11:30:23 +02:00
tun,
poll,
iface,
2023-08-05 15:52:32 +08:00
connection_map: HashMap::default(),
2023-04-15 11:46:54 +08:00
next_token: usize::from(EXIT_TOKEN) + 1,
2023-08-05 15:52:32 +08:00
token_to_info: HashMap::default(),
2023-03-24 14:31:22 +08:00
connection_managers: Vec::default(),
2023-03-20 15:34:41 +01:00
sockets: SocketSet::new([]),
2023-08-05 15:52:32 +08:00
device,
options,
2023-03-24 14:31:22 +08:00
write_sockets: HashSet::default(),
2023-04-27 22:41:54 +02:00
_exit_receiver: exit_receiver,
exit_sender,
2023-03-24 09:27:31 +08:00
};
Ok(tun)
2021-09-02 11:30:23 +02:00
}
2023-04-15 11:46:54 +08:00
fn new_token(&mut self) -> Token {
let token = Token(self.next_token);
self.next_token += 1;
token
}
2023-03-23 16:31:33 +08:00
pub(crate) fn add_connection_manager(&mut self, manager: Rc<dyn ConnectionManager>) {
2021-09-02 11:30:23 +02:00
self.connection_managers.push(manager);
}
2023-08-09 16:50:59 +08:00
/// Read data from virtual device (remote server) and inject it into tun interface.
2023-03-24 14:31:22 +08:00
fn expect_smoltcp_send(&mut self) -> Result<(), Error> {
2023-08-08 23:45:16 +08:00
self.iface.poll(Instant::now(), &mut self.device, &mut self.sockets);
2021-09-02 11:30:23 +02:00
2023-03-20 15:34:41 +01:00
while let Some(vec) = self.device.exfiltrate_packet() {
2021-09-02 11:30:23 +02:00
let slice = vec.as_slice();
// TODO: Actual write. Replace.
2022-08-01 14:36:58 +00:00
self.tun
2023-03-20 15:34:41 +01:00
.transmit(Instant::now())
2023-03-24 14:31:22 +08:00
.ok_or("tx token not available")?
2023-03-20 15:34:41 +01:00
.consume(slice.len(), |buf| {
2022-08-01 14:36:58 +00:00
buf[..].clone_from_slice(slice);
2023-03-20 15:34:41 +01:00
});
2021-09-02 11:30:23 +02:00
}
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
fn remove_connection(&mut self, info: &ConnectionInfo) -> Result<(), Error> {
if let Some(mut conn) = self.connection_map.remove(info) {
2023-08-09 16:50:59 +08:00
_ = conn.mio_stream.shutdown(Shutdown::Both);
2023-08-05 15:52:32 +08:00
if let Some(handle) = conn.smoltcp_handle {
let socket = self.sockets.get_mut::<tcp::Socket>(handle);
socket.close();
self.sockets.remove(handle);
}
self.expect_smoltcp_send()?;
let token = &conn.token;
2023-08-05 15:52:32 +08:00
self.token_to_info.remove(token);
_ = self.poll.registry().deregister(&mut conn.mio_stream);
2023-08-08 23:45:16 +08:00
log::info!("Close {}", info);
}
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
fn get_connection_manager(&self, info: &ConnectionInfo) -> Option<Rc<dyn ConnectionManager>> {
2021-09-02 11:30:23 +02:00
for manager in self.connection_managers.iter() {
2023-08-05 15:52:32 +08:00
if manager.handles_connection(info) {
2023-03-22 01:02:27 +01:00
return Some(manager.clone());
2021-09-02 11:30:23 +02:00
}
}
None
}
2023-08-05 15:52:32 +08:00
fn check_change_close_state(&mut self, info: &ConnectionInfo) -> Result<(), Error> {
2023-08-09 16:50:59 +08:00
let state = match self.connection_map.get_mut(info) {
None => return Ok(()),
Some(state) => state,
};
let mut closed_ends = 0;
if (state.close_state & SERVER_WRITE_CLOSED) == SERVER_WRITE_CLOSED
&& !state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.have_data(Direction::Incoming(IncomingDirection::FromServer))
&& !state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.have_data(Direction::Outgoing(OutgoingDirection::ToClient))
{
2023-08-05 15:52:32 +08:00
if let Some(socket_handle) = state.smoltcp_handle {
let socket = self.sockets.get_mut::<tcp::Socket>(socket_handle);
socket.close();
}
closed_ends += 1;
}
2021-09-02 11:30:23 +02:00
if (state.close_state & CLIENT_WRITE_CLOSED) == CLIENT_WRITE_CLOSED
&& !state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.have_data(Direction::Incoming(IncomingDirection::FromClient))
&& !state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.have_data(Direction::Outgoing(OutgoingDirection::ToServer))
{
_ = state.mio_stream.shutdown(Shutdown::Write);
closed_ends += 1;
}
2021-09-02 11:30:23 +02:00
if closed_ends == 2 {
2023-08-05 15:52:32 +08:00
self.remove_connection(info)?;
}
Ok(())
}
2023-08-05 15:52:32 +08:00
fn tunsocket_read_and_forward(&mut self, info: &ConnectionInfo) -> Result<(), Error> {
// Scope for mutable borrow of self.
{
2023-08-05 15:52:32 +08:00
let state = match self.connection_map.get_mut(info) {
Some(state) => state,
None => return Ok(()),
};
let socket = match state.smoltcp_handle {
Some(handle) => self.sockets.get_mut::<tcp::Socket>(handle),
None => return Ok(()),
};
let mut error = Ok(());
while socket.can_recv() && error.is_ok() {
socket.recv(|data| {
let event = IncomingDataEvent {
direction: IncomingDirection::FromClient,
buffer: data,
};
2023-08-05 15:52:32 +08:00
error = state.tcp_proxy_handler.push_data(event);
(data.len(), ())
})?;
}
if !socket.may_recv()
&& socket.state() != State::Listen
&& socket.state() != State::SynSent
&& socket.state() != State::SynReceived
{
// We cannot yet close the write end of the mio stream here because we may still
// need to send data.
state.close_state |= CLIENT_WRITE_CLOSED;
2021-09-02 11:30:23 +02:00
}
// Expect ACKs etc. from smoltcp sockets.
self.expect_smoltcp_send()?;
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
self.check_change_close_state(info)?;
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
fn update_mio_socket_interest(poll: &mut Poll, state: &mut TcpConnectState) -> Result<()> {
// Maybe we did not listen for any events before. Therefore, just swallow the error.
2023-08-05 15:52:32 +08:00
_ = poll.registry().deregister(&mut state.mio_stream);
// If we do not wait for read or write events, we do not need to register them.
if !state.wait_read && !state.wait_write {
return Ok(());
}
// This ugliness is due to the way Interest is implemented (as a NonZeroU8 wrapper).
2023-08-09 00:38:32 +08:00
let interest = match (state.wait_read, state.wait_write) {
(true, false) => Interest::READABLE,
(false, true) => Interest::WRITABLE,
2023-08-09 14:31:33 +08:00
_ => Interest::READABLE | Interest::WRITABLE,
2023-08-09 00:38:32 +08:00
};
2023-08-08 23:45:16 +08:00
poll.registry().register(&mut state.mio_stream, state.token, interest)?;
Ok(())
}
// A raw packet was received on the tunnel interface.
2023-03-24 14:31:22 +08:00
fn receive_tun(&mut self, frame: &mut [u8]) -> Result<(), Error> {
2023-08-05 15:52:32 +08:00
let mut handler = || -> Result<(), Error> {
let (info, first_packet, payload_offset, payload_size) = connection_tuple(frame)?;
let dst = SocketAddr::try_from(&info.dst)?;
let connection_info = match &mut self.options.virtual_dns {
None => info.clone(),
Some(virtual_dns) => {
let dst_ip = dst.ip();
virtual_dns.touch_ip(&dst_ip);
match virtual_dns.resolve_ip(&dst_ip) {
None => info.clone(),
Some(name) => info.to_named(name.clone()),
}
}
};
2023-08-05 15:52:32 +08:00
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 {
2023-08-07 12:29:36 +08:00
let mut done = false;
for manager in self.connection_managers.iter_mut() {
let tcp_proxy_handler = manager.new_tcp_proxy(&connection_info);
if tcp_proxy_handler.is_err() {
continue;
}
let tcp_proxy_handler = tcp_proxy_handler?;
2023-08-10 11:18:15 +08:00
self.create_new_tcp_proxy_connection(
server_addr,
dst,
2023-08-05 15:52:32 +08:00
tcp_proxy_handler,
2023-08-10 11:18:15 +08:00
connection_info.clone(),
)?;
2023-08-05 15:52:32 +08:00
2023-08-07 12:29:36 +08:00
log::info!("Connect done {} ({})", connection_info, dst);
done = true;
break;
}
if !done {
log::debug!("No connection manager for {} ({})", connection_info, dst);
}
2023-08-05 15:52:32 +08:00
} else if !self.connection_map.contains_key(&connection_info) {
2023-08-07 12:29:36 +08:00
log::debug!("Not found {} ({})", connection_info, dst);
2023-08-05 15:52:32 +08:00
return Ok(());
2023-08-07 12:29:36 +08:00
} else {
log::trace!("Subsequent packet {} ({})", connection_info, dst);
2023-08-05 15:52:32 +08:00
}
2021-09-02 11:30:23 +02:00
2023-08-09 16:50:59 +08:00
// Inject the packet to advance the remote proxy server smoltcp socket state
2023-08-05 15:52:32 +08:00
self.device.inject_packet(frame);
// Having advanced the socket state, we expect the socket to ACK
// Exfiltrate the response packets generated by the socket and inject them
// into the tunnel interface.
self.expect_smoltcp_send()?;
// Read from the smoltcp socket and push the data to the connection handler.
self.tunsocket_read_and_forward(&connection_info)?;
// The connection handler builds up the connection or encapsulates the data.
// Therefore, we now expect it to write data to the server.
self.write_to_server(&connection_info)?;
} else if connection_info.protocol == IpProtocol::Udp {
2023-08-07 12:29:36 +08:00
log::trace!("{} ({})", connection_info, dst);
2023-08-05 15:52:32 +08:00
let port = connection_info.dst.port();
2023-08-09 16:50:59 +08:00
let payload = &frame[payload_offset..payload_offset + payload_size];
2023-08-05 15:52:32 +08:00
if let (Some(virtual_dns), true) = (&mut self.options.virtual_dns, port == 53) {
2023-08-06 13:48:56 +08:00
let response = virtual_dns.receive_query(payload)?;
{
2023-08-08 23:45:16 +08:00
let rx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
let tx_buffer = udp::PacketBuffer::new(vec![udp::PacketMetadata::EMPTY], vec![0; 4096]);
2023-08-05 15:52:32 +08:00
let mut socket = udp::Socket::new(rx_buffer, tx_buffer);
socket.bind(dst)?;
2023-08-08 09:20:43 +08:00
let meta = UdpMetadata::from(connection_info.src);
socket.send_slice(response.as_slice(), meta)?;
2023-08-05 15:52:32 +08:00
let handle = self.sockets.add(socket);
self.expect_smoltcp_send()?;
self.sockets.remove(handle);
2021-09-02 11:30:23 +02:00
}
}
2023-08-05 15:52:32 +08:00
// Otherwise, UDP is not yet supported.
2023-08-07 12:29:36 +08:00
} else {
log::warn!("Unsupported protocol: {} ({})", connection_info, dst);
2023-07-23 02:03:15 +08:00
}
2023-08-05 15:52:32 +08:00
Ok::<(), Error>(())
};
if let Err(error) = handler() {
log::error!("{}", error);
2021-09-02 11:30:23 +02:00
}
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-08-10 11:18:15 +08:00
fn create_new_tcp_proxy_connection(
&mut self,
server_addr: SocketAddr,
dst: SocketAddr,
tcp_proxy_handler: Box<dyn TcpProxy>,
connection_info: ConnectionInfo,
) -> Result<()> {
let mut socket = tcp::Socket::new(
tcp::SocketBuffer::new(vec![0; 1024 * 128]),
tcp::SocketBuffer::new(vec![0; 1024 * 128]),
);
socket.set_ack_delay(None);
socket.listen(dst)?;
let handle = self.sockets.add(socket);
let mut client = TcpStream::connect(server_addr)?;
let token = self.new_token();
let i = Interest::READABLE;
self.poll.registry().register(&mut client, token, i)?;
let state = TcpConnectState {
smoltcp_handle: Some(handle),
mio_stream: client,
token,
tcp_proxy_handler,
close_state: 0,
wait_read: true,
wait_write: false,
};
self.connection_map.insert(connection_info.clone(), state);
self.token_to_info.insert(token, connection_info.clone());
Ok(())
}
2023-08-05 15:52:32 +08:00
fn write_to_server(&mut self, info: &ConnectionInfo) -> Result<(), Error> {
if let Some(state) = self.connection_map.get_mut(info) {
2023-08-08 23:45:16 +08:00
let event = state.tcp_proxy_handler.peek_data(OutgoingDirection::ToServer);
let buffer_size = event.buffer.len();
if buffer_size == 0 {
state.wait_write = false;
2023-08-05 15:52:32 +08:00
Self::update_mio_socket_interest(&mut self.poll, state)?;
self.check_change_close_state(info)?;
return Ok(());
2021-09-02 11:30:23 +02:00
}
2021-09-02 22:36:47 +02:00
let result = state.mio_stream.write(event.buffer);
2021-09-02 11:30:23 +02:00
match result {
Ok(written) => {
2022-08-01 14:36:58 +00:00
state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.consume_data(OutgoingDirection::ToServer, written);
state.wait_write = written < buffer_size;
2023-08-05 15:52:32 +08:00
Self::update_mio_socket_interest(&mut self.poll, state)?;
2021-09-02 11:30:23 +02:00
}
2022-08-01 14:36:58 +00:00
Err(error) if error.kind() != std::io::ErrorKind::WouldBlock => {
return Err(error.into());
2021-09-02 11:30:23 +02:00
}
_ => {
// WOULDBLOCK case
state.wait_write = true;
2023-08-05 15:52:32 +08:00
Self::update_mio_socket_interest(&mut self.poll, state)?;
}
2021-09-02 11:30:23 +02:00
}
}
2023-08-05 15:52:32 +08:00
self.check_change_close_state(info)?;
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
fn write_to_client(&mut self, token: Token, info: &ConnectionInfo) -> Result<(), Error> {
while let Some(state) = self.connection_map.get_mut(info) {
let socket_handle = match state.smoltcp_handle {
Some(handle) => handle,
None => break,
};
2023-08-08 23:45:16 +08:00
let event = state.tcp_proxy_handler.peek_data(OutgoingDirection::ToClient);
let buflen = event.buffer.len();
let consumed;
{
let socket = self.sockets.get_mut::<tcp::Socket>(socket_handle);
if socket.may_send() {
2023-08-05 15:52:32 +08:00
if let Some(virtual_dns) = &mut self.options.virtual_dns {
// Unwrapping is fine because every smoltcp socket is bound to an.
2023-08-05 15:52:32 +08:00
virtual_dns.touch_ip(&IpAddr::from(socket.local_endpoint().unwrap().addr));
}
consumed = socket.send_slice(event.buffer)?;
state
2023-08-05 15:52:32 +08:00
.tcp_proxy_handler
.consume_data(OutgoingDirection::ToClient, consumed);
self.expect_smoltcp_send()?;
if consumed < buflen {
self.write_sockets.insert(token);
break;
} else {
self.write_sockets.remove(&token);
if consumed == 0 {
break;
}
}
} else {
break;
}
2021-09-02 11:30:23 +02:00
}
2023-08-05 15:52:32 +08:00
self.check_change_close_state(info)?;
2021-09-02 11:30:23 +02:00
}
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-03-24 14:31:22 +08:00
fn tun_event(&mut self, event: &Event) -> Result<(), Error> {
2021-09-02 11:30:23 +02:00
if event.is_readable() {
2023-03-20 15:34:41 +01:00
while let Some((rx_token, _)) = self.tun.receive(Instant::now()) {
2023-03-24 14:31:22 +08:00
rx_token.consume(|frame| self.receive_tun(frame))?;
2021-09-02 11:30:23 +02:00
}
}
2023-03-24 14:31:22 +08:00
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-03-24 14:31:22 +08:00
fn send_to_smoltcp(&mut self) -> Result<(), Error> {
let cloned = self.write_sockets.clone();
for token in cloned.iter() {
2023-08-05 15:52:32 +08:00
if let Some(connection) = self.token_to_info.get(token) {
let connection = connection.clone();
if let Err(error) = self.write_to_client(*token, &connection) {
self.remove_connection(&connection)?;
log::error!("Write to client: {}: ", error);
}
}
}
2023-03-24 14:31:22 +08:00
Ok(())
}
2023-03-24 14:31:22 +08:00
fn mio_socket_event(&mut self, event: &Event) -> Result<(), Error> {
let e = "connection not found";
2023-08-05 15:52:32 +08:00
let conn_info = match self.token_to_info.get(&event.token()) {
Some(conn_info) => conn_info.clone(),
None => {
// 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
// should be closed.
log::trace!("{e}");
return Ok(());
}
};
2023-08-08 23:45:16 +08:00
let server = self.get_connection_manager(&conn_info).ok_or(e)?.get_server_addr();
2023-07-23 02:03:15 +08:00
let mut block = || -> Result<(), Error> {
if event.is_readable() || event.is_read_closed() {
{
2023-08-05 15:52:32 +08:00
let state = self.connection_map.get_mut(&conn_info).ok_or(e)?;
// TODO: Move this reading process to its own function.
let mut vecbuf = Vec::<u8>::new();
let read_result = state.mio_stream.read_to_end(&mut vecbuf);
let read = match read_result {
Ok(read_result) => read_result,
Err(error) => {
if error.kind() != std::io::ErrorKind::WouldBlock {
2023-07-23 02:03:15 +08:00
log::error!("Read from proxy: {}", error);
}
vecbuf.len()
}
};
2021-09-02 21:02:17 +02:00
let data = vecbuf.as_slice();
let data_event = IncomingDataEvent {
direction: IncomingDirection::FromServer,
buffer: &data[0..read],
};
2023-08-05 15:52:32 +08:00
if let Err(error) = state.tcp_proxy_handler.push_data(data_event) {
log::error!("{}", error);
self.remove_connection(&conn_info.clone())?;
return Ok(());
}
2023-06-22 13:09:36 -04:00
// The handler request for reset the server connection
2023-08-05 15:52:32 +08:00
if state.tcp_proxy_handler.reset_connection() {
_ = self.poll.registry().deregister(&mut state.mio_stream);
2023-06-22 13:09:36 -04:00
// Closes the connection with the proxy
2023-08-09 16:50:59 +08:00
state.mio_stream.shutdown(Shutdown::Both)?;
2023-06-22 13:09:36 -04:00
2023-08-05 15:52:32 +08:00
log::info!("RESET {}", conn_info);
2023-06-22 13:09:36 -04:00
state.mio_stream = TcpStream::connect(server)?;
state.wait_read = true;
state.wait_write = true;
2023-08-05 15:52:32 +08:00
Self::update_mio_socket_interest(&mut self.poll, state)?;
2023-06-30 21:16:57 +02:00
2023-06-22 13:09:36 -04:00
return Ok(());
}
if read == 0 || event.is_read_closed() {
state.wait_read = false;
state.close_state |= SERVER_WRITE_CLOSED;
2023-08-05 15:52:32 +08:00
Self::update_mio_socket_interest(&mut self.poll, state)?;
self.check_change_close_state(&conn_info)?;
self.expect_smoltcp_send()?;
2023-03-24 14:31:22 +08:00
}
}
// We have read from the proxy server and pushed the data to the connection handler.
// Thus, expect data to be processed (e.g. decapsulated) and forwarded to the client.
2023-08-05 15:52:32 +08:00
self.write_to_client(event.token(), &conn_info)?;
// The connection handler could have produced data that is to be written to the
// server.
2023-08-05 15:52:32 +08:00
self.write_to_server(&conn_info)?;
}
if event.is_writable() {
2023-08-05 15:52:32 +08:00
self.write_to_server(&conn_info)?;
}
2023-07-23 02:03:15 +08:00
Ok::<(), Error>(())
};
if let Err(error) = block() {
log::error!("{}", error);
2023-08-05 15:52:32 +08:00
self.remove_connection(&conn_info)?;
2023-07-23 02:03:15 +08:00
}
Ok(())
2021-09-02 11:30:23 +02:00
}
2023-04-27 22:41:54 +02:00
pub fn run(&mut self) -> Result<(), Error> {
2021-09-02 11:30:23 +02:00
let mut events = Events::with_capacity(1024);
loop {
2023-08-05 15:52:32 +08:00
if let Err(err) = self.poll.poll(&mut events, None) {
if err.kind() == std::io::ErrorKind::Interrupted {
log::warn!("Poll interrupted: \"{err}\", ignored, continue polling");
continue;
2023-03-24 18:11:04 +01:00
}
2023-08-05 15:52:32 +08:00
return Err(err.into());
}
for event in events.iter() {
match event.token() {
EXIT_TOKEN => {
log::info!("Exiting tun2proxy...");
return Ok(());
2023-03-24 18:11:04 +01:00
}
2023-08-05 15:52:32 +08:00
TUN_TOKEN => self.tun_event(event)?,
_ => self.mio_socket_event(event)?,
2021-09-02 11:30:23 +02:00
}
}
2023-08-05 15:52:32 +08:00
self.send_to_smoltcp()?;
2021-09-02 11:30:23 +02:00
}
}
2023-04-14 17:27:37 +08:00
2023-04-27 22:41:54 +02:00
pub fn shutdown(&mut self) -> Result<(), Error> {
self.exit_sender.write_all(&[1])?;
2023-04-14 17:27:37 +08:00
Ok(())
}
2022-08-01 14:36:58 +00:00
}