# chenjiandongx/sniffer architecture

> Architecture diagram of the GitHub repository chenjiandongx/sniffer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chenjiandongx/sniffer
- Repository: https://github.com/chenjiandongx/sniffer

## Overview

sniffer is a cross-platform network troubleshooting TUI. The user starts it with device, BPF, refresh, DNS, and view options; it captures packets, attributes connections to processes, aggregates traffic, and renders byte, packet, connection, and plot views. Linux uses netlink and /proc for socket attribution, macOS invokes lsof, and Windows uses gopsutil. The excerpts show the main orchestration and aggregation paths; platform-specific attribution is represented by the documented approaches. Optional DNS resolution and OS interfaces are shown as integrations.

Main entry points: [`sniffer.go`](https://github.com/chenjiandongx/sniffer/blob/master/sniffer.go) (Sniffer loop), [`pcap_linux.go`](https://github.com/chenjiandongx/sniffer/blob/master/pcap_linux.go) (Capture client), [`pcap_others.go`](https://github.com/chenjiandongx/sniffer/blob/master/pcap_others.go) (Other platforms), [`stat.go`](https://github.com/chenjiandongx/sniffer/blob/master/stat.go) (Traffic aggregation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Command and orchestration"]
  node_cli["CLI options<br/>[cli.go]"]
  node_sniffer["Sniffer loop<br/>[sniffer.go]"]
end

subgraph group_capture["Packet capture"]
  node_capture_client["Capture client<br/>[pcap_linux.go]"]
  node_capture_other["Other platforms<br/>[pcap_others.go]"]
  node_packet_sink["Traffic sink<br/>[pcap.go]"]
  node_dns["DNS resolver<br/>[dns.go]"]
  node_device_list["Device discovery<br/>[pcap.go]"]
end

subgraph group_attribution["Connection attribution"]
  node_socket_fetcher["Socket fetcher<br/>[conn_linux.go]"]
  node_linux_sockets["Linux socket mapping<br/>[conn_linux.go]"]
  node_mac_sockets["macOS socket mapping<br/>[conn_darwin.go]"]
  node_windows_sockets["Windows socket mapping<br/>[conn_windows.go]"]
end

subgraph group_presentation["Aggregation and views"]
  node_stats["Traffic aggregation<br/>[stat.go]"]
  node_ui["TUI views<br/>[ui.go]"]
end

node_user(("Operator"))
node_pcap_service["Packet capture"]
node_os_metadata["OS socket metadata"]

node_user -->|"sets options"| node_cli
node_cli -->|"starts"| node_sniffer
node_cli -->|"lists devices"| node_device_list
node_sniffer -->|"creates client"| node_capture_client
node_sniffer -->|"creates resolver"| node_dns
node_sniffer -->|"selects fetcher"| node_socket_fetcher
node_capture_client -->|"selects devices"| node_device_list
node_capture_other -->|"selects devices"| node_device_list
node_capture_client -->|"reads packets"| node_pcap_service
node_capture_other -->|"reads packets"| node_pcap_service
node_capture_client -->|"records segments"| node_packet_sink
node_capture_other -->|"records segments"| node_packet_sink
node_capture_client -->|"resolves addresses"| node_dns
node_capture_other -->|"resolves addresses"| node_dns
node_socket_fetcher -->|"implements"| node_linux_sockets
node_linux_sockets -->|"reads netlink and proc"| node_os_metadata
node_mac_sockets -->|"invokes lsof"| node_os_metadata
node_windows_sockets -->|"queries processes"| node_os_metadata
node_sniffer -->|"takes utilization"| node_packet_sink
node_sniffer -->|"gets open sockets"| node_socket_fetcher
node_sniffer -->|"supplies measurements"| node_stats
node_stats -->|"matches and aggregates"| node_stats
node_sniffer -->|"renders snapshot"| node_ui
node_ui -->|"ranks view data"| node_stats

click node_cli "https://github.com/chenjiandongx/sniffer/blob/master/cli.go"
click node_sniffer "https://github.com/chenjiandongx/sniffer/blob/master/sniffer.go"
click node_capture_client "https://github.com/chenjiandongx/sniffer/blob/master/pcap_linux.go"
click node_capture_other "https://github.com/chenjiandongx/sniffer/blob/master/pcap_others.go"
click node_packet_sink "https://github.com/chenjiandongx/sniffer/blob/master/pcap.go"
click node_dns "https://github.com/chenjiandongx/sniffer/blob/master/dns.go"
click node_socket_fetcher "https://github.com/chenjiandongx/sniffer/blob/master/conn_linux.go"
click node_linux_sockets "https://github.com/chenjiandongx/sniffer/blob/master/conn_linux.go"
click node_mac_sockets "https://github.com/chenjiandongx/sniffer/blob/master/conn_darwin.go"
click node_windows_sockets "https://github.com/chenjiandongx/sniffer/blob/master/conn_windows.go"
click node_stats "https://github.com/chenjiandongx/sniffer/blob/master/stat.go"
click node_ui "https://github.com/chenjiandongx/sniffer/blob/master/ui.go"
click node_device_list "https://github.com/chenjiandongx/sniffer/blob/master/pcap.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_sniffer toneBlue
class node_capture_client,node_capture_other,node_packet_sink,node_dns,node_device_list toneAmber
class node_socket_fetcher,node_linux_sockets,node_mac_sockets,node_windows_sockets toneMint
class node_stats,node_ui toneRose
class node_user,node_pcap_service,node_os_metadata toneIndigo
```

## Components

### Command and orchestration

- [CLI options](https://github.com/chenjiandongx/sniffer/blob/master/cli.go): `cli.go`
- [Sniffer loop](https://github.com/chenjiandongx/sniffer/blob/master/sniffer.go): `sniffer.go`

### Packet capture

- [Capture client](https://github.com/chenjiandongx/sniffer/blob/master/pcap_linux.go): `pcap_linux.go`
- [Other platforms](https://github.com/chenjiandongx/sniffer/blob/master/pcap_others.go): `pcap_others.go`
- [Traffic sink](https://github.com/chenjiandongx/sniffer/blob/master/pcap.go): `pcap.go`
- [DNS resolver](https://github.com/chenjiandongx/sniffer/blob/master/dns.go): `dns.go`
- [Device discovery](https://github.com/chenjiandongx/sniffer/blob/master/pcap.go): `pcap.go`

### Connection attribution

- [Socket fetcher](https://github.com/chenjiandongx/sniffer/blob/master/conn_linux.go): `conn_linux.go`
- [Linux socket mapping](https://github.com/chenjiandongx/sniffer/blob/master/conn_linux.go): `conn_linux.go`
- [macOS socket mapping](https://github.com/chenjiandongx/sniffer/blob/master/conn_darwin.go): `conn_darwin.go`
- [Windows socket mapping](https://github.com/chenjiandongx/sniffer/blob/master/conn_windows.go): `conn_windows.go`

### Aggregation and views

- [Traffic aggregation](https://github.com/chenjiandongx/sniffer/blob/master/stat.go): `stat.go`
- [TUI views](https://github.com/chenjiandongx/sniffer/blob/master/ui.go): `ui.go`

### Other components

- **Operator**
- **Packet capture**
- **OS socket metadata**

## Connections

- Operator → CLI options: sets options (evidence: `cli.go`)
- CLI options → Sniffer loop: starts (evidence: `cli.go`)
- CLI options → Device discovery: lists devices (evidence: `cli.go`)
- Sniffer loop → Capture client: creates client (evidence: `sniffer.go`)
- Sniffer loop → DNS resolver: creates resolver (evidence: `sniffer.go`)
- Sniffer loop → Socket fetcher: selects fetcher (evidence: `sniffer.go`)
- Capture client → Device discovery: selects devices (evidence: `pcap_linux.go`)
- Other platforms → Device discovery: selects devices (evidence: `pcap_others.go`)
- Capture client → Packet capture: reads packets (evidence: `pcap_linux.go`)
- Other platforms → Packet capture: reads packets (evidence: `pcap_others.go`)
- Capture client → Traffic sink: records segments (evidence: `pcap_linux.go`)
- Other platforms → Traffic sink: records segments (evidence: `pcap_others.go`)
- Capture client → DNS resolver: resolves addresses (evidence: `pcap_linux.go`)
- Other platforms → DNS resolver: resolves addresses (evidence: `pcap_others.go`)
- Socket fetcher → Linux socket mapping: implements (evidence: `conn_linux.go`)
- Linux socket mapping → OS socket metadata: reads netlink and proc (evidence: `conn_linux.go`)
- macOS socket mapping → OS socket metadata: invokes lsof (evidence: `conn_darwin.go`)
- Windows socket mapping → OS socket metadata: queries processes (evidence: `conn_windows.go`)
- Sniffer loop → Traffic sink: takes utilization (evidence: `sniffer.go`)
- Sniffer loop → Socket fetcher: gets open sockets (evidence: `sniffer.go`)
- Sniffer loop → Traffic aggregation: supplies measurements (evidence: `sniffer.go`)
- Traffic aggregation → Traffic aggregation: matches and aggregates (evidence: `stat.go`)
- Sniffer loop → TUI views: renders snapshot (evidence: `sniffer.go`)
- TUI views → Traffic aggregation: ranks view data (evidence: `ui.go`)
