From cf781a3a9c322d5d09b27de557e8a8c5dcc5f90c Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sat, 7 Jan 2023 12:12:58 +0100 Subject: [PATCH] update: clap 4, various code adapt --- Cargo.lock | 103 ++++++++++++++++++++++++++++++++++----------- Cargo.toml | 2 +- {src => old}/db.rs | 0 src/config.rs | 27 ++++++------ src/ipblc.rs | 6 +-- 5 files changed, 95 insertions(+), 43 deletions(-) rename {src => old}/db.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 8fd5b1e..d8cc989 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,17 +20,6 @@ dependencies = [ "libc", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -103,24 +92,22 @@ dependencies = [ [[package]] name = "clap" -version = "3.2.22" +version = "4.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" +checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39" dependencies = [ - "atty", "bitflags", "clap_lex", - "indexmap", + "is-terminal", "strsim", "termcolor", - "textwrap", ] [[package]] name = "clap_lex" -version = "0.2.4" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" dependencies = [ "os_str_bytes", ] @@ -292,6 +279,27 @@ dependencies = [ "synstructure", ] +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "fnv" version = "1.0.7" @@ -408,6 +416,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" +dependencies = [ + "libc", +] + [[package]] name = "http" version = "0.2.8" @@ -523,6 +540,16 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "io-lifetimes" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" +dependencies = [ + "libc", + "windows-sys 0.42.0", +] + [[package]] name = "ipblc" version = "1.0.1" @@ -549,6 +576,18 @@ version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e" +[[package]] +name = "is-terminal" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" +dependencies = [ + "hermit-abi 0.2.6", + "io-lifetimes", + "rustix", + "windows-sys 0.42.0", +] + [[package]] name = "itoa" version = "1.0.4" @@ -604,6 +643,12 @@ dependencies = [ "cc", ] +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + [[package]] name = "lock_api" version = "0.4.9" @@ -739,7 +784,7 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", ] @@ -952,6 +997,20 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustix" +version = "0.36.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4feacf7db682c6c329c4ede12649cd36ecab0f3be5b7d74e6a20304725db4549" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys 0.42.0", +] + [[package]] name = "rustls" version = "0.20.6" @@ -1156,12 +1215,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "textwrap" -version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" - [[package]] name = "time" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index aa7224e..9989b3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://git.paulbsd.com/paulbsd/ipblc" [dependencies] chrono = { version = "0.4", features = ["serde"] } -clap = "3.2" +clap = { version = "4.0", features = ["string"] } git-version = "0.3.5" ipnet = "2.7" lazy_static = "1.4" diff --git a/src/db.rs b/old/db.rs similarity index 100% rename from src/db.rs rename to old/db.rs diff --git a/src/config.rs b/src/config.rs index edabe5f..b516ced 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use crate::utils::{gethostname, sleep_s}; use chrono::prelude::*; use chrono::Duration; -use clap::{Arg, ArgMatches, Command}; +use clap::{Arg, ArgAction, ArgMatches, Command}; use git_version::git_version; use ipnet::IpNet; use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify, WatchDescriptor}; @@ -49,13 +49,13 @@ pub struct Flags { } impl Context { - pub async fn new() -> Self { + pub async fn new<'a>() -> Self { + let argp = Context::argparse(); // Get flags - let debug = Context::argparse().is_present("debug"); - let server = Context::argparse() - .value_of("server") - .unwrap_or(format!("https://{}", MASTERSERVER).as_str()) - .to_string(); + let debug: bool = argp.contains_id("debug"); + let server: String = argp.get_one::("server").unwrap().to_string(); + //.unwrap_or(format!("https://{}", MASTERSERVER).as_str()) + //.to_string(); // Build context let mut ctx = Context { @@ -97,9 +97,9 @@ impl Context { ctx } - pub fn argparse() -> ArgMatches { + pub fn argparse<'a>() -> ArgMatches { Command::new(env!("CARGO_PKG_NAME")) - .version(format!("{}@{}", env!("CARGO_PKG_VERSION"), GIT_VERSION).as_str()) + .version(format!("{}@{}", env!("CARGO_PKG_VERSION"), GIT_VERSION)) .author(env!("CARGO_PKG_AUTHORS")) .about(env!("CARGO_PKG_DESCRIPTION")) .arg( @@ -107,15 +107,14 @@ impl Context { .short('s') .long("server") .value_name("server") - .default_value(format!("https://{MASTERSERVER}").as_str()) - .help("Sets a http server") - .takes_value(true), + .default_value(format!("https://{MASTERSERVER}")) + .help("Sets a http server"), ) .arg( Arg::new("debug") .short('d') - .takes_value(false) - .help("Enable debugging"), + .help("Enable debugging") + .action(ArgAction::SetTrue), ) .get_matches() } diff --git a/src/ipblc.rs b/src/ipblc.rs index 704c660..055bc6b 100644 --- a/src/ipblc.rs +++ b/src/ipblc.rs @@ -1,5 +1,5 @@ use crate::config::{Context, GIT_VERSION}; -use crate::fw; +use crate::fw::{block, init}; use crate::ip::{filter, push_ip, IpData}; use crate::utils::{gethostname, read_lines, sleep_s}; use crate::zmqcom::zconnect; @@ -27,7 +27,7 @@ pub async fn run() { let (ipdatatx, mut ipdatarx): (Sender, Receiver) = channel(ZMQ_CHAN_SIZE); // initialize the firewall table - fw::init(&env!("CARGO_PKG_NAME").to_string()); + init(&env!("CARGO_PKG_NAME").to_string()); let mut fwlen: usize = 0; // initialize zeromq sockets @@ -92,7 +92,7 @@ pub async fn run() { } // apply firewall blocking - fw::block( + block( &env!("CARGO_PKG_NAME").to_string(), &ctx.get_blocklist_toblock().await, &mut ret,