iroh.rs
· 2.2 KiB · Rust
Raw
#[tokio::main]
async fn iroh_test(cmd: IrohCommands) -> Result<()> {
let trusted_nodes = [
NodeId::from_str("e218fe657a62e2049f66252e85e665480fa9ecae1a93bb523138580f49e71877")?, // receive
NodeId::from_str("1f5b63485335333cd011e23960e7d0859490ad24fefb8ee625e231d312844034")?, // send
];
let endpoint = iroh::Endpoint::builder()
// .secret_key(get_or_create_secret()?)
.secret_key(match cmd {
IrohCommands::Send { .. } => iroh::SecretKey::from_str(
"b1749135e836aec4fb63edbd0f09dabcd95cdde5231b8207508b91f10fe47f06",
)?,
IrohCommands::Receive { .. } => iroh::SecretKey::from_str(
"b1749135e836aec4fb63edbd0f09dabcd95cdde5231b8207508b91f10fe47f07",
)?,
})
.discovery_local_network()
.relay_mode(iroh::RelayMode::Disabled)
.bind()
.await?;
let node_id = endpoint.node_id();
println!("> our node id: {node_id:?}"); // TODO remove this is secret info
for local_endpoint in endpoint.direct_addresses().initialized().await? {
println!("\t{}", local_endpoint.addr)
}
// let access_limiter = iroh::protocol::AccessLimit::new(Echo, move |node_id: NodeId| {
// trusted_nodes.contains(&node_id)
// });
let router = iroh::protocol::Router::builder(endpoint)
.accept(ALPN, Echo)
.spawn();
match cmd {
IrohCommands::Send { addr, msg } => {
println!("Sending message:{msg}");
let conn = router
.endpoint()
.connect(NodeId::from_str(&addr)?, ALPN)
.await?;
let (mut send, mut recv) = conn.open_bi().await?;
send.write_all(msg.as_bytes()).await?;
send.finish()?;
send.stopped().await?;
let response = recv.read_to_end(1000).await?;
println!("Received response: {}", String::from_utf8_lossy(&response));
conn.close(0u32.into(), b"done");
}
IrohCommands::Receive {} => {
println!("Waiting for incoming messages...");
tokio::signal::ctrl_c().await?;
}
};
router.shutdown().await?;
Ok(())
}
| 1 | #[tokio::main] |
| 2 | async fn iroh_test(cmd: IrohCommands) -> Result<()> { |
| 3 | let trusted_nodes = [ |
| 4 | NodeId::from_str("e218fe657a62e2049f66252e85e665480fa9ecae1a93bb523138580f49e71877")?, // receive |
| 5 | NodeId::from_str("1f5b63485335333cd011e23960e7d0859490ad24fefb8ee625e231d312844034")?, // send |
| 6 | ]; |
| 7 | |
| 8 | let endpoint = iroh::Endpoint::builder() |
| 9 | // .secret_key(get_or_create_secret()?) |
| 10 | .secret_key(match cmd { |
| 11 | IrohCommands::Send { .. } => iroh::SecretKey::from_str( |
| 12 | "b1749135e836aec4fb63edbd0f09dabcd95cdde5231b8207508b91f10fe47f06", |
| 13 | )?, |
| 14 | IrohCommands::Receive { .. } => iroh::SecretKey::from_str( |
| 15 | "b1749135e836aec4fb63edbd0f09dabcd95cdde5231b8207508b91f10fe47f07", |
| 16 | )?, |
| 17 | }) |
| 18 | .discovery_local_network() |
| 19 | .relay_mode(iroh::RelayMode::Disabled) |
| 20 | .bind() |
| 21 | .await?; |
| 22 | let node_id = endpoint.node_id(); |
| 23 | println!("> our node id: {node_id:?}"); // TODO remove this is secret info |
| 24 | for local_endpoint in endpoint.direct_addresses().initialized().await? { |
| 25 | println!("\t{}", local_endpoint.addr) |
| 26 | } |
| 27 | |
| 28 | // let access_limiter = iroh::protocol::AccessLimit::new(Echo, move |node_id: NodeId| { |
| 29 | // trusted_nodes.contains(&node_id) |
| 30 | // }); |
| 31 | |
| 32 | let router = iroh::protocol::Router::builder(endpoint) |
| 33 | .accept(ALPN, Echo) |
| 34 | .spawn(); |
| 35 | |
| 36 | match cmd { |
| 37 | IrohCommands::Send { addr, msg } => { |
| 38 | println!("Sending message:{msg}"); |
| 39 | let conn = router |
| 40 | .endpoint() |
| 41 | .connect(NodeId::from_str(&addr)?, ALPN) |
| 42 | .await?; |
| 43 | let (mut send, mut recv) = conn.open_bi().await?; |
| 44 | |
| 45 | send.write_all(msg.as_bytes()).await?; |
| 46 | |
| 47 | send.finish()?; |
| 48 | send.stopped().await?; |
| 49 | |
| 50 | let response = recv.read_to_end(1000).await?; |
| 51 | println!("Received response: {}", String::from_utf8_lossy(&response)); |
| 52 | |
| 53 | conn.close(0u32.into(), b"done"); |
| 54 | } |
| 55 | IrohCommands::Receive {} => { |
| 56 | println!("Waiting for incoming messages..."); |
| 57 | tokio::signal::ctrl_c().await?; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | router.shutdown().await?; |
| 62 | Ok(()) |
| 63 | } |