1- /// Example how to limit blob requests by hash and node id, and to add
1+ /// Example how to limit blob requests by hash and endpoint id, and to add
22/// throttling or limiting the maximum number of connections.
33///
44/// Limiting is done via a fn that returns an EventSender and internally
@@ -21,7 +21,7 @@ use std::{
2121use anyhow:: Result ;
2222use clap:: Parser ;
2323use common:: setup_logging;
24- use iroh:: { protocol:: Router , NodeAddr , NodeId , SecretKey } ;
24+ use iroh:: { endpoint :: presets , protocol:: Router , EndpointAddr , EndpointId , SecretKey } ;
2525use iroh_blobs:: {
2626 provider:: events:: {
2727 AbortReason , ConnectMode , EventMask , EventSender , ProviderMessage , RequestMode ,
@@ -38,14 +38,14 @@ use crate::common::get_or_generate_secret_key;
3838#[ derive( Debug , Parser ) ]
3939#[ command( version, about) ]
4040pub enum Args {
41- /// Limit requests by node id
42- ByNodeId {
41+ /// Limit requests by endpoint id
42+ ByEndpointId {
4343 /// Path for files to add.
4444 paths : Vec < PathBuf > ,
4545 #[ clap( long( "allow" ) ) ]
46- /// Nodes that are allowed to download content.
47- allowed_nodes : Vec < NodeId > ,
48- /// Number of secrets to generate for allowed node ids.
46+ /// Endpoints that are allowed to download content.
47+ allowed_endpoints : Vec < EndpointId > ,
48+ /// Number of secrets to generate for allowed endpoint ids.
4949 #[ clap( long, default_value_t = 1 ) ]
5050 secrets : usize ,
5151 } ,
@@ -77,7 +77,7 @@ pub enum Args {
7777 } ,
7878}
7979
80- fn limit_by_node_id ( allowed_nodes : HashSet < NodeId > ) -> EventSender {
80+ fn limit_by_node_id ( allowed_nodes : HashSet < EndpointId > ) -> EventSender {
8181 let mask = EventMask {
8282 // We want a request for each incoming connection so we can accept
8383 // or reject them. We don't need any other events.
@@ -88,17 +88,17 @@ fn limit_by_node_id(allowed_nodes: HashSet<NodeId>) -> EventSender {
8888 n0_future:: task:: spawn ( async move {
8989 while let Some ( msg) = rx. recv ( ) . await {
9090 if let ProviderMessage :: ClientConnected ( msg) = msg {
91- let res = match msg. node_id {
92- Some ( node_id ) if allowed_nodes. contains ( & node_id ) => {
93- println ! ( "Client connected: {node_id }" ) ;
91+ let res: std :: result :: Result < ( ) , AbortReason > = match msg. endpoint_id {
92+ Some ( endpoint_id ) if allowed_nodes. contains ( & endpoint_id ) => {
93+ println ! ( "Client connected: {endpoint_id }" ) ;
9494 Ok ( ( ) )
9595 }
96- Some ( node_id ) => {
97- println ! ( "Client rejected: {node_id }" ) ;
96+ Some ( endpoint_id ) => {
97+ println ! ( "Client rejected: {endpoint_id }" ) ;
9898 Err ( AbortReason :: Permission )
9999 }
100100 None => {
101- println ! ( "Client rejected: no node id" ) ;
101+ println ! ( "Client rejected: no endpoint id" ) ;
102102 Err ( AbortReason :: Permission )
103103 }
104104 } ;
@@ -206,7 +206,7 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
206206 match msg {
207207 ProviderMessage :: ClientConnected ( msg) => {
208208 let connection_id = msg. connection_id ;
209- let node_id = msg. node_id ;
209+ let node_id = msg. endpoint_id ;
210210 let res = if let Ok ( n) = requests. inc ( ) {
211211 println ! ( "Accepting connection {n}, node_id {node_id:?}, connection_id {connection_id}" ) ;
212212 Ok ( ( ) )
@@ -233,50 +233,50 @@ async fn main() -> Result<()> {
233233 let secret = get_or_generate_secret_key ( ) ?;
234234 let endpoint = iroh:: Endpoint :: builder ( )
235235 . secret_key ( secret)
236- . discovery_n0 ( )
236+ . preset ( presets :: N0 )
237237 . bind ( )
238238 . await ?;
239239 match args {
240240 Args :: Get { ticket } => {
241241 let connection = endpoint
242- . connect ( ticket. node_addr ( ) . clone ( ) , iroh_blobs:: ALPN )
242+ . connect ( ticket. addr ( ) . clone ( ) , iroh_blobs:: ALPN )
243243 . await ?;
244244 let ( data, stats) = iroh_blobs:: get:: request:: get_blob ( connection, ticket. hash ( ) )
245245 . bytes_and_stats ( )
246246 . await ?;
247247 println ! ( "Downloaded {} bytes" , data. len( ) ) ;
248248 println ! ( "Stats: {stats:?}" ) ;
249249 }
250- Args :: ByNodeId {
250+ Args :: ByEndpointId {
251251 paths,
252- allowed_nodes ,
252+ allowed_endpoints ,
253253 secrets,
254254 } => {
255- let mut allowed_nodes = allowed_nodes . into_iter ( ) . collect :: < HashSet < _ > > ( ) ;
255+ let mut allowed_endpoints = allowed_endpoints . into_iter ( ) . collect :: < HashSet < _ > > ( ) ;
256256 if secrets > 0 {
257- println ! ( "Generating {secrets} new secret keys for allowed nodes :" ) ;
257+ println ! ( "Generating {secrets} new secret keys for allowed endpoints :" ) ;
258258 let mut rand = rng ( ) ;
259259 for _ in 0 ..secrets {
260260 let secret = SecretKey :: generate ( & mut rand) ;
261261 let public = secret. public ( ) ;
262- allowed_nodes . insert ( public) ;
262+ allowed_endpoints . insert ( public) ;
263263 println ! ( "IROH_SECRET={}" , hex:: encode( secret. to_bytes( ) ) ) ;
264264 }
265265 }
266266
267267 let store = MemStore :: new ( ) ;
268268 let hashes = add_paths ( & store, paths) . await ?;
269- let events = limit_by_node_id ( allowed_nodes . clone ( ) ) ;
269+ let events = limit_by_node_id ( allowed_endpoints . clone ( ) ) ;
270270 let ( router, addr) = setup ( store, events) . await ?;
271271
272272 for ( path, hash) in hashes {
273273 let ticket = BlobTicket :: new ( addr. clone ( ) , hash, BlobFormat :: Raw ) ;
274274 println ! ( "{}: {ticket}" , path. display( ) ) ;
275275 }
276276 println ! ( ) ;
277- println ! ( "Node id: {}\n " , router. endpoint( ) . node_id ( ) ) ;
278- for id in & allowed_nodes {
279- println ! ( "Allowed node : {id}" ) ;
277+ println ! ( "Endpoint id: {}\n " , router. endpoint( ) . id ( ) ) ;
278+ for id in & allowed_endpoints {
279+ println ! ( "Allowed endpoint : {id}" ) ;
280280 }
281281
282282 tokio:: signal:: ctrl_c ( ) . await ?;
@@ -350,15 +350,15 @@ async fn add_paths(store: &MemStore, paths: Vec<PathBuf>) -> Result<HashMap<Path
350350 Ok ( hashes)
351351}
352352
353- async fn setup ( store : MemStore , events : EventSender ) -> Result < ( Router , NodeAddr ) > {
353+ async fn setup ( store : MemStore , events : EventSender ) -> Result < ( Router , EndpointAddr ) > {
354354 let secret = get_or_generate_secret_key ( ) ?;
355355 let endpoint = iroh:: Endpoint :: builder ( )
356- . discovery_n0 ( )
356+ . preset ( presets :: N0 )
357357 . secret_key ( secret)
358358 . bind ( )
359359 . await ?;
360360 endpoint. online ( ) . await ;
361- let addr = endpoint. node_addr ( ) ;
361+ let addr = endpoint. addr ( ) ;
362362 let blobs = BlobsProtocol :: new ( & store, Some ( events) ) ;
363363 let router = Router :: builder ( endpoint)
364364 . accept ( iroh_blobs:: ALPN , blobs)
0 commit comments