Skip to content

Commit

Permalink
fix: more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Oct 2, 2023
1 parent 8b96769 commit fda31b4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
32 changes: 15 additions & 17 deletions iroh-gossip/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use iroh_gossip::{
proto::{util::base32, Event, TopicId},
};
use iroh_net::{
defaults::default_derp_map,
derp::DerpMap,
derp::{DerpMap, DerpMode},
key::{PublicKey, SecretKey},
magic_endpoint::accept_conn,
MagicEndpoint, PeerAddr,
Expand Down Expand Up @@ -95,13 +94,13 @@ async fn main() -> anyhow::Result<()> {
println!("> our secret key: {}", base32::fmt(secret_key.to_bytes()));

// configure our derp map
let derp_map = match (args.no_derp, args.derp) {
(false, None) => Some(default_derp_map()),
(false, Some(url)) => Some(DerpMap::from_url(url, 0)),
(true, None) => None,
let derp_mode = match (args.no_derp, args.derp) {
(false, None) => DerpMode::Default,
(false, Some(url)) => DerpMode::Custom(DerpMap::from_url(url, 0)),
(true, None) => DerpMode::Disabled,
(true, Some(_)) => bail!("You cannot set --no-derp and --derp at the same time"),
};
println!("> using DERP servers: {}", fmt_derp_map(&derp_map));
println!("> using DERP servers: {}", fmt_derp_mode(&derp_mode));

// init a cell that will hold our gossip handle to be used in endpoint callbacks
let gossip_cell: OnceCell<Gossip> = OnceCell::new();
Expand All @@ -113,6 +112,7 @@ async fn main() -> anyhow::Result<()> {
let endpoint = MagicEndpoint::builder()
.secret_key(secret_key)
.alpns(vec![GOSSIP_ALPN.to_vec()])
.derp_mode(derp_mode)
.on_endpoints({
let gossip_cell = gossip_cell.clone();
let notify = notify.clone();
Expand All @@ -124,12 +124,9 @@ async fn main() -> anyhow::Result<()> {
// notify the outer task of the initial endpoint update (later updates are not interesting)
notify.notify_one();
})
});
let endpoint = match derp_map {
Some(derp_map) => endpoint.enable_derp(derp_map),
None => endpoint,
};
let endpoint = endpoint.bind(args.bind_port).await?;
})
.bind(args.bind_port)
.await?;
println!("> our peer id: {}", endpoint.peer_id());

// create the gossip protocol
Expand Down Expand Up @@ -328,10 +325,11 @@ fn parse_secret_key(secret: &str) -> anyhow::Result<SecretKey> {
Ok(SecretKey::from(bytes))
}

fn fmt_derp_map(derp_map: &Option<DerpMap>) -> String {
match derp_map {
None => "None".to_string(),
Some(map) => map
fn fmt_derp_mode(derp_mode: &DerpMode) -> String {
match derp_mode {
DerpMode::Disabled => "None".to_string(),
DerpMode::Default => "Default Derp servers".to_string(),
DerpMode::Custom(map) => map
.regions()
.flat_map(|region| region.nodes.iter().map(|node| node.url.to_string()))
.collect::<Vec<_>>()
Expand Down
5 changes: 4 additions & 1 deletion iroh-gossip/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,10 @@ mod test {
use std::time::Duration;

use iroh_net::PeerAddr;
use iroh_net::{derp::{DerpMap, DerpMode}, MagicEndpoint};
use iroh_net::{
derp::{DerpMap, DerpMode},
MagicEndpoint,
};
use tokio::spawn;
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;
Expand Down
12 changes: 6 additions & 6 deletions iroh-net/examples/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::net::SocketAddr;

use clap::Parser;
use iroh_net::{
defaults::{default_derp_map, TEST_REGION_ID},
derp::DerpMap,
defaults::TEST_REGION_ID,
derp::{DerpMap, DerpMode},
key::SecretKey,
magic_endpoint::accept_conn,
MagicEndpoint, PeerAddr,
Expand Down Expand Up @@ -50,16 +50,16 @@ async fn main() -> anyhow::Result<()> {
Some(key) => parse_secret(&key)?,
};

let derp_map = match args.derp_url {
None => default_derp_map(),
let derp_mode = match args.derp_url {
None => DerpMode::Default,
// use `region_id` 65535, which is reserved for testing and experiments
Some(url) => DerpMap::from_url(url, TEST_REGION_ID),
Some(url) => DerpMode::Custom(DerpMap::from_url(url, TEST_REGION_ID)),
};

let endpoint = MagicEndpoint::builder()
.secret_key(secret_key)
.alpns(vec![args.alpn.to_string().into_bytes()])
.enable_derp(derp_map)
.derp_mode(derp_mode)
.bind(args.bind_port)
.await?;

Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2648,7 +2648,7 @@ pub(crate) mod tests {
use tracing_subscriber::{prelude::*, EnvFilter};

use super::*;
use crate::{test_utils::run_derper, tls, MagicEndpoint, derp::DerpMode};
use crate::{derp::DerpMode, test_utils::run_derper, tls, MagicEndpoint};

fn make_transmit(destination: SocketAddr) -> quinn_udp::Transmit {
quinn_udp::Transmit {
Expand Down
3 changes: 0 additions & 3 deletions iroh/tests/provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn test_node<D: Store>(
let store = iroh_sync::store::memory::Store::default();
Node::builder(db, store)
.collection_parser(LinkSeqCollectionParser)
.enable_derp(iroh_net::defaults::default_derp_map())
.bind_addr(addr)
}

Expand Down Expand Up @@ -732,7 +731,6 @@ async fn test_custom_collection_parser() {
let doc_store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db, doc_store)
.collection_parser(CollectionsAreJustLinks)
.enable_derp(iroh_net::defaults::default_derp_map())
.bind_addr(addr)
.runtime(&rt)
.spawn()
Expand Down Expand Up @@ -1056,7 +1054,6 @@ async fn test_token_passthrough() -> Result<()> {
tokio::time::timeout(Duration::from_secs(30), async move {
let endpoint = MagicEndpoint::builder()
.secret_key(SecretKey::generate())
.enable_derp(iroh_net::defaults::default_derp_map())
.keylog(true)
.bind(0)
.await?;
Expand Down
5 changes: 1 addition & 4 deletions iroh/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ fn test_node(
) -> Builder<iroh::baomap::mem::Store, store::memory::Store, DummyServerEndpoint> {
let db = iroh::baomap::mem::Store::new(rt.clone());
let store = iroh_sync::store::memory::Store::default();
Node::builder(db, store)
.enable_derp(iroh_net::defaults::default_derp_map())
.runtime(&rt)
.bind_addr(addr)
Node::builder(db, store).runtime(&rt).bind_addr(addr)
}

async fn spawn_node(
Expand Down

0 comments on commit fda31b4

Please sign in to comment.