kkv 1.2.1

A Peer-to-Peer Decentralized, Temporary and Anonymous Message System.
Documentation
/**************************************************************************
 *   src/compile_server.rs  --  This file is part of kkv.                 *
 *                                                                        *
 *   Copyright (C) 2025 Mateo Lafalce                                     *
 *                                                                        *
 *   kkv is free software: you can redistribute it and/or modify          *
 *   it under the terms of the GNU General Public License as published    *
 *   by the Free Software Foundation, either version 3 of the License,    *
 *   or (at your option) any later version.                               *
 *                                                                        *
 *   kkv is distributed in the hope that it will be useful,               *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
 *   See the GNU General Public License for more details.                 *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
 *                                                                        *
 **************************************************************************/

use argon2::{
    password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
    Argon2,
};
use clap::Parser;
use colored::*;
use rpassword::read_password;
use std::io::Error;

pub const MAX_MSG: usize = 50;
const COST_POW: u32 = 30;
const OPEN_GATEWAY_IN_SEG: u64 = 480; // 8 minutes

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// The maximum number of messages you want to store
    #[arg(short, long)]
    max_messages: Option<usize>,
    /// The cost of the Proof of Work that will be sent to the client
    #[arg(short, long)]
    cost_pow: Option<u32>,
    /// The expected time to solve the proof of work in seconds
    #[arg(short, long)]
    expected_time: Option<u64>,
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let args = Args::parse();
    let max_messages: usize = match args.max_messages {
        Some(max_messages) => max_messages,
        None => MAX_MSG,
    };
    let cost_pow: u32 = match args.cost_pow {
        Some(cost_pow) => cost_pow,
        None => COST_POW,
    };
    let expected_time: u64 = match args.expected_time {
        Some(expected_time) => expected_time,
        None => OPEN_GATEWAY_IN_SEG,
    };
    println!("Enter the server password (secure as posible & change it every time)");
    let password = read_password().unwrap().into_bytes();

    let salt = SaltString::generate(&mut OsRng);
    let argon2 = Argon2::default();
    let password_hash = argon2.hash_password(&password, &salt).unwrap().to_string();

    #[allow(clippy::unit_arg)]
    let _ = match kkv::server::server(max_messages, cost_pow, expected_time, password_hash).await {
        Ok(_) => Ok(()),
        Err(e) => Err(println!("{}", e.to_string().red())),
    };

    Ok(())
}