# bia-pain-bache/cloudflare-clean-ip-scanner architecture

> Architecture diagram of the GitHub repository bia-pain-bache/cloudflare-clean-ip-scanner, 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/bia-pain-bache/cloudflare-clean-ip-scanner
- Repository: https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner

## Overview

1. This repository is a single Go command-line binary, not a service. Its job is to benchmark Cloudflare CDN IPs and pick the fastest candidate based on latency, loss, and optional download throughput.

2. `main.go` is the orchestration layer. It parses flags, loads the IP inventory, chooses the probe mode, runs the test pipeline, sorts/filter results, and controls whether results are printed or written to disk.

3. The input boundary is file- and flag-driven. The default IP datasets are `ip.txt` for IPv4 and `ipv6.txt` for IPv6, but users can also supply custom ranges via `-f` or inline ranges via `-ip`. The tool supports both single IPs and CIDR ranges, and can either sample one IP per range or test every IPv4 address with `-allip`.

4. The probing subsystem lives under `task/` and is split by responsibility:
   - `task/tcping.go` for default TCP-based latency tests
   - `task/httping.go` for HTTP-based latency tests and region matching
   - `task/download.go` for sequential download speed testing
   - `task/ip.go` for IP-range parsing, expansion, and candidate selection

5. The pipeline is staged. First it performs a concurrency-heavy latency sweep across the candidate IPs. Then it filters by configured latency and packet-loss bounds. Only the remaining “queue” proceeds to the slower, one-by-one download benchmark, which is why the final result set can be much smaller than the initial probe count.

6. There are two distinct latency modes with different semantics. TCPing is the default and lighter-weight. HTTPing is used when the user wants protocol-level reachability checks against a specific URL, and it also enables Cloudflare/AWS CloudFront colo filtering by airport code. That means the architecture has both transport-level and application-level probe paths.

7. The output subsystem is split between terminal progress and CSV persistence. `utils/progress.go` handles live progress-bar/status reporting, while `utils/csv.go` serializes the final ranking to `result.csv` or a user-specified path. The result schema is fixed around sent/received counts, loss rate, average latency, and download speed.

8. Sorting and ranking happen after each stage. Latency is used as the first gate, then download speed becomes the primary ranking signal when the download phase is enabled. If download testing is disabled with `-dd`, the final ordering is latency-based instead.

9. The repo is designed for offline execution with external network dependencies only at probe time. It talks to Cloudflare endpoints or user-specified URLs to measure connectivity and throughput, but it does not depend on a backend API or database.

10. Platform-wise, the application targets Windows and Linux as a portable Go binary. The GitHub Actions workflow under `.github/workflows/build.yml` is for CI packaging/release builds, while `go.mod`/`go.sum` define the Go runtime and dependencies.

11. For an architecture diagram, the most important boundaries are:
   - configuration/input layer: CLI flags + IP files
   - probe engine: TCPing/HTTPing/download workers
   - filtering/sorting layer: latency, loss, speed thresholds
   - presentation/output layer: console progress + CSV export

12. The core data flow is:
   IP inventory in → candidate selection/expansion → concurrent latency probes → threshold filtering → optional serialized download probes → ranking → terminal output and/or CSV file.

Main entry points: [`main.go`](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/main.go) (main.go), [`task/httping.go`](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/httping.go) (HTTPing), [`task/ip.go`](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/ip.go) (IP parser), [`task/tcping.go`](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/tcping.go) (TCPing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_input["Input & config"]
  node_main["main.go<br/>orchestrator<br/>[main.go]"]
  node_ip_files["IP lists<br/>inventory<br/>[ip.txt]"]
  node_ipv6_files["IPv6 list<br/>inventory<br/>[ipv6.txt]"]
end

subgraph group_group_probe["Probe engine"]
  node_ip_parse["IP parser<br/>range expansion<br/>[ip.go]"]
  node_tcping(("TCPing<br/>latency probe<br/>[tcping.go]"))
  node_httping(("HTTPing<br/>http probe<br/>[httping.go]"))
  node_download(("Download test<br/>throughput probe<br/>[download.go]"))
end

subgraph group_group_pipeline["Ranking pipeline"]
  node_latency_sweep["Latency sweep<br/>concurrent stage"]
  node_threshold_filter{{"Threshold filter<br/>quality gate"}}
  node_queue_stage["Download queue<br/>staged subset"]
  node_ranking["Ranking<br/>sorter"]
end

subgraph group_group_output["Output"]
  node_progress["Progress UI<br/>terminal status<br/>[progress.go]"]
  node_csv["CSV export<br/>result writer<br/>[csv.go]"]
end

node_cloudflare[("Cloudflare endpoints<br/>external target")]
node_user_url[("User URL<br/>external target")]
node_release_ci["Build workflow<br/>ci packaging<br/>[build.yml]"]
node_gomod["Go module<br/>dependency manifest<br/>[go.mod]"]

node_main -->|"reads"| node_ip_files
node_main -->|"reads"| node_ipv6_files
node_main -->|"loads"| node_ip_parse
node_ip_parse -->|"candidates"| node_latency_sweep
node_main -->|"default mode"| node_tcping
node_main -->|"optional mode"| node_httping
node_tcping -->|"measures"| node_cloudflare
node_httping -->|"measures"| node_cloudflare
node_httping -->|"optional"| node_user_url
node_latency_sweep -->|"results"| node_threshold_filter
node_threshold_filter -->|"passes"| node_queue_stage
node_queue_stage -->|"tests"| node_download
node_download -->|"scores"| node_ranking
node_threshold_filter -->|"sorts"| node_ranking
node_main -->|"reports"| node_progress
node_ranking -->|"writes"| node_csv
node_ranking -->|"returns"| node_main
node_main -.->|"builds"| node_release_ci
node_main -.->|"depends on"| node_gomod

click node_main "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/main.go"
click node_ip_files "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/ip.txt"
click node_ipv6_files "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/ipv6.txt"
click node_ip_parse "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/ip.go"
click node_tcping "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/tcping.go"
click node_httping "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/httping.go"
click node_download "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/download.go"
click node_progress "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/utils/progress.go"
click node_csv "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/utils/csv.go"
click node_release_ci "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/.github/workflows/build.yml"
click node_gomod "https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/go.mod"

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_main,node_ip_files,node_ipv6_files toneBlue
class node_ip_parse,node_tcping,node_httping,node_download toneAmber
class node_latency_sweep,node_threshold_filter,node_queue_stage,node_ranking toneMint
class node_progress,node_csv toneRose
class node_cloudflare,node_user_url,node_release_ci,node_gomod toneNeutral
```

## Components

### Input & config

CLI flags and IP inventory sources

- [main.go](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/main.go): orchestrator · Parses flags and coordinates the full benchmark pipeline · `main.go`
- [IP lists](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/ip.txt): inventory · Default IPv4 and IPv6 datasets · `ip.txt`
- [IPv6 list](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/ipv6.txt): inventory · Default IPv6 dataset · `ipv6.txt`

### Probe engine

Latency, reachability, and throughput workers

- [IP parser](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/ip.go): range expansion · Loads single IPs and CIDR ranges, expands or samples candidates · `task/ip.go`
- [TCPing](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/tcping.go): latency probe · Default TCP-based latency and loss test · `task/tcping.go`
- [HTTPing](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/httping.go): http probe · Protocol-level reachability test with region matching · `task/httping.go`
- [Download test](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/task/download.go): throughput probe · Sequential download benchmark for surviving candidates · `task/download.go`

### Ranking pipeline

Filtering, sorting, and final selection

- **Latency sweep**: concurrent stage · High-concurrency probing across the candidate IP set
- **Threshold filter**: quality gate · Filters by latency and packet-loss bounds
- **Download queue**: staged subset · Smaller set of best candidates that proceed to throughput testing
- **Ranking**: sorter · Orders by download speed when enabled, otherwise by latency

### Output

Progress reporting and CSV persistence

- [Progress UI](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/utils/progress.go): terminal status · Live progress-bar and status output during probing · `utils/progress.go`
- [CSV export](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/utils/csv.go): result writer · Writes final ranking to result.csv or a user-specified path · `utils/csv.go`

### Other components

- **Cloudflare endpoints**: external target · External measurement target used for probing
- **User URL**: external target · Optional user-specified HTTP endpoint for application-level checks
- [Build workflow](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/.github/workflows/build.yml): ci packaging · GitHub Actions build and release packaging · `.github/workflows/build.yml`
- [Go module](https://github.com/bia-pain-bache/cloudflare-clean-ip-scanner/blob/master/go.mod): dependency manifest · Defines the Go runtime and dependencies · `go.mod`

## Connections

- main.go → IP lists: reads · Uses the default IPv4 inventory
- main.go → IPv6 list: reads · Uses the default IPv6 inventory
- main.go → IP parser: loads · Parses files, inline ranges, or custom IP input
- IP parser → Latency sweep: candidates · Produces sampled or expanded probe candidates
- main.go → TCPing: default mode · Runs TCPing when HTTPing is not selected
- main.go → HTTPing: optional mode · Runs HTTP-level probes when requested
- TCPing → Cloudflare endpoints: measures · Probes Cloudflare IPs for latency and loss
- HTTPing → Cloudflare endpoints: measures · Uses HTTP reachability against Cloudflare targets
- HTTPing → User URL: optional · Can probe a user-specified URL
- Latency sweep → Threshold filter: results · Outputs latency and packet-loss measurements
- Threshold filter → Download queue: passes · Only surviving candidates enter download testing
- Download queue → Download test: tests · Runs sequential throughput benchmarks
- Download test → Ranking: scores · Produces download-speed results for final ordering
- Threshold filter → Ranking: sorts · Latency-based ordering when download testing is disabled
- main.go → Progress UI: reports · Streams live progress and status updates
- Ranking → CSV export: writes · Persists the final ranked results
- Ranking → main.go: returns · Feeds final results back to orchestration for printing
- main.go → Build workflow: builds · CI workflow packages binaries for release
- main.go → Go module: depends on · Uses the module definition and dependency graph
