# matinsenpai/senpaiscanner architecture

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

- Interactive diagram: https://gitdiagram.com/matinsenpai/senpaiscanner
- Repository: https://github.com/matinsenpai/senpaiscanner

## Overview

1. **What this repository is**
   - `senpaiscanner` is a Go command-line application with a full terminal UI for finding Cloudflare IPs that are reachable and fast from the user’s network.
   - It is not a library-first project: the executable entrypoint is `cmd/senpaiscanner`, and most behavior lives in `internal/*`.

2. **Top-level runtime shape**
   - The app is a **single interactive TUI process**: the user opens the menu, chooses a scan mode, and the UI drives the rest of the workflow.
   - There are no scan-oriented CLI subcommands or flags in normal use; the only common non-TUI path is version reporting.

3. **UI / interaction boundary**
   - `internal/ui` contains the terminal screens and navigation state:
     - `model.go` holds UI state,
     - `pages.go` defines the menu and screen transitions,
     - `cmds.go` likely defines asynchronous actions/messages for scans and navigation.
   - This layer is the boundary between human input and the scanning engine. It starts scans, shows live tables, handles cancel/back, and later shows the results screen.

4. **Configuration boundary**
   - `internal/config` is the schema for scan settings coming from the TUI forms.
   - It captures the scan mode and operational parameters such as count, workers, timeout, tries, port, CIDR filter, SNI, IPv4/IPv6 toggles, colo filter, and output path.
   - Quick Scan, Custom Scan, Test IPs, and Discover Colos are all different presets feeding the same lower-level scan machinery.

5. **Scan orchestration**
   - `internal/engine` is the coordinator between configuration, IP source, probing, result aggregation, and output.
   - It owns the lifecycle of a scan: start, fan out work to workers, collect probe results, handle cancellation, and decide when the UI should move to Results.
   - This is the main “business logic” boundary of the repo.

6. **IP source generation**
   - `internal/ipsrc` supplies candidate Cloudflare addresses.
   - The repository embeds Cloudflare IPv4/IPv6 range snapshots in `ranges_v4.txt` and `ranges_v6.txt`, rather than fetching them at runtime.
   - That means address selection is deterministic per build and independent of network access until probing begins.

7. **Probing / validation layer**
   - `internal/prober` performs the actual network checks against candidate IPs.
   - The README shows three materially different validation modes:
     - **TCP**: socket reachability,
     - **TLS**: handshake validation,
     - **HTTP/HTTPS**: real request validation through Cloudflare endpoints.
   - External endpoints matter here: `speed.cloudflare.com` is used for trace/download behavior, and `/cdn-cgi/trace` plus `CF-Ray` are used to identify the colo.

8. **Result model and ranking**
   - `internal/result` is the shared data model for probe outcomes.
   - It holds the metrics the scanner ranks by: latency, loss, jitter, download throughput, colo, TLS/HTTP status, and whether a speed test was performed.
   - The UI’s “Results” page and live scan table are both driven from this result abstraction.

9. **Output pipeline**
   - `internal/output` is the file-export boundary.
   - When the user provides an output path in Custom Scan, results are streamed to disk in one of three formats: CSV, JSON, or TXT.
   - Important architectural point: output is incremental during the scan, not only after completion.

10. **User-visible scan modes map to one backend**
   - **Quick Scan** and **Custom Scan** are the same engine with different presets and UI entry points.
   - **Test IPs** reads `ips.txt` from the working directory and validates a user-provided list rather than random Cloudflare ranges.
   - **Discover Colos** is a specialized HTTP-only sweep that groups healthy results by colo and reports average/best latency.
   - This shows the repo is centered on a shared probing pipeline with multiple front-end workflows.

11. **External services and network assumptions**
   - The app depends on Cloudflare’s public infrastructure, especially their published IP ranges and trace/speed endpoints.
   - It is designed for unstable or restricted networks, so concurrency, timeout, retry count, and protocol choice materially affect behavior.
   - The colo filter and SNI handling exist because the network path and TLS identity are important to real-world success, not just raw reachability.

12. **Build, release, and packaging**
   - The project uses standard Go tooling plus `Makefile`, GitHub Actions (`ci.yml`, `release.yml`), and GoReleaser (`.goreleaser.yaml`) to produce platform binaries.
   - `install.sh` provides a shell-based install path from GitHub releases.
   - `pkg/version` centralizes version stamping for the binary and `--version` output.

13. **Architecture summary for a diagram**
   - A clean diagram would show: **terminal user → UI/state machine → config → engine → ipsrc + prober → result aggregation → UI/results and optional output file**.
   - Cloudflare endpoints sit outside the app as the external network dependency, while embedded IP range files are an internal static data source.

Main entry points: [`internal/engine/engine.go`](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/engine/engine.go) (Engine), [`internal/ipsrc/ipsrc.go`](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ipsrc.go) (IP source), [`internal/prober/prober.go`](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/prober/prober.go) (Prober), [`cmd/senpaiscanner/main.go`](https://github.com/matinsenpai/senpaiscanner/blob/main/cmd/senpaiscanner/main.go) (Main)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_user(("User<br/>operator"))
  node_entrypoint["Main<br/>cli entrypoint<br/>[main.go]"]
  node_ui_model["UI state<br/>[model.go]"]
  node_ui_pages["Pages<br/>tui screens<br/>[pages.go]"]
  node_ui_cmds["UI cmds<br/>tui actions<br/>[cmds.go]"]
  node_banner["Banner<br/>ui branding<br/>[banner.go]"]
  node_terminal(("Terminal UI<br/>runtime surface"))
end

subgraph group_core["Core"]
  node_config["Config<br/>scan settings<br/>[config.go]"]
  node_engine["Engine<br/>scan orchestrator<br/>[engine.go]"]
  node_ipsrc["IP source<br/>candidate generator<br/>[ipsrc.go]"]
  node_prober["Prober<br/>network checks<br/>[prober.go]"]
  node_output["Output<br/>export pipeline<br/>[output.go]"]
end

subgraph group_data["Data"]
  node_result["Results<br/>scan model<br/>[result.go]"]
  node_ranges_v4["IPv4 ranges<br/>embedded data<br/>[ranges_v4.txt]"]
  node_ranges_v6["IPv6 ranges<br/>embedded data<br/>[ranges_v6.txt]"]
end

subgraph group_release["Release"]
  node_version["Version<br/>build metadata<br/>[version.go]"]
end

node_cloudflare(("Cloudflare<br/>external service"))
node_results_file["Results file<br/>export target"]

node_user -->|"uses"| node_terminal
node_terminal -->|"navigates"| node_ui_pages
node_ui_pages -->|"updates"| node_ui_model
node_ui_pages -->|"triggers"| node_ui_cmds
node_ui_cmds -->|"builds"| node_config
node_config -->|"feeds"| node_engine
node_engine -->|"requests"| node_ipsrc
node_ipsrc -->|"reads"| node_ranges_v4
node_ipsrc -->|"reads"| node_ranges_v6
node_ipsrc -->|"yields"| node_prober
node_engine -->|"orchestrates"| node_prober
node_prober -->|"targets"| node_cloudflare
node_prober -->|"emits"| node_result
node_engine -->|"aggregates"| node_result
node_result -->|"renders"| node_ui_model
node_engine -->|"streams"| node_output
node_output -->|"writes"| node_results_file
node_entrypoint -->|"reports"| node_version
node_entrypoint -->|"shows"| node_banner
node_entrypoint -->|"starts"| node_ui_model

click node_entrypoint "https://github.com/matinsenpai/senpaiscanner/blob/main/cmd/senpaiscanner/main.go"
click node_ui_model "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/model.go"
click node_ui_pages "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/pages.go"
click node_ui_cmds "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/cmds.go"
click node_config "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/config/config.go"
click node_engine "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/engine/engine.go"
click node_ipsrc "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ipsrc.go"
click node_prober "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/prober/prober.go"
click node_result "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/result/result.go"
click node_output "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/output/output.go"
click node_ranges_v4 "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ranges_v4.txt"
click node_ranges_v6 "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ranges_v6.txt"
click node_banner "https://github.com/matinsenpai/senpaiscanner/blob/main/internal/banner/banner.go"
click node_version "https://github.com/matinsenpai/senpaiscanner/blob/main/pkg/version/version.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_user,node_entrypoint,node_ui_model,node_ui_pages,node_ui_cmds,node_banner,node_terminal toneBlue
class node_config,node_engine,node_ipsrc,node_prober,node_output toneAmber
class node_result,node_ranges_v4,node_ranges_v6 toneMint
class node_version toneRose
class node_cloudflare,node_results_file toneNeutral
```

## Components

### Runtime

Interactive application flow

- **User**: operator · Terminal operator driving scans
- [Main](https://github.com/matinsenpai/senpaiscanner/blob/main/cmd/senpaiscanner/main.go): cli entrypoint · Program startup and version dispatch · `cmd/senpaiscanner/main.go`
- [UI state](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/model.go): tui state · Screen state and navigation model · `internal/ui/model.go`
- [Pages](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/pages.go): tui screens · Menu and screen transitions · `internal/ui/pages.go`
- [UI cmds](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ui/cmds.go): tui actions · Async commands and messages · `internal/ui/cmds.go`
- [Banner](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/banner/banner.go): ui branding · Startup banner and branding text · `internal/banner/banner.go`
- **Terminal UI**: runtime surface · Interactive screen presented to the user

### Core

Scanning and data pipeline

- [Config](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/config/config.go): scan settings · Scan modes and operational parameters · `internal/config/config.go`
- [Engine](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/engine/engine.go): scan orchestrator · Coordinates scanning lifecycle and cancellation · `internal/engine/engine.go`
- [IP source](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ipsrc.go): candidate generator · Produces Cloudflare IP candidates · `internal/ipsrc/ipsrc.go`
- [Prober](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/prober/prober.go): network checks · TCP, TLS, and HTTP validation · `internal/prober/prober.go`
- [Output](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/output/output.go): export pipeline · Streams scan results to files · `internal/output/output.go`

### Data

Static inputs and shared models

- [Results](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/result/result.go): scan model · Shared probe outcomes and ranking metrics · `internal/result/result.go`
- [IPv4 ranges](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ranges_v4.txt): embedded data · Bundled Cloudflare IPv4 snapshot · `internal/ipsrc/ranges_v4.txt`
- [IPv6 ranges](https://github.com/matinsenpai/senpaiscanner/blob/main/internal/ipsrc/ranges_v6.txt): embedded data · Bundled Cloudflare IPv6 snapshot · `internal/ipsrc/ranges_v6.txt`

### Release

Build and packaging

- [Version](https://github.com/matinsenpai/senpaiscanner/blob/main/pkg/version/version.go): build metadata · Central version stamping · `pkg/version/version.go`

### Other components

- **Cloudflare**: external service · Published ranges and trace/speed endpoints
- **Results file**: export target · CSV, JSON, or TXT scan output

## Connections

- User → Terminal UI: uses · Operates the interactive scanner
- Terminal UI → Pages: navigates · Moves through menu and scan screens
- Pages → UI state: updates · Maintains current screen and scan state
- Pages → UI cmds: triggers · Starts scans and async actions
- UI cmds → Config: builds · Collects scan settings from forms
- Config → Engine: feeds · Passes scan mode and parameters
- Engine → IP source: requests · Gets candidate IP addresses
- IP source → IPv4 ranges: reads · Uses bundled IPv4 ranges
- IP source → IPv6 ranges: reads · Uses bundled IPv6 ranges
- IP source → Prober: yields · Supplies candidate addresses for validation
- Engine → Prober: orchestrates · Fans out probes and handles cancellation
- Prober → Cloudflare: targets · Checks reachability and Cloudflare behavior
- Prober → Results: emits · Produces probe outcomes and metrics
- Engine → Results: aggregates · Ranks and collects results
- Results → UI state: renders · Drives live table and results screen
- Engine → Output: streams · Writes incremental scan output
- Output → Results file: writes · Exports CSV, JSON, or TXT
- Main → Version: reports · Supports version output
- Main → Banner: shows · Displays startup branding
- Main → UI state: starts · Launches the interactive application
