Skip to content

Commit

Permalink
fix: macOS launch activate socket set nonblocking (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Mar 13, 2024
1 parent dd056c8 commit dcdeca0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions crates/shadowsocks-service/src/local/dns/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tunnel/tcprelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tunnel/udprelay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 12 additions & 4 deletions crates/shadowsocks-service/src/net/launch_activate_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TcpListener> {
pub fn get_launch_activate_tcp_listener(name: &str, nonblock: bool) -> io::Result<TcpListener> {
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<UdpSocket> {
pub fn get_launch_activate_udp_socket(name: &str, nonblock: bool) -> io::Result<UdpSocket> {
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)
}

0 comments on commit dcdeca0

Please sign in to comment.