clean up, format, start adding an install subcmd
							parent
							
								
									319d8e6d29
								
							
						
					
					
						commit
						b3a576e13d
					
				|  | @ -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<ProjectDirs> { | ||||
| pub fn try_project_dir() -> Option<ProjectDirs> { | ||||
|     ProjectDirs::from("", "ReactorScram", "LookAround") | ||||
| } | ||||
| 
 | ||||
| pub fn try_config_dir() -> Option<PathBuf> { | ||||
|     Some(try_project_dir()?.config_local_dir().into()) | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, thiserror::Error)] | ||||
| pub enum AppError { | ||||
|     #[error(transparent)] | ||||
|  |  | |||
|  | @ -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]] | ||||
| } | ||||
|  | @ -165,9 +165,9 @@ fn configure_client<I: Iterator<Item = String>>(mut args: I) -> Result<ClientPar | |||
| fn load_config_file() -> ConfigFile { | ||||
|     let mut nicknames: HashMap<String, String> = 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") { | ||||
|  |  | |||
							
								
								
									
										41
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										41
									
								
								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<String> = 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 <nickname>"); | ||||
|                 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."); | ||||
|  |  | |||
|  | @ -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, | ||||
|                 ], | ||||
|             ), | ||||
|  |  | |||
|  | @ -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, | ||||
|  |  | |||
|  | @ -44,9 +44,9 @@ fn configure<I: Iterator<Item = String>>(mut args: I) -> Result<Params, AppError | |||
|     let mut bind_addrs = vec![]; | ||||
|     let mut nickname = String::new(); | ||||
| 
 | ||||
|     if let Some(proj_dirs) = find_project_dirs() { | ||||
|     if let Some(dir) = app_common::try_config_dir() { | ||||
|         let path = dir.join("server.ini"); | ||||
|         let mut ini = Ini::new_cs(); | ||||
|         let path = proj_dirs.config_local_dir().join("server.ini"); | ||||
|         if ini.load(&path).is_ok() { | ||||
|             if let Some(x) = ini.get("server", "nickname") { | ||||
|                 nickname = x; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 _
						_