Star 历史趋势
数据来源: GitHub API · 生成自 Stargazers.cn
README.md

listeners

Stars Downloads Codecov CI Docs Crates

Cross-platform library for Rust to efficiently find out processes listening on network ports.

Motivation

It turns out that associating open network ports with the processes that are using them is not a trivial task: this requires interacting with low-level system APIs that are platform-specific, written in C, not well documented, and expensive to call if not used properly.

Despite some Rust libraries to get process information already exist, none of them correlates process ID and name to open network ports in a cross-platform way.

This library wants to fill this gap, and it aims to be:

  • Cross-platform: it currently supports Windows, Linux, macOS, FreeBSD, OpenBSD and NetBSD
  • Fast: it focuses on performance (see benchmarks) by internally using caching and low-level system APIs
  • Simple: it exposes intuitive APIs to get details about the listening processes
  • Lightweight: it has only the strictly necessary dependencies

Roadmap

  • Windows
  • Linux
  • macOS
  • FreeBSD
  • OpenBSD
  • NetBSD
  • Android
  • iOS
  • Other?

Usage

Add this to your Cargo.toml:

[dependencies]

listeners = "0.6"

Get all the listening processes:

if let Ok(listeners) = listeners::get_all() {
    for l in listeners {
        println!("{l}");
    }
}

Output:

PID: 440     Process name: ControlCenter             Socket: 0.0.0.0:0                      Protocol: UDP     State: UNKNOWN
PID: 456     Process name: rapportd                  Socket: [::]:49158                     Protocol: TCP     State: LISTEN
PID: 456     Process name: rapportd                  Socket: 0.0.0.0:49158                  Protocol: TCP     State: LISTEN
PID: 456     Process name: rapportd                  Socket: 0.0.0.0:0                      Protocol: UDP     State: UNKNOWN
PID: 485     Process name: sharingd                  Socket: 0.0.0.0:0                      Protocol: UDP     State: UNKNOWN
PID: 516     Process name: WiFiAgent                 Socket: 0.0.0.0:0                      Protocol: UDP     State: UNKNOWN
PID: 1480    Process name: rustrover                 Socket: [::7f00:1]:63342               Protocol: TCP     State: ESTABLISHED
PID: 2123    Process name: Telegram                  Socket: 192.168.1.102:49659            Protocol: TCP     State: ESTABLISHED
PID: 2123    Process name: Telegram                  Socket: 192.168.1.102:49656            Protocol: TCP     State: ESTABLISHED
PID: 2156    Process name: Google Chrome             Socket: 0.0.0.0:0                      Protocol: UDP     State: UNKNOWN
PID: 2167    Process name: Google Chrome Helper      Socket: 192.168.1.102:60834            Protocol: UDP     State: UNKNOWN
PID: 2167    Process name: Google Chrome Helper      Socket: 192.168.1.102:53220            Protocol: UDP     State: UNKNOWN
PID: 2167    Process name: Google Chrome Helper      Socket: 192.168.1.102:59216            Protocol: UDP     State: UNKNOWN

For more examples of usage, including how to get listening processes in a more granular way, check the examples folder.

Benchmarks

Below you can find exhaustive benchmarks measuring the performance of this library APIs on all supported platforms, varying the system load (number of running processes and open ports).

The benchmarks include:

  • listeners::get_all: get all the listening processes and their sockets
  • listeners::get_process_by_port: get the process listening on a specific port
    • active port: the tested port is randomly selected among the open ports
    • inactive port: the tested port is not open

Benchmarks are run on GitHub Actions runners, and results are generated with the help of criterion.

Windows

See benchmarks
System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium
high

Linux

See benchmarks
System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium
high

macOS

See benchmarks
System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium
high

FreeBSD

See benchmarks
System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium
high

OpenBSD

See benchmarks

Benchmarks for OpenBSD under high system load are currently unavailable due to the impossibility to further increase the maximum limit of open files within the GitHub Actions runner.

System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium

NetBSD

See benchmarks
System load      listeners::get_all      listeners::get_process_by_port
(active port)
listeners::get_process_by_port
(inactive port)
low
medium
high

关于 About

Cross-platform library to find out processes listening on network ports
cross-platformnetwork-programmingportrustsockettcpudp

语言 Languages

Rust80.4%
C19.6%

提交活跃度 Commit Activity

代码提交热力图
过去 52 周的开发活跃度
292
Total Commits
峰值: 89次/周
Less
More

核心贡献者 Contributors