feat: adding a bind shell example with more stuff
adding a bind shell that can allow for more practice with future features such as multiple transports, encryption, transferring files, and a more robust client interface
This commit is contained in:
15
sparse-05/sparse-05-client/Cargo.toml
Normal file
15
sparse-05/sparse-05-client/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "sparse-05-client"
|
||||
version = "0.5.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.75"
|
||||
ed25519-dalek = "1.0.1"
|
||||
rand = "0.7"
|
||||
rmp-serde = "1.1.2"
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" }
|
||||
sparse-05-server = { version = "0.5.0", path = "../sparse-05-server" }
|
||||
structopt = { version = "0.3.26", features = ["paw"] }
|
||||
tokio = { version = "1.32.0", features = ["io-std", "net", "fs", "macros", "rt"] }
|
||||
27
sparse-05/sparse-05-client/src/commands/connect/mod.rs
Normal file
27
sparse-05/sparse-05-client/src/commands/connect/mod.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
|
||||
|
||||
use ed25519_dalek::{Keypair, Signer};
|
||||
use sparse_05_common::messages::CONNECT_MESSAGE;
|
||||
use tokio::{fs, net::UdpSocket};
|
||||
|
||||
use crate::configs::ClientConfig;
|
||||
|
||||
enum State {
|
||||
Ready,
|
||||
UploadingFile,
|
||||
DownloadingFile,
|
||||
}
|
||||
|
||||
pub async fn connect(config: PathBuf, ip: SocketAddr) -> anyhow::Result<()> {
|
||||
let config = fs::read(&config).await?;
|
||||
let config: ClientConfig = rmp_serde::from_slice(&config)?;
|
||||
|
||||
let remote = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
|
||||
|
||||
let connect_signature = config.keypair.sign(CONNECT_MESSAGE).to_bytes();
|
||||
let connect_msg = &[&connect_signature, CONNECT_MESSAGE].concat();
|
||||
|
||||
remote.send_to(connect_msg, ip).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
48
sparse-05/sparse-05-client/src/commands/generate.rs
Normal file
48
sparse-05/sparse-05-client/src/commands/generate.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
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 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?;
|
||||
|
||||
let config = ClientConfig { keypair, port };
|
||||
|
||||
let mut file_part = name.file_name().unwrap().to_owned();
|
||||
file_part.push(OsString::from(".conf"));
|
||||
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(())
|
||||
}
|
||||
2
sparse-05/sparse-05-client/src/commands/mod.rs
Normal file
2
sparse-05/sparse-05-client/src/commands/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod connect;
|
||||
pub mod generate;
|
||||
8
sparse-05/sparse-05-client/src/configs.rs
Normal file
8
sparse-05/sparse-05-client/src/configs.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use ed25519_dalek::Keypair;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ClientConfig {
|
||||
pub keypair: Keypair,
|
||||
pub port: u16,
|
||||
}
|
||||
17
sparse-05/sparse-05-client/src/main.rs
Normal file
17
sparse-05/sparse-05-client/src/main.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use structopt::StructOpt;
|
||||
|
||||
mod commands;
|
||||
mod configs;
|
||||
mod options;
|
||||
|
||||
use options::{Command, Options};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let options = Options::from_args();
|
||||
|
||||
match options.command {
|
||||
Command::Generate { name, port } => commands::generate::generate(name, port).await,
|
||||
Command::Connect { config, ip } => commands::connect::connect(config, ip).await,
|
||||
}
|
||||
}
|
||||
43
sparse-05/sparse-05-client/src/options.rs
Normal file
43
sparse-05/sparse-05-client/src/options.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::{
|
||||
net::{Ipv4Addr, SocketAddr, ToSocketAddrs},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use structopt::{self, StructOpt};
|
||||
|
||||
fn to_socket_addr(src: &str) -> Result<SocketAddr, std::io::Error> {
|
||||
use std::io::{Error, ErrorKind};
|
||||
|
||||
src.to_socket_addrs()?.next().ok_or(Error::new(
|
||||
ErrorKind::Other,
|
||||
"could not get a valid socket address",
|
||||
))
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
pub enum Command {
|
||||
Generate {
|
||||
#[structopt(parse(from_os_str))]
|
||||
name: PathBuf,
|
||||
|
||||
#[structopt(default_value = "54248")]
|
||||
port: u16,
|
||||
},
|
||||
Connect {
|
||||
#[structopt(parse(from_os_str))]
|
||||
config: PathBuf,
|
||||
|
||||
#[structopt(parse(try_from_str = to_socket_addr))]
|
||||
ip: SocketAddr,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(
|
||||
name = "sparse-client",
|
||||
about = "Client to and generator of sparse shells"
|
||||
)]
|
||||
pub struct Options {
|
||||
#[structopt(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
Reference in New Issue
Block a user