diff --git a/src/common.rs b/src/common.rs deleted file mode 100644 index ee85112..0000000 --- a/src/common.rs +++ /dev/null @@ -1,74 +0,0 @@ -use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf}; - -use aes_gcm::{aead::consts::U12, aes::Aes256, AesGcm}; -use rand::rngs::OsRng; -use tokio::{ - io::{BufReader, BufWriter}, - net::{ - tcp::{ReadHalf, WriteHalf}, - TcpStream, - }, -}; - -use crate::crypto; - -const PUBLIC_IPV4: &str = "https://ipinfo.io/ip"; -const PUBLIC_IPV6: &str = "https://ipv6.icanhazip.com"; - -#[derive(Debug, PartialEq, Eq)] -pub enum Message { - Error(String), - Files(Vec), - Metadata(HashMap), - ClientConnect(SocketAddr), - ClientDisconnect(SocketAddr), - ClientReq(String), - ConnectionReady, - Shutdown, -} - -pub struct Connection<'a> { - pub reader: BufReader>, - pub writer: BufWriter>, - pub cipher: AesGcm, - pub rng: OsRng, -} - -impl<'a> Connection<'a> { - pub async fn new( - socket: &'a mut TcpStream, - ) -> Result, Box> { - let (reader, writer) = socket.split(); - let mut reader = BufReader::new(reader); - let mut writer = BufWriter::new(writer); - let cipher = crypto::aes_cipher(&mut reader, &mut writer, true).await?; - let rng = OsRng; - - Ok(Self { - reader, - writer, - cipher, - rng, - }) - } -} - -#[derive(PartialEq, Eq)] -pub enum Ip { - V4, - V6, -} - -impl Ip { - pub fn fetch(self) -> Result> { - let addr = match self { - Ip::V4 => PUBLIC_IPV4, - Ip::V6 => PUBLIC_IPV6, - }; - - let res = ureq::get(addr).call()?.into_string()?; - let addr: SocketAddr = res.trim().parse()?; - - Ok(addr) - } -} diff --git a/src/lib.rs b/src/lib.rs index 9128f46..6853227 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,7 @@ pub mod cli; -pub mod common; pub mod connector; pub mod crypto; pub mod listener; -pub mod parsers; +pub mod parser; pub mod sockets; pub mod util; diff --git a/src/parsers.rs b/src/parser.rs similarity index 100% rename from src/parsers.rs rename to src/parser.rs diff --git a/src/util.rs b/src/util.rs index 761c41c..16abb5b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,34 @@ -use std::{error::Error, fs, path::PathBuf}; +use std::{error::Error, fs, net::SocketAddr, path::PathBuf}; -fn get_filepaths( +const PUBLIC_IPV4: &str = "https://ipinfo.io/ip"; +const PUBLIC_IPV6: &str = "https://ipv6.icanhazip.com"; + +#[derive(PartialEq, Eq)] +pub enum Ip { + V4, + V6, + Local, +} + +impl Ip { + pub fn fetch(self, port: u16) -> Result> { + let addr = match self { + Ip::V4 => PUBLIC_IPV4, + Ip::V6 => PUBLIC_IPV6, + Local => { + let addr_str = format!("127.0.0.1:{}", port); + return addr_str.parse::(); + } + }; + + let res = format!("{}:{}", ureq::get(addr).call()?.into_string()?.trim(), port); + let addr = res.parse::()?; + + Ok(addr) + } +} + +fn filepaths( infile: Option, files: Option>, ) -> Result, Box> {