diff --git a/Cargo.lock b/Cargo.lock index d51437c..c0c5414 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,6 +60,15 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "1.0.0" @@ -128,11 +137,11 @@ dependencies = [ "base64", "ctrlc", "dashmap", - "env_logger", "futures", "instant-acme", "jsonwebtoken", "log", + "log4rs", "mimalloc", "moka", "notify", @@ -517,7 +526,9 @@ version = "0.4.44" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0" dependencies = [ + "iana-time-zone", "num-traits", + "windows-link", ] [[package]] @@ -895,6 +906,34 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.117", + "unicode-xid", +] + +[[package]] +name = "destructure_traitobject" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c877555693c14d2f84191cfd3ad8582790fc52b5e2274b40b59cf5f5cea25c7" + [[package]] name = "digest" version = "0.10.7" @@ -1020,29 +1059,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "env_filter" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32e90c2accc4b07a8456ea0debdc2e7587bdd890680d71173a15d4ae604f6eef" -dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.11.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0621c04f2196ac3f488dd583365b9c09be011a4ab8b9f37248ffcc8f6198b56a" -dependencies = [ - "anstream", - "anstyle", - "env_filter", - "jiff", - "log", -] - [[package]] name = "equivalent" version = "1.0.2" @@ -1463,6 +1479,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424" + [[package]] name = "hybrid-array" version = "0.4.11" @@ -1549,6 +1571,30 @@ dependencies = [ "windows-registry", ] +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "2.2.0" @@ -1750,30 +1796,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" -[[package]] -name = "jiff" -version = "0.2.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f00b5dbd620d61dfdcb6007c9c1f6054ebd75319f163d886a9055cec1155073d" -dependencies = [ - "jiff-static", - "log", - "portable-atomic", - "portable-atomic-util", - "serde_core", -] - -[[package]] -name = "jiff-static" -version = "0.2.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e000de030ff8022ea1da3f466fbb0f3a809f5e51ed31f6dd931c35181ad8e6d7" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.117", -] - [[package]] name = "jni" version = "0.22.4" @@ -1976,6 +1998,44 @@ name = "log" version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +dependencies = [ + "serde_core", +] + +[[package]] +name = "log-mdc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7" + +[[package]] +name = "log4rs" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e947bb896e702c711fccc2bf02ab2abb6072910693818d1d6b07ee2b9dfd86c" +dependencies = [ + "anyhow", + "arc-swap", + "chrono", + "derive_more", + "fnv", + "humantime", + "libc", + "log", + "log-mdc", + "mock_instant", + "parking_lot", + "rand 0.9.4", + "serde", + "serde-value", + "serde_json", + "serde_yaml", + "thiserror 2.0.18", + "thread-id", + "typemap-ors", + "unicode-segmentation", + "winapi", +] [[package]] name = "lru" @@ -2066,6 +2126,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "mock_instant" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce6dd36094cac388f119d2e9dc82dc730ef91c32a6222170d630e5414b956e6" + [[package]] name = "moka" version = "0.12.15" @@ -2372,6 +2438,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-float" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" +dependencies = [ + "num-traits", +] + [[package]] name = "p256" version = "0.13.2" @@ -2777,15 +2852,6 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" -[[package]] -name = "portable-atomic-util" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618" -dependencies = [ - "portable-atomic", -] - [[package]] name = "potential_utf" version = "0.1.5" @@ -3452,6 +3518,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float", + "serde", +] + [[package]] name = "serde_core" version = "1.0.228" @@ -3816,6 +3892,16 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "thread-id" +version = "5.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2010d27add3f3240c1fef7959f46c814487b216baee662af53be645ba7831c07" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "thread_local" version = "1.1.9" @@ -4119,6 +4205,15 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typemap-ors" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a68c24b707f02dd18f1e4ccceb9d49f2058c2fb86384ef9972592904d7a28867" +dependencies = [ + "unsafe-any-ors", +] + [[package]] name = "typenum" version = "1.20.0" @@ -4137,12 +4232,27 @@ version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" +[[package]] +name = "unicode-segmentation" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9629274872b2bfaf8d66f5f15725007f635594914870f65218920345aa11aa8c" + [[package]] name = "unicode-xid" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "unsafe-any-ors" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a303d30665362d9680d7d91d78b23f5f899504d4f08b3c4cf08d055d87c0ad" +dependencies = [ + "destructure_traitobject", +] + [[package]] name = "unsafe-libyaml" version = "0.2.11" @@ -4388,6 +4498,22 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.11" @@ -4397,6 +4523,47 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 80991ab..6e4f6b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ pingora-proxy = "0.8.0" pingora-http = "0.8.0" pingora-limits = "0.8.0" async-trait = "0.1.89" -env_logger = "0.11.10" log = "0.4.29" futures = "0.3.32" notify = "9.0.0-rc.4" @@ -48,3 +47,4 @@ moka = { version = "0.12.15", features = ["sync"] } ahash = "0.8.12" instant-acme = "0.8.5" rcgen = "0.14.7" +log4rs = "1.4.0" diff --git a/src/utils/parceyaml.rs b/src/utils/parceyaml.rs index d4a7af4..f082d92 100644 --- a/src/utils/parceyaml.rs +++ b/src/utils/parceyaml.rs @@ -3,12 +3,18 @@ use crate::utils::state::{is_first_run, mark_not_first_run}; use crate::utils::structs::*; use crate::utils::tools::{clone_dashmap, clone_dashmap_into, print_upstreams}; use dashmap::DashMap; +use log::LevelFilter; use log::{error, info, warn}; +use log4rs::{ + append::{console::ConsoleAppender, file::FileAppender}, + config::{Appender, Config as Log4rsConfig, Root}, + encode::pattern::PatternEncoder, +}; use std::collections::HashMap; +use std::fs; use std::path::Path; use std::sync::atomic::AtomicUsize; use std::sync::{Arc, LazyLock}; -use std::{env, fs}; pub static DOMAINS: LazyLock> = LazyLock::new(DashMap::new); @@ -230,7 +236,7 @@ pub fn parce_main_config(path: &str) -> AppConfig { let reply = DashMap::new(); let cfg: HashMap = serde_yml::from_str(&data).expect("Failed to parse main config file"); let mut cfo: AppConfig = serde_yml::from_str(&data).expect("Failed to parse main config file"); - log_builder(&cfo); + log_builder(&cfo, &cfo.log_file); cfo.hc_method = cfo.hc_method.to_uppercase(); for (k, v) in cfg { reply.insert(k.to_string(), v.to_string()); @@ -240,14 +246,6 @@ pub fn parce_main_config(path: &str) -> AppConfig { cfo.local_server = Option::from((ip.to_string(), port)); } } - // if let Some(tlsport_cfg) = cfo.proxy_address_tls.clone() { - // if let Some((_, port_str)) = tlsport_cfg.split_once(':') { - // if let Ok(port) = port_str.parse::() { - // cfo.proxy_port_tls = Some(port); - // } - // } - // }; - if let Some(tlsport_cfg) = cfo.proxy_address_tls.clone() { if let Some((_, port_str)) = tlsport_cfg.split_once(':') { cfo.proxy_port_tls = Some(port_str.to_string()); @@ -289,7 +287,8 @@ fn parce_tls_grades(what: Option) -> Option { } } -fn log_builder(conf: &AppConfig) { +/* +fn log_builder1(conf: &AppConfig) { let log_level = conf.log_level.clone(); unsafe { match log_level.as_str() { @@ -307,6 +306,7 @@ fn log_builder(conf: &AppConfig) { } env_logger::builder().init(); } +*/ pub fn build_headers(path_config: &Option>, _config: &Configuration, hl: &mut Vec<(String, Arc)>) { if let Some(headers) = &path_config { @@ -317,3 +317,34 @@ pub fn build_headers(path_config: &Option>, _config: &Configuration, } } } + +fn log_builder(conf: &AppConfig, location: &Option) { + let log_level = match conf.log_level.as_str() { + "info" => LevelFilter::Info, + "error" => LevelFilter::Error, + "warn" => LevelFilter::Warn, + "debug" => LevelFilter::Debug, + "trace" => LevelFilter::Trace, + "off" => LevelFilter::Off, + _ => { + println!("Error reading log level, defaulting to: INFO"); + LevelFilter::Info + } + }; + let pattern = "{d(%Y-%m-%d %H:%M:%S)} {l} {t} - {m}{n}"; + if let Some(location) = location { + let file = FileAppender::builder().encoder(Box::new(PatternEncoder::new(pattern))).build(location).unwrap(); + let config = Log4rsConfig::builder() + .appender(Appender::builder().build("file", Box::new(file))) + .build(Root::builder().appender("file").build(log_level)) + .unwrap(); + log4rs::init_config(config).unwrap(); + } else { + let stdout = ConsoleAppender::builder().encoder(Box::new(PatternEncoder::new(pattern))).build(); + let config = Log4rsConfig::builder() + .appender(Appender::builder().build("stdout", Box::new(stdout))) + .build(Root::builder().appender("stdout").build(log_level)) + .unwrap(); + log4rs::init_config(config).unwrap(); + } +} diff --git a/src/utils/structs.rs b/src/utils/structs.rs index 8c0b202..7f31b0b 100644 --- a/src/utils/structs.rs +++ b/src/utils/structs.rs @@ -125,6 +125,7 @@ pub struct AppConfig { pub file_server_folder: Option, pub runuser: Option, pub rungroup: Option, + pub log_file: Option, } #[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] diff --git a/src/utils/tools.rs b/src/utils/tools.rs index 30b6421..e8ffa5d 100644 --- a/src/utils/tools.rs +++ b/src/utils/tools.rs @@ -20,17 +20,15 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use std::{fs, process, thread, time}; -#[allow(dead_code)] pub fn print_upstreams(upstreams: &UpstreamsDashMap) { + let mut out = String::new(); for host_entry in upstreams.iter() { - let hostname = host_entry.key(); - println!("Hostname: {}", hostname); - + writeln!(out, "Hostname: {}", host_entry.key()).unwrap(); for path_entry in host_entry.value().iter() { - let path = path_entry.key(); - println!(" Path: {}", path); + writeln!(out, " Path: {}", path_entry.key()).unwrap(); for f in path_entry.value().0.clone() { - println!( + writeln!( + out, " IP: {}, Port: {}, SSL: {}, H2: {}, To HTTPS: {}, Rate Limit: {}", f.address, f.port, @@ -38,12 +36,13 @@ pub fn print_upstreams(upstreams: &UpstreamsDashMap) { f.is_http2, f.to_https, f.rate_limit.unwrap_or(0) - ); + ) + .unwrap(); } } } + info!("\n{}", out.trim_end()); } - #[allow(dead_code)] pub fn typeoff(_: T) { let to = type_name::(); @@ -184,7 +183,6 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) { }; cloned.insert(id, Arc::from(to_add)); cloned.insert(hh, x.to_owned()); - // println!("CLONNED :===========> {:?}", cloned); } new_inner_map.insert(path.clone(), new_vec); }