-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
916 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use std::net::SocketAddr; | ||
|
||
use clap::Parser; | ||
use ed25519_dalek::SigningKey as SecretKey; | ||
use iroh_net::{ | ||
defaults::default_derp_map, | ||
hp::derp::{DerpMap, UseIpv4, UseIpv6}, | ||
magic_endpoint::accept_conn, | ||
tls::{Keypair, PeerId}, | ||
MagicEndpoint, | ||
}; | ||
use tracing::{debug, info}; | ||
use url::Url; | ||
|
||
const EXAMPLE_ALPN: &[u8] = b"n0/iroh/examples/magic/0"; | ||
|
||
#[derive(Debug, Parser)] | ||
struct Cli { | ||
#[clap(short, long)] | ||
secret: Option<String>, | ||
#[clap(short, long, default_value = "n0/iroh/examples/magic/0")] | ||
alpn: String, | ||
#[clap(short, long, default_value = "0")] | ||
bind_port: u16, | ||
#[clap(short, long)] | ||
derp_url: Option<Url>, | ||
#[clap(subcommand)] | ||
command: Command, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
enum Command { | ||
Listen, | ||
Connect { | ||
peer_id: String, | ||
addrs: Option<Vec<SocketAddr>>, | ||
}, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::fmt::init(); | ||
let args = Cli::parse(); | ||
let keypair = match args.secret { | ||
None => { | ||
let keypair = Keypair::generate(); | ||
println!("our secret key: {}", fmt_secret(&keypair)); | ||
keypair | ||
} | ||
Some(key) => parse_secret(&key)?, | ||
}; | ||
|
||
let derp_map = match args.derp_url { | ||
None => default_derp_map(), | ||
Some(url) => { | ||
// TODO: This should be done by the DERP client. | ||
let derp_port = match url.port() { | ||
Some(port) => port, | ||
None => match url.scheme() { | ||
"http" => 80, | ||
"https" => 443, | ||
_ => anyhow::bail!( | ||
"Invalid scheme in DERP URL, only http: and https: schemes are supported." | ||
), | ||
}, | ||
}; | ||
DerpMap::default_from_node(url, 3478, derp_port, UseIpv4::None, UseIpv6::None) | ||
} | ||
}; | ||
|
||
let endpoint = MagicEndpoint::builder() | ||
.keypair(keypair) | ||
.alpns(vec![args.alpn.to_string().into_bytes()]) | ||
.derp_map(Some(derp_map)) | ||
.bind(args.bind_port) | ||
.await?; | ||
|
||
let me = endpoint.peer_id(); | ||
let local_addr = endpoint.local_addr()?; | ||
println!("magic socket listening on {local_addr:?}"); | ||
println!("our peer id: {me}"); | ||
|
||
match args.command { | ||
Command::Listen => { | ||
while let Some(conn) = endpoint.accept().await { | ||
let (peer_id, alpn, conn) = accept_conn(conn).await?; | ||
info!( | ||
"new connection from {peer_id} with ALPN {alpn} (coming from {})", | ||
conn.remote_address() | ||
); | ||
tokio::spawn(async move { | ||
let (mut send, mut recv) = conn.accept_bi().await?; | ||
debug!("accepted bi stream, waiting for data..."); | ||
let message = recv.read_to_end(1000).await?; | ||
let message = String::from_utf8(message)?; | ||
println!("received: {message}"); | ||
|
||
let message = format!("hi! you connected to {me}. bye bye"); | ||
send.write_all(message.as_bytes()).await?; | ||
send.finish().await?; | ||
|
||
Ok::<_, anyhow::Error>(()) | ||
}); | ||
} | ||
} | ||
Command::Connect { peer_id, addrs } => { | ||
let peer_id: PeerId = peer_id.parse()?; | ||
let addrs = addrs.unwrap_or_default(); | ||
let conn = endpoint.connect(peer_id, EXAMPLE_ALPN, &addrs).await?; | ||
info!("connected"); | ||
|
||
let (mut send, mut recv) = conn.open_bi().await?; | ||
|
||
let message = format!("hello here's {me}"); | ||
send.write_all(message.as_bytes()).await?; | ||
send.finish().await?; | ||
let message = recv.read_to_end(100).await?; | ||
let message = String::from_utf8(message)?; | ||
println!("received: {message}"); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn fmt_secret(keypair: &Keypair) -> String { | ||
let mut text = data_encoding::BASE32_NOPAD.encode(&keypair.secret().to_bytes()); | ||
text.make_ascii_lowercase(); | ||
text | ||
} | ||
fn parse_secret(secret: &str) -> anyhow::Result<Keypair> { | ||
let bytes: [u8; 32] = data_encoding::BASE32_NOPAD | ||
.decode(secret.to_ascii_uppercase().as_bytes())? | ||
.try_into() | ||
.map_err(|_| anyhow::anyhow!("Invalid secret"))?; | ||
let key = SecretKey::from_bytes(&bytes); | ||
Ok(key.into()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::hp::derp::{DerpMap, DerpNode, DerpRegion, UseIpv4, UseIpv6}; | ||
|
||
pub fn default_derp_map() -> DerpMap { | ||
DerpMap { | ||
regions: HashMap::from_iter([(1, default_derp_region())].into_iter()), | ||
} | ||
} | ||
|
||
pub fn default_derp_region() -> DerpRegion { | ||
// The default derper run by number0. | ||
let default_n0_derp = DerpNode { | ||
name: "default-1".into(), | ||
region_id: 1, | ||
host_name: "https://derp.iroh.network".parse().unwrap(), | ||
stun_only: false, | ||
stun_port: 3478, | ||
ipv4: UseIpv4::Some([35, 175, 99, 113].into()), | ||
ipv6: UseIpv6::None, | ||
derp_port: 443, | ||
stun_test_ip: None, | ||
}; | ||
DerpRegion { | ||
region_id: 1, | ||
nodes: vec![default_n0_derp], | ||
avoid: false, | ||
region_code: "default-1".into(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.