Close #664: Add subcommand to generate keys

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2025-09-19 13:08:02 +02:00
parent 4d6cedba07
commit 3e6565b2a9
3 changed files with 40 additions and 0 deletions
+2
View File
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
always disabled.
- Added auto discovery of Socks5 proxies on the overlay, and the ability to proxy
local Socks5 connections to a chosen (manual or automatic) remote.
- New `generate-keys` subcommand which generates the key file without running a
daemon. It can also be used to generate fresh keys, should that be needed.
### Changed
+19
View File
@@ -126,6 +126,14 @@ pub enum Command {
key: Option<String>,
},
/// Generate a set of new keys for the system at the default path, or the path provided by the
/// --key-file parameter
GenerateKeys {
/// Force generating new keys, removing any existing key in the process
#[arg(long = "force")]
force: bool,
},
/// Actions on the message subsystem
Message {
#[command(subcommand)]
@@ -697,6 +705,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
Command::GenerateKeys { force } => {
let node_keys = get_node_keys(&key_path).await?;
if node_keys.is_none() || force {
info!(?key_path, "Generating new node keys");
let secret_key = crypto::SecretKey::new();
save_key_file(&secret_key, &key_path).await?;
} else {
warn!(?key_path, "Refusing to generate new keys as key file already exists, use `--force` to generate them anyway");
}
}
Command::Message { command } => match command {
MessageCommand::Send {
wait,
@@ -829,6 +847,7 @@ where
Ok(T::from(secret_bytes))
}
/// Save a key to a file at the given path. If the file already exists, it will be overwritten.
async fn save_key_file(key: &crypto::SecretKey, path: &Path) -> io::Result<()> {
#[cfg(target_family = "unix")]
{
+19
View File
@@ -126,6 +126,14 @@ pub enum Command {
key: Option<String>,
},
/// Generate a set of new keys for the system at the default path, or the path provided by the
/// --key-file parameter
GenerateKeys {
/// Force generating new keys, removing any existing key in the process
#[arg(long = "force")]
force: bool,
},
/// Actions on the message subsystem
Message {
#[command(subcommand)]
@@ -664,6 +672,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
Command::GenerateKeys { force } => {
let node_keys = get_node_keys(&key_path).await?;
if node_keys.is_none() || force {
info!(?key_path, "Generating new node keys");
let secret_key = crypto::SecretKey::new();
save_key_file(&secret_key, &key_path).await?;
} else {
warn!(?key_path, "Refusing to generate new keys as key file already exists, use `--force` to generate them anyway");
}
}
Command::Message { command } => match command {
MessageCommand::Send {
wait,
@@ -796,6 +814,7 @@ where
Ok(T::from(secret_bytes))
}
/// Save a key to a file at the given path. If the file already exists, it will be overwritten.
async fn save_key_file(key: &crypto::SecretKey, path: &Path) -> io::Result<()> {
#[cfg(target_family = "unix")]
{