Skip to content

Commit

Permalink
Rename tcp_socket_config to socket_transformer since it accept a TcpS…
Browse files Browse the repository at this point in the history
…ocket and return a Result<TcpSocket>
  • Loading branch information
eval-exec committed Dec 19, 2024
1 parent cbc82b7 commit c2f84e0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions tentacle/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ where
where
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static,
{
self.config.tcp_config.tcp.tcp_socket_config = Arc::new(f);
self.config.tcp_config.tcp.socket_transformer = Arc::new(f);
self
}

Expand All @@ -238,7 +238,7 @@ where
where
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static,
{
self.config.tcp_config.ws.tcp_socket_config = Arc::new(f);
self.config.tcp_config.ws.socket_transformer = Arc::new(f);
self
}

Expand All @@ -262,7 +262,7 @@ where
where
F: Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static,
{
self.config.tcp_config.tls.tcp_socket_config = Arc::new(f);
self.config.tcp_config.tls.socket_transformer = Arc::new(f);
self
}
}
Expand Down
12 changes: 6 additions & 6 deletions tentacle/src/runtime/async_runtime/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub(crate) fn listen(addr: SocketAddr, tcp_config: TcpSocketConfig) -> io::Resul
let socket = Socket::new(domain, Type::STREAM, Some(SocketProtocol::TCP))?;

let socket = {
let t = (tcp_config.tcp_socket_config)(TcpSocket { inner: socket })?;
let t = (tcp_config.socket_transformer)(TcpSocket { inner: socket })?;
t.inner
};
// `bind` twice will return error
Expand Down Expand Up @@ -235,12 +235,12 @@ pub(crate) async fn connect(
tcp_config: TcpSocketConfig,
) -> io::Result<TcpStream> {
let TcpSocketConfig {
tcp_socket_config,
socket_transformer,
proxy_config,
} = tcp_config;
match proxy_config {
Some(proxy_config) => connect_by_proxy(addr, tcp_socket_config, proxy_config).await,
None => connect_direct(addr, tcp_socket_config).await,
Some(proxy_config) => connect_by_proxy(addr, socket_transformer, proxy_config).await,
None => connect_direct(addr, socket_transformer).await,
}
}

Expand All @@ -249,7 +249,7 @@ pub(crate) async fn connect_onion(
tcp_config: TcpSocketConfig,
) -> io::Result<TcpStream> {
let TcpSocketConfig {
tcp_socket_config,
socket_transformer,
proxy_config,
} = tcp_config;
let proxy_config = proxy_config.ok_or(io::Error::other(
Expand All @@ -258,5 +258,5 @@ pub(crate) async fn connect_onion(
let onion_address = shadowsocks::relay::Address::from_str(onion_addr.to_string().as_str())
.map_err(std::io::Error::other)?;

connect_by_proxy(onion_address, tcp_socket_config, proxy_config).await
connect_by_proxy(onion_address, socket_transformer, proxy_config).await
}
12 changes: 6 additions & 6 deletions tentacle/src/runtime/tokio_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub(crate) fn listen(addr: SocketAddr, tcp_config: TcpSocketConfig) -> io::Resul
// user can disable it on tcp_config
#[cfg(not(windows))]
socket.set_reuse_address(true)?;
let t = (tcp_config.tcp_socket_config)(TcpSocket { inner: socket })?;
let t = (tcp_config.socket_transformer)(TcpSocket { inner: socket })?;
t.inner.set_nonblocking(true)?;
// safety: fd convert by socket2
unsafe {
Expand Down Expand Up @@ -164,13 +164,13 @@ pub(crate) async fn connect(
tcp_config: TcpSocketConfig,
) -> io::Result<TcpStream> {
let TcpSocketConfig {
tcp_socket_config,
socket_transformer,
proxy_config,
} = tcp_config;

match proxy_config {
Some(proxy_config) => connect_by_proxy(target_addr, tcp_socket_config, proxy_config).await,
None => connect_direct(target_addr, tcp_socket_config).await,
Some(proxy_config) => connect_by_proxy(target_addr, socket_transformer, proxy_config).await,
None => connect_direct(target_addr, socket_transformer).await,
}
}

Expand All @@ -179,7 +179,7 @@ pub(crate) async fn connect_onion(
tcp_config: TcpSocketConfig,
) -> io::Result<TcpStream> {
let TcpSocketConfig {
tcp_socket_config,
socket_transformer,
proxy_config,
} = tcp_config;
let proxy_config = proxy_config.ok_or(io::Error::other(
Expand All @@ -188,5 +188,5 @@ pub(crate) async fn connect_onion(
let onion_address = shadowsocks::relay::Address::from_str(onion_addr.to_string().as_str())
.map_err(std::io::Error::other)?;

connect_by_proxy(onion_address, tcp_socket_config, proxy_config).await
connect_by_proxy(onion_address, socket_transformer, proxy_config).await
}
6 changes: 3 additions & 3 deletions tentacle/src/service/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ pub struct ProxyConfig {

pub(crate) type TcpSocketTransformer =
Arc<dyn Fn(TcpSocket) -> Result<TcpSocket, std::io::Error> + Send + Sync + 'static>;
#[derive(Clone)]

#[derive(Clone)]
pub(crate) struct TcpSocketConfig {
pub(crate) tcp_socket_config: TcpSocketTransformer,
pub(crate) socket_transformer: TcpSocketTransformer,
pub(crate) proxy_config: Option<ProxyConfig>,
}

impl Default for TcpSocketConfig {
fn default() -> Self {
Self {
tcp_socket_config: Arc::new(Ok),
socket_transformer: Arc::new(Ok),
proxy_config: None,
}
}
Expand Down

0 comments on commit c2f84e0

Please sign in to comment.