Skip to content

Zhousiru/opti-hosts

Repository files navigation

Opti Hosts

[English] [中文]

Resolve domains by latency, not CDN zones.

This tool retrieves DNS records from Globalping, selects IPs with lowest latency, and updates the hosts file.

Quickstart

  1. Download from Releases.

    For example:

    sudo wget https://github.com/Zhousiru/opti-hosts/releases/download/latest/opti-hosts-x86_64-unknown-linux-gnu -O /usr/local/bin/opti-hosts
    sudo chmod +x /usr/local/bin/opti-hosts
  2. Add directives in your /etc/hosts:

    # ...
    
    # OPTI-HOSTS example.com [Beijing, HK * 2, AS6939]
    # HINT: Record for example.com will be generated here.
    
    # ...
    
  3. Run:

    sudo opti-hosts
    sudo opti-hosts --dry-run # Preview changes
  4. Add to your crontab (recommend):

    0 */12 * * * opti-hosts

Hosts Directive

# OPTI-HOSTS <Domain> [<Location> * <Limit>, <Location>, ...]
  • Domain

    The domain you want to resolve.

  • Location

    The geographic location or network from which you want to resolve the domain.

    See Globalping Network, or try it on Globalping.

  • Limit

    The maximum number of nodes to use for a particular location.

    Defaults to 1.

Usage

opti-hosts [OPTIONS]
Options:
      --dry-run      Preview outputs without making any changes to hosts file
      --file <FILE>  Hosts file path [default: /etc/hosts]
  -h, --help         Print help
  -V, --version      Print version