Andrew Rioux 4449a771e2
feat: added connection and authentication
client can now generate a server binary, and try to connect to it and
get capabilities
2023-09-02 22:29:13 -04:00

56 lines
1.6 KiB
Rust

use std::{ffi::OsString, path::PathBuf};
use ed25519_dalek::Keypair;
use sparse_05_common::CONFIG_SEPARATOR;
use tokio::{fs, io::AsyncWriteExt};
use crate::configs::ClientConfig;
#[cfg(debug_assertions)]
pub const SPARSE_SERVER_BINARY: &'static [u8] =
include_bytes!("../../../../target/debug/sparse-05-server");
#[cfg(not(debug_assertions))]
pub const SPARSE_SERVER_BINARY: &'static [u8] =
include_bytes!("../../../../target/release/sparse-05-server");
pub async fn generate(mut name: PathBuf, port: u16) -> anyhow::Result<()> {
let mut csprng = rand::thread_rng();
let keypair = Keypair::generate(&mut csprng);
let (enc_privkey, enc_pubkey) = ecies_ed25519::generate_keypair(&mut csprng);
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.mode(0o755)
.open(&name)
.await?;
file.write_all(SPARSE_SERVER_BINARY).await?;
file.write_all(CONFIG_SEPARATOR).await?;
file.write_all(&port.to_be_bytes()[..]).await?;
file.write_all(keypair.public.as_bytes()).await?;
file.write_all(enc_pubkey.as_bytes()).await?;
let config = ClientConfig {
keypair,
enc_privkey,
enc_pubkey,
port,
};
let mut file_part = name.file_name().unwrap().to_owned();
file_part.push(OsString::from(".scon"));
name.pop();
name.push(file_part);
let mut file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&name)
.await?;
let config = rmp_serde::to_vec(&config)?;
file.write_all(&config).await?;
Ok(())
}