# xykt/netquality architecture

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

- Interactive diagram: https://gitdiagram.com/xykt/netquality
- Repository: https://github.com/xykt/netquality

## Overview

##### Project type and purpose
- This is an open-source, CLI‐based network quality check tool implemented as a single Bash script (`net.sh`) that orchestrates multiple test modules.

##### File structure
- Top level:
- `net.sh` – main entrypoint script
- `ref/` – module scripts, helper binaries, JSON data sources
- `res/` – sample output JSON and sponsor assets
- `README*.md`, LICENSE, GitHub metadata
- In `ref/`:
- Shell modules (`ping.sh`, `route.sh`, `upgrade_bash.sh`, `sponsor.ans`)
- Precompiled binaries for speedtests (`speedtest-*`) and STUN servers (`stun_*`)
- JSON lookup tables (`iso3166.json`, `province.json`, `iperf.json`, etc.)
- Utility scripts (`net.sh` pulls these in as “plugins”)

##### Key insights from README
- Modular test phases (IPv4/IPv6 dual-stack, BGP info, local policy, access, TCP large-packet latency, traceroute, domestic & international speedtests)
- Flexible CLI flags to skip modules, change output format, language, or run in low-data mode
- Relies on external data sources: BGP.TOOLS, BGP.HE.NET, speedtest.net, public STUN and iperf servers

##### Main components
a. User Interface Layer
– CLI parser in `net.sh`
– Command-line flags, interactive mode
b. Module Layer (plugin components in `ref/`)
– BGP Info Module
– Local Policy & Access Module
– TCP Latency Module (`iperf.json`, iperf servers)
– Traceroute Module (`route.sh`, `stun/*`)
– Domestic Speedtest Module (`speedtest` binaries + `speedtest_cn.json`)
– International Speedtest Module (`ref/speedtest_cn.json`?)
– Output Formatter (ANSI, JSON, plain text)
c. Data Sources & External Services
– BGP.TOOLS / BGP.HE.NET APIs
– Public STUN servers
– iperf servers (`iperf.json`)
– speedtest.net endpoints
d. Local Assets
– JSON lookup tables (`iso3166.json`, `province.json`, `ad1.ans`)
– Sponsor data (`sponsor.ans`, `res/sponsor/*`)
e. Output Targets
– Terminal (ANSI)
– File (JSON / text)
– SVG badges via external hit counters

##### Relationships and interactions
- The CLI layer invokes each module in sequence (or skipped via flags), passing parameters.
- Modules fetch data from external services or local JSON, parse responses, aggregate results.
- Results flow into the Output Formatter, which emits ANSI, writes JSON, or redirects plain text to file.
- `upgrade_bash.sh` can be invoked to update the script; `sponsor.ans` pulls sponsor metadata for display.

##### Architectural patterns & design principles
- Plugin-style modularity: each test lives in its own shell script or binary under `ref/`.
- Single orchestration layer (`net.sh`) for CLI parsing, module sequencing, and formatting.
- Data‐driven configuration: JSON files drive lists of servers, provinces, ISPs.
- Shell universality: supports many Linux distros and macOS via portable bash/curl/awk patterns.

- Clear labels on each component (script names, JSON files, services)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Orchestration Layer
    CLI["net.sh<br/>(CLI Parser & Orchestration)"]:::orchestration
    click CLI "https://github.com/xykt/netquality/blob/main/net.sh"

    %% Modules Layer
    subgraph "Test Modules" 
        direction TB
        BGP["BGP Info Module<br/>(AS_Mapping.txt)"]:::module
        click BGP "https://github.com/xykt/netquality/blob/main/ref/AS_Mapping.txt"
        LocalAcc["Local Policy & Access Module<br/>(ping.sh)"]:::module
        click LocalAcc "https://github.com/xykt/netquality/blob/main/ref/ping.sh"
        TCPLat["TCP Latency Module<br/>(iperf.json)"]:::module
        click TCPLat "https://github.com/xykt/netquality/blob/main/ref/iperf.json"
        Trace["Traceroute Module<br/>(route.sh + stun/)"]:::module
        click Trace "https://github.com/xykt/netquality/blob/main/ref/route.sh"
        click Trace "https://github.com/xykt/netquality/tree/main/ref/stun/"
        DomSpeed["Domestic Speedtest Module<br/>(speedtest/ + speedtest_cn.json)"]:::module
        click DomSpeed "https://github.com/xykt/netquality/tree/main/ref/speedtest/"
        click DomSpeed "https://github.com/xykt/netquality/blob/main/ref/speedtest_cn.json"
        IntlSpeed["International Speedtest Module<br/>(speedtest/)"]:::module
        click IntlSpeed "https://github.com/xykt/netquality/tree/main/ref/speedtest/"
        Upgrade["Upgrade Script<br/>(upgrade_bash.sh)"]:::module
        click Upgrade "https://github.com/xykt/netquality/blob/main/ref/upgrade_bash.sh"
        SponsorMod["Sponsor Metadata<br/>(sponsor.ans)"]:::module
        click SponsorMod "https://github.com/xykt/netquality/blob/main/ref/sponsor.ans"
    end

    %% Local Data Assets
    subgraph "Local Data Stores" 
        direction TB
        ISO["ISO Country Codes<br/>(iso3166.json)"]:::local
        click ISO "https://github.com/xykt/netquality/blob/main/ref/iso3166.json"
        Province["Province Mapping<br/>(province.json)"]:::local
        click Province "https://github.com/xykt/netquality/blob/main/ref/province.json"
        AdBanner["Advertising Banner<br/>(ad1.ans)"]:::local
        click AdBanner "https://github.com/xykt/netquality/blob/main/ref/ad1.ans"
    end

    %% External Services
    subgraph "External Services"
        direction TB
        BGPAPI["BGP.TOOLS / BGP.HE.NET APIs"]:::external
        STUNSrv["Public STUN Servers"]:::external
        IperfSrv["iPerf Servers"]:::external
        SpeedSrv["speedtest.net Endpoints"]:::external
    end

    %% Output Formatter
    Formatter["Output Formatter<br/>(ANSI, JSON, Text)"]:::orchestration
    click Formatter "https://github.com/xykt/netquality/blob/main/res/output.json"

    %% Output Targets
    subgraph "Output Targets"
        direction TB
        Term["Terminal (ANSI)"]:::output
        File["File Output (JSON/Text)"]:::output
        Badge["SVG Badges"]:::output
        click Badge "https://github.com/xykt/netquality/tree/main/res/sponsor/"
    end

    %% Flows
    CLI -->|"invoke"| BGP
    CLI -->|"invoke"| LocalAcc
    CLI -->|"invoke"| TCPLat
    CLI -->|"invoke"| Trace
    CLI -->|"invoke"| DomSpeed
    CLI -->|"invoke"| IntlSpeed
    CLI -->|"invoke"| Upgrade
    CLI -->|"invoke"| SponsorMod

    BGP -->|"lookup"| LocalData
    LocalAcc -->|"access test"| LocalData
    TCPLat -->|"server list"| IperfSrv
    Trace -->|"STUN lookup"| STUNSrv
    DomSpeed -->|"speed test"| SpeedSrv
    IntlSpeed -->|"speed test"| SpeedSrv

    BGP -->|"API call"| BGPAPI
    LocalAcc -->|"ICMP ping"| LocalAcc
    Trace -->|"trace route"| STUNSrv

    BGP --> Formatter
    LocalAcc --> Formatter
    TCPLat --> Formatter
    Trace --> Formatter
    DomSpeed --> Formatter
    IntlSpeed --> Formatter
    Upgrade --> Formatter
    SponsorMod --> Formatter

    Formatter --> Term
    Formatter --> File
    Formatter --> Badge

    %% Legend
    subgraph Legend
        direction TB
        orch["Orchestration Layer"]:::orchestration
        mod["Test Modules"]:::module
        ext["External Services"]:::external
        loc["Local Data Stores"]:::local
        out["Output Targets"]:::output
    end

    classDef orchestration fill:#cce5ff,stroke:#333,stroke-width:1px;
    classDef module fill:#d4edda,stroke:#333,stroke-width:1px;
    classDef external fill:#ffe5b4,stroke:#333,stroke-width:1px;
    classDef local fill:#e2e3e5,stroke:#333,stroke-width:1px;
    classDef output fill:#fff3cd,stroke:#333,stroke-width:1px;
```
