daemonize

This commit is contained in:
sqlerrorthing
2025-10-30 03:48:41 +08:00
committed by white
parent 4b9e60336b
commit 7ea4a516be
4 changed files with 98 additions and 5 deletions

67
rust/Cargo.lock generated
View File

@@ -266,6 +266,15 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[package]]
name = "daemonize"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e"
dependencies = [
"libc",
]
[[package]]
name = "dlv-list"
version = "0.5.2"
@@ -293,6 +302,19 @@ dependencies = [
"syn",
]
[[package]]
name = "env_logger"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.3.14"
@@ -348,12 +370,24 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "humantime"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424"
[[package]]
name = "iana-time-zone"
version = "0.1.64"
@@ -388,6 +422,17 @@ dependencies = [
"regex",
]
[[package]]
name = "is-terminal"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.61.2",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
@@ -575,6 +620,16 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pretty_env_logger"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "prettyplease"
version = "0.2.37"
@@ -832,6 +887,15 @@ dependencies = [
"walkdir",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "2.0.17"
@@ -1119,10 +1183,13 @@ version = "0.0.1"
dependencies = [
"anyhow",
"clap",
"daemonize",
"iptables",
"libc",
"libnfqws",
"log",
"once_cell",
"pretty_env_logger",
"procfs",
"regex",
"rust-ini",

View File

@@ -20,4 +20,6 @@ serde_json = "1.0.145"
sysctl = "0.7.1"
tokio = { version = "1.48.0", features = ["full"] }
once_cell = "1.21.3"
daemonize = "0.5.0"
log = "0.4.28"
pretty_env_logger = "0.5.0"

View File

@@ -17,4 +17,7 @@ serde_json = { workspace = true }
sysctl ={ workspace = true }
tokio = { workspace = true }
once_cell = { workspace = true }
libnfqws = { path = "../libnfqws" }
libnfqws = { path = "../libnfqws" }
daemonize = { workspace = true }
pretty_env_logger = { workspace = true }
log = { workspace = true }

View File

@@ -13,8 +13,12 @@ use std::io::{Read, Write};
use std::os::raw::c_char;
use std::sync::atomic::{AtomicBool, Ordering};
use std::{fs, path::Path};
use std::time::Duration;
use daemonize::Daemonize;
use log::{error, info};
use sysctl::{CtlValue, Sysctl};
use tokio::task;
use tokio::time::sleep;
use libnfqws::nfqws_main;
#[derive(Parser)]
@@ -73,6 +77,8 @@ struct Config {
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let cli = Cli::parse();
match &cli.cmd {
Some(Commands::Start) => start_service(),
@@ -84,9 +90,24 @@ async fn main() {
Some(Commands::ModuleVer) => module_version(),
Some(Commands::BinVer) => todo!(), //bin_version(),
//None => println!("zaprett installed. Join us: t.me/zaprett_module"),
None => run_nfqws("--version".to_string()).await.unwrap(),
None => daemonize_nfqws("--uid=0:0 --qnum=200".to_string()).await,
}
}
async fn daemonize_nfqws(args: String) {
info!("Starting nfqws as a daemon");
let daemonize = Daemonize::new()
.working_directory("/tmp")
.group("daemon")
.privileged_action(|| "Executed before drop privileges");
match daemonize.start() {
Ok(_) => {
info!("Success, daemonized");
run_nfqws(args).await.unwrap()
},
Err(e) => error!("Error while starting nfqws daemon: {e}"),
}
tokio::signal::ctrl_c().await.unwrap();
}
fn start_service() {
@@ -331,7 +352,7 @@ async fn run_nfqws(args_str: String) -> anyhow::Result<()> {
}
RUNNING.store(false, Ordering::SeqCst);
});
}).await?;
Ok(())
}