Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove some extra deps #278

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 0 additions & 190 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ crossterm = "0.27.0"
structopt = "0.3"
chrono = "0.4"
regex = "1.9.3"
lazy_static = "1.4.0"
tokio = { version = "1.32", features = ["rt", "sync"] }
trust-dns-resolver = "0.23.0"
async-trait = "0.1.73"
Expand All @@ -48,9 +47,7 @@ procfs = "0.15.1"
[dev-dependencies]
insta = "1.31.0"
pnet_base = "0.34.0"
cargo-insta = "1.31.0"
packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" }
regex = "1"

[target.'cfg(target_os="windows")'.build-dependencies]
http_req = "0.9.2"
Expand Down
18 changes: 9 additions & 9 deletions src/os/lsof_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{ffi::OsStr, net::IpAddr, process::Command};
use std::{ffi::OsStr, net::IpAddr, process::Command, sync::OnceLock};

use lazy_static::lazy_static;
use regex::Regex;

use crate::network::Protocol;
Expand All @@ -16,11 +15,8 @@ pub struct RawConnection {
pub process_name: String,
}

lazy_static! {
static ref CONNECTION_REGEX: Regex =
Regex::new(r"\[?([^\s\]]*)\]?:(\d+)->\[?([^\s\]]*)\]?:(\d+)").unwrap();
static ref LISTEN_REGEX: Regex = Regex::new(r"\[?([^\s\[\]]*)\]?:(.*)").unwrap();
}
static CONNECTION_REGEX_ONCE: OnceLock<Regex> = OnceLock::new();
static LISTEN_REGEX: OnceLock<Regex> = OnceLock::new();

fn get_null_addr(ip_type: &str) -> &str {
if ip_type.contains('4') {
Expand All @@ -32,6 +28,10 @@ fn get_null_addr(ip_type: &str) -> &str {

impl RawConnection {
pub fn new(raw_line: &str) -> Option<RawConnection> {
let connection_regex = CONNECTION_REGEX_ONCE
.get_or_init(|| Regex::new(r"\[?([^\s\]]*)\]?:(\d+)->\[?([^\s\]]*)\]?:(\d+)").unwrap());
let listen_regex =
LISTEN_REGEX.get_or_init(|| Regex::new(r"\[?([^\s\[\]]*)\]?:(.*)").unwrap());
// Example row
// com.apple 664 user 198u IPv4 0xeb179a6650592b8d 0t0 TCP 192.168.1.187:58535->1.2.3.4:443 (ESTABLISHED)
let columns: Vec<&str> = raw_line.split_ascii_whitespace().collect();
Expand All @@ -57,7 +57,7 @@ impl RawConnection {
// "(LISTEN)" or "(ESTABLISHED)", this column may or may not be present
// let connection_state = columns[9];
// If this socket is in a "connected" state
if let Some(caps) = CONNECTION_REGEX.captures(connection_str) {
if let Some(caps) = connection_regex.captures(connection_str) {
// Example
// 192.168.1.187:64230->0.1.2.3:5228
// *:*
Expand All @@ -75,7 +75,7 @@ impl RawConnection {
process_name,
};
Some(connection)
} else if let Some(caps) = LISTEN_REGEX.captures(connection_str) {
} else if let Some(caps) = listen_regex.captures(connection_str) {
let local_ip = if caps.get(1).unwrap().as_str() == "*" {
get_null_addr(ip_type)
} else {
Expand Down
Loading