mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 17:15:43 +00:00
move bind_tcp_socket and bind_udp_socket to common
This commit is contained in:
@@ -18,7 +18,7 @@ use crate::gateway::kcp_proxy::NatDstKcpConnector;
|
|||||||
use crate::{
|
use crate::{
|
||||||
common::{
|
common::{
|
||||||
config::PortForwardConfig, global_ctx::GlobalCtxEvent, join_joinset_background,
|
config::PortForwardConfig, global_ctx::GlobalCtxEvent, join_joinset_background,
|
||||||
netns::NetNS, scoped_task::ScopedTask,
|
scoped_task::ScopedTask,
|
||||||
},
|
},
|
||||||
gateway::{
|
gateway::{
|
||||||
fast_socks5::{
|
fast_socks5::{
|
||||||
@@ -30,10 +30,7 @@ use crate::{
|
|||||||
ip_reassembler::IpReassembler,
|
ip_reassembler::IpReassembler,
|
||||||
tokio_smoltcp::{channel_device, BufferSize, Net, NetConfig},
|
tokio_smoltcp::{channel_device, BufferSize, Net, NetConfig},
|
||||||
},
|
},
|
||||||
tunnel::{
|
tunnel::packet_def::{PacketType, ZCPacket},
|
||||||
common::setup_sokcet2,
|
|
||||||
packet_def::{PacketType, ZCPacket},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
@@ -42,21 +39,21 @@ use pnet::packet::{
|
|||||||
};
|
};
|
||||||
use tokio::{
|
use tokio::{
|
||||||
io::{AsyncRead, AsyncWrite},
|
io::{AsyncRead, AsyncWrite},
|
||||||
net::{TcpListener, TcpSocket, UdpSocket},
|
net::{TcpListener, UdpSocket},
|
||||||
select,
|
select,
|
||||||
sync::{mpsc, Mutex, Notify},
|
sync::{mpsc, Mutex, Notify},
|
||||||
task::JoinSet,
|
task::JoinSet,
|
||||||
time::timeout,
|
time::timeout,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "kcp")]
|
||||||
|
use super::tcp_proxy::NatDstConnector as _;
|
||||||
|
use crate::tunnel::common::{bind_tcp_socket, bind_udp_socket};
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{error::Error, global_ctx::GlobalCtx},
|
common::{error::Error, global_ctx::GlobalCtx},
|
||||||
peers::{peer_manager::PeerManager, PeerPacketFilter},
|
peers::{peer_manager::PeerManager, PeerPacketFilter},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "kcp")]
|
|
||||||
use super::tcp_proxy::NatDstConnector as _;
|
|
||||||
|
|
||||||
enum SocksUdpSocket {
|
enum SocksUdpSocket {
|
||||||
UdpSocket(Arc<tokio::net::UdpSocket>),
|
UdpSocket(Arc<tokio::net::UdpSocket>),
|
||||||
SmolUdpSocket(super::tokio_smoltcp::UdpSocket),
|
SmolUdpSocket(super::tokio_smoltcp::UdpSocket),
|
||||||
@@ -332,38 +329,6 @@ impl AsyncTcpConnector for Socks5AutoConnector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bind_tcp_socket(addr: SocketAddr, net_ns: NetNS) -> Result<TcpListener, Error> {
|
|
||||||
let _g = net_ns.guard();
|
|
||||||
let socket2_socket = socket2::Socket::new(
|
|
||||||
socket2::Domain::for_address(addr),
|
|
||||||
socket2::Type::STREAM,
|
|
||||||
Some(socket2::Protocol::TCP),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
setup_sokcet2(&socket2_socket, &addr)?;
|
|
||||||
|
|
||||||
let socket = TcpSocket::from_std_stream(socket2_socket.into());
|
|
||||||
|
|
||||||
if let Err(e) = socket.set_nodelay(true) {
|
|
||||||
tracing::warn!(?e, "set_nodelay fail in listen");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(socket.listen(1024)?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bind_udp_socket(addr: SocketAddr, net_ns: NetNS) -> Result<UdpSocket, Error> {
|
|
||||||
let _g = net_ns.guard();
|
|
||||||
let socket2_socket = socket2::Socket::new(
|
|
||||||
socket2::Domain::for_address(addr),
|
|
||||||
socket2::Type::DGRAM,
|
|
||||||
Some(socket2::Protocol::UDP),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
setup_sokcet2(&socket2_socket, &addr)?;
|
|
||||||
|
|
||||||
Ok(UdpSocket::from_std(socket2_socket.into())?)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Socks5ServerNet {
|
struct Socks5ServerNet {
|
||||||
ipv4_addr: cidr::Ipv4Inet,
|
ipv4_addr: cidr::Ipv4Inet,
|
||||||
auth: Option<SimpleUserPassword>,
|
auth: Option<SimpleUserPassword>,
|
||||||
|
|||||||
@@ -11,13 +11,15 @@ use network_interface::NetworkInterfaceConfig as _;
|
|||||||
use pin_project_lite::pin_project;
|
use pin_project_lite::pin_project;
|
||||||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
|
|
||||||
|
use super::TunnelInfo;
|
||||||
|
use crate::common::error::Error;
|
||||||
|
use crate::common::netns::NetNS;
|
||||||
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
||||||
|
use tokio::net::{TcpListener, TcpSocket, UdpSocket};
|
||||||
use tokio_stream::StreamExt;
|
use tokio_stream::StreamExt;
|
||||||
use tokio_util::io::poll_write_buf;
|
use tokio_util::io::poll_write_buf;
|
||||||
use zerocopy::FromBytes as _;
|
use zerocopy::FromBytes as _;
|
||||||
|
|
||||||
use super::TunnelInfo;
|
|
||||||
|
|
||||||
use crate::tunnel::packet_def::{ZCPacket, PEER_MANAGER_HEADER_SIZE};
|
use crate::tunnel::packet_def::{ZCPacket, PEER_MANAGER_HEADER_SIZE};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -439,6 +441,56 @@ pub(crate) fn setup_sokcet2(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Bindable: Sized {
|
||||||
|
const TY: socket2::Type;
|
||||||
|
const PROTOCOL: Option<socket2::Protocol>;
|
||||||
|
|
||||||
|
fn finalize(socket: socket2::Socket) -> Result<Self, Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bindable for TcpListener {
|
||||||
|
const TY: socket2::Type = socket2::Type::STREAM;
|
||||||
|
const PROTOCOL: Option<socket2::Protocol> = Some(socket2::Protocol::TCP);
|
||||||
|
|
||||||
|
fn finalize(socket: socket2::Socket) -> Result<Self, Error> {
|
||||||
|
let tcp_socket = TcpSocket::from_std_stream(socket.into());
|
||||||
|
|
||||||
|
if let Err(e) = tcp_socket.set_nodelay(true) {
|
||||||
|
tracing::warn!(?e, "set_nodelay fail in listen");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(tcp_socket.listen(1024)?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bindable for UdpSocket {
|
||||||
|
const TY: socket2::Type = socket2::Type::DGRAM;
|
||||||
|
const PROTOCOL: Option<socket2::Protocol> = Some(socket2::Protocol::UDP);
|
||||||
|
|
||||||
|
fn finalize(socket: socket2::Socket) -> Result<Self, Error> {
|
||||||
|
Ok(UdpSocket::from_std(socket.into())?)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bind_socket<B: Bindable>(addr: SocketAddr, net_ns: Option<NetNS>) -> Result<B, Error> {
|
||||||
|
let _g = net_ns.map(|n| n.guard());
|
||||||
|
|
||||||
|
let socket2_socket =
|
||||||
|
socket2::Socket::new(socket2::Domain::for_address(addr), B::TY, B::PROTOCOL)?;
|
||||||
|
|
||||||
|
setup_sokcet2(&socket2_socket, &addr)?;
|
||||||
|
|
||||||
|
B::finalize(socket2_socket)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bind_tcp_socket(addr: SocketAddr, net_ns: NetNS) -> Result<TcpListener, Error> {
|
||||||
|
bind_socket(addr, Some(net_ns))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bind_udp_socket(addr: SocketAddr, net_ns: NetNS) -> Result<UdpSocket, Error> {
|
||||||
|
bind_socket(addr, Some(net_ns))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn reserve_buf(buf: &mut BytesMut, min_size: usize, max_size: usize) {
|
pub fn reserve_buf(buf: &mut BytesMut, min_size: usize, max_size: usize) {
|
||||||
if buf.capacity() < min_size {
|
if buf.capacity() < min_size {
|
||||||
buf.reserve(max_size);
|
buf.reserve(max_size);
|
||||||
|
|||||||
Reference in New Issue
Block a user