From 6b3f466b3cb1ccccc2168514ea198bf5224532ea Mon Sep 17 00:00:00 2001 From: 17ms <79069176+17ms@users.noreply.github.com> Date: Mon, 22 May 2023 04:54:49 +0300 Subject: [PATCH] arg groups & formatting --- src/cli.rs | 0 src/common.rs | 6 ++++-- src/comms.rs | 6 ++++-- src/connector.rs | 10 ++++++---- src/crypto.rs | 11 ++++++----- src/lib.rs | 3 ++- src/listener.rs | 10 ++++++---- src/main.rs | 39 +++++++++++++++++++++++++++++++-------- src/ui.rs | 17 ----------------- src/util.rs | 23 +++++++++++++++++++++++ 10 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 src/cli.rs delete mode 100644 src/ui.rs create mode 100644 src/util.rs diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/common.rs b/src/common.rs index fc98596..ee85112 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,7 +1,7 @@ -use crate::crypto; +use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf}; + use aes_gcm::{aead::consts::U12, aes::Aes256, AesGcm}; use rand::rngs::OsRng; -use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf}; use tokio::{ io::{BufReader, BufWriter}, net::{ @@ -10,6 +10,8 @@ use tokio::{ }, }; +use crate::crypto; + const PUBLIC_IPV4: &str = "https://ipinfo.io/ip"; const PUBLIC_IPV6: &str = "https://ipv6.icanhazip.com"; diff --git a/src/comms.rs b/src/comms.rs index b772890..407f14c 100755 --- a/src/comms.rs +++ b/src/comms.rs @@ -1,13 +1,15 @@ -use crate::crypto; +use std::error::Error; + use aes_gcm::{aead::consts::U12, aes::Aes256, AesGcm}; use base64::{engine::general_purpose, Engine}; use rand::rngs::OsRng; -use std::error::Error; use tokio::{ io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, net::tcp::{ReadHalf, WriteHalf}, }; +use crate::crypto; + pub async fn send( writer: &mut BufWriter>, cipher: Option<&mut AesGcm>, diff --git a/src/connector.rs b/src/connector.rs index 20d56f2..271c86e 100755 --- a/src/connector.rs +++ b/src/connector.rs @@ -1,8 +1,5 @@ -use crate::{ - common::{Connection, Message}, - comms, crypto, -}; use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf}; + use tokio::{ fs::File, io::{AsyncWriteExt, BufWriter}, @@ -10,6 +7,11 @@ use tokio::{ sync::mpsc, }; +use crate::{ + common::{Connection, Message}, + comms, crypto, +}; + #[derive(Debug)] pub struct Request { pub name: String, diff --git a/src/crypto.rs b/src/crypto.rs index c662cfb..2ac9592 100755 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,18 +1,19 @@ -use super::comms; +use std::{error::Error, path::Path}; + use aes_gcm::{ - aead::{consts::U12, AeadMut}, + aead::{consts::U12, Aead}, aes::Aes256, Aes256Gcm, AesGcm, KeyInit, Nonce, }; -use rand::{distributions::Alphanumeric, Rng}; -use rand::{rngs::OsRng, RngCore}; -use std::{error::Error, path::Path}; +use rand::{distributions::Alphanumeric, rngs::OsRng, Rng, RngCore}; use tokio::{ io::{BufReader, BufWriter}, net::tcp::{ReadHalf, WriteHalf}, }; use x25519_dalek::{EphemeralSecret, PublicKey, SharedSecret}; +use crate::comms; + const AES_NONCE_SIZE: usize = 12; const DH_PBK_SIZE: usize = 32; diff --git a/src/lib.rs b/src/lib.rs index 68ad603..ac229c8 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ +pub mod cli; pub mod common; pub mod comms; pub mod connector; pub mod crypto; pub mod listener; pub mod parsers; -pub mod ui; +pub mod util; diff --git a/src/listener.rs b/src/listener.rs index f3edcef..49d1366 100755 --- a/src/listener.rs +++ b/src/listener.rs @@ -1,8 +1,5 @@ -use crate::{ - common::{Connection, Message}, - comms, crypto, -}; use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf, sync::Arc}; + use tokio::{ fs::File, io::AsyncReadExt, @@ -10,6 +7,11 @@ use tokio::{ sync::mpsc, }; +use crate::{ + common::{Connection, Message}, + comms, crypto, +}; + #[derive(Debug, Clone)] pub struct Listener { host_addr: SocketAddr, diff --git a/src/main.rs b/src/main.rs index 8e13dbc..0da85a6 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ -use clap::{Parser, Subcommand}; -use contego::parsers::{addr_parser, dirpath_parser, filepath_parser}; use std::{error::Error, net::SocketAddr, path::PathBuf}; +use clap::{command, ArgGroup, Parser, Subcommand}; + +use contego::parsers::{addr_parser, dirpath_parser, filepath_parser}; + #[derive(Debug, Parser)] #[command(about, version)] struct Cli { @@ -15,25 +17,35 @@ struct Cli { #[derive(Debug, Subcommand)] enum Commands { /// Host fileserver instance by providing JSON file with paths or list of paths + #[clap(group(ArgGroup::new("input").required(true).args(&["infile", "files"])))] Host { + /// Host port + #[clap(short = 'p', long, default_value_t = 8080)] + port: u16, /// Use IPv6 instead of IPv4 #[clap(short = '6', long, default_value_t = false)] ipv6: bool, /// Path to the inputfile - #[clap(short = 'i', long, value_parser = filepath_parser)] + #[clap(short = 'i', long, value_parser = filepath_parser, conflicts_with = "files", group = "input")] infile: Option, - /// Paths to the files (comma separated) - #[clap(short = 'f', long, num_args = 1.., value_parser = filepath_parser)] + /// Paths to the files + #[clap(short = 'f', long, num_args = 1.., value_parser = filepath_parser, conflicts_with = "infile", group = "input")] files: Option>, + /// Outgoing traffic chunksize in bytes + #[clap(short = 'c', long, default_value_t = 8192)] + chunksize: usize, + /// Host the files locally + #[clap(short = 'l', long, default_value_t = false)] + local: bool, }, /// Connect to hosted server by providing address, output folder and access key Connect { /// IP address of the server (IPv4 or IPv6) #[clap(short = 'a', long, value_parser = addr_parser)] - address: SocketAddr, + addr: SocketAddr, /// Path to the output folder #[clap(short = 'o', long, value_parser = dirpath_parser)] - outdir: PathBuf, + out: PathBuf, /// Access key for the fileserver #[clap(short = 'k', long)] key: String, @@ -43,7 +55,18 @@ enum Commands { #[tokio::main] async fn main() -> Result<(), Box> { let cli = Cli::parse(); - println!("{:?}", cli); + + match cli.command { + Commands::Host { + port, + ipv6, + infile, + files, + chunksize, + local, + } => {} + Commands::Connect { addr, out, key } => {} + }; Ok(()) } diff --git a/src/ui.rs b/src/ui.rs deleted file mode 100644 index 83e9ea4..0000000 --- a/src/ui.rs +++ /dev/null @@ -1,17 +0,0 @@ -/* -TODO: - -- suspend everything except errors and critical exceptions that lead to panic or abort - -server: -- ip & access key on display -- commands to copy ip/key to clipboard -- updating list/log of connecting/disconnecting clients (?) - -client: -- connection ip on display -- comamnds to download one or more files -- updating list of available/downloading/downloaded files (?) - -* (?): maybe requires a separate UI thread for updates -*/ diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..761c41c --- /dev/null +++ b/src/util.rs @@ -0,0 +1,23 @@ +use std::{error::Error, fs, path::PathBuf}; + +fn get_filepaths( + infile: Option, + files: Option>, +) -> Result, Box> { + let mut filepaths = Vec::new(); + + if let Some(infile) = infile { + let paths = fs::read_to_string(infile)?; + for path in paths.lines() { + filepaths.push(PathBuf::from(path)); + } + } + + if let Some(files) = files { + for file in files { + filepaths.push(file); + } + } + + Ok(filepaths) +}