Simplify construction of Socks5Stream

This commit is contained in:
Thomas Eizinger 2021-06-09 11:30:48 +10:00
parent ea0fd1eb53
commit 8bd6c9dcfc
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -7,7 +7,7 @@ use libp2p::core::Transport;
use libp2p::tcp::tokio::{Tcp, TcpStream}; use libp2p::tcp::tokio::{Tcp, TcpStream};
use libp2p::tcp::{GenTcpConfig, TcpListenStream, TokioTcpConfig}; use libp2p::tcp::{GenTcpConfig, TcpListenStream, TokioTcpConfig};
use std::io; use std::io;
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use std::net::Ipv4Addr;
use tokio_socks::tcp::Socks5Stream; use tokio_socks::tcp::Socks5Stream;
/// Represents the configuration for a Tor transport for libp2p. /// Represents the configuration for a Tor transport for libp2p.
@ -45,10 +45,12 @@ impl Transport for TorTcpConfig {
Ok(tor_address_string) => Ok(async move { Ok(tor_address_string) => Ok(async move {
tracing::trace!("Connecting through Tor proxy to address: {}", addr); tracing::trace!("Connecting through Tor proxy to address: {}", addr);
let sock = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, self.socks_port)); let stream = Socks5Stream::connect(
let stream = Socks5Stream::connect(sock, tor_address_string) (Ipv4Addr::LOCALHOST, self.socks_port),
.await tor_address_string,
.map_err(|e| io::Error::new(io::ErrorKind::ConnectionRefused, e))?; )
.await
.map_err(|e| io::Error::new(io::ErrorKind::ConnectionRefused, e))?;
tracing::trace!("Connection through Tor established"); tracing::trace!("Connection through Tor established");