feat: added windows support

factored out the packet parsing logic from libpcap

will probably come back to linking against libpcap in a later version
This commit is contained in:
Andrew Rioux
2023-09-02 23:09:05 -04:00
parent 4449a771e2
commit 81fb2ed548
19 changed files with 196 additions and 30 deletions

View File

@@ -4,28 +4,51 @@ use ed25519_dalek::Keypair;
use sparse_05_common::CONFIG_SEPARATOR;
use tokio::{fs, io::AsyncWriteExt};
use crate::configs::ClientConfig;
use crate::{configs::ClientConfig, options::TargetOs};
#[cfg(debug_assertions)]
pub const SPARSE_SERVER_BINARY: &'static [u8] =
pub const SPARSE_LINUX_SERVER_BINARY: &'static [u8] =
include_bytes!("../../../../target/debug/sparse-05-server");
#[cfg(not(debug_assertions))]
pub const SPARSE_SERVER_BINARY: &'static [u8] =
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) -> anyhow::Result<()> {
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()
.write(true)
.create(true)
.mode(0o755)
.open(&name)
.await?;
let mut file = fs::OpenOptions::new();
file.write(true).create(true);
#[cfg(unix)]
file.mode(0o755);
file.write_all(SPARSE_SERVER_BINARY).await?;
#[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?;

View File

@@ -11,7 +11,9 @@ async fn main() -> anyhow::Result<()> {
let options = Options::from_args();
match options.command {
Command::Generate { name, port } => commands::generate::generate(name, port).await,
Command::Generate { name, port, target } => {
commands::generate::generate(name, port, target).await
}
Command::Connect { config, ip } => commands::connect::connect(config, ip).await,
}
}

View File

@@ -14,14 +14,33 @@ fn to_socket_addr(src: &str) -> Result<SocketAddr, std::io::Error> {
))
}
pub enum TargetOs {
Linux,
Windows,
}
impl std::str::FromStr for TargetOs {
type Err = &'static str;
fn from_str(input: &str) -> Result<Self, Self::Err> {
match input {
"linux" => Ok(Self::Linux),
"windows" => Ok(Self::Windows),
_ => Err("could not parse target operating system"),
}
}
}
#[derive(StructOpt)]
pub enum Command {
Generate {
#[structopt(parse(from_os_str))]
name: PathBuf,
#[structopt(default_value = "54248")]
#[structopt(long, short, default_value = "54248")]
port: u16,
#[structopt(long, short, default_value = "linux")]
target: TargetOs,
},
Connect {
#[structopt(parse(from_os_str))]