Refactor transports to construct them specific for each application

Instead of splitting up the transports into capabilities, we compose
them directly for each application. This allows us to remove the
websocket transport for the CLI which is really only needed for the
ASB to allow retrieval of quotes via the browser.
This commit is contained in:
Thomas Eizinger 2021-06-09 12:13:29 +10:00
parent 90deb6451c
commit 8a30ef725c
No known key found for this signature in database
GPG key ID: 651AC83A6C6C8B96
7 changed files with 111 additions and 104 deletions

32
swap/src/cli/transport.rs Normal file
View file

@ -0,0 +1,32 @@
use crate::network::tor_transport::TorDialOnlyTransport;
use crate::network::transport::authenticate_and_multiplex;
use anyhow::Result;
use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::{Boxed, OptionalTransport};
use libp2p::dns::TokioDnsConfig;
use libp2p::tcp::TokioTcpConfig;
use libp2p::{identity, PeerId, Transport};
/// Creates the libp2p transport for the swap CLI.
///
/// The CLI's transport needs the following capabilities:
/// - Establish TCP connections
/// - Resolve DNS entries
/// - Dial onion-addresses through a running Tor daemon by connecting to the
/// socks5 port. If the port is not given, we will fall back to the regular
/// TCP transport.
pub fn new(
identity: &identity::Keypair,
maybe_tor_socks5_port: Option<u16>,
) -> Result<Boxed<(PeerId, StreamMuxerBox)>> {
let tcp = TokioTcpConfig::new().nodelay(true);
let tcp_with_dns = TokioDnsConfig::system(tcp)?;
let maybe_tor_transport = match maybe_tor_socks5_port {
Some(port) => OptionalTransport::some(TorDialOnlyTransport::new(port)),
None => OptionalTransport::none(),
};
let transport = maybe_tor_transport.or_transport(tcp_with_dns).boxed();
authenticate_and_multiplex(transport, identity)
}