@@ -18,17 +18,18 @@ use std::{
18
18
} ,
19
19
} ;
20
20
21
+ use anyhow:: Result ;
21
22
use clap:: Parser ;
22
23
use common:: setup_logging;
23
- use iroh:: { NodeId , SecretKey , Watcher } ;
24
+ use iroh:: { protocol :: Router , NodeAddr , NodeId , SecretKey , Watcher } ;
24
25
use iroh_blobs:: {
25
26
provider:: events:: {
26
27
AbortReason , ConnectMode , EventMask , EventSender , ProviderMessage , RequestMode ,
27
28
ThrottleMode ,
28
29
} ,
29
30
store:: mem:: MemStore ,
30
31
ticket:: BlobTicket ,
31
- BlobsProtocol , Hash ,
32
+ BlobFormat , BlobsProtocol , Hash ,
32
33
} ;
33
34
use rand:: thread_rng;
34
35
@@ -77,7 +78,13 @@ pub enum Args {
77
78
}
78
79
79
80
fn limit_by_node_id ( allowed_nodes : HashSet < NodeId > ) -> EventSender {
80
- let ( tx, mut rx) = tokio:: sync:: mpsc:: channel ( 32 ) ;
81
+ let mask = EventMask {
82
+ // We want a request for each incoming connection so we can accept
83
+ // or reject them. We don't need any other events.
84
+ connected : ConnectMode :: Request ,
85
+ ..EventMask :: DEFAULT
86
+ } ;
87
+ let ( tx, mut rx) = EventSender :: channel ( 32 , mask) ;
81
88
n0_future:: task:: spawn ( async move {
82
89
while let Some ( msg) = rx. recv ( ) . await {
83
90
if let ProviderMessage :: ClientConnected ( msg) = msg {
@@ -93,19 +100,18 @@ fn limit_by_node_id(allowed_nodes: HashSet<NodeId>) -> EventSender {
93
100
}
94
101
}
95
102
} ) ;
96
- EventSender :: new (
97
- tx,
98
- EventMask {
99
- // We want a request for each incoming connection so we can accept
100
- // or reject them. We don't need any other events.
101
- connected : ConnectMode :: Request ,
102
- ..EventMask :: DEFAULT
103
- } ,
104
- )
103
+ tx
105
104
}
106
105
107
106
fn limit_by_hash ( allowed_hashes : HashSet < Hash > ) -> EventSender {
108
- let ( tx, mut rx) = tokio:: sync:: mpsc:: channel ( 32 ) ;
107
+ let mask = EventMask {
108
+ // We want to get a request for each get request that we can answer
109
+ // with OK or not OK depending on the hash. We do not want detailed
110
+ // events once it has been decided to handle a request.
111
+ get : RequestMode :: Request ,
112
+ ..EventMask :: DEFAULT
113
+ } ;
114
+ let ( tx, mut rx) = EventSender :: channel ( 32 , mask) ;
109
115
n0_future:: task:: spawn ( async move {
110
116
while let Some ( msg) = rx. recv ( ) . await {
111
117
if let ProviderMessage :: GetRequestReceived ( msg) = msg {
@@ -123,20 +129,17 @@ fn limit_by_hash(allowed_hashes: HashSet<Hash>) -> EventSender {
123
129
}
124
130
}
125
131
} ) ;
126
- EventSender :: new (
127
- tx,
128
- EventMask {
129
- // We want to get a request for each get request that we can answer
130
- // with OK or not OK depending on the hash. We do not want detailed
131
- // events once it has been decided to handle a request.
132
- get : RequestMode :: Request ,
133
- ..EventMask :: DEFAULT
134
- } ,
135
- )
132
+ tx
136
133
}
137
134
138
135
fn throttle ( delay_ms : u64 ) -> EventSender {
139
- let ( tx, mut rx) = tokio:: sync:: mpsc:: channel ( 32 ) ;
136
+ let mask = EventMask {
137
+ // We want to get requests for each sent user data blob, so we can add a delay.
138
+ // Other than that, we don't need any events.
139
+ throttle : ThrottleMode :: Throttle ,
140
+ ..EventMask :: DEFAULT
141
+ } ;
142
+ let ( tx, mut rx) = EventSender :: channel ( 32 , mask) ;
140
143
n0_future:: task:: spawn ( async move {
141
144
while let Some ( msg) = rx. recv ( ) . await {
142
145
if let ProviderMessage :: Throttle ( msg) = msg {
@@ -153,15 +156,7 @@ fn throttle(delay_ms: u64) -> EventSender {
153
156
}
154
157
}
155
158
} ) ;
156
- EventSender :: new (
157
- tx,
158
- EventMask {
159
- // We want to get requests for each sent user data blob, so we can add a delay.
160
- // Other than that, we don't need any events.
161
- throttle : ThrottleMode :: Throttle ,
162
- ..EventMask :: DEFAULT
163
- } ,
164
- )
159
+ tx
165
160
}
166
161
167
162
fn limit_max_connections ( max_connections : usize ) -> EventSender {
@@ -190,7 +185,15 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
190
185
}
191
186
}
192
187
193
- let ( tx, mut rx) = tokio:: sync:: mpsc:: channel ( 32 ) ;
188
+ let mask = EventMask {
189
+ // For each get request, we want to get a request so we can decide
190
+ // based on the current connection count if we want to accept or reject.
191
+ // We also want detailed logging of events for the get request, so we can
192
+ // detect when the request is finished one way or another.
193
+ get : RequestMode :: RequestLog ,
194
+ ..EventMask :: DEFAULT
195
+ } ;
196
+ let ( tx, mut rx) = EventSender :: channel ( 32 , mask) ;
194
197
n0_future:: task:: spawn ( async move {
195
198
let requests = ConnectionCounter :: new ( max_connections) ;
196
199
while let Some ( msg) = rx. recv ( ) . await {
@@ -224,21 +227,11 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
224
227
}
225
228
}
226
229
} ) ;
227
- EventSender :: new (
228
- tx,
229
- EventMask {
230
- // For each get request, we want to get a request so we can decide
231
- // based on the current connection count if we want to accept or reject.
232
- // We also want detailed logging of events for the get request, so we can
233
- // detect when the request is finished one way or another.
234
- get : RequestMode :: RequestLog ,
235
- ..EventMask :: DEFAULT
236
- } ,
237
- )
230
+ tx
238
231
}
239
232
240
233
#[ tokio:: main]
241
- async fn main ( ) -> anyhow :: Result < ( ) > {
234
+ async fn main ( ) -> Result < ( ) > {
242
235
setup_logging ( ) ;
243
236
let args = Args :: parse ( ) ;
244
237
match args {
@@ -274,35 +267,28 @@ async fn main() -> anyhow::Result<()> {
274
267
println ! ( "IROH_SECRET={}" , hex:: encode( secret. to_bytes( ) ) ) ;
275
268
}
276
269
}
277
- let endpoint = iroh :: Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ? ;
270
+
278
271
let store = MemStore :: new ( ) ;
279
- let mut hashes = HashMap :: new ( ) ;
280
- for path in paths {
281
- let tag = store. add_path ( & path) . await ?;
282
- hashes. insert ( path, tag. hash ) ;
283
- }
284
- let _ = endpoint. home_relay ( ) . initialized ( ) . await ;
285
- let addr = endpoint. node_addr ( ) . initialized ( ) . await ;
272
+ let hashes = add_paths ( & store, paths) . await ?;
286
273
let events = limit_by_node_id ( allowed_nodes. clone ( ) ) ;
287
- let blobs = BlobsProtocol :: new ( & store, endpoint. clone ( ) , Some ( events) ) ;
288
- let router = iroh:: protocol:: Router :: builder ( endpoint)
289
- . accept ( iroh_blobs:: ALPN , blobs)
290
- . spawn ( ) ;
274
+ let ( router, addr) = setup ( MemStore :: new ( ) , events) . await ?;
275
+
276
+ for ( path, hash) in hashes {
277
+ let ticket = BlobTicket :: new ( addr. clone ( ) , hash, BlobFormat :: Raw ) ;
278
+ println ! ( "{}: {ticket}" , path. display( ) ) ;
279
+ }
280
+ println ! ( ) ;
291
281
println ! ( "Node id: {}\n " , router. endpoint( ) . node_id( ) ) ;
292
282
for id in & allowed_nodes {
293
283
println ! ( "Allowed node: {id}" ) ;
294
284
}
295
- println ! ( ) ;
296
- for ( path, hash) in & hashes {
297
- let ticket = BlobTicket :: new ( addr. clone ( ) , * hash, iroh_blobs:: BlobFormat :: Raw ) ;
298
- println ! ( "{}: {ticket}" , path. display( ) ) ;
299
- }
285
+
300
286
tokio:: signal:: ctrl_c ( ) . await ?;
301
287
router. shutdown ( ) . await ?;
302
288
}
303
289
Args :: ByHash { paths } => {
304
- let endpoint = iroh:: Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ?;
305
290
let store = MemStore :: new ( ) ;
291
+
306
292
let mut hashes = HashMap :: new ( ) ;
307
293
let mut allowed_hashes = HashSet :: new ( ) ;
308
294
for ( i, path) in paths. into_iter ( ) . enumerate ( ) {
@@ -312,38 +298,25 @@ async fn main() -> anyhow::Result<()> {
312
298
allowed_hashes. insert ( tag. hash ) ;
313
299
}
314
300
}
315
- let _ = endpoint. home_relay ( ) . initialized ( ) . await ;
316
- let addr = endpoint. node_addr ( ) . initialized ( ) . await ;
317
- let events = limit_by_hash ( allowed_hashes. clone ( ) ) ;
318
- let blobs = BlobsProtocol :: new ( & store, endpoint. clone ( ) , Some ( events) ) ;
319
- let router = iroh:: protocol:: Router :: builder ( endpoint)
320
- . accept ( iroh_blobs:: ALPN , blobs)
321
- . spawn ( ) ;
301
+
302
+ let events = limit_by_hash ( allowed_hashes) ;
303
+ let ( router, addr) = setup ( MemStore :: new ( ) , events) . await ?;
304
+
322
305
for ( i, ( path, hash) ) in hashes. iter ( ) . enumerate ( ) {
323
- let ticket = BlobTicket :: new ( addr. clone ( ) , * hash, iroh_blobs :: BlobFormat :: Raw ) ;
306
+ let ticket = BlobTicket :: new ( addr. clone ( ) , * hash, BlobFormat :: Raw ) ;
324
307
let permitted = if i == 0 { "" } else { "limited" } ;
325
308
println ! ( "{}: {ticket} ({permitted})" , path. display( ) ) ;
326
309
}
327
310
tokio:: signal:: ctrl_c ( ) . await ?;
328
311
router. shutdown ( ) . await ?;
329
312
}
330
313
Args :: Throttle { paths, delay_ms } => {
331
- let endpoint = iroh:: Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ?;
332
314
let store = MemStore :: new ( ) ;
333
- let mut hashes = HashMap :: new ( ) ;
334
- for path in paths {
335
- let tag = store. add_path ( & path) . await ?;
336
- hashes. insert ( path, tag. hash ) ;
337
- }
338
- let _ = endpoint. home_relay ( ) . initialized ( ) . await ;
339
- let addr = endpoint. node_addr ( ) . initialized ( ) . await ;
315
+ let hashes = add_paths ( & store, paths) . await ?;
340
316
let events = throttle ( delay_ms) ;
341
- let blobs = BlobsProtocol :: new ( & store, endpoint. clone ( ) , Some ( events) ) ;
342
- let router = iroh:: protocol:: Router :: builder ( endpoint)
343
- . accept ( iroh_blobs:: ALPN , blobs)
344
- . spawn ( ) ;
317
+ let ( router, addr) = setup ( MemStore :: new ( ) , events) . await ?;
345
318
for ( path, hash) in hashes {
346
- let ticket = BlobTicket :: new ( addr. clone ( ) , hash, iroh_blobs :: BlobFormat :: Raw ) ;
319
+ let ticket = BlobTicket :: new ( addr. clone ( ) , hash, BlobFormat :: Raw ) ;
347
320
println ! ( "{}: {ticket}" , path. display( ) ) ;
348
321
}
349
322
tokio:: signal:: ctrl_c ( ) . await ?;
@@ -353,22 +326,12 @@ async fn main() -> anyhow::Result<()> {
353
326
paths,
354
327
max_connections,
355
328
} => {
356
- let endpoint = iroh:: Endpoint :: builder ( ) . discovery_n0 ( ) . bind ( ) . await ?;
357
329
let store = MemStore :: new ( ) ;
358
- let mut hashes = HashMap :: new ( ) ;
359
- for path in paths {
360
- let tag = store. add_path ( & path) . await ?;
361
- hashes. insert ( path, tag. hash ) ;
362
- }
363
- let _ = endpoint. home_relay ( ) . initialized ( ) . await ;
364
- let addr = endpoint. node_addr ( ) . initialized ( ) . await ;
330
+ let hashes = add_paths ( & store, paths) . await ?;
365
331
let events = limit_max_connections ( max_connections) ;
366
- let blobs = BlobsProtocol :: new ( & store, endpoint. clone ( ) , Some ( events) ) ;
367
- let router = iroh:: protocol:: Router :: builder ( endpoint)
368
- . accept ( iroh_blobs:: ALPN , blobs)
369
- . spawn ( ) ;
332
+ let ( router, addr) = setup ( MemStore :: new ( ) , events) . await ?;
370
333
for ( path, hash) in hashes {
371
- let ticket = BlobTicket :: new ( addr. clone ( ) , hash, iroh_blobs :: BlobFormat :: Raw ) ;
334
+ let ticket = BlobTicket :: new ( addr. clone ( ) , hash, BlobFormat :: Raw ) ;
372
335
println ! ( "{}: {ticket}" , path. display( ) ) ;
373
336
}
374
337
tokio:: signal:: ctrl_c ( ) . await ?;
@@ -377,3 +340,28 @@ async fn main() -> anyhow::Result<()> {
377
340
}
378
341
Ok ( ( ) )
379
342
}
343
+
344
+ async fn add_paths ( store : & MemStore , paths : Vec < PathBuf > ) -> Result < HashMap < PathBuf , Hash > > {
345
+ let mut hashes = HashMap :: new ( ) ;
346
+ for path in paths {
347
+ let tag = store. add_path ( & path) . await ?;
348
+ hashes. insert ( path, tag. hash ) ;
349
+ }
350
+ Ok ( hashes)
351
+ }
352
+
353
+ async fn setup ( store : MemStore , events : EventSender ) -> Result < ( Router , NodeAddr ) > {
354
+ let secret = get_or_generate_secret_key ( ) ?;
355
+ let endpoint = iroh:: Endpoint :: builder ( )
356
+ . discovery_n0 ( )
357
+ . secret_key ( secret)
358
+ . bind ( )
359
+ . await ?;
360
+ let _ = endpoint. home_relay ( ) . initialized ( ) . await ;
361
+ let addr = endpoint. node_addr ( ) . initialized ( ) . await ;
362
+ let blobs = BlobsProtocol :: new ( & store, endpoint. clone ( ) , Some ( events) ) ;
363
+ let router = Router :: builder ( endpoint)
364
+ . accept ( iroh_blobs:: ALPN , blobs)
365
+ . spawn ( ) ;
366
+ Ok ( ( router, addr) )
367
+ }
0 commit comments