Skip to content

Commit

Permalink
transit: Make log_transit_connection private
Browse files Browse the repository at this point in the history
  • Loading branch information
felinira committed Jul 10, 2024
1 parent eedee84 commit 62603f9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 67 deletions.
11 changes: 5 additions & 6 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

- \[all\]\[breaking\] By default websocket TLS support is now disabled in the library crate. TLS is required for secure websocket connections to the mailbox server (`wss://`). As the handshake protocol itself is encrypted, this extra layer of encryption is superfluous. Most WASM targets however refuse to connect to non-TLS websockets. For maximum compatibility with all mailbox servers, or for web browser support, select a TLS implementation by specifying the feature flag `tls` for a statically linked implementation via the `ring` crate, or `native-tls` for dynamically linking with the system-native TLS implementation.
- \[all\] For experimental (unstable) `transfer-v2` protocol support, enable feature flag `experimental-transfer-v2`. The protocol is not yet finalized.
- \[cli\] Added compilation support for WASM targets.
- \[cli\] Include `native-tls` in the `all` feature preset.
- \[lib\]\[breaking\] replaced `transit::TransitInfo` with a struct containing the address, the old enum has been renamed to `transit::ConnectionType`.
- \[lib\]\[breaking\] replaced `transit::TransitInfo` with a struct containing the address and a `conn_type` field which contains the old enum that has been renamed to `transit::ConnectionType`
- \[all\] Added compilation support for WASM targets.
- \[lib\]\[breaking\] replaced `transit::TransitInfo` with a struct containing the address and a `conn_type` field which contains the old enum as `transit::ConnectionType`
- \[lib\]\[breaking\] replaced `transfer::AppVersion` with a struct with private fields that implements `std::default::Default`
- \[lib\]\[breaking\] split `Wormhole` in `MailboxConnection` and `Wormhole`. `Wormhole` now uses accessor methods for the previously exposed fields.
- \[lib\]\[breaking\] `WormholeWelcome` ow uses accessor methods for the previously exposed fields.
- \[lib\]\[breaking\] refactor `magic_wormhole::transfer::` methods to take an `OfferSend` and `OfferReceive` instead of using separate methods for files and folders. Use `transfer::send()` and `transfer::receive()`
- \[lib\]\[breaking\] `WormholeWelcome` now uses accessor methods for the previously exposed fields.
- \[lib\]\[breaking\] refactor `magic_wormhole::transfer::` methods to take an `OfferSend` and `OfferReceive` instead of using separate methods for files and folders. Use `transfer::send()` and `transfer::receive()`.
- \[lib\]\[breaking\] struct `transfer::ReceiveRequest` became an enum to prepare for transfer v2
- \[lib\]\[breaking\] removed `transit::log_transit_connection` and implemented `Display` on `TransitInfo` instead.

## Version 0.6.1

Expand Down
33 changes: 18 additions & 15 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use futures::{future::Either, Future, FutureExt};
use indicatif::{MultiProgress, ProgressBar};
use std::{io::Write, path::PathBuf};

use magic_wormhole::{forwarding, transfer, transit, MailboxConnection, Wormhole};
use magic_wormhole::{
forwarding, transfer,
transit::{self, TransitInfo},
MailboxConnection, Wormhole,
};

fn install_ctrlc_handler(
) -> eyre::Result<impl Fn() -> futures::future::BoxFuture<'static, ()> + Clone> {
Expand Down Expand Up @@ -461,7 +465,7 @@ async fn main() -> eyre::Result<()> {
};
async_std::task::spawn(forwarding::serve(
wormhole,
&transit::log_transit_connection,
&transit_handler,
relay_hints,
targets.clone(),
ctrl_c(),
Expand Down Expand Up @@ -494,7 +498,7 @@ async fn main() -> eyre::Result<()> {

let offer = forwarding::connect(
wormhole,
&transit::log_transit_connection,
&transit_handler,
relay_hints,
Some(bind_address),
&ports,
Expand Down Expand Up @@ -800,7 +804,7 @@ async fn send(
relay_hints,
transit_abilities,
offer,
&transit::log_transit_connection,
&transit_handler,
create_progress_handler(pb),
ctrl_c(),
)
Expand Down Expand Up @@ -893,7 +897,7 @@ async fn send_many(
relay_hints,
transit_abilities,
offer,
&transit::log_transit_connection,
&transit_handler,
create_progress_handler(pb2),
cancel,
)
Expand Down Expand Up @@ -991,7 +995,7 @@ async fn receive_inner_v1(
.context("Failed to create destination file")?;
return req
.accept(
&transit::log_transit_connection,
&transit_handler,
&mut file,
create_progress_handler(pb),
ctrl_c(),
Expand All @@ -1017,7 +1021,7 @@ async fn receive_inner_v1(
.open(&file_path)
.await?;
req.accept(
&transit::log_transit_connection,
&transit_handler,
&mut file,
create_progress_handler(pb),
ctrl_c(),
Expand Down Expand Up @@ -1077,14 +1081,9 @@ async fn receive_inner_v2(

/* Accept the offer and receive it */
let answer = offer.accept_all(&tmp_dir);
req.accept(
&transit::log_transit_connection,
answer,
on_progress,
ctrl_c(),
)
.await
.context("Receive process failed")?;
req.accept(&transit_handler, answer, on_progress, ctrl_c())
.await
.context("Receive process failed")?;

// /* Put in all the symlinks last, this greatly reduces the attack surface */
// offer.create_symlinks(&tmp_dir).await?;
Expand Down Expand Up @@ -1129,6 +1128,10 @@ async fn receive_inner_v2(
Ok(())
}

fn transit_handler(info: TransitInfo) {
log::info!("{info}");
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
37 changes: 30 additions & 7 deletions src/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ fn init_logger() {
.try_init();
}

/// Utility method that logs information of the transit result
///
/// Example usage:
///
/// ```no_run
/// use magic_wormhole as mw;
/// # #[async_std::main] async fn main() -> Result<(), mw::transit::TransitConnectError> {
/// # let derived_key = unimplemented!();
/// # let their_abilities = unimplemented!();
/// # let their_hints = unimplemented!();
/// let connector: mw::transit::TransitConnector = unimplemented!("transit::init(…).await?");
/// let (mut transit, info) = connector
/// .leader_connect(derived_key, their_abilities, their_hints)
/// .await?;
/// mw::log_transit_connection(info);
/// # Ok(())
/// # }
/// ```
#[cfg(not(target_family = "wasm"))]
pub(crate) fn log_transit_connection(info: crate::transit::TransitInfo) {
log::info!("{info}")
}

fn default_relay_hints() -> Vec<transit::RelayHint> {
vec![
transit::RelayHint::from_urls(None, [transit::DEFAULT_RELAY_SERVER.parse().unwrap()])
Expand Down Expand Up @@ -199,7 +222,7 @@ pub async fn test_file_rust2rust_deprecated() -> eyre::Result<()> {
default_relay_hints(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
offer,
&transit::log_transit_connection,
&log_transit_connection,
|_sent, _total| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -233,7 +256,7 @@ pub async fn test_file_rust2rust_deprecated() -> eyre::Result<()> {
panic!("v2 should be disabled for now")
};
req.accept(
&transit::log_transit_connection,
&log_transit_connection,
&mut answer,
|_received, _total| {},
futures::future::pending(),
Expand Down Expand Up @@ -275,7 +298,7 @@ pub async fn test_file_rust2rust() -> eyre::Result<()> {
default_relay_hints(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
offer,
&transit::log_transit_connection,
&log_transit_connection,
|_sent, _total| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -308,7 +331,7 @@ pub async fn test_file_rust2rust() -> eyre::Result<()> {
panic!("v2 should be disabled for now")
};
req.accept(
&transit::log_transit_connection,
&log_transit_connection,
&mut answer,
|_received, _total| {},
futures::future::pending(),
Expand Down Expand Up @@ -359,7 +382,7 @@ pub async fn test_send_many() -> eyre::Result<()> {
default_relay_hints(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
gen_offer().await?,
&transit::log_transit_connection,
&log_transit_connection,
|_, _| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -387,7 +410,7 @@ pub async fn test_send_many() -> eyre::Result<()> {
default_relay_hints(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
gen_offer().await?,
&transit::log_transit_connection,
&log_transit_connection,
|_, _| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -431,7 +454,7 @@ pub async fn test_send_many() -> eyre::Result<()> {
.await?;

req.accept(
&transit::log_transit_connection,
&log_transit_connection,
&mut answer,
|_, _| {},
futures::future::pending(),
Expand Down
64 changes: 25 additions & 39 deletions src/transit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,46 +622,32 @@ enum StunError {
),
}

/// Utility method that logs information of the transit result
///
/// Example usage:
///
/// ```no_run
/// use magic_wormhole as mw;
/// # #[async_std::main] async fn main() -> Result<(), mw::transit::TransitConnectError> {
/// # let derived_key = unimplemented!();
/// # let their_abilities = unimplemented!();
/// # let their_hints = unimplemented!();
/// let connector: mw::transit::TransitConnector = unimplemented!("transit::init(…).await?");
/// let (mut transit, info) = connector
/// .leader_connect(derived_key, their_abilities, their_hints)
/// .await?;
/// mw::transit::log_transit_connection(info);
/// # Ok(())
/// # }
/// ```
#[cfg(not(target_family = "wasm"))]
pub fn log_transit_connection(info: TransitInfo) {
match info.conn_type {
ConnectionType::Direct => {
log::info!(
"Established direct transit connection to '{}'",
info.peer_addr,
);
},
ConnectionType::Relay { name: Some(name) } => {
log::info!(
"Established transit connection via relay '{}' ({})",
name,
info.peer_addr,
);
},
ConnectionType::Relay { name: None } => {
log::info!(
"Established transit connection via relay ({})",
info.peer_addr,
);
},
impl std::fmt::Display for TransitInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.conn_type {
ConnectionType::Direct => {
write!(
f,
"Established direct transit connection to '{}'",
self.peer_addr,
)
},
ConnectionType::Relay { name: Some(name) } => {
write!(
f,
"Established transit connection via relay '{}' ({})",
name, self.peer_addr,
)
},
ConnectionType::Relay { name: None } => {
write!(
f,
"Established transit connection via relay ({})",
self.peer_addr,
)
},
}
}
}

Expand Down

0 comments on commit 62603f9

Please sign in to comment.