Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples #155

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
36 changes: 4 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

- \[lib\]\[breaking\] Removed `relay-v2` ability again.
- This fixed some relay signalling issues, where no connection could be made with `--force-relay` under some circumstances.
- \[lib\]\[breaking\] Exposed the state of the established transit connection
- The `transit` module now returns whether the established connection is direct or not and the peer/relay's IP address
- The `transfer` and `forwarding` modules now take a `transit_handler` argument. Use `&transit::log_transit_connection` as default value

## Version 0.4.0

- When sending, the code will now aumatically be copied into clipboard. So you don't have to select it in the terminal anymore before pasting!
Expand Down
4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ serde_derive = "1.0.120"
log = "0.4.13"
url = { version = "2.2.2", features = ["serde"] }
futures = "0.3.12"
async-std = { version = "1.9.0", features = ["attributes", "unstable"], git = "https://github.com/async-rs/async-std" }
async-std = { version = "1.11.0", features = ["attributes", "unstable"] }

# CLI specific dependencies
magic-wormhole = { path = "..", features = ["all"] }
Expand All @@ -22,4 +22,4 @@ dialoguer = "0.10.0"
color-eyre = "0.6.0"
number_prefix = "0.4.0"
ctrlc = "3.2.1"
cli-clipboard = { git = "https://github.com/piegamesde/cli-clipboard" }
cli-clipboard = { git = "https://github.com/ActuallyAllie/cli-clipboard" }
29 changes: 24 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::too_many_arguments)]
mod util;

use std::{
Expand Down Expand Up @@ -501,6 +502,7 @@ async fn main() -> eyre::Result<()> {
let relay_server = vec![transit::RelayHint::from_urls(None, [relay_server])];
async_std::task::spawn(forwarding::serve(
wormhole,
&transit::log_transit_connection,
relay_server,
targets.clone(),
ctrl_c(),
Expand Down Expand Up @@ -532,8 +534,14 @@ async fn main() -> eyre::Result<()> {
.await?;
let relay_server = vec![transit::RelayHint::from_urls(None, [relay_server])];

let offer =
forwarding::connect(wormhole, relay_server, Some(bind_address), &ports).await?;
let offer = forwarding::connect(
wormhole,
&transit::log_transit_connection,
relay_server,
Some(bind_address),
&ports,
)
.await?;
log::info!("Mapping the following open ports to targets:");
log::info!(" local port -> remote target (no address = localhost on remote)");
for (port, target) in &offer.mapping {
Expand Down Expand Up @@ -700,6 +708,7 @@ async fn send(
file_path,
file_name,
transit_abilities,
&transit::log_transit_connection,
move |sent, total| {
if sent == 0 {
pb.reset_elapsed();
Expand All @@ -716,7 +725,6 @@ async fn send(
Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn send_many(
relay_server: url::Url,
code: &magic_wormhole::Code,
Expand Down Expand Up @@ -810,6 +818,7 @@ async fn send_many(
file_path.deref(),
file_name.deref(),
transit_abilities,
&transit::log_transit_connection,
move |_sent, _total| {
// if sent == 0 {
// pb2.reset_elapsed();
Expand Down Expand Up @@ -904,7 +913,12 @@ async fn receive(
.await
.context("Failed to create destination file")?;
return req
.accept(on_progress, &mut file, ctrl_c())
.accept(
&transit::log_transit_connection,
on_progress,
&mut file,
ctrl_c(),
)
.await
.context("Receive process failed");
}
Expand All @@ -926,7 +940,12 @@ async fn receive(
.open(&file_path)
.await?;
Ok(req
.accept(on_progress, &mut file, ctrl_c())
.accept(
&transit::log_transit_connection,
on_progress,
&mut file,
ctrl_c(),
)
.await
.context("Receive process failed")?)
}
53 changes: 41 additions & 12 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,28 @@ pub struct Wormhole {
}

impl Wormhole {
/**
* Generate a code and connect to the rendezvous server.
*
* # Returns
*
* A tuple with a [`WormholeWelcome`] and a [`std::future::Future`] that will
* do the rest of the client-client handshake and yield the [`Wormhole`] object
* on success.
*/
/// Generate a code and connect to the rendezvous server, `code_length` is the
/// number of words after the nameplate
///
/// # Returns
///
/// A tuple with a [`WormholeWelcome`] and a [`std::future::Future`] that will
/// do the rest of the client-client handshake and yield the [`Wormhole`] object
/// on success.
///
/// # Example
/// ```
/// # #[async_std::main]
/// # async fn main() -> Result<(), magic_wormhole::WormholeError> {
/// # use magic_wormhole::Wormhole;
/// use magic_wormhole::transfer::APP_CONFIG;
///
/// let num_words = 2;
/// let (welcome, wormhole) = Wormhole::connect_without_code(APP_CONFIG, num_words).await?;
/// let wormhole_code = welcome.code.0;
/// # Ok(())
/// # }
/// ```
pub async fn connect_without_code(
config: AppConfig<impl serde::Serialize + Send + Sync + 'static>,
code_length: usize,
Expand Down Expand Up @@ -160,9 +173,25 @@ impl Wormhole {
))
}

/**
* Connect to a peer with a code.
*/
/// Connect to a peer with a code.
///
/// # Returns
///
/// A [`WormholeWelcome`] and a [`std::future::Future`] which yields
/// a Wormhole object
///
/// # Example
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), magic_wormhole::WormholeError> {
/// # use magic_wormhole::Wormhole;
/// use magic_wormhole::{transfer::APP_CONFIG, Code};
///
/// let code = Code("1-aardvark-Zulu".to_string());
/// let (welcome, wormhole) = Wormhole::connect_with_code(APP_CONFIG, code).await?;
/// # Ok(())
/// # }
/// ```
pub async fn connect_with_code(
config: AppConfig<impl serde::Serialize + Send + Sync + 'static>,
code: Code,
Expand Down
15 changes: 13 additions & 2 deletions src/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub async fn test_file_rust2rust() -> eyre::Result<()> {
.unwrap()
.len(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
&transit::log_transit_connection,
|_sent, _total| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -85,6 +86,7 @@ pub async fn test_file_rust2rust() -> eyre::Result<()> {

let mut buffer = Vec::<u8>::new();
req.accept(
&transit::log_transit_connection,
|_received, _total| {},
&mut buffer,
futures::future::pending(),
Expand Down Expand Up @@ -130,6 +132,7 @@ pub async fn test_4096_file_rust2rust() -> eyre::Result<()> {
"example-file.bin",
std::fs::metadata(FILENAME).unwrap().len(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
&transit::log_transit_connection,
|_sent, _total| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -158,6 +161,7 @@ pub async fn test_4096_file_rust2rust() -> eyre::Result<()> {

let mut buffer = Vec::<u8>::new();
req.accept(
&transit::log_transit_connection,
|_received, _total| {},
&mut buffer,
futures::future::pending(),
Expand Down Expand Up @@ -211,6 +215,7 @@ pub async fn test_send_many() -> eyre::Result<()> {
.unwrap()
.len(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
&transit::log_transit_connection,
|_, _| {},
futures::future::pending(),
)
Expand All @@ -236,6 +241,7 @@ pub async fn test_send_many() -> eyre::Result<()> {
.unwrap()
.len(),
magic_wormhole::transit::Abilities::ALL_ABILITIES,
&transit::log_transit_connection,
|_, _| {},
futures::future::pending(),
)
Expand Down Expand Up @@ -263,8 +269,13 @@ pub async fn test_send_many() -> eyre::Result<()> {
.unwrap();

let mut buffer = Vec::<u8>::new();
req.accept(|_, _| {}, &mut buffer, futures::future::pending())
.await?;
req.accept(
&transit::log_transit_connection,
|_, _| {},
&mut buffer,
futures::future::pending(),
)
.await?;
assert_eq!(correct_data, buffer, "Files #{} differ", i);
}

Expand Down
8 changes: 6 additions & 2 deletions src/forwarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl ForwardingError {
/// as the value.
pub async fn serve(
mut wormhole: Wormhole,
transit_handler: impl FnOnce(transit::TransitInfo, std::net::SocketAddr),
relay_hints: Vec<transit::RelayHint>,
targets: Vec<(Option<url::Host>, u16)>,
cancel: impl Future<Output = ()>,
Expand Down Expand Up @@ -189,7 +190,7 @@ pub async fn serve(
},
};

let mut transit = match connector
let (mut transit, info, addr) = match connector
.leader_connect(
wormhole.key().derive_transit_key(wormhole.appid()),
peer_version.transit_abilities,
Expand All @@ -206,6 +207,7 @@ pub async fn serve(
return Err(error);
},
};
transit_handler(info, addr);

/* We got a transit, now close the Wormhole */
wormhole.close().await?;
Expand Down Expand Up @@ -516,6 +518,7 @@ impl ForwardingServe {
/// no more than 1024 ports may be forwarded at once.
pub async fn connect(
mut wormhole: Wormhole,
transit_handler: impl FnOnce(transit::TransitInfo, std::net::SocketAddr),
relay_hints: Vec<transit::RelayHint>,
bind_address: Option<std::net::IpAddr>,
custom_ports: &[u16],
Expand Down Expand Up @@ -558,7 +561,7 @@ pub async fn connect(
},
};

let mut transit = match connector
let (mut transit, info, addr) = match connector
.follower_connect(
wormhole.key().derive_transit_key(wormhole.appid()),
peer_version.transit_abilities,
Expand All @@ -575,6 +578,7 @@ pub async fn connect(
return Err(error);
},
};
transit_handler(info, addr);

/* We got a transit, now close the Wormhole */
wormhole.close().await?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#![forbid(unsafe_code)]
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::too_many_arguments)]

#[macro_use]
mod util;
Expand Down
Loading