Skip to content

Commit 8240aea

Browse files
feat: add option to supply fixed secret key to the matchmaker (#406)
Key can be passed in via `BONES_MATCHMAKER_SECRET_KEY` To show the secret key when generating, you can pass `--print-secret-key` to the binary now Closes #405
1 parent 3cb6a88 commit 8240aea

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

other_crates/bones_matchmaker/src/cli.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
use clap::Parser;
22
use tracing::metadata::LevelFilter;
3+
use tracing::warn;
34

45
pub async fn start() {
56
configure_logging();
67

78
let args = crate::Config::parse();
9+
let secret_key = match std::env::var("BONES_MATCHMAKER_SECRET_KEY") {
10+
Ok(key) => match key.parse::<iroh_net::key::SecretKey>() {
11+
Ok(key) => Some(key),
12+
Err(_) => {
13+
warn!("invalid matchmaker key provided");
14+
None
15+
}
16+
},
17+
Err(_) => None,
18+
};
819

9-
if let Err(e) = super::server(args).await {
20+
if let Err(e) = super::server(args, secret_key).await {
1021
eprintln!("Error: {e}");
1122
}
1223
}

other_crates/bones_matchmaker/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern crate tracing;
88
use std::net::SocketAddr;
99

1010
use bones_matchmaker_proto::MATCH_ALPN;
11+
use iroh_net::key::SecretKey;
1112

1213
pub mod cli;
1314

@@ -19,12 +20,29 @@ struct Config {
1920
/// The server address to listen on
2021
#[clap(short, long = "listen", default_value = "0.0.0.0:8943")]
2122
listen_addr: SocketAddr,
23+
/// If enabled, prints the current secret key. Use with caution.
24+
#[clap(long)]
25+
print_secret_key: bool,
2226
}
2327

24-
async fn server(args: Config) -> anyhow::Result<()> {
28+
async fn server(args: Config, secret_key: Option<SecretKey>) -> anyhow::Result<()> {
2529
let port = args.listen_addr.port();
2630

27-
let secret_key = iroh_net::key::SecretKey::generate();
31+
match secret_key {
32+
Some(ref key) => {
33+
info!("Using existing key: {}", key.public());
34+
}
35+
None => {
36+
info!("Generating new key");
37+
}
38+
}
39+
40+
let secret_key = secret_key.unwrap_or_else(SecretKey::generate);
41+
42+
if args.print_secret_key {
43+
println!("Secret Key: {}", secret_key);
44+
}
45+
2846
let endpoint = iroh_net::MagicEndpoint::builder()
2947
.alpns(vec![MATCH_ALPN.to_vec()])
3048
.discovery(Box::new(

0 commit comments

Comments
 (0)