clean up, format, start adding an install subcmd
parent
319d8e6d29
commit
b3a576e13d
|
@ -1,11 +1,14 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
|
||||||
pub const LOOKAROUND_VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub fn try_project_dir() -> Option<ProjectDirs> {
|
||||||
|
|
||||||
pub fn find_project_dirs() -> Option<ProjectDirs> {
|
|
||||||
ProjectDirs::from("", "ReactorScram", "LookAround")
|
ProjectDirs::from("", "ReactorScram", "LookAround")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_config_dir() -> Option<PathBuf> {
|
||||||
|
Some(try_project_dir()?.config_local_dir().into())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum AppError {
|
pub enum AppError {
|
||||||
#[error(transparent)]
|
#[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 {
|
fn load_config_file() -> ConfigFile {
|
||||||
let mut nicknames: HashMap<String, String> = Default::default();
|
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 mut ini = Ini::new_cs();
|
||||||
let path = proj_dirs.config_local_dir().join("client.ini");
|
|
||||||
if ini.load(&path).is_ok() {
|
if ini.load(&path).is_ok() {
|
||||||
let map_ref = ini.get_map_ref();
|
let map_ref = ini.get_map_ref();
|
||||||
if let Some(x) = map_ref.get("nicknames") {
|
if let Some(x) = map_ref.get("nicknames") {
|
||||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -1,8 +1,8 @@
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
pub mod app_common;
|
pub mod app_common;
|
||||||
mod avalanche;
|
|
||||||
mod client;
|
mod client;
|
||||||
|
mod install;
|
||||||
mod ip;
|
mod ip;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
mod prelude;
|
mod prelude;
|
||||||
|
@ -25,25 +25,36 @@ async fn async_main() -> Result<(), AppError> {
|
||||||
|
|
||||||
let _exe_name = args.next();
|
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[..]) {
|
match subcommand.as_ref() {
|
||||||
None => return Err(CliArgError::MissingSubcommand.into()),
|
"--version" => {
|
||||||
Some("--version") => println!("lookaround v{}", LOOKAROUND_VERSION),
|
println!("lookaround v{}", env!("CARGO_PKG_VERSION"));
|
||||||
Some("client") => client::client(args).await?,
|
Ok(())
|
||||||
Some("config") => config(),
|
}
|
||||||
Some("debug-avalanche") => avalanche::debug(),
|
"client" => client::client(args).await,
|
||||||
Some("find-nick") => client::find_nick(args).await?,
|
"config" => {
|
||||||
Some("my-ips") => my_ips()?,
|
config();
|
||||||
Some("server") => server::server(args).await?,
|
Ok(())
|
||||||
Some(x) => return Err(CliArgError::UnknownSubcommand(x.to_string()).into()),
|
}
|
||||||
|
"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() {
|
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());
|
println!("Using config dir {:?}", proj_dirs.config_local_dir());
|
||||||
} else {
|
} else {
|
||||||
println!("Can't detect config dir.");
|
println!("Can't detect config dir.");
|
||||||
|
|
|
@ -198,7 +198,7 @@ mod test {
|
||||||
}],
|
}],
|
||||||
vec![
|
vec![
|
||||||
154, 74, 67, 129, // Request tag
|
154, 74, 67, 129, // Request tag
|
||||||
1, // Idem ID
|
1, // Idem ID
|
||||||
1, 2, 3, 4, 5, 6, 7, 8, // MAC is None
|
1, 2, 3, 4, 5, 6, 7, 8, // MAC is None
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
|
@ -214,10 +214,10 @@ mod test {
|
||||||
vec![
|
vec![
|
||||||
// Magic number for LookAround packets
|
// Magic number for LookAround packets
|
||||||
154, 74, 67, 129, // Response1 tag
|
154, 74, 67, 129, // Response1 tag
|
||||||
2, // MAC is Some
|
2, // MAC is Some
|
||||||
1, // MAC
|
1, // MAC
|
||||||
17, 34, 51, 68, 85, 102, // Response2 tag
|
17, 34, 51, 68, 85, 102, // Response2 tag
|
||||||
3, // Length prefix
|
3, // Length prefix
|
||||||
14, 0, 0, 0, // Idem ID
|
14, 0, 0, 0, // Idem ID
|
||||||
1, 2, 3, 4, 5, 6, 7, 8, // Length-prefixed string
|
1, 2, 3, 4, 5, 6, 7, 8, // Length-prefixed string
|
||||||
2, 0, 0, 0, 58, 86,
|
2, 0, 0, 0, 58, 86,
|
||||||
|
@ -241,7 +241,7 @@ mod test {
|
||||||
},
|
},
|
||||||
vec![
|
vec![
|
||||||
154, 74, 67, 129, // Request tag
|
154, 74, 67, 129, // Request tag
|
||||||
1, // Idem ID
|
1, // Idem ID
|
||||||
1, 2, 3, 4, 5, 6, 7, 8, // MAC is None
|
1, 2, 3, 4, 5, 6, 7, 8, // MAC is None
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
|
@ -251,8 +251,8 @@ mod test {
|
||||||
vec![
|
vec![
|
||||||
// Magic number for LookAround packets
|
// Magic number for LookAround packets
|
||||||
154, 74, 67, 129, // Response tag
|
154, 74, 67, 129, // Response tag
|
||||||
2, // MAC is Some
|
2, // MAC is Some
|
||||||
1, // MAC
|
1, // MAC
|
||||||
17, 34, 51, 68, 85, 102,
|
17, 34, 51, 68, 85, 102,
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -261,7 +261,7 @@ mod test {
|
||||||
vec![
|
vec![
|
||||||
// Magic number for LookAround packets
|
// Magic number for LookAround packets
|
||||||
154, 74, 67, 129, // Response tag
|
154, 74, 67, 129, // Response tag
|
||||||
2, // MAC is None
|
2, // MAC is None
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
|
@ -3,13 +3,13 @@ pub use std::{
|
||||||
env,
|
env,
|
||||||
io::{Cursor, Write},
|
io::{Cursor, Write},
|
||||||
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
|
||||||
|
path::PathBuf,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use configparser::ini::Ini;
|
pub use configparser::ini::Ini;
|
||||||
pub use directories::ProjectDirs;
|
|
||||||
pub use mac_address::{MacAddress, get_mac_address};
|
pub use mac_address::{MacAddress, get_mac_address};
|
||||||
pub use rand::RngCore;
|
pub use rand::RngCore;
|
||||||
pub use tokio::{
|
pub use tokio::{
|
||||||
|
@ -18,9 +18,7 @@ pub use tokio::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
app_common::{
|
app_common::{self, AppError, CliArgError, recv_msg_from},
|
||||||
self, AppError, CliArgError, LOOKAROUND_VERSION, find_project_dirs, recv_msg_from,
|
|
||||||
},
|
|
||||||
ip::get_ips,
|
ip::get_ips,
|
||||||
message::{self, Message, PACKET_SIZE},
|
message::{self, Message, PACKET_SIZE},
|
||||||
tlv,
|
tlv,
|
||||||
|
|
|
@ -44,9 +44,9 @@ fn configure<I: Iterator<Item = String>>(mut args: I) -> Result<Params, AppError
|
||||||
let mut bind_addrs = vec![];
|
let mut bind_addrs = vec![];
|
||||||
let mut nickname = String::new();
|
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 mut ini = Ini::new_cs();
|
||||||
let path = proj_dirs.config_local_dir().join("server.ini");
|
|
||||||
if ini.load(&path).is_ok() {
|
if ini.load(&path).is_ok() {
|
||||||
if let Some(x) = ini.get("server", "nickname") {
|
if let Some(x) = ini.get("server", "nickname") {
|
||||||
nickname = x;
|
nickname = x;
|
||||||
|
|
Loading…
Reference in New Issue