1+ #![ cfg( feature = "test" ) ]
2+ use std:: { net:: SocketAddr , path:: PathBuf , sync:: Arc } ;
3+
4+ use iroh_blobs:: { net_protocol:: Blobs , util:: local_pool:: { self , LocalPool } } ;
5+
6+ use quinn:: {
7+ crypto:: rustls:: { QuicClientConfig , QuicServerConfig } ,
8+ rustls, ClientConfig , Endpoint , ServerConfig ,
9+ } ;
10+
11+
12+ /// Returns default server configuration along with its certificate.
13+ #[ allow( clippy:: field_reassign_with_default) ] // https://github.com/rust-lang/rust-clippy/issues/6527
14+ fn configure_server ( ) -> anyhow:: Result < ( ServerConfig , Vec < u8 > ) > {
15+ let cert = rcgen:: generate_simple_self_signed ( vec ! [ "localhost" . into( ) ] ) ?;
16+ let cert_der = cert. cert . der ( ) ;
17+ let priv_key = rustls:: pki_types:: PrivatePkcs8KeyDer :: from ( cert. key_pair . serialize_der ( ) ) ;
18+ let cert_chain = vec ! [ cert_der. clone( ) ] ;
19+
20+ let crypto_server_config = rustls:: ServerConfig :: builder_with_provider ( Arc :: new (
21+ rustls:: crypto:: ring:: default_provider ( ) ,
22+ ) )
23+ . with_protocol_versions ( & [ & rustls:: version:: TLS13 ] )
24+ . expect ( "valid versions" )
25+ . with_no_client_auth ( )
26+ . with_single_cert ( cert_chain, priv_key. into ( ) ) ?;
27+ let quic_server_config = QuicServerConfig :: try_from ( crypto_server_config) ?;
28+ let mut server_config = ServerConfig :: with_crypto ( Arc :: new ( quic_server_config) ) ;
29+
30+ Arc :: get_mut ( & mut server_config. transport )
31+ . unwrap ( )
32+ . max_concurrent_uni_streams ( 0_u8 . into ( ) ) ;
33+
34+ Ok ( ( server_config, cert_der. to_vec ( ) ) )
35+ }
36+
37+ pub fn make_server_endpoint ( bind_addr : SocketAddr ) -> anyhow:: Result < ( Endpoint , Vec < u8 > ) > {
38+ let ( server_config, server_cert) = configure_server ( ) ?;
39+ let endpoint = Endpoint :: server ( server_config, bind_addr) ?;
40+ Ok ( ( endpoint, server_cert) )
41+ }
42+
43+ /// An iroh node that just has the blobs transport
44+ #[ derive( Debug ) ]
45+ pub struct Node {
46+ pub router : iroh:: protocol:: Router ,
47+ pub blobs : Blobs < iroh_blobs:: store:: fs:: Store > ,
48+ pub _local_pool : LocalPool ,
49+ }
50+
51+ impl Node {
52+ pub async fn new ( path : PathBuf ) -> anyhow:: Result < Self > {
53+ let store = iroh_blobs:: store:: fs:: Store :: load ( path) . await ?;
54+ let local_pool = LocalPool :: default ( ) ;
55+ let endpoint = iroh:: Endpoint :: builder ( ) . bind ( ) . await ?;
56+ let blobs = Blobs :: builder ( store) . build ( local_pool. handle ( ) , & endpoint) ;
57+ let router = iroh:: protocol:: Router :: builder ( endpoint)
58+ . accept ( iroh_blobs:: ALPN , blobs. clone ( ) )
59+ . spawn ( )
60+ . await ?;
61+ let endpoint = quinn:: Endpoint :: server ( config, "0.0.0.0:12345" . parse ( ) . unwrap ( ) ) ?;
62+ let rpc_server = quic_rpc:: transport:: quinn:: QuinnListener :: new ( endpoint)
63+ Ok ( Self {
64+ router,
65+ blobs,
66+ _local_pool : local_pool,
67+ } )
68+ }
69+ }
0 commit comments