netwatch-tui 0.3.0

Real-time network diagnostics in your terminal — like htop for your network
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# NetWatch

**Real-time network diagnostics in your terminal — like htop for your network.**

NetWatch is a lightweight, keyboard-driven TUI application that gives you instant visibility into network traffic, active connections, interface health, live packet capture with deep protocol inspection, network topology mapping, connection timelines, and AI-powered network insights. Built with Rust for speed and low overhead.

![Rust](https://img.shields.io/badge/Rust-000000?logo=rust&logoColor=white)
![Platform](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-blue)
![License](https://img.shields.io/badge/license-MIT-green)

---

## Demo

<p align="center">
  <img src="demo.gif" alt="NetWatch demo — Dashboard, Connections, Interfaces, and Help overlay" width="800">
</p>

> Dashboard with live interface stats, bandwidth graphs, top connections, health probes, and latency heatmap. Packet capture with deep protocol inspection available when run with `sudo`.

---

## Features

- **Live interface monitoring** — RX/TX rates, totals, and 60-second sparkline history for every network interface
- **Aggregate bandwidth graph** — Full-width RX/TX sparklines across all active interfaces on the Dashboard
- **Active connections** — Every open socket with process name, PID, protocol, state, and addresses (sortable)
- **Network health** — ICMP ping probes to gateway and DNS with RTT and packet loss
- **Latency heatmap** — Color-coded RTT history for gateway and DNS on the Dashboard
- **Packet capture** — Wireshark-style live capture with deep protocol decoding:
  - **DNS** — Query names, types (A, AAAA, CNAME…), response codes
  - **TLS** — Handshake type, version, SNI hostname extraction
  - **HTTP** — Method, path, and response status lines
  - **ICMP** — Human-readable type/code (Echo Request, Dest Unreachable, TTL Exceeded…)
  - **ARP, DHCP, NTP, mDNS** — Decoded with meaningful summaries
  - **TCP payload** — Readable text content extracted and displayed
  - **25+ service labels** — Ports mapped to names (SSH, HTTPS, PostgreSQL, Redis…)
- **TCP stream reassembly** — Follow TCP/UDP conversations with text and hex views
- **TCP handshake timing** — Automatic SYN→SYN-ACK→ACK latency measurement per connection
- **Display filters** — Wireshark-style filter bar with protocol, IP, port, stream, text search, and/or/not combinators
- **BPF capture filters** — Set Berkeley Packet Filter expressions applied at capture time
- **Expert info & coloring** — Automatic severity classification (Error/Warn/Note/Chat) with color-coded rows
- **Packet bookmarks** — Mark packets of interest, jump between bookmarks
- **PCAP export** — Save captured packets (or filtered subset) to standard .pcap files
- **Protocol statistics** — Protocol hierarchy table with packet counts, byte totals, and distribution bars
- **Handshake histogram** — Latency distribution chart with min/avg/median/p95/max stats
- **GeoIP location** — Background IP geolocation with country, city, and org display
- **Whois lookup** — On-demand RDAP whois for any IP address
- **Connection → packet linking** — Jump from a connection to filtered packet view
- **Help overlay** — Full scrollable keybinding reference with filter syntax and expert legend
- **Network config** — Default gateway, DNS servers, hostname at a glance
- **Cross-platform** — macOS, Linux, and Windows with platform-specific collectors
- **Network topology** — ASCII box diagram showing local machine, gateway, DNS servers, and top remote hosts with connection counts and health indicators
- **Connection timeline** — Gantt-style bar chart of connection lifetimes, color-coded by state with adjustable time windows (30s to 1h)
- **AI network insights** — Real-time AI analysis via Ollama (llama3.2). Auto-analyzes every 15s, on-demand with `a` key. Detects security concerns, performance issues, and anomalies

---

## Install

### From crates.io

```bash
cargo install netwatch-tui
```

### From source

```bash
git clone https://github.com/matthart1983/netwatch.git
cd netwatch
cargo build --release
```

### Prerequisites

- **Rust** toolchain (1.70+): https://rustup.rs
- **libpcap** (for packet capture):
  - macOS: included with Xcode Command Line Tools
  - Linux: `sudo apt install libpcap-dev` (Debian/Ubuntu) or `sudo dnf install libpcap-devel` (Fedora)
  - Windows: install [Npcap]https://npcap.com/ with "Install Npcap in WinPcap API-compatible Mode" checked

### Run

```bash
# Basic mode — interface stats, connections, config
netwatch

# Full mode — adds health probes + packet capture (requires root for BPF/ICMP)
sudo netwatch
```

---

## Tabs

NetWatch has eight tabs, switched with number keys `1`–`8`:

### `1` Dashboard

The default view. Everything at a glance:

- **Interfaces** — All network interfaces with live RX/TX rates and UP/DOWN status
- **Bandwidth graph** — Full-width aggregate RX/TX sparklines across all active interfaces (last 60s)
- **Top connections** — The 5 most active established connections
- **Health** — Gateway and DNS latency with packet loss percentage
- **Latency heatmap** — Color-coded RTT history bars for gateway and DNS (green→yellow→orange→red)

### `2` Connections

Full scrollable list of every active network socket:

| Process | PID | Proto | State | Local Address | Remote Address | Location |
|---------|-----|-------|-------|---------------|----------------|----------|
| firefox | 1234 | TCP | ESTABLISHED | 192.168.1.42:54321 | 142.250.1.1:443 | US Mountain View, Google |

- Press `s` to cycle the sort column
- Press `Enter` to jump to Packets tab with a filter matching the selected connection
- Press `W` for whois lookup on the remote IP
- Press `g` to toggle GeoIP location column

### `3` Interfaces

Detailed per-interface view with:
- IPv4 and IPv6 addresses, MAC address, MTU
- Total RX/TX bytes, packets, errors, and drops
- Individual sparkline history per interface

### `4` Packets

Live packet capture with Wireshark-style protocol inspection:

- **Packet list** — Scrollable table with expert severity indicator, stream index, protocol coloring
- **Protocol detail** — Layer-by-layer decode (Ethernet → IP → TCP/UDP → Application)
- **GeoIP & Whois** — Location and network ownership in the detail pane
- **Handshake timing**`⏱ SYN→SYN-ACK: 5.2ms │ SYN-ACK→ACK: 3.1ms │ Total: 8.3ms`
- **Payload content** — Readable text extracted from application data
- **Hex/ASCII dump** — Raw packet bytes with side-by-side hex and ASCII
- **Stream view** — Press `s` to follow the TCP/UDP conversation with direction arrows
- **Bookmarks** — Press `m` to mark packets, `n`/`N` to jump between them

**Decoded protocols:**

| Protocol | What's shown |
|----------|-------------|
| DNS | Query/Response, domain name, record type (A, AAAA, MX…), response code |
| TLS | Client Hello / Server Hello, TLS version, SNI hostname |
| HTTP | Full request line (method, path, version) or response status |
| ICMP | Echo Request/Reply, Dest Unreachable (with reason), TTL Exceeded |
| ARP | "Who has 192.168.1.1? Tell 192.168.1.42" |
| DHCP | Discover/Offer/Request/ACK |
| NTP | Version and mode (Client/Server/Broadcast) |

### `5` Stats

Protocol statistics and performance analysis:

- **Protocol hierarchy** — Table of all seen protocols with packet counts, byte totals, percentages, and distribution bars
- **Handshake histogram** — TCP handshake latency distribution across 7 buckets (<1ms to >500ms) with min/avg/median/p95/max summary

### `6` Topology

ASCII network topology map showing your machine's network neighbourhood:

- **Local machine** — Hostname, active interfaces, aggregate bandwidth
- **Infrastructure** — Gateway and DNS servers with health indicators (RTT, loss)
- **Remote hosts** — Top destinations sorted by connection count, with process names
- **Health dots** — Color-coded `` indicators (green/yellow/red) for latency and loss
- Press `Enter` to jump to Connections tab filtered to the selected host

### `7` Timeline

Gantt-style connection timeline showing when connections were active:

- **Horizontal bars** — Each row is a connection (process + remote), bar spans first-seen to last-seen
- **Color-coded** — Green (ESTABLISHED), Yellow (LISTEN), Cyan (SYN), Red (closing states)
- **Time windows** — Press `t` to cycle: 30s, 1m, 5m, 15m, 1h
- Press `Enter` to jump to Connections tab for the selected entry

### `8` Insights

AI-powered network analysis via local Ollama:

- **Auto-analysis** — Sends network snapshots to Ollama every 15 seconds
- **On-demand** — Press `a` from any tab for immediate analysis
- **Detects** — Security concerns, performance issues, anomalies, connection health
- **Graceful fallback** — Shows setup instructions if Ollama is unavailable
- Uses `llama3.2` model by default

---

## Keyboard Controls

### Global

| Key | Action |
|-----|--------|
| `1``8` | Switch tab: Dashboard / Connections / Interfaces / Packets / Stats / Topology / Timeline / Insights |
| `a` | Request AI analysis (from any tab) |
| `` `` | Scroll / select |
| `p` | Pause / resume all data collection |
| `r` | Force refresh all data |
| `g` | Toggle GeoIP location display |
| `?` | Show help overlay |
| `q` / `Ctrl+C` | Quit |

### Connections tab

| Key | Action |
|-----|--------|
| `s` | Cycle sort column |
| `Enter` | Jump to Packets tab with auto-filter for selected connection |
| `W` | Whois lookup for selected connection's remote IP |

### Packets tab

| Key | Action |
|-----|--------|
| `c` | Start / stop packet capture |
| `i` | Cycle capture interface (while stopped) |
| `b` | Set BPF capture filter (while stopped) |
| `/` | Open display filter bar |
| `Esc` | Clear display filter |
| `Enter` | Select packet at cursor |
| `s` | Open stream view for selected packet |
| `w` | Export packets to .pcap file |
| `f` | Toggle auto-follow (scroll to newest) |
| `x` | Clear all captured packets |
| `m` | Toggle bookmark on selected packet |
| `n` / `N` | Jump to next / previous bookmark |
| `W` | Whois lookup for selected packet's IPs |

### Stream view (within Packets tab)

| Key | Action |
|-----|--------|
| `Esc` | Close stream view |
| `` `` | Scroll stream content |
| `` `` | Filter to A→B / B→A direction |
| `a` | Show both directions |
| `h` | Toggle hex / text mode |

### Topology tab

| Key | Action |
|-----|--------|
| `` `` | Scroll through remote hosts |
| `Enter` | Jump to Connections tab for selected host |

### Timeline tab

| Key | Action |
|-----|--------|
| `` `` | Scroll through connections |
| `t` | Cycle time window (30s / 1m / 5m / 15m / 1h) |
| `Enter` | Jump to Connections tab for selected entry |

### Insights tab

| Key | Action |
|-----|--------|
| `a` | Trigger on-demand AI analysis |
| `` `` | Scroll insights |

### Display filter syntax

| Filter | Example | Matches |
|--------|---------|---------|
| Protocol | `tcp`, `udp`, `dns`, `icmp`, `arp` | Protocol field match |
| IP address | `192.168.1.42` | Source or destination IP |
| Directional IP | `ip.src == 10.0.0.1` | Source IP only |
| Port | `port 443` | Source or destination port |
| Stream | `stream 7` | Stream index match |
| Text search | `contains "hello"` | Search info, payload, IPs |
| Negation | `!dns`, `not arp` | Invert match |
| Combinators | `tcp and port 443`, `dns or icmp` | Logical AND / OR |
| Bare word | `google` | Shorthand for `contains "google"` |

---

## Permissions

NetWatch works in two modes:

| Feature | Without `sudo` | With `sudo` |
|---------|---------------|-------------|
| Interface stats & rates |||
| Active connections |||
| Network configuration |||
| Health probes (ICMP ping) | ❌ Shows N/A ||
| Packet capture | ❌ Permission denied ||
| AI insights (Ollama) | ✅ (if Ollama running) | ✅ (if Ollama running) |

The app degrades gracefully — features that require elevated privileges show a clear message rather than crashing.

### macOS BPF permissions

Packet capture on macOS requires access to `/dev/bpf*` devices, which are root-only by default. You have two options:

1. **Run with sudo** (recommended for occasional use):
   ```bash
   sudo ./target/release/netwatch
   ```

2. **Open BPF devices** (persistent, for frequent use):
   ```bash
   sudo chmod 644 /dev/bpf*
   ```
   > ⚠️ This allows any user to capture packets. Resets on reboot.

---

## Project Structure

```
netwatch/
├── Cargo.toml
├── src/
│   ├── main.rs                  # Entry point, terminal setup
│   ├── app.rs                   # App state, event loop, tab management
│   ├── event.rs                 # Keyboard & tick event handling
│   ├── ui/
│   │   ├── dashboard.rs         # Dashboard composite view
│   │   ├── connections.rs       # Connections table view
│   │   ├── interfaces.rs        # Interface detail view
│   │   ├── packets.rs           # Packet capture & inspection view
│   │   ├── stats.rs             # Protocol statistics & handshake histogram
│   │   ├── topology.rs          # Network topology map view
│   │   ├── timeline.rs          # Connection timeline view
│   │   ├── insights.rs          # AI network insights view
│   │   ├── help.rs              # Scrollable help overlay
│   │   └── widgets.rs           # Formatting helpers
│   ├── collectors/
│   │   ├── traffic.rs           # Interface RX/TX byte polling & rate calc
│   │   ├── connections.rs       # Socket enumeration + PID mapping
│   │   ├── config.rs            # Gateway, DNS, hostname discovery
│   │   ├── health.rs            # ICMP ping probes + RTT history
│   │   ├── packets.rs           # libpcap capture + protocol decoding + stream tracking
│   │   ├── geo.rs               # Background GeoIP lookup (ip-api.com)
│   │   ├── insights.rs          # AI insights via Ollama
│   │   └── whois.rs             # Background RDAP whois lookup
│   └── platform/
│       ├── linux.rs             # Linux /proc, /sys collectors
│       └── macos.rs             # macOS ifconfig, netstat collectors
├── SPEC.md                      # Design specification
└── README.md
```

---

## How It Works

### Data Collection

| Collector | Interval | Source (macOS) | Source (Linux) |
|-----------|----------|----------------|----------------|
| Interface stats | 1s | `netstat -ib` | `/sys/class/net/*/statistics` |
| Interface info | 10s | `ifconfig` | `/sys/class/net/*` + `ip addr` |
| Connections | 2s | `lsof -i -n -P` | `/proc/net/tcp` + `/proc/*/fd` |
| Config | 10s | `netstat -rn`, `scutil --dns` | `ip route`, `/etc/resolv.conf` |
| Health | 5s | `ping -c 3 -t 1` | `ping -c 3 -W 1` |
| Packets | Real-time | libpcap (BPF) | libpcap |
| GeoIP | On-demand | ip-api.com (HTTP) | ip-api.com (HTTP) |
| Whois | On-demand | rdap.org (HTTPS) | rdap.org (HTTPS) |

### Packet Decoding Pipeline

```
Raw bytes → Ethernet → IPv4/IPv6/ARP → TCP/UDP/ICMP → DNS/TLS/HTTP/DHCP/NTP
                              Stream tracking (per 4-tuple)
                              TCP handshake timing (SYN/SYN-ACK/ACK)
                              Expert info classification
                              Payload text extraction
```

---

## Dependencies

| Crate | Purpose |
|-------|---------|
| [ratatui]https://crates.io/crates/ratatui | Terminal UI framework |
| [crossterm]https://crates.io/crates/crossterm | Cross-platform terminal manipulation |
| [tokio]https://crates.io/crates/tokio | Async runtime |
| [pcap]https://crates.io/crates/pcap | libpcap bindings for packet capture |
| [nix]https://crates.io/crates/nix | Unix system call wrappers |
| [chrono]https://crates.io/crates/chrono | Timestamps |
| [anyhow]https://crates.io/crates/anyhow | Error handling |
| [ureq]https://crates.io/crates/ureq | HTTP client (GeoIP, Whois, Ollama AI) |
| [serde_json]https://crates.io/crates/serde_json | JSON parsing (API responses) |

---

## Troubleshooting

| Problem | Solution |
|---------|----------|
| `Permission denied` on packet capture | Run with `sudo` |
| `BIOCPROMISC: operation not supported` | Interface doesn't support promiscuous mode — NetWatch falls back automatically |
| Health shows `N/A` | ICMP ping requires root — run with `sudo` |
| No connections listed | `lsof` (macOS) or `/proc` (Linux) access may be restricted |
| Binary not found after build | Check `./target/release/netwatch` exists |
| Blank screen | Ensure terminal supports 256 colors and is at least 80×24 |
| GeoIP/Whois not loading | Requires internet access; results appear after a short delay |
| AI insights shows "Ollama unavailable" | Install and start Ollama: `ollama serve`, then `ollama pull llama3.2` |
| AI analysis is slow | Ollama runs locally — performance depends on your hardware. Consider a smaller model |

---

## Contributing

1. Fork the repo
2. Create a feature branch: `git checkout -b feature/my-feature`
3. Make your changes and test with `cargo build --release`
4. Submit a pull request

---

## License

MIT