From b3a576e13d18b1f654641cb57aa059f758040ae3 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 21 Mar 2025 12:42:03 -0500 Subject: [PATCH] clean up, format, start adding an install subcmd --- src/app_common.rs | 9 ++++++--- src/avalanche.rs | 26 -------------------------- src/client.rs | 4 ++-- src/main.rs | 41 ++++++++++++++++++++++++++--------------- src/message.rs | 16 ++++++++-------- src/prelude.rs | 6 ++---- src/server.rs | 4 ++-- 7 files changed, 46 insertions(+), 60 deletions(-) delete mode 100644 src/avalanche.rs diff --git a/src/app_common.rs b/src/app_common.rs index 14ebd17..c2dd2fc 100644 --- a/src/app_common.rs +++ b/src/app_common.rs @@ -1,11 +1,14 @@ use crate::prelude::*; +use directories::ProjectDirs; -pub const LOOKAROUND_VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub fn find_project_dirs() -> Option { +pub fn try_project_dir() -> Option { ProjectDirs::from("", "ReactorScram", "LookAround") } +pub fn try_config_dir() -> Option { + Some(try_project_dir()?.config_local_dir().into()) +} + #[derive(Debug, thiserror::Error)] pub enum AppError { #[error(transparent)] diff --git a/src/avalanche.rs b/src/avalanche.rs deleted file mode 100644 index bba3541..0000000 --- a/src/avalanche.rs +++ /dev/null @@ -1,26 +0,0 @@ -type Mac = [u8; 6]; - -pub fn debug() { - for input in [ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1], - [1, 0, 0, 0, 0, 0], - [1, 0, 0, 0, 0, 1], - ] { - assert_eq!(unmix(mix(input)), input); - } - - println!("Passed"); -} - -// NOT intended for any cryptography or security. This is TRIVIALLY reversible. -// It's just to make it easier for humans to tell apart MACs where only a couple -// numbers differ. - -fn mix(i: Mac) -> Mac { - [i[0] ^ i[5], i[1] ^ i[4], i[2] ^ i[3], i[3], i[4], i[5]] -} - -fn unmix(i: Mac) -> Mac { - [i[0] ^ i[5], i[1] ^ i[4], i[2] ^ i[3], i[3], i[4], i[5]] -} diff --git a/src/client.rs b/src/client.rs index f04120f..5770cd7 100644 --- a/src/client.rs +++ b/src/client.rs @@ -165,9 +165,9 @@ fn configure_client>(mut args: I) -> Result ConfigFile { let mut nicknames: HashMap = Default::default(); - if let Some(proj_dirs) = find_project_dirs() { + if let Some(dir) = app_common::try_config_dir() { + let path = dir.join("client.ini"); let mut ini = Ini::new_cs(); - let path = proj_dirs.config_local_dir().join("client.ini"); if ini.load(&path).is_ok() { let map_ref = ini.get_map_ref(); if let Some(x) = map_ref.get("nicknames") { diff --git a/src/main.rs b/src/main.rs index c771536..28f109c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ use prelude::*; pub mod app_common; -mod avalanche; mod client; +mod install; mod ip; pub mod message; mod prelude; @@ -25,25 +25,36 @@ async fn async_main() -> Result<(), AppError> { let _exe_name = args.next(); - let subcommand: Option = args.next(); + let Some(subcommand) = args.next() else { + return Err(CliArgError::MissingSubcommand.into()); + }; - match subcommand.as_ref().map(|x| &x[..]) { - None => return Err(CliArgError::MissingSubcommand.into()), - Some("--version") => println!("lookaround v{}", LOOKAROUND_VERSION), - Some("client") => client::client(args).await?, - Some("config") => config(), - Some("debug-avalanche") => avalanche::debug(), - Some("find-nick") => client::find_nick(args).await?, - Some("my-ips") => my_ips()?, - Some("server") => server::server(args).await?, - Some(x) => return Err(CliArgError::UnknownSubcommand(x.to_string()).into()), + match subcommand.as_ref() { + "--version" => { + println!("lookaround v{}", env!("CARGO_PKG_VERSION")); + Ok(()) + } + "client" => client::client(args).await, + "config" => { + config(); + Ok(()) + } + "find-nick" => client::find_nick(args).await, + "install" => { + let Some(nickname) = args.next() else { + eprintln!("Usage: lookaround install "); + return Err(CliArgError::MissingArgumentValue("install".into()).into()); + }; + install::main(&nickname) + } + "my-ips" => my_ips(), + "server" => server::server(args).await, + x => Err(CliArgError::UnknownSubcommand(x.to_string()).into()), } - - Ok(()) } fn config() { - if let Some(proj_dirs) = ProjectDirs::from("", "ReactorScram", "LookAround") { + if let Some(proj_dirs) = directories::ProjectDirs::from("", "ReactorScram", "LookAround") { println!("Using config dir {:?}", proj_dirs.config_local_dir()); } else { println!("Can't detect config dir."); diff --git a/src/message.rs b/src/message.rs index cfc6064..b4a54cc 100644 --- a/src/message.rs +++ b/src/message.rs @@ -198,7 +198,7 @@ mod test { }], vec![ 154, 74, 67, 129, // Request tag - 1, // Idem ID + 1, // Idem ID 1, 2, 3, 4, 5, 6, 7, 8, // MAC is None 0, ], @@ -214,10 +214,10 @@ mod test { vec![ // Magic number for LookAround packets 154, 74, 67, 129, // Response1 tag - 2, // MAC is Some - 1, // MAC + 2, // MAC is Some + 1, // MAC 17, 34, 51, 68, 85, 102, // Response2 tag - 3, // Length prefix + 3, // Length prefix 14, 0, 0, 0, // Idem ID 1, 2, 3, 4, 5, 6, 7, 8, // Length-prefixed string 2, 0, 0, 0, 58, 86, @@ -241,7 +241,7 @@ mod test { }, vec![ 154, 74, 67, 129, // Request tag - 1, // Idem ID + 1, // Idem ID 1, 2, 3, 4, 5, 6, 7, 8, // MAC is None 0, ], @@ -251,8 +251,8 @@ mod test { vec![ // Magic number for LookAround packets 154, 74, 67, 129, // Response tag - 2, // MAC is Some - 1, // MAC + 2, // MAC is Some + 1, // MAC 17, 34, 51, 68, 85, 102, ], ), @@ -261,7 +261,7 @@ mod test { vec![ // Magic number for LookAround packets 154, 74, 67, 129, // Response tag - 2, // MAC is None + 2, // MAC is None 0, ], ), diff --git a/src/prelude.rs b/src/prelude.rs index 7fe255c..2f8effa 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -3,13 +3,13 @@ pub use std::{ env, io::{Cursor, Write}, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, + path::PathBuf, str::FromStr, sync::Arc, time::Duration, }; pub use configparser::ini::Ini; -pub use directories::ProjectDirs; pub use mac_address::{MacAddress, get_mac_address}; pub use rand::RngCore; pub use tokio::{ @@ -18,9 +18,7 @@ pub use tokio::{ }; pub use crate::{ - app_common::{ - self, AppError, CliArgError, LOOKAROUND_VERSION, find_project_dirs, recv_msg_from, - }, + app_common::{self, AppError, CliArgError, recv_msg_from}, ip::get_ips, message::{self, Message, PACKET_SIZE}, tlv, diff --git a/src/server.rs b/src/server.rs index 179943b..0d458e2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -44,9 +44,9 @@ fn configure>(mut args: I) -> Result