{"id":20847368,"url":"https://github.com/zingerlittlebee/netraffic","last_synced_at":"2025-09-16T20:34:38.190Z","repository":{"id":38364248,"uuid":"496234689","full_name":"ZingerLittleBee/netraffic","owner":"ZingerLittleBee","description":"Statistics network traffic --- Rust Library","archived":false,"fork":false,"pushed_at":"2025-08-11T18:44:37.000Z","size":41,"stargazers_count":25,"open_issues_count":5,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-09T18:21:10.957Z","etag":null,"topics":["network","port","rust","statistics","traffic","traffic-monitoring"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZingerLittleBee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-25T13:06:22.000Z","updated_at":"2025-07-30T09:51:57.000Z","dependencies_parsed_at":"2024-04-25T20:30:17.896Z","dependency_job_id":"3cb30e76-b476-46e4-8a62-9b2c008d5b62","html_url":"https://github.com/ZingerLittleBee/netraffic","commit_stats":{"total_commits":43,"total_committers":3,"mean_commits":"14.333333333333334","dds":"0.16279069767441856","last_synced_commit":"ff308c5d10994197f531e613d1024c0a473f2275"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ZingerLittleBee/netraffic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZingerLittleBee%2Fnetraffic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZingerLittleBee%2Fnetraffic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZingerLittleBee%2Fnetraffic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZingerLittleBee%2Fnetraffic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZingerLittleBee","download_url":"https://codeload.github.com/ZingerLittleBee/netraffic/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZingerLittleBee%2Fnetraffic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275482298,"owners_count":25473107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-16T02:00:10.229Z","response_time":65,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["network","port","rust","statistics","traffic","traffic-monitoring"],"created_at":"2024-11-18T02:19:45.448Z","updated_at":"2025-09-16T20:34:38.107Z","avatar_url":"https://github.com/ZingerLittleBee.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"Language : 🇺🇸 English | [🇨🇳 简体中文](./README.zh-CN.md)\n\n\u003ch1 align=\"center\"\u003enetraffic\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n\n[![Build Status](https://img.shields.io/crates/v/netraffic?style=for-the-badge)](https://crates.io/crates/netraffic)\n![Crates Downloads](https://img.shields.io/crates/d/netraffic?style=for-the-badge)\n![Last Commit](https://img.shields.io/github/last-commit/ZingerLittleBee/netraffic?style=for-the-badge)\n\n\u003c/div\u003e\n\u003cdiv align=\"center\"\u003e\n\n[![Docs](https://img.shields.io/docsrs/netraffic?style=for-the-badge)](https://docs.rs/netraffic/0.1.0/netraffic/)\n[![GitHub Actions CI](https://img.shields.io/github/actions/workflow/status/ZingerLittleBee/netraffic/test.yml?style=for-the-badge)](https://github.com/ZingerLittleBee/netraffic/actions)\n[![LICENSE](https://img.shields.io/crates/l/netraffic?style=for-the-badge)](./LICENSE)\n\n\u003c/div\u003e\n\n## Overview\nnetraffic is a rust library that provides ability to **statistics network traffic**.\n\n## Prerequisites\n### Windows\nDownload the [WinPcap](http://www.winpcap.org/install/default.htm) Developer's Pack. Add the `/Lib` or `/Lib/x64` folder to your LIB environment variable.\n\n### Linux\nInstall `libpcap`\n\nOn Debian based Linux, `apt install libpcap-dev`\n\n### Mac OS X\nlibpcap should be installed on Mac OS X by default.\n\n## Installation\n1. Get the latest version -\u003e https://crates.io/crates/netraffic\n\n2. Add the dependent\n```toml\n[dependencies]\nnetraffic = \"0.1.0\"\n```\n\n3. Usage\n```rust\nuse std::{thread, time::Duration};\nuse netraffic::{Filter, Traffic};\n\nfn main() {\n    let mut traffic = Traffic::new();\n    // rule look here: https://biot.com/capstats/bpf.html\n    let rule1 = \"port 443\";\n    let rule2 = \"src host 127.0.0.1\";\n    traffic.add_listener(Filter::new(\"eth0\".to_string(), rule1.to_string()));\n    traffic.add_listener(Filter::new(\"eth0\".to_string(), rule2.to_string()));\n    loop {\n        thread::sleep(Duration::from_millis(1000));\n        println!(\n            \"rule1: {}, traffic: {:#?} Bytes\",\n            rule1,\n            traffic.get_data().get(rule1).unwrap().total\n        );\n        println!(\n            \"rule2: {}, traffic: {:#?} Bytes\",\n            rule2,\n            traffic.get_data().get(rule2).unwrap().total\n        );\n    }\n}\n```\n\u003e Learn More [Examples](#examples)\n\n## Examples\n[🖥 Get network interface device](./examples/device.rs)\n\n[🚥 Statistical traffic](./examples/traffic.rs)\n\n[🚄 Calculate network speed](./examples/speed.rs)\n\n## Goods\nstruct -\u003e [Traffic](#traffic) · [Filter](#filter) · [Snapshot](#snapshot)\n\nmod (`device`) -\u003e [get_device](#get_device) · [get_default_device](#get_default_device)\n\n## Documentation\n### `Traffic`\n```rust\nimpl Traffic {\n    /// Init traffic\n    pub fn new() -\u003e Self\n    /// Add a new filter to the traffic data center.\n    pub fn add_listener(\u0026mut self, filter: Filter)\n    /// Remove a filter from the traffic data center.\n    pub fn remove_listener(\u0026self, rule: String)\n    /// Suspend a listener by rule.\n    pub fn suspend_listener(\u0026self, rule: String)\n    /// Resume a listener by rule.\n    pub fn resume_listener(\u0026self, rule: String)\n    /// Get the traffic snapshot, until Rwlock is free.\n    pub fn get_data(\u0026self) -\u003e HashMap\u003cString, Snapshot\u003e\n    /// Try to get the traffic snapshot.\n    /// if Rwlock is locked, return None.\n    pub fn try_get_data(\u0026self) -\u003e Option\u003cHashMap\u003cString, Snapshot\u003e\u003e\n}\n```\n\n### `Filter`\n```rust\n#[derive(Debug, Clone)]\npub struct Filter {\n    /// Name of network interface\n    pub device: String,\n    /// Filtering rules\n    /// BPF : https://biot.com/capstats/bpf.html\n    pub rule: String,\n    /// Whether the mode is immediately modeled, the default true\n    /// https://www.tcpdump.org/manpages/pcap_set_immediate_mode.3pcap.html\n    pub immediate_mode: bool,\n}\n\n/// Init filter, the default immediate_mode = true\nFilter::new(\"eth0\".to_string(), \"tcp port 80\".to_string());\n/// or set immediate_mode field\nFilter {\n    device: \"eth0\".to_string(),\n    rule: \"tcp port 80\".to_string(),\n    immediate_mode: true,\n}\n```\n\n### `Snapshot`\n```rust\n#[derive(Debug, Clone, Copy)]\npub struct Snapshot {\n    /// The total byte after add_listener\n    pub total: u64,\n    /// The latest package of data byte\n    pub len: u64,\n    /// The latest package of data timestamp\n    pub timestamp: u64,\n}\n```\n\n### `get_device`\n```rust\n/// Get all network interface\npub fn get_device() -\u003e Result\u003cVec\u003cDevice\u003e, Error\u003e\n```\n\n### `get_default_device`\n```rust\n/// Get default network interface\npub fn get_default_device() -\u003e Result\u003cDevice, Error\u003e\n```\n\n## Thanks\n[pcap](https://github.com/rust-pcap/pcap)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzingerlittlebee%2Fnetraffic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzingerlittlebee%2Fnetraffic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzingerlittlebee%2Fnetraffic/lists"}