Skip to content

Commit

Permalink
Add TransportType::Onion
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Dec 18, 2024
1 parent 1d61ca9 commit 36312c5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
18 changes: 17 additions & 1 deletion tentacle/src/runtime/tokio_runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub(crate) mod socks5;
use multiaddr::MultiAddr;
use socks5::Socks5Config;
pub use tokio::{
net::{TcpListener, TcpStream},
Expand All @@ -12,7 +13,7 @@ use socket2::{Domain, Protocol as SocketProtocol, Socket, Type};
use std::os::unix::io::{FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
use std::{io, net::SocketAddr};
use std::{io, net::SocketAddr, str::FromStr};
use tokio::net::TcpSocket as TokioTcp;

#[cfg(feature = "tokio-timer")]
Expand Down Expand Up @@ -147,3 +148,18 @@ pub(crate) async fn connect(
}
}
}

pub(crate) async fn connect_tor_proxy(
onion_addr: MultiAddr,
tcp_config: TcpSocketConfig,
) -> io::Result<TcpStream> {
let proxy_config = tcp_config.proxy_config.ok_or(std::io::Error::other(
"need tor proxy server to connect to onion address",
))?;
let socks5_config: Socks5Config = super::socks5::parse(&proxy_config.proxy_url)?;
let address = shadowsocks::relay::Address::from_str(onion_addr.to_string().as_str())
.map_err(|err| std::io::Error::other(err))?;
super::socks5::connect(address, socks5_config)
.await
.map_err(|err| io::Error::other(err))
}
30 changes: 29 additions & 1 deletion tentacle/src/transports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod browser;
#[cfg(not(target_family = "wasm"))]
mod memory;
#[cfg(not(target_family = "wasm"))]
mod onion;
#[cfg(not(target_family = "wasm"))]
mod tcp;
#[cfg(not(target_family = "wasm"))]
mod tcp_base_listen;
Expand Down Expand Up @@ -95,6 +97,8 @@ mod os {
};

use futures::{prelude::Stream, FutureExt, StreamExt};
use multiaddr::MultiAddr;
use onion::OnionTransport;
use std::{
collections::HashMap,
fmt,
Expand Down Expand Up @@ -162,7 +166,7 @@ mod os {

fn listen(self, address: Multiaddr) -> Result<Self::ListenFuture> {
match find_type(&address) {
TransportType::Tcp => {
TransportType::Tcp | TransportType::Onion => {
match TcpTransport::from_multi_transport(self, TcpListenMode::Tcp)
.listen(address)
{
Expand Down Expand Up @@ -216,6 +220,12 @@ mod os {
Err(e) => Err(e),
}
}
TransportType::Onion => {
match OnionTransport::new(self.timeout, self.tcp_config.tcp).dial(address) {
Ok(res) => Ok(MultiDialFuture::Tcp(res)),
Err(e) => Err(e),
}
}
#[cfg(feature = "ws")]
TransportType::Ws => {
match WsTransport::new(self.timeout, self.tcp_config.ws).dial(address) {
Expand Down Expand Up @@ -436,6 +446,24 @@ mod os {
Ok(res) => res.map_err(Into::into),
}
}

/// onion common dial realization
#[inline(always)]
pub async fn onion_dial(
onion_addr: MultiAddr,
tcp_config: TcpSocketConfig,
timeout: Duration,
) -> Result<TcpStream> {
match crate::runtime::timeout(
timeout,
crate::runtime::connect_tor_proxy(onion_addr, tcp_config),
)
.await
{
Err(_) => Err(TransportErrorKind::Io(io::ErrorKind::TimedOut.into())),
Ok(res) => res.map_err(Into::into),
}
}
}

#[cfg(target_family = "wasm")]
Expand Down
62 changes: 62 additions & 0 deletions tentacle/src/transports/onion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use futures::future::ok;
use std::{
collections::HashMap, future::Future, net::SocketAddr, pin::Pin, sync::Arc, time::Duration,
};

#[cfg(feature = "tls")]
use crate::service::TlsConfig;
use crate::{
multiaddr::Multiaddr,
runtime::TcpStream,
service::config::TcpSocketConfig,
transports::{
onion_dial, tcp_base_listen::UpgradeMode, Result, TcpListenMode, TransportDial,
TransportFuture,
},
};

/// Onion connect
async fn connect(
onion_address: impl Future<Output = Result<Multiaddr>>,
timeout: Duration,
tcp_config: TcpSocketConfig,
) -> Result<(Multiaddr, TcpStream)> {
let onion_addr = onion_address.await?;
let stream = onion_dial(onion_addr.clone(), tcp_config, timeout).await?;
Ok((onion_addr, stream))
}

/// Onion transport
pub struct OnionTransport {
timeout: Duration,
tcp_config: TcpSocketConfig,
listen_mode: TcpListenMode,
global: Arc<crate::lock::Mutex<HashMap<SocketAddr, UpgradeMode>>>,
#[cfg(feature = "tls")]
tls_config: TlsConfig,
}

impl OnionTransport {
pub fn new(timeout: Duration, tcp_config: TcpSocketConfig) -> Self {
Self {
timeout,
tcp_config,
listen_mode: TcpListenMode::Tcp,
global: Arc::new(crate::lock::Mutex::new(Default::default())),
#[cfg(feature = "tls")]
tls_config: Default::default(),
}
}
}

pub type OnionDialFuture =
TransportFuture<Pin<Box<dyn Future<Output = Result<(Multiaddr, TcpStream)>> + Send>>>;

impl TransportDial for OnionTransport {
type DialFuture = OnionDialFuture;

fn dial(self, address: Multiaddr) -> Result<Self::DialFuture> {
let dial = connect(ok(address), self.timeout, self.tcp_config);
Ok(TransportFuture::new(Box::pin(dial)))
}
}
4 changes: 4 additions & 0 deletions tentacle/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub enum TransportType {
Tls,
/// Memory
Memory,
/// Onion
Onion,
}

/// Confirm the transport used by multiaddress
Expand All @@ -142,6 +144,8 @@ pub fn find_type(addr: &Multiaddr) -> TransportType {
Some(TransportType::Tls)
} else if let Protocol::Memory(_) = proto {
Some(TransportType::Memory)
} else if let Protocol::Onion3(_) = proto {
Some(TransportType::Onion)
} else {
None
}
Expand Down
4 changes: 4 additions & 0 deletions tentacle/src/utils/dns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures::FutureExt;
use log::warn;
use std::{
borrow::Cow,
future::Future,
Expand Down Expand Up @@ -86,6 +87,9 @@ impl DnsResolver {
}
TransportType::Ws => address.push(Protocol::Ws),
TransportType::Wss => address.push(Protocol::Wss),
TransportType::Onion => {
warn!("Onion transport type should not have dns resolve")
}
}

if let Some(peer_id) = self.peer_id.take() {
Expand Down

0 comments on commit 36312c5

Please sign in to comment.