@@ -19,16 +19,9 @@ use iroh::{
19
19
discovery:: mdns:: MdnsDiscovery , protocol:: Router , Endpoint , PublicKey , RelayMode , SecretKey ,
20
20
} ;
21
21
use iroh_blobs:: { store:: mem:: MemStore , BlobsProtocol , Hash } ;
22
- use tracing_subscriber:: { prelude:: * , EnvFilter } ;
23
22
24
- // set the RUST_LOG env var to one of {debug,info,warn} to see logging info
25
- pub fn setup_logging ( ) {
26
- tracing_subscriber:: registry ( )
27
- . with ( tracing_subscriber:: fmt:: layer ( ) . with_writer ( std:: io:: stderr) )
28
- . with ( EnvFilter :: from_default_env ( ) )
29
- . try_init ( )
30
- . ok ( ) ;
31
- }
23
+ mod common;
24
+ use common:: { get_or_generate_secret_key, setup_logging} ;
32
25
33
26
#[ derive( Debug , Parser ) ]
34
27
#[ command( version, about) ]
@@ -64,13 +57,12 @@ async fn accept(path: &Path) -> Result<()> {
64
57
}
65
58
66
59
let key = get_or_generate_secret_key ( ) ?;
67
- let discovery = MdnsDiscovery :: new ( key. public ( ) ) ?;
68
60
69
61
println ! ( "Starting iroh node with mdns discovery..." ) ;
70
62
// create a new node
71
63
let endpoint = Endpoint :: builder ( )
72
64
. secret_key ( key)
73
- . add_discovery ( discovery )
65
+ . add_discovery ( MdnsDiscovery :: builder ( ) )
74
66
. relay_mode ( RelayMode :: Disabled )
75
67
. bind ( )
76
68
. await ?;
@@ -97,7 +89,7 @@ async fn accept(path: &Path) -> Result<()> {
97
89
async fn connect ( node_id : PublicKey , hash : Hash , out : Option < PathBuf > ) -> Result < ( ) > {
98
90
let key = SecretKey :: generate ( rand:: rngs:: OsRng ) ;
99
91
// todo: disable discovery publishing once https://github.com/n0-computer/iroh/issues/3401 is implemented
100
- let discovery = MdnsDiscovery :: new ( key . public ( ) ) ? ;
92
+ let discovery = MdnsDiscovery :: builder ( ) ;
101
93
102
94
println ! ( "Starting iroh node with mdns discovery..." ) ;
103
95
// create a new node
@@ -150,25 +142,3 @@ async fn main() -> anyhow::Result<()> {
150
142
}
151
143
Ok ( ( ) )
152
144
}
153
-
154
- /// Gets a secret key from the IROH_SECRET environment variable or generates a new random one.
155
- /// If the environment variable is set, it must be a valid string representation of a secret key.
156
- pub fn get_or_generate_secret_key ( ) -> Result < SecretKey > {
157
- use std:: { env, str:: FromStr } ;
158
-
159
- use anyhow:: Context ;
160
- use rand:: thread_rng;
161
- if let Ok ( secret) = env:: var ( "IROH_SECRET" ) {
162
- // Parse the secret key from string
163
- SecretKey :: from_str ( & secret) . context ( "Invalid secret key format" )
164
- } else {
165
- // Generate a new random key
166
- let secret_key = SecretKey :: generate ( & mut thread_rng ( ) ) ;
167
- println ! (
168
- "Generated new secret key: {}" ,
169
- hex:: encode( secret_key. to_bytes( ) )
170
- ) ;
171
- println ! ( "To reuse this key, set the IROH_SECRET environment variable to this value" ) ;
172
- Ok ( secret_key)
173
- }
174
- }
0 commit comments