diff options
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/linux.rs | 23 | ||||
| -rw-r--r-- | src/platform/linux/iptables.rs | 24 | ||||
| -rw-r--r-- | src/platform/linux/nftables.rs | 4 | ||||
| -rw-r--r-- | src/platform/windows.rs | 33 |
4 files changed, 42 insertions, 42 deletions
diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 2015484..d21e218 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -11,7 +11,7 @@ use std::io::Write; use anyhow::{Result, Context, anyhow}; use socket2::{Domain, Protocol, Socket, Type}; -use crate::{log::LogLevel, log_println, splash, opt}; +use crate::opt; mod iptables; mod nftables; @@ -69,8 +69,8 @@ fn install_rules() -> Result<()> { Ok(_) => {}, Err(e) => { IS_NFT_NOT_SUPPORTED.store(true, Ordering::Relaxed); - log_println!(LogLevel::Warning, "nftables: {}", e.to_string()); - log_println!(LogLevel::Warning, "fallback to iptables"); + crate::warn!("nftables: {}", e.to_string()); + crate::warn!("fallback to iptables"); let ipt = IPTables::new(false).map_err(iptables_err)?; let ip6 = IPTables::new(true).map_err(iptables_err)?; @@ -123,7 +123,7 @@ fn lock_pid_file() -> Result<()> { fn exit_if_not_root() { if !nix::unistd::geteuid().is_root() { - log_println!(LogLevel::Error, "{PKG_NAME} must be run as root. Try sudo."); + crate::error!("{PKG_NAME} must be run as root. Try sudo."); std::process::exit(3); } } @@ -185,7 +185,7 @@ fn open_nfqueue() -> Result<nfq::Queue> { let mut q = nfq::Queue::open()?; q.bind(crate::opt::queue_num())?; - log_println!(LogLevel::Info, "nfqueue: bound to queue number {}", crate::opt::queue_num()); + crate::info!("nfqueue: bound to queue number {}", crate::opt::queue_num()); // to check inturrupts let raw_fd = q.as_raw_fd(); @@ -221,9 +221,10 @@ fn open_rxring() -> Result<rxring::RxRing> { let rx = rxring::RxRing::new(SYNACK_443_CBPF)?; - log_println!(LogLevel::Info, "rxring: initialized"); - log_println!(LogLevel::Debug, - "rxring: tcp src port 443 and tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)"); + crate::info!("rxring: initialized"); + crate::debug!( + "rxring: tcp src port 443 and tcp[tcpflags] & (tcp-syn|tcp-ack) == (tcp-syn|tcp-ack)" + ); Ok(rx) } @@ -279,7 +280,7 @@ pub fn run() -> Result<()> { let mut rx = if opt::fake_autottl() { Some(open_rxring()?) } else { None }; let mut buf = Vec::<u8>::with_capacity(PACKET_SIZE_CAP); - splash!("{}", super::MESSAGE_AT_RUN); + crate::splash!("{}", super::MESSAGE_AT_RUN); while RUNNING.load(Ordering::SeqCst) { let (q_ready, rx_ready) = match poll_once(&q, rx.as_ref())? { @@ -338,7 +339,7 @@ fn daemonize_1() -> Result<()> { daemonize.start()?; log_file.set_len(0)?; - log_println!(LogLevel::Info, "start as daemon: pid {}", std::process::id()); + crate::info!("start as daemon: pid {}", std::process::id()); Ok(()) } @@ -349,7 +350,7 @@ fn daemonize() { match daemonize_1() { Ok(_) => {}, Err(e) => { - log_println!(LogLevel::Error, "fail to start as daemon: {e}"); + crate::error!("fail to start as daemon: {e}"); std::process::exit(EXIT_DAEMON_FAIL); } } diff --git a/src/platform/linux/iptables.rs b/src/platform/linux/iptables.rs index 4264967..52d0350 100644 --- a/src/platform/linux/iptables.rs +++ b/src/platform/linux/iptables.rs @@ -7,7 +7,7 @@ use std::sync::{ atomic::{AtomicBool, Ordering} }; -use crate::{log::LogLevel, log_println, opt}; +use crate::opt; use super::{exec_process, INJECT_MARK, IS_U32_SUPPORTED}; static IS_XT_U32_LOADED_BY_US: AtomicBool = AtomicBool::new(false); @@ -89,11 +89,11 @@ fn is_u32_supported(ipt: &IPTables) -> bool { } if ensure_xt_u32().is_err() { - log_println!(LogLevel::Warning, "xt_u32 not supported"); + crate::warn!("xt_u32 not supported"); return false; } - log_println!(LogLevel::Info, "xt_u32 loaded"); + crate::info!("xt_u32 loaded"); let rule = ["-m", "u32", "--u32", "0x0=0x0", "-j", "RETURN"]; @@ -147,36 +147,36 @@ pub fn install_iptables_rules(ipt: &IPTables) -> Result<()> { ]; ipt.append("mangle", DPIBREAK_CHAIN, &synack_rule).map_err(iptables_err)?; - log_println!(LogLevel::Info, "{}: add SYN/ACK learning rule on mangle/{}", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: add SYN/ACK learning rule on mangle/{}", ipt.cmd, DPIBREAK_CHAIN); ipt.insert("mangle", "INPUT", &["-j", DPIBREAK_CHAIN], 1).map_err(iptables_err)?; - log_println!(LogLevel::Info, "{}: add jump to {} chain on INPUT", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: add jump to {} chain on INPUT", ipt.cmd, DPIBREAK_CHAIN); } ipt.append("mangle", DPIBREAK_CHAIN, &rule).map_err(iptables_err)?; - log_println!(LogLevel::Info, "{}: new chain {} on table mangle", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: new chain {} on table mangle", ipt.cmd, DPIBREAK_CHAIN); ipt.insert("mangle", "POSTROUTING", &["-j", DPIBREAK_CHAIN], 1).map_err(iptables_err)?; - log_println!(LogLevel::Info, "{}: add jump to {} chain on POSTROUTING", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: add jump to {} chain on POSTROUTING", ipt.cmd, DPIBREAK_CHAIN); Ok(()) } pub fn cleanup_iptables_rules(ipt: &IPTables) -> Result<()> { if ipt.delete("mangle", "POSTROUTING", &["-j", DPIBREAK_CHAIN]).is_ok() { - log_println!(LogLevel::Info, "{}: delete jump to {} from mangle/POSTROUTING", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: delete jump to {} from mangle/POSTROUTING", ipt.cmd, DPIBREAK_CHAIN); } if opt::fake_autottl() && ipt.delete("mangle", "INPUT", &["-j", DPIBREAK_CHAIN]).is_ok() { - log_println!(LogLevel::Info, "{}: delete jump to {} from mangle/INPUT", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: delete jump to {} from mangle/INPUT", ipt.cmd, DPIBREAK_CHAIN); } if ipt.flush_chain("mangle", DPIBREAK_CHAIN).is_ok() { - log_println!(LogLevel::Info, "{}: flush chain {}", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: flush chain {}", ipt.cmd, DPIBREAK_CHAIN); } if ipt.delete_chain("mangle", DPIBREAK_CHAIN).is_ok() { - log_println!(LogLevel::Info, "{}: delete chain {}", ipt.cmd, DPIBREAK_CHAIN); + crate::info!("{}: delete chain {}", ipt.cmd, DPIBREAK_CHAIN); } Ok(()) @@ -185,7 +185,7 @@ pub fn cleanup_iptables_rules(ipt: &IPTables) -> Result<()> { pub fn cleanup_xt_u32() -> Result<()> { if IS_XT_U32_LOADED_BY_US.load(Ordering::Relaxed) { exec_process(&["modprobe", "-q", "-r", "xt_u32"], None)?; - log_println!(LogLevel::Info, "cleanup: unload xt_u32"); + crate::info!("cleanup: unload xt_u32"); } Ok(()) diff --git a/src/platform/linux/nftables.rs b/src/platform/linux/nftables.rs index 7f26668..4809000 100644 --- a/src/platform/linux/nftables.rs +++ b/src/platform/linux/nftables.rs @@ -4,14 +4,14 @@ use std::sync::atomic::Ordering; use anyhow::Result; -use crate::{log::LogLevel, log_println, opt}; +use crate::opt; use super::{exec_process, INJECT_MARK, IS_U32_SUPPORTED}; const DPIBREAK_TABLE: &str = "dpibreak"; /// Apply nft rules with `nft_command() -f -`. fn nft(rule: &str) -> Result<()> { - log_println!(LogLevel::Info, "nft: {rule}"); + crate::info!("nft: {rule}"); exec_process(&[opt::nft_command(), "-f", "-"], Some(rule)) } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index eb7127c..da9c567 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -23,7 +23,6 @@ use windivert::{ }; use std::sync::{LazyLock, Mutex}; use std::thread; -use crate::{log::LogLevel, log_println, splash}; use crate::{opt, pkt}; fn open_handle(filter: &str, flags: prelude::WinDivertFlags) -> WinDivert<NetworkLayer> { @@ -31,12 +30,12 @@ fn open_handle(filter: &str, flags: prelude::WinDivertFlags) -> WinDivert<Networ let h = match WinDivert::network(&filter, 0, flags) { Ok(h) => { - log_println!(LogLevel::Info, "windivert: open: {}", filter); + crate::info!("windivert: open: {}", filter); h }, Err(e) => { - log_println!(LogLevel::Error, "windivert: {}", e); - log_println!(LogLevel::Error, "windivert: {}", filter); + crate::error!("windivert: {}", e); + crate::error!("windivert: {}", filter); std::process::exit(1); } }; @@ -83,7 +82,7 @@ macro_rules! recv_loop { loop { match $handle.recv(Some(&mut buf)) { Ok($pkt) => { $body } - Err(e) => { log_println!(LogLevel::Warning, "windivert: recv: {}", e); } + Err(e) => { crate::warn!("windivert: recv: {}", e); } } } }; @@ -91,15 +90,15 @@ macro_rules! recv_loop { pub fn run() -> Result<()> { let mut buf = Vec::<u8>::with_capacity(super::PACKET_SIZE_CAP); - + if opt::fake_autottl() { let handle = open_handle( "!outbound and tcp and tcp.SrcPort == 443 and tcp.Syn and tcp.Ack", prelude::WinDivertFlags::new().set_sniff() ); - thread::spawn(move || { recv_loop!(handle, pkt => pkt::put_hop(&pkt.data)); }); + thread::spawn(move || { recv_loop!(handle, pkt => pkt::put_hop(&pkt.data)); }); } - + let divert = open_handle( concat!( "outbound and tcp and tcp.DstPort == 443", @@ -109,16 +108,16 @@ pub fn run() -> Result<()> { prelude::WinDivertFlags::new() ); - splash!("{}", super::MESSAGE_AT_RUN); + crate::splash!("{}", super::MESSAGE_AT_RUN); - recv_loop!(divert, pkt => { - crate::handle_packet!( - &pkt.data, - &mut buf, - handled => {}, - rejected => send_to_raw_1(&pkt.data)? - ) - }); + recv_loop!(divert, pkt => { + crate::handle_packet!( + &pkt.data, + &mut buf, + handled => {}, + rejected => send_to_raw_1(&pkt.data)? + ) + }); } fn service_run() { |
