set SO_REUSEPORT for UDP redir sockets for sending responds

different sources may receive packets from the same destination, so
SO_REUSEPORT have to be set, or we will get EADDRINUSE
This commit is contained in:
zonyitoo
2021-03-04 14:22:47 +08:00
parent 94ef9faec8
commit 2ddf150999
7 changed files with 73 additions and 9 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ rustls-native-certs = { version = "0.5", optional = true }
async-trait = "0.1"
mio = { version = "0.7", optional = true }
socket2 = "0.3"
socket2 = { version = "0.3", features = ["reuseport"] }
libc = "0.2"
http = { version = "0.2", optional = true }
@@ -49,7 +49,10 @@ impl UdpInboundWrite for UdpRedirInboundWriter {
// Create a socket binds to destination addr
// This only works for systems that supports binding to non-local addresses
let inbound = UdpRedirSocket::bind(self.redir_ty, addr)?;
//
// This socket has to set SO_REUSEADDR and SO_REUSEPORT.
// Outbound addresses could be connected from different source addresses.
let inbound = UdpRedirSocket::bind_nonlocal(self.redir_ty, addr)?;
// Send back to client
inbound.send_to(data, peer_addr).await.map(|_| ())
@@ -80,10 +83,10 @@ impl UdpRedir {
pub async fn run(&self, client_config: &ClientConfig, balancer: PingBalancer) -> io::Result<()> {
let listener = match *client_config {
ClientConfig::SocketAddr(ref saddr) => UdpRedirSocket::bind(self.redir_ty, *saddr)?,
ClientConfig::SocketAddr(ref saddr) => UdpRedirSocket::listen(self.redir_ty, *saddr)?,
ClientConfig::DomainName(ref dname, port) => {
lookup_then!(self.context.context_ref(), dname, port, |addr| {
UdpRedirSocket::bind(self.redir_ty, addr)
UdpRedirSocket::listen(self.redir_ty, addr)
})?
.1
}
@@ -23,10 +23,21 @@ pub struct UdpRedirSocket {
}
impl UdpRedirSocket {
/// Create a new UDP socket binded to `addr`
///
/// This will allow listening to `addr` that is not in local host
pub fn listen(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, false)
}
/// Create a new UDP socket binded to `addr`
///
/// This will allow binding to `addr` that is not in local host
pub fn bind(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
pub fn bind_nonlocal(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, true)
}
fn bind(ty: RedirType, addr: SocketAddr, reuse_port: bool) -> io::Result<UdpRedirSocket> {
if ty == RedirType::NotSupported {
return Err(Error::new(
ErrorKind::InvalidInput,
@@ -43,6 +54,9 @@ impl UdpRedirSocket {
socket.set_nonblocking(true)?;
socket.set_reuse_address(true)?;
if reuse_port {
socket.set_reuse_port(true)?;
}
socket.bind(&SockAddr::from(addr))?;
@@ -19,10 +19,21 @@ pub struct UdpRedirSocket {
}
impl UdpRedirSocket {
/// Create a new UDP socket binded to `addr`
///
/// This will allow listening to `addr` that is not in local host
pub fn listen(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, false)
}
/// Create a new UDP socket binded to `addr`
///
/// This will allow binding to `addr` that is not in local host
pub fn bind(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
pub fn bind_nonlocal(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, true)
}
fn bind(ty: RedirType, addr: SocketAddr, reuse_port: bool) -> io::Result<UdpRedirSocket> {
if ty != RedirType::TProxy {
return Err(Error::new(
ErrorKind::InvalidInput,
@@ -39,6 +50,9 @@ impl UdpRedirSocket {
socket.set_nonblocking(true)?;
socket.set_reuse_address(true)?;
if reuse_port {
socket.set_reuse_port(true)?;
}
socket.bind(&SockAddr::from(addr))?;
@@ -9,10 +9,21 @@ use crate::{config::RedirType, local::redir::redir_ext::UdpSocketRedir};
pub struct UdpRedirSocket;
impl UdpRedirSocket {
/// Create a new UDP socket binded to `addr`
///
/// This will allow listening to `addr` that is not in local host
pub fn listen(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, false)
}
/// Create a new UDP socket binded to `addr`
///
/// This will allow binding to `addr` that is not in local host
pub fn bind(_ty: RedirType, _addr: SocketAddr) -> io::Result<UdpRedirSocket> {
pub fn bind_nonlocal(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, true)
}
fn bind(_ty: RedirType, _addr: SocketAddr, _reuse_port: bool) -> io::Result<UdpRedirSocket> {
unimplemented!("UDP transparent proxy is not supported on macOS, iOS, ...")
}
@@ -5,10 +5,21 @@ use crate::{config::RedirType, local::redir::redir_ext::UdpSocketRedir};
pub struct UdpRedirSocket;
impl UdpRedirSocket {
/// Create a new UDP socket binded to `addr`
///
/// This will allow listening to `addr` that is not in local host
pub fn listen(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, false)
}
/// Create a new UDP socket binded to `addr`
///
/// This will allow binding to `addr` that is not in local host
pub fn bind(_ty: RedirType, _addr: SocketAddr) -> io::Result<UdpRedirSocket> {
pub fn bind_nonlocal(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, true)
}
fn bind(_ty: RedirType, _addr: SocketAddr, _reuse_port: bool) -> io::Result<UdpRedirSocket> {
unimplemented!("UDP transparent proxy is not supported on this platform")
}
@@ -5,10 +5,21 @@ use crate::{config::RedirType, local::redir::redir_ext::UdpSocketRedir};
pub struct UdpRedirSocket;
impl UdpRedirSocket {
/// Create a new UDP socket binded to `addr`
///
/// This will allow listening to `addr` that is not in local host
pub fn listen(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, false)
}
/// Create a new UDP socket binded to `addr`
///
/// This will allow binding to `addr` that is not in local host
pub fn bind(_ty: RedirType, _addr: &SocketAddr) -> io::Result<UdpRedirSocket> {
pub fn bind_nonlocal(ty: RedirType, addr: SocketAddr) -> io::Result<UdpRedirSocket> {
UdpRedirSocket::bind(ty, addr, true)
}
fn bind(_ty: RedirType, _addr: SocketAddr, _reuse_port: bool) -> io::Result<UdpRedirSocket> {
unimplemented!("UDP transparent proxy is not supported on Windows")
}