factored out the packet parsing logic from libpcap will probably come back to linking against libpcap in a later version
79 lines
2.3 KiB
Rust
79 lines
2.3 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, options::TargetOs};
|
|
|
|
#[cfg(debug_assertions)]
|
|
pub const SPARSE_LINUX_SERVER_BINARY: &'static [u8] =
|
|
include_bytes!("../../../../target/debug/sparse-05-server");
|
|
#[cfg(not(debug_assertions))]
|
|
pub const SPARSE_LINUX_SERVER_BINARY: &'static [u8] =
|
|
include_bytes!("../../../../target/release/sparse-05-server");
|
|
#[cfg(debug_assertions)]
|
|
pub const SPARSE_WINDOWS_SERVER_BINARY: &'static [u8] =
|
|
include_bytes!("../../../../target/x86_64-pc-windows-gnu/debug/sparse-05-server.exe");
|
|
#[cfg(not(debug_assertions))]
|
|
pub const SPARSE_WINDOWS_SERVER_BINARY: &'static [u8] =
|
|
include_bytes!("../../../../target/x86_64-pc-windows-gnu/release/sparse-05-server.exe");
|
|
|
|
pub async fn generate(mut name: PathBuf, port: u16, target: TargetOs) -> 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();
|
|
file.write(true).create(true);
|
|
#[cfg(unix)]
|
|
file.mode(0o755);
|
|
|
|
#[cfg(windows)]
|
|
let old_file_part = name.file_name().unwrap().to_owned();
|
|
|
|
#[cfg(windows)]
|
|
{
|
|
let mut file_part = name.file_name().unwrap().to_owned();
|
|
file_part.push(OsString::from(".exe"));
|
|
name.pop();
|
|
name.push(file_part);
|
|
}
|
|
|
|
let mut file = file.open(&name).await?;
|
|
|
|
#[cfg(windows)]
|
|
{
|
|
name.pop();
|
|
name.push(old_file_part);
|
|
}
|
|
|
|
file.write_all(SPARSE_LINUX_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(())
|
|
}
|