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>

Signed-off-by: Eval EXEC <[email protected]>
  • Loading branch information
eval-exec committed Dec 19, 2024
1 parent 7c1f596 commit 0b033f4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 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
20 changes: 10 additions & 10 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 All @@ -160,7 +160,7 @@ pub(crate) fn listen(addr: SocketAddr, tcp_config: TcpSocketConfig) -> io::Resul

async fn connect_direct(
addr: SocketAddr,
tcp_socket_transformer: TcpSocketTransformer,
socket_transformer: TcpSocketTransformer,
) -> io::Result<TcpStream> {
let domain = Domain::for_address(addr);
let socket = Socket::new(domain, Type::STREAM, Some(SocketProtocol::TCP))?;
Expand All @@ -177,7 +177,7 @@ async fn connect_direct(
// user can disable it on tcp_config
#[cfg(not(windows))]
socket.set_reuse_address(true)?;
let t = tcp_socket_transformer(TcpSocket { inner: socket })?;
let t = socket_transformer(TcpSocket { inner: socket })?;
t.inner
};

Expand Down Expand Up @@ -214,7 +214,7 @@ async fn connect_direct(

async fn connect_by_proxy<A>(
target_addr: A,
tcp_socket_transformer: TcpSocketTransformer,
socket_transformer: TcpSocketTransformer,
proxy_config: ProxyConfig,
) -> io::Result<TcpStream>
where
Expand All @@ -223,7 +223,7 @@ where
let socks5_config = socks5_config::parse(&proxy_config.proxy_url)?;

let dial_addr: SocketAddr = socks5_config.proxy_url.parse().map_err(io::Error::other)?;
let stream = connect_direct(dial_addr, tcp_socket_transformer).await?;
let stream = connect_direct(dial_addr, socket_transformer).await?;

crate::runtime::proxy::socks5::establish_connection(stream, target_addr, socks5_config)
.await
Expand All @@ -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 @@ -271,5 +271,5 @@ pub(crate) async fn connect_onion(
let onion_address =
shadowsocks::relay::Address::from_str(onion_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
}
20 changes: 10 additions & 10 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 All @@ -120,13 +120,13 @@ pub(crate) fn listen(addr: SocketAddr, tcp_config: TcpSocketConfig) -> io::Resul

async fn connect_direct(
addr: SocketAddr,
tcp_socket_transformer: TcpSocketTransformer,
socket_transformer: TcpSocketTransformer,
) -> io::Result<TcpStream> {
let domain = Domain::for_address(addr);
let socket = Socket::new(domain, Type::STREAM, Some(SocketProtocol::TCP))?;

let socket = {
let t = tcp_socket_transformer(TcpSocket { inner: socket })?;
let t = socket_transformer(TcpSocket { inner: socket })?;
t.inner.set_nonblocking(true)?;
// safety: fd convert by socket2
unsafe {
Expand All @@ -143,7 +143,7 @@ async fn connect_direct(

async fn connect_by_proxy<A>(
target_addr: A,
tcp_socket_transformer: TcpSocketTransformer,
socket_transformer: TcpSocketTransformer,
proxy_config: ProxyConfig,
) -> io::Result<TcpStream>
where
Expand All @@ -152,7 +152,7 @@ where
let socks5_config = socks5_config::parse(&proxy_config.proxy_url)?;

let dial_addr: SocketAddr = socks5_config.proxy_url.parse().map_err(io::Error::other)?;
let stream = connect_direct(dial_addr, tcp_socket_transformer).await?;
let stream = connect_direct(dial_addr, socket_transformer).await?;

super::proxy::socks5::establish_connection(stream, target_addr, socks5_config)
.await
Expand All @@ -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 @@ -200,5 +200,5 @@ pub(crate) async fn connect_onion(
let onion_address =
shadowsocks::relay::Address::from_str(onion_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 0b033f4

Please sign in to comment.