Skip to content

Commit

Permalink
feat: add option to supply fixed secret key to the matchmaker (#406)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dignifiedquire committed May 18, 2024
1 parent 3cb6a88 commit 8240aea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
13 changes: 12 additions & 1 deletion other_crates/bones_matchmaker/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use clap::Parser;
use tracing::metadata::LevelFilter;
use tracing::warn;

pub async fn start() {
configure_logging();

let args = crate::Config::parse();
let secret_key = match std::env::var("BONES_MATCHMAKER_SECRET_KEY") {
Ok(key) => match key.parse::<iroh_net::key::SecretKey>() {
Ok(key) => Some(key),
Err(_) => {
warn!("invalid matchmaker key provided");
None
}
},
Err(_) => None,
};

if let Err(e) = super::server(args).await {
if let Err(e) = super::server(args, secret_key).await {
eprintln!("Error: {e}");
}
}
Expand Down
22 changes: 20 additions & 2 deletions other_crates/bones_matchmaker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate tracing;
use std::net::SocketAddr;

use bones_matchmaker_proto::MATCH_ALPN;
use iroh_net::key::SecretKey;

pub mod cli;

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

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

let secret_key = iroh_net::key::SecretKey::generate();
match secret_key {
Some(ref key) => {
info!("Using existing key: {}", key.public());
}
None => {
info!("Generating new key");
}
}

let secret_key = secret_key.unwrap_or_else(SecretKey::generate);

if args.print_secret_key {
println!("Secret Key: {}", secret_key);
}

let endpoint = iroh_net::MagicEndpoint::builder()
.alpns(vec![MATCH_ALPN.to_vec()])
.discovery(Box::new(
Expand Down

0 comments on commit 8240aea

Please sign in to comment.