#traffic #statistics #networking #port

netraffic

netraffic is a rust library that provides ability to statistics network traffic

1 unstable release

0.1.0 May 27, 2022

#31 in #traffic

Download history 67/week @ 2023-12-15 10/week @ 2023-12-22 2/week @ 2023-12-29 6/week @ 2024-01-05 4/week @ 2024-01-12 3/week @ 2024-02-09 11/week @ 2024-02-16 20/week @ 2024-02-23 18/week @ 2024-03-01 13/week @ 2024-03-08 18/week @ 2024-03-15 18/week @ 2024-03-22 31/week @ 2024-03-29

81 downloads per month
Used in netop

MIT license

14KB
230 lines

Language : ๐Ÿ‡บ๐Ÿ‡ธ English | ๐Ÿ‡จ๐Ÿ‡ณ ็ฎ€ไฝ“ไธญๆ–‡

netraffic

Build Status Crates Downloads Last Commit

Docs GitHub Actions CI LICENSE

Overview

netraffic is a rust library that provides ability to statistics network traffic.

Prerequisites

Windows

Download the WinPcap Developer's Pack. Add the /Lib or /Lib/x64 folder to your LIB environment variable.

Linux

Install libpcap

On Debian based Linux, apt install libpcap-dev

Mac OS X

libpcap should be installed on Mac OS X by default.

Installation

  1. Get the latest version -> https://crates.io/crates/netraffic

  2. Add the dependent

[dependencies]
netraffic = "0.1.0"
  1. Usage
use std::{thread, time::Duration};
use netraffic::{Filter, Traffic};

fn main() {
    let mut traffic = Traffic::new();
    // rule look here: https://biot.com/capstats/bpf.html
    let rule1 = "port 443";
    let rule2 = "src host 127.0.0.1";
    traffic.add_listener(Filter::new("eth0".to_string(), rule1.to_string()));
    traffic.add_listener(Filter::new("eth0".to_string(), rule2.to_string()));
    loop {
        thread::sleep(Duration::from_millis(1000));
        println!(
            "rule1: {}, traffic: {:#?} Bytes",
            rule1,
            traffic.get_data().get(rule1).unwrap().total
        );
        println!(
            "rule2: {}, traffic: {:#?} Bytes",
            rule2,
            traffic.get_data().get(rule2).unwrap().total
        );
    }
}

Learn More Examples

Goods

struct -> Traffic ยท Filter ยท Snapshot

mod (device) -> get_device ยท get_default_device

Documentation

Traffic

impl Traffic {
    /// Init traffic
    pub fn new() -> Self
    /// Add a new filter to the traffic data center.
    pub fn add_listener(&mut self, filter: Filter)
    /// Remove a filter from the traffic data center.
    pub fn remove_listener(&self, rule: String)
    /// Suspend a listener by rule.
    pub fn suspend_listener(&self, rule: String)
    /// Resume a listener by rule.
    pub fn resume_listener(&self, rule: String)
    /// Get the traffic snapshot, until Rwlock is free.
    pub fn get_data(&self) -> HashMap<String, Snapshot>
    /// Try to get the traffic snapshot.
    /// if Rwlock is locked, return None.
    pub fn try_get_data(&self) -> Option<HashMap<String, Snapshot>>
}

Filter

#[derive(Debug, Clone)]
pub struct Filter {
    /// Name of network interface
    pub device: String,
    /// Filtering rules
    /// BPF : https://biot.com/capstats/bpf.html
    pub rule: String,
    /// Whether the mode is immediately modeled, the default true
    /// https://www.tcpdump.org/manpages/pcap_set_immediate_mode.3pcap.html
    pub immediate_mode: bool,
}

/// Init filter, the default immediate_mode = true
Filter::new("eth0".to_string(), "tcp port 80".to_string());
/// or set immediate_mode field
Filter {
    device: "eth0".to_string(),
    rule: "tcp port 80".to_string(),
    immediate_mode: true,
}

Snapshot

#[derive(Debug, Clone, Copy)]
pub struct Snapshot {
    /// The total byte after add_listener
    pub total: u64,
    /// The latest package of data byte
    pub len: u64,
    /// The latest package of data timestamp
    pub timestamp: u64,
}

get_device

/// Get all network interface
pub fn get_device() -> Result<Vec<Device>, Error>

get_default_device

/// Get default network interface
pub fn get_default_device() -> Result<Device, Error>

Examples

๐Ÿ–ฅ Get network interface device

๐Ÿšฅ Statistical traffic

๐Ÿš„ Calculate network speed

Thanks

pcap

Dependencies

~180โ€“800KB
~12K SLoC