Skip to content

Commit

Permalink
fix(libp2p): Use new Transport API for TorDialOnlyTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
binarybaron committed Oct 21, 2024
1 parent f402c1a commit 93cb6cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion swap/src/cli/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn new(
) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
let tcp = tcp::Config::new().nodelay(true);
let tcp_with_dns = dns::tokio::Transport::system(tcp)?;

let maybe_tor_transport = match maybe_tor_socks5_port {
Some(port) => OptionalTransport::some(TorDialOnlyTransport::new(port)),
None => OptionalTransport::none(),
Expand Down
31 changes: 27 additions & 4 deletions swap/src/network/tor_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use anyhow::Result;
use data_encoding::BASE32;
use futures::future::{BoxFuture, FutureExt, Ready};
use libp2p::core::multiaddr::{Multiaddr, Protocol};
use libp2p::core::transport::TransportError;
use libp2p::core::transport::{ListenerId, TransportError};
use libp2p::core::Transport;
use libp2p::tcp::tokio::{Tcp, TcpStream};
use libp2p::tcp::tokio::TcpStream;
use std::borrow::Cow;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::{fmt, io};
Expand All @@ -25,11 +25,14 @@ impl TorDialOnlyTransport {
impl Transport for TorDialOnlyTransport {
type Output = TcpStream;
type Error = io::Error;
type Listener = TcpListenStream<Tcp>;
type ListenerUpgrade = Ready<Result<Self::Output, Self::Error>>;
type Dial = BoxFuture<'static, Result<Self::Output, Self::Error>>;

fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
fn listen_on(
&mut self,
id: ListenerId,
addr: Multiaddr,
) -> Result<(), TransportError<Self::Error>> {
Err(TransportError::MultiaddrNotSupported(addr))
}

Expand Down Expand Up @@ -59,6 +62,7 @@ impl Transport for TorDialOnlyTransport {
fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> {
None
}

fn dial_as_listener(
&mut self,
addr: Multiaddr,
Expand All @@ -84,6 +88,25 @@ impl Transport for TorDialOnlyTransport {

Ok(dial_future.boxed())
}

fn remove_listener(&mut self, id: ListenerId) -> bool {
// TODO: Libp2p Upgrade: What do we need to do here?
// I believe nothing because we are not using the transport to listen.
false
}

fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<libp2p::core::transport::TransportEvent<Self::ListenerUpgrade, Self::Error>>
{
// TODO: Libp2p Upgrade: What do we need to do here?
// See: https://github.com/libp2p/rust-libp2p/pull/2652
// I believe we do not need to do anything here because we are not using the transport to listen.
// But we need to verify this before merging.

return std::task::Poll::Pending;
}
}

/// Represents an address that is _compatible_ with Tor, i.e. can be resolved by
Expand Down
8 changes: 3 additions & 5 deletions swap/src/network/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
let auth_upgrade = noise::Config::new(&identity);
let multiplex_upgrade: SelectUpgrade<_, _> = SelectUpgrade::new(
yamux::Config::default(),
mplex::MplexConfig::new()
);

let multiplex_upgrade: SelectUpgrade<_, _> =
SelectUpgrade::new(yamux::Config::default(), mplex::MplexConfig::new());

let transport = transport
.upgrade(Version::V1)
.authenticate(auth_upgrade)
Expand Down

0 comments on commit 93cb6cb

Please sign in to comment.