Skip to content

Commit

Permalink
Update the server name on verification. Generate keys on startup. Imp…
Browse files Browse the repository at this point in the history
…rove error handling.
  • Loading branch information
Makosai committed Dec 26, 2024
1 parent 31f37a1 commit 96ca9cc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ backup/
debug/
target/

# Security
keys/
**/keys/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
Expand Down
31 changes: 24 additions & 7 deletions rust/cluster/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::sync::mpsc;

use shared::config::cluster::{ read, Settings };
use shared::packets::master::{ FromUnknown, ToUnknown };
use shared::security::aes::{decrypt, load_key};
use shared::security::aes::{decrypt, generate_key, load_key, save_key};
use shared::utils;
use shared::utils::constants::DEFAULT_IP;

Expand Down Expand Up @@ -42,8 +42,25 @@ async fn start() {
master_port,
domain_pub_key: _,
} = read();
info(&server_name);
let key = load_key(key_name.as_str()).expect("Failed to load the key.");
let key = match load_key(key_name.as_str()) {
Ok(key) => key,
Err(_) => {
if std::fs::DirBuilder::new().recursive(true).create("keys").is_err() {
error("Failed to create the 'keys' directory.");
panic!();
}

let key = generate_key();
if save_key(key_name.as_str(), key).is_err() {
error("Failed to save the generated key.");
panic!();
}

warning(format!("A new AES key at 'keys/{key_name}' has been generated and saved. Make sure the Master Server also has this key for authentication.").as_str());

key
}
};

let (tx, mut rx) = mpsc::channel::<Box<[u8]>>(10);
let tx_clone = tx.clone();
Expand Down Expand Up @@ -83,6 +100,9 @@ async fn start() {

data.push(decrypted_passphrase.len() as u8);
data.extend_from_slice(&decrypted_passphrase);
data.push(server_name.len() as u8);
data.extend_from_slice(&server_name.as_bytes());

send_data(&tx_clone, data.into_boxed_slice()).await;
}
x if x == ToUnknown::CreateCluster as u8 => {
Expand All @@ -105,14 +125,11 @@ async fn start() {
}
});

// Should send the server name with the passphrase as 2 separate strings but 1 packet.
println!("Need to send {} and {} to Master Server.", server_name, key_name);
let command = FromUnknown::BecomeCluster as u8;
let key_name = b"cluster_key";

let mut data = [command].to_vec();
data.push(key_name.len() as u8);
data.extend_from_slice(key_name);
data.extend_from_slice(key_name.as_bytes());

let data = data.into_boxed_slice();
send_data(&tx, data).await;
Expand Down
26 changes: 25 additions & 1 deletion rust/master/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,36 @@ impl ServerClient {
};

let name = name.read().await;
if (*name).is_none() || passphrase != *name.as_ref().unwrap() {
if (*name).is_none() || passphrase != *name.as_ref().expect("Failed to get saved passphrase.") {
error("The passphrase doesn't match the name.");
continue;
} else {
success(format!("The passphrase matches the name: {:?} is {}", *name, passphrase).as_str());
}
}

{
// Read their new name they sent.
let len = reader.read_u8().await.unwrap() as usize;
let mut server_name = vec![0u8; len];
match reader.read_exact(&mut server_name).await {
Ok(_) => {},
Err(e) => {
error(format!("Failed to read the server name to String: {:?}", e).as_str());
continue;
}
};

let server_name = match String::from_utf8(server_name) {
Ok(server_name) => server_name,
Err(e) => {
error(format!("Failed to convert server name to String: {:?}", e).as_str());
continue;
}
};
*name.write().await = Some(server_name);
}

Self::send_data(&tx, Box::new([ToUnknown::CreateCluster as u8])).await;

success("We did it! We got an answer back from the cluster.");
Expand Down

0 comments on commit 96ca9cc

Please sign in to comment.