How cnnvito/dnsregion works
The project in question is a command-line tool written in Go that performs two main actions: (1) it obtains DNS resolution records (using protocols such as DOH (DNS-over-HTTPS) and DOT (DNS-over-TLS)) and (2) it looks up the geographic “归属地” (region) for the resolved IP addresses. In addition, the tool supports simulating DNS resolution from different client regions (EDNS support) via a command-line subcommand, making it easier to diagnose region-specific DNS behavior (for example, for CDN setups).
Main Components
• Command-Line Interface (CLI) Layer:
• Represented by “cmd/main.go” and specialized subcommands in the “cmd” directory (e.g., dns.go, edns.go, list.go, base.go).
• This is the entry point of the application that accepts user arguments and chooses the appropriate action.
• DNS Resolution Engine:
• Implementations for handling different DNS protocols live within modules like dns.go and dns_doh.go.
• This component is responsible for making network calls to remote DNS servers over protocols such as DOH and DOT.
• EDNS Simulation Module:
• Contains parts such as “cmd/edns.go” and likely makes use of the “client_subnet.go” file.
• It simulates queries from different client subnets using data located in the “data” folder (e.g., client_ips.json).
• IP Region Lookup Module:
• Handled by “ip_database.go” along with the data file “data/ip2region.xdb”.
• This component maps the resolved IPs to geographical regions.
• Data and Configuration:
• The “data” directory holds essential static resources (client_ips.json and ip2region.xdb) required for lookups.
• Other configuration elements or scripts (e.g., build.sh) are used for deployment or building the project.
• External Dependencies:
• Interaction with external DNS servers and the ip2region database (an external open-source project referenced in the README) is vital.
• Although not explicitly separated in files, this module is implied from the README and file structure.
Relationships and Interactions
• The CLI layer collects input and dispatches control to the respective DNS resolution module based on the selected command (dns, edns, list).
• The DNS resolution engine in turn calls out to external DNS servers (via DOH or DOT) to obtain DNS records.
• Once IPs are returned, the IP region lookup module uses the ip2region data to determine the geographic attribution of these IPs.
• When using the EDNS simulation, the tool uses the client subnet data (from the JSON file) to simulate queries from various regions.
• Data flows from the network (via DNS queries) to internal processing (DNS resolution engine) and then through the region lookup module before presenting results to the user.
Architectural Patterns and Design Principles
• Modular Command Architecture: Each command (dns, edns, list) is separated into its own file under the “cmd” directory. This promotes ease of extension and clear separation of concerns.
• Separation of Concerns: There is a clear division between command-line input processing, DNS query handling, and regional data lookup.
• Use of External Resources: The system relies on external services (DNS servers, ip2region database) and local data configurations to deliver its functionality.
Technologies, Frameworks, or Libraries
• Go language is the primary technology used.
• Built-in or custom modules for DNS querying (handling DOH, DOT, EDNS).
• Integration with ip2region’s data lookup (referenced in the README).
• GitHub workflows are set up for CI/CD, as indicated by the .github/workflows directory.