Skip to content

Commit f949e84

Browse files
committed
@v0.6.4
- update edition to 2024 - fix initializing logging with unsafe code - fix home dir var for exporting
1 parent 05a899f commit f949e84

4 files changed

Lines changed: 37 additions & 26 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "netscanner"
3-
version = "0.6.3"
4-
edition = "2021"
3+
version = "0.6.4"
4+
edition = "2024"
55
description = "Network Scanner"
66
license = "MIT"
77
authors = ["Chleba <chlebik@gmail.com>"]

src/components/export.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use ratatui::{prelude::*, widgets::*};
66
use std::env;
77
use tokio::sync::mpsc::UnboundedSender;
88

9-
use super::{discovery::ScannedIp, ports::ScannedIpPorts, Component, Frame};
9+
use super::{Component, Frame, discovery::ScannedIp, ports::ScannedIpPorts};
1010
use crate::{action::Action, enums::PacketsInfoTypesEnum};
1111

1212
#[derive(Default)]
@@ -29,14 +29,20 @@ impl Export {
2929

3030
#[cfg(target_os = "linux")]
3131
fn get_user_home_dir(&mut self) {
32-
let mut home_dir = String::from("/root");
32+
let mut home_dir: Option<String> = None;
3333
if let Some(h_dir) = env::var_os("HOME") {
34-
home_dir = String::from(h_dir.to_str().unwrap());
34+
home_dir = Some(String::from(h_dir.to_str().unwrap()));
3535
}
36-
if let Some(sudo_user) = env::var_os("SUDO_USER") {
37-
home_dir = format!("/home/{}", sudo_user.to_str().unwrap());
36+
if let Some(sudo_user) = env::var_os("SUDO_USER")
37+
&& home_dir.is_none()
38+
{
39+
home_dir = Some(format!("/home/{}", sudo_user.to_str().unwrap()));
40+
}
41+
if let Some(home_dir) = home_dir {
42+
self.home_dir = format!("{}/.netscanner", home_dir)
43+
} else {
44+
self.home_dir = String::from("/root");
3845
}
39-
self.home_dir = format!("{}/.netscanner", home_dir);
4046

4147
// -- create dot folder
4248
if std::fs::metadata(self.home_dir.clone()).is_err()
@@ -84,7 +90,6 @@ impl Export {
8490
}
8591
}
8692

87-
8893
pub fn write_discovery(&mut self, data: Vec<ScannedIp>, timestamp: &String) -> Result<()> {
8994
let mut w = Writer::from_path(format!("{}/scanned_ips.{}.csv", self.home_dir, timestamp))?;
9095

src/utils.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cmp;
22
use std::path::PathBuf;
3+
use std::sync::Mutex;
34

45
use cidr::Ipv4Cidr;
56
use color_eyre::eyre::Result;
@@ -9,9 +10,7 @@ use lazy_static::lazy_static;
910
use std::net::Ipv4Addr;
1011
use tracing::error;
1112
use tracing_error::ErrorLayer;
12-
use tracing_subscriber::{
13-
self, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Layer,
14-
};
13+
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
1514

1615
use crate::components::sniff::IPTraffic;
1716

@@ -122,7 +121,7 @@ pub fn initialize_panic_handler() -> Result<()> {
122121

123122
#[cfg(not(debug_assertions))]
124123
{
125-
use human_panic::{handle_dump, print_msg, Metadata};
124+
use human_panic::{Metadata, handle_dump, print_msg};
126125
let meta = metadata!()
127126
.authors("Chleba <chlebik@gmail.com>")
128127
.homepage("https://github.com/Chleba/netscanner")
@@ -176,26 +175,33 @@ pub fn get_config_dir() -> PathBuf {
176175

177176
pub fn initialize_logging() -> Result<()> {
178177
let directory = get_data_dir();
179-
std::fs::create_dir_all(directory.clone())?;
180-
let log_path = directory.join(LOG_FILE.clone());
178+
std::fs::create_dir_all(&directory)?;
179+
180+
let log_path = directory.join(&*LOG_FILE);
181181
let log_file = std::fs::File::create(log_path)?;
182-
std::env::set_var(
183-
"RUST_LOG",
184-
std::env::var("RUST_LOG")
185-
.or_else(|_| std::env::var(LOG_ENV.clone()))
186-
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME"))),
187-
);
182+
183+
let env_filter_str = std::env::var("RUST_LOG")
184+
.or_else(|_| std::env::var(&*LOG_ENV))
185+
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME")));
186+
187+
let env_filter = tracing_subscriber::filter::EnvFilter::new(env_filter_str);
188+
189+
let log_file = Mutex::new(log_file);
190+
188191
let file_subscriber = tracing_subscriber::fmt::layer()
189192
.with_file(true)
190193
.with_line_number(true)
191-
.with_writer(log_file)
194+
.with_writer(log_file) // Now accepts the Mutex<File>
192195
.with_target(false)
193196
.with_ansi(false)
194-
.with_filter(tracing_subscriber::filter::EnvFilter::from_default_env());
195-
tracing_subscriber::registry()
197+
.with_filter(env_filter);
198+
199+
// 4. SAFETY: try_init prevents crashes if logging is initialized twice (e.g. tests)
200+
let _ = tracing_subscriber::registry()
196201
.with(file_subscriber)
197202
.with(ErrorLayer::default())
198-
.init();
203+
.try_init();
204+
199205
Ok(())
200206
}
201207

0 commit comments

Comments
 (0)