$ show 521xueweihan/GitHub520
This repository provides a utility script that automatically resolves optimal IP addresses for GitHub-related domains and generates a hosts file to improve access speed, particularly for users in regions with restricted or slow connectivity to GitHub. The core logic is implemented in two Python scripts: `fetch_ips.py` performs DNS resolution via DoH (DNS over HTTPS) and asynchronous DNS queries, then measures TCP latency on port 443 to select the fastest IP for each domain. `update_ips.py` is a lighter variant used in the GitHub Actions CI pipeline, which fetches pre-computed IPs from a remote JSON source and updates the README with the latest hosts content. The project is not a traditional application but a scheduled data-generation pipeline that runs every two hours via GitHub Actions, committing the updated hosts file and README back to the repository. The architecture is monolithic, with shared utility functions in `common.py` handling file I/O and template rendering. External dependencies include `requests`, `aiodns`, and `retry`. While the code is straightforward and lacks tests, it demonstrates practical network programming patterns such as TCP-based latency measurement without root privileges and multi-source DNS resolution.
The LLM identified 3 files worth highlighting for reuse:
The `ping_cached` function implements TCP-based latency measurement on port 443 without requiring root privileges, using `socket.create_connection` with a timeout. It caches results in a global dictionary and returns the median of three attempts. This pattern is reusable for any network health check or IP selection task where ICMP ping is unavailable or restricted.
The `get_ip_list_from_doh` function queries multiple DNS-over-HTTPS (DoH) servers (Alibaba, DNSPod, Google) to resolve A records for a domain, with retry logic. It returns a list of IP addresses and gracefully falls back to the next server on failure. This is a self-contained, reusable component for DNS resolution in environments where traditional DNS may be unreliable.
The `select_ip_from_list` function takes a list of IPs, measures their TCP latency via `ping_cached`, sorts by latency, and returns the best IP. It handles empty lists and logs results. This simple but effective selection strategy can be extracted for any multi-IP service discovery or load balancing scenario.