Skip to content

Commit cf47abb

Browse files
committed
net: Allow connection to TorV3 addresses with Socks5
1 parent e22d164 commit cf47abb

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/network/mod.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bitcoin::{
2323
},
2424
Block, BlockHash, FeeRate, Wtxid,
2525
};
26-
use socks::create_socks5;
26+
use socks::{create_socks5, SocksConnection};
2727
use tokio::{net::TcpStream, time::Instant};
2828

2929
use error::PeerError;
@@ -141,7 +141,9 @@ impl ConnectionType {
141141
pub(crate) fn can_connect(&self, addr: &AddrV2) -> bool {
142142
match &self {
143143
Self::ClearNet => matches!(addr, AddrV2::Ipv4(_) | AddrV2::Ipv6(_)),
144-
Self::Socks5Proxy(_) => matches!(addr, AddrV2::Ipv4(_) | AddrV2::Ipv6(_)),
144+
Self::Socks5Proxy(_) => {
145+
matches!(addr, AddrV2::Ipv4(_) | AddrV2::Ipv6(_) | AddrV2::TorV3(_))
146+
}
145147
}
146148
}
147149

@@ -151,13 +153,13 @@ impl ConnectionType {
151153
port: u16,
152154
handshake_timeout: Duration,
153155
) -> Result<TcpStream, PeerError> {
154-
let socket_addr = match addr {
155-
AddrV2::Ipv4(ip) => IpAddr::V4(ip),
156-
AddrV2::Ipv6(ip) => IpAddr::V6(ip),
157-
_ => return Err(PeerError::UnreachableSocketAddr),
158-
};
159156
match &self {
160157
Self::ClearNet => {
158+
let socket_addr = match addr {
159+
AddrV2::Ipv4(ip) => IpAddr::V4(ip),
160+
AddrV2::Ipv6(ip) => IpAddr::V6(ip),
161+
_ => return Err(PeerError::UnreachableSocketAddr),
162+
};
161163
let timeout = tokio::time::timeout(
162164
handshake_timeout,
163165
TcpStream::connect((socket_addr, port)),
@@ -168,12 +170,16 @@ impl ConnectionType {
168170
Ok(tcp_stream)
169171
}
170172
Self::Socks5Proxy(proxy) => {
171-
let socks5_timeout = tokio::time::timeout(
172-
handshake_timeout,
173-
create_socks5(*proxy, socket_addr.into(), port),
174-
)
175-
.await
176-
.map_err(|_| PeerError::ConnectionFailed)?;
173+
let addr = match addr {
174+
AddrV2::Ipv4(ipv4) => SocksConnection::ClearNet(IpAddr::V4(ipv4)),
175+
AddrV2::Ipv6(ipv6) => SocksConnection::ClearNet(IpAddr::V6(ipv6)),
176+
AddrV2::TorV3(onion) => SocksConnection::OnionService(onion),
177+
_ => return Err(PeerError::UnreachableSocketAddr),
178+
};
179+
let socks5_timeout =
180+
tokio::time::timeout(handshake_timeout, create_socks5(*proxy, addr, port))
181+
.await
182+
.map_err(|_| PeerError::ConnectionFailed)?;
177183
let tcp_stream = socks5_timeout.map_err(PeerError::Socks5)?;
178184
Ok(tcp_stream)
179185
}

0 commit comments

Comments
 (0)