Skip to content

Commit

Permalink
feat: allow dns discovery in transfer example (#3121)
Browse files Browse the repository at this point in the history
## Description

<!-- A summary of what this pull request achieves and a rough list of
changes. -->

## Breaking Changes

<!-- Optional, if there are any breaking changes document them,
including how to migrate older code. -->

## Notes & open questions

<!-- Any notes, remarks or open questions you have to make about the PR.
-->

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates following the [style
guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text),
if relevant.
- [ ] Tests if relevant.
- [ ] All breaking changes documented.
  • Loading branch information
Arqu authored Jan 13, 2025
1 parent eb90bfc commit a5bb926
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/netsim_runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
cp target/${{inputs.build_profile}}/examples/* ../chuck/netsim/bins/
cp target/${{inputs.build_profile}}/examples/transfer ../chuck/netsim/bins/iroh-transfer
cp target/${{inputs.build_profile}}/iroh-relay ../chuck/netsim/bins/iroh-relay
cp target/${{inputs.build_profile}}/iroh-dns-server ../chuck/netsim/bins/iroh-dns-server
cp ../chuck/target/release/chuck ../chuck/netsim/bins/chuck
- name: Get commit sha
Expand Down
96 changes: 90 additions & 6 deletions iroh/examples/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bytes::Bytes;
use clap::{Parser, Subcommand};
use indicatif::HumanBytes;
use iroh::{
discovery::{dns::DnsDiscovery, pkarr::PkarrPublisher},
endpoint::{ConnectionError, PathSelection},
Endpoint, NodeAddr, RelayMap, RelayMode, RelayUrl, SecretKey,
};
Expand All @@ -32,6 +33,10 @@ enum Commands {
relay_url: Option<String>,
#[clap(long, default_value = "false")]
relay_only: bool,
#[clap(long)]
pkarr_relay_url: Option<String>,
#[clap(long)]
dns_origin_domain: Option<String>,
},
Fetch {
#[arg(index = 1)]
Expand All @@ -40,6 +45,10 @@ enum Commands {
relay_url: Option<String>,
#[clap(long, default_value = "false")]
relay_only: bool,
#[clap(long)]
pkarr_relay_url: Option<String>,
#[clap(long)]
dns_origin_domain: Option<String>,
},
}

Expand All @@ -53,18 +62,46 @@ async fn main() -> anyhow::Result<()> {
size,
relay_url,
relay_only,
} => provide(*size, relay_url.clone(), *relay_only).await?,
pkarr_relay_url,
dns_origin_domain,
} => {
provide(
*size,
relay_url.clone(),
*relay_only,
pkarr_relay_url.clone(),
dns_origin_domain.clone(),
)
.await?
}
Commands::Fetch {
ticket,
relay_url,
relay_only,
} => fetch(ticket, relay_url.clone(), *relay_only).await?,
pkarr_relay_url,
dns_origin_domain,
} => {
fetch(
ticket,
relay_url.clone(),
*relay_only,
pkarr_relay_url.clone(),
dns_origin_domain.clone(),
)
.await?
}
}

Ok(())
}

async fn provide(size: u64, relay_url: Option<String>, relay_only: bool) -> anyhow::Result<()> {
async fn provide(
size: u64,
relay_url: Option<String>,
relay_only: bool,
pkarr_relay_url: Option<String>,
dns_origin_domain: Option<String>,
) -> anyhow::Result<()> {
let secret_key = SecretKey::generate(rand::rngs::OsRng);
let relay_mode = match relay_url {
Some(relay_url) => {
Expand All @@ -78,7 +115,28 @@ async fn provide(size: u64, relay_url: Option<String>, relay_only: bool) -> anyh
true => PathSelection::RelayOnly,
false => PathSelection::default(),
};
let endpoint = Endpoint::builder()

let mut endpoint_builder = Endpoint::builder();

if let Some(pkarr_relay_url) = pkarr_relay_url {
let pkarr_relay_url = pkarr_relay_url
.parse()
.context("Invalid pkarr URL provided")?;

let pkarr_discovery_closure = move |secret_key: &SecretKey| {
let pkarr_d = PkarrPublisher::new(secret_key.clone(), pkarr_relay_url);
Some(pkarr_d)
};
endpoint_builder = endpoint_builder.add_discovery(pkarr_discovery_closure);
}

if let Some(dns_origin_domain) = dns_origin_domain {
let dns_discovery_closure = move |_: &SecretKey| Some(DnsDiscovery::new(dns_origin_domain));

endpoint_builder = endpoint_builder.add_discovery(dns_discovery_closure);
}

let endpoint = endpoint_builder
.secret_key(secret_key)
.alpns(vec![TRANSFER_ALPN.to_vec()])
.relay_mode(relay_mode)
Expand Down Expand Up @@ -160,7 +218,13 @@ async fn provide(size: u64, relay_url: Option<String>, relay_only: bool) -> anyh
Ok(())
}

async fn fetch(ticket: &str, relay_url: Option<String>, relay_only: bool) -> anyhow::Result<()> {
async fn fetch(
ticket: &str,
relay_url: Option<String>,
relay_only: bool,
pkarr_relay_url: Option<String>,
dns_origin_domain: Option<String>,
) -> anyhow::Result<()> {
let ticket: NodeTicket = ticket.parse()?;
let secret_key = SecretKey::generate(rand::rngs::OsRng);
let relay_mode = match relay_url {
Expand All @@ -175,7 +239,27 @@ async fn fetch(ticket: &str, relay_url: Option<String>, relay_only: bool) -> any
true => PathSelection::RelayOnly,
false => PathSelection::default(),
};
let endpoint = Endpoint::builder()
let mut endpoint_builder = Endpoint::builder();

if let Some(pkarr_relay_url) = pkarr_relay_url {
let pkarr_relay_url = pkarr_relay_url
.parse()
.context("Invalid pkarr URL provided")?;

let pkarr_discovery_closure = move |secret_key: &SecretKey| {
let pkarr_d = PkarrPublisher::new(secret_key.clone(), pkarr_relay_url);
Some(pkarr_d)
};
endpoint_builder = endpoint_builder.add_discovery(pkarr_discovery_closure);
}

if let Some(dns_origin_domain) = dns_origin_domain {
let dns_discovery_closure = move |_: &SecretKey| Some(DnsDiscovery::new(dns_origin_domain));

endpoint_builder = endpoint_builder.add_discovery(dns_discovery_closure);
}

let endpoint = endpoint_builder
.secret_key(secret_key)
.alpns(vec![TRANSFER_ALPN.to_vec()])
.relay_mode(relay_mode)
Expand Down

0 comments on commit a5bb926

Please sign in to comment.