From dcdeca032cad409d1ad133abeafa0270bb32a945 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Wed, 13 Mar 2024 22:35:14 +0800 Subject: [PATCH] fix: macOS launch activate socket set nonblocking (#1451) --- .../shadowsocks-service/src/local/dns/server.rs | 4 ++-- .../shadowsocks-service/src/local/http/server.rs | 2 +- .../src/local/socks/server/server.rs | 2 +- .../src/local/socks/server/socks5/udprelay.rs | 2 +- .../src/local/tunnel/tcprelay.rs | 2 +- .../src/local/tunnel/udprelay.rs | 2 +- .../src/net/launch_activate_socket.rs | 16 ++++++++++++---- 7 files changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/shadowsocks-service/src/local/dns/server.rs b/crates/shadowsocks-service/src/local/dns/server.rs index 567a5eed55d3..bc5782654be5 100644 --- a/crates/shadowsocks-service/src/local/dns/server.rs +++ b/crates/shadowsocks-service/src/local/dns/server.rs @@ -216,7 +216,7 @@ impl DnsTcpServerBuilder { use tokio::net::TcpListener as TokioTcpListener; use crate::net::launch_activate_socket::get_launch_activate_tcp_listener; - let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name)?; + let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name, true)?; let tokio_listener = TokioTcpListener::from_std(std_listener)?; TcpListener::from_listener(tokio_listener, self.context.accept_opts())? } else { @@ -389,7 +389,7 @@ impl DnsUdpServerBuilder { use tokio::net::UdpSocket as TokioUdpSocket; use crate::net::launch_activate_socket::get_launch_activate_udp_socket; - let std_socket = get_launch_activate_udp_socket(&launchd_socket_name)?; + let std_socket = get_launch_activate_udp_socket(&launchd_socket_name, true)?; TokioUdpSocket::from_std(std_socket)? } else { create_standard_udp_listener(&self.context, &self.bind_addr).await?.into() diff --git a/crates/shadowsocks-service/src/local/http/server.rs b/crates/shadowsocks-service/src/local/http/server.rs index 4e7b6a65d8f6..b01ada9673b2 100644 --- a/crates/shadowsocks-service/src/local/http/server.rs +++ b/crates/shadowsocks-service/src/local/http/server.rs @@ -64,7 +64,7 @@ impl HttpBuilder { use tokio::net::TcpListener as TokioTcpListener; use crate::net::launch_activate_socket::get_launch_activate_tcp_listener; - let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name)?; + let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name, true)?; let tokio_listener = TokioTcpListener::from_std(std_listener)?; TcpListener::from_listener(tokio_listener, self.context.accept_opts())? } else { diff --git a/crates/shadowsocks-service/src/local/socks/server/server.rs b/crates/shadowsocks-service/src/local/socks/server/server.rs index 83eb4d319ff2..79f6557fce22 100644 --- a/crates/shadowsocks-service/src/local/socks/server/server.rs +++ b/crates/shadowsocks-service/src/local/socks/server/server.rs @@ -62,7 +62,7 @@ impl SocksTcpServerBuilder { use tokio::net::TcpListener as TokioTcpListener; use crate::net::launch_activate_socket::get_launch_activate_tcp_listener; - let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name)?; + let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name, true)?; let tokio_listener = TokioTcpListener::from_std(std_listener)?; ShadowTcpListener::from_listener(tokio_listener, self.context.accept_opts())? } else { diff --git a/crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs b/crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs index 4c313d089996..7d36091b08cf 100644 --- a/crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs +++ b/crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs @@ -71,7 +71,7 @@ impl Socks5UdpServerBuilder { use tokio::net::UdpSocket as TokioUdpSocket; use crate::net::launch_activate_socket::get_launch_activate_udp_socket; - let std_socket = get_launch_activate_udp_socket(&launchd_socket_name)?; + let std_socket = get_launch_activate_udp_socket(&launchd_socket_name, true)?; TokioUdpSocket::from_std(std_socket)? } else { create_standard_udp_listener(&self.context, &self.client_config).await?.into() diff --git a/crates/shadowsocks-service/src/local/tunnel/tcprelay.rs b/crates/shadowsocks-service/src/local/tunnel/tcprelay.rs index 43bc864589c5..d4f2f91b30ae 100644 --- a/crates/shadowsocks-service/src/local/tunnel/tcprelay.rs +++ b/crates/shadowsocks-service/src/local/tunnel/tcprelay.rs @@ -52,7 +52,7 @@ impl TunnelTcpServerBuilder { use tokio::net::TcpListener as TokioTcpListener; use crate::net::launch_activate_socket::get_launch_activate_tcp_listener; - let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name)?; + let std_listener = get_launch_activate_tcp_listener(&launchd_socket_name, true)?; let tokio_listener = TokioTcpListener::from_std(std_listener)?; ShadowTcpListener::from_listener(tokio_listener, self.context.accept_opts())? } else { diff --git a/crates/shadowsocks-service/src/local/tunnel/udprelay.rs b/crates/shadowsocks-service/src/local/tunnel/udprelay.rs index 4bace85ff225..daab725efdcc 100644 --- a/crates/shadowsocks-service/src/local/tunnel/udprelay.rs +++ b/crates/shadowsocks-service/src/local/tunnel/udprelay.rs @@ -61,7 +61,7 @@ impl TunnelUdpServerBuilder { use tokio::net::UdpSocket as TokioUdpSocket; use crate::net::launch_activate_socket::get_launch_activate_udp_socket; - let std_socket = get_launch_activate_udp_socket(&launchd_socket_name)?; + let std_socket = get_launch_activate_udp_socket(&launchd_socket_name, true)?; TokioUdpSocket::from_std(std_socket)? } else { create_standard_udp_listener(&self.context, &self.client_config).await?.into() diff --git a/crates/shadowsocks-service/src/net/launch_activate_socket.rs b/crates/shadowsocks-service/src/net/launch_activate_socket.rs index 85cafc0b79be..750504d2dcc3 100644 --- a/crates/shadowsocks-service/src/net/launch_activate_socket.rs +++ b/crates/shadowsocks-service/src/net/launch_activate_socket.rs @@ -14,15 +14,23 @@ use log::debug; use crate::sys::get_launch_activate_socket; /// Get a macOS launch active socket as a `TcpListener` -pub fn get_launch_activate_tcp_listener(name: &str) -> io::Result { +pub fn get_launch_activate_tcp_listener(name: &str, nonblock: bool) -> io::Result { let fd = get_launch_activate_socket(name)?; debug!("created TCP listener from launch activate socket {}", fd); - Ok(unsafe { TcpListener::from_raw_fd(fd) }) + let listener = unsafe { TcpListener::from_raw_fd(fd) }; + if nonblock { + listener.set_nonblocking(true)?; + } + Ok(listener) } /// Get a macOS launch activate socket as a `UdpSocket` -pub fn get_launch_activate_udp_socket(name: &str) -> io::Result { +pub fn get_launch_activate_udp_socket(name: &str, nonblock: bool) -> io::Result { let fd = get_launch_activate_socket(name)?; debug!("created UDP socket from launch activate socket {}", fd); - Ok(unsafe { UdpSocket::from_raw_fd(fd) }) + let socket = unsafe { UdpSocket::from_raw_fd(fd) }; + if nonblock { + socket.set_nonblocking(true)?; + } + Ok(socket) }