Skip to content

Instantly share code, notes, and snippets.

@zlocate
Last active July 26, 2024 17:10
Show Gist options
  • Save zlocate/7b76de31916e62f9e40011644ce1a170 to your computer and use it in GitHub Desktop.
Save zlocate/7b76de31916e62f9e40011644ce1a170 to your computer and use it in GitHub Desktop.
Get fastest npm cdn mirror

IP Address Latency Checker (for NPM registry)

This script reads a list of IP addresses from a file, performs multiple iterations of latency checks using curl, and outputs the top 3 IP addresses with the lowest latency. It also displays progress information for each iteration.

Usage

  1. Save the script to a file, e.g., check_ips.sh.
  2. Make the script executable:
    chmod +x check_npm.sh
  3. Create a file named ips.txt in the same directory as the script. List all the IP addresses you want to check, one per line.
  4. Run the script:
    ./check_npm.sh

The script will output the top 3 IP addresses with the fastest response times.

Sample ips.txt file

104.16.27.34
104.16.0.35
104.16.28.34
104.16.29.34
104.16.25.34
104.16.1.35
104.16.31.34
104.16.30.34
104.16.26.34
104.16.2.35
104.16.24.34
104.16.3.35

How to Resolve Domain to IP Addresses from Different Locations

To get a list of IP addresses that a domain resolves to from different geographic locations, you can use the following methods:

  1. Online DNS tools:

    • Use websites like WhatsMyDNS or ViewDNS to see how a domain resolves from different DNS servers around the world.
  2. Command Line Tools:

    • Use the dig command with different DNS servers:
      dig +short @8.8.8.8 example.com  # Google DNS
      dig +short @1.1.1.1 example.com  # Cloudflare DNS
      dig +short @9.9.9.9 example.com  # Quad9 DNS
    • To automate this for multiple DNS servers, you can create a script that queries these servers and aggregates the results.
  3. Third-Party APIs:

Example Script to Get IP Addresses from Multiple DNS Servers

#!/bin/bash

DOMAIN="example.com"
SERVERS=("8.8.8.8" "1.1.1.1" "9.9.9.9")

for SERVER in "${SERVERS[@]}"; do
  echo "DNS Server: $SERVER"
  dig +short @$SERVER $DOMAIN
done

This script will query the specified domain against multiple DNS servers and return the resolved IP addresses.

#!/bin/bash
# External file containing IP addresses
FILE="ips.txt"
ITERATIONS=10
# Temporary file to store the curl output
TEMP_FILE=$(mktemp)
# Loop for 10 iterations
for ((i=1; i<=ITERATIONS; i++)); do
echo "Iteration $i/$ITERATIONS"
# Read each IP address from the file and execute the curl command
while IFS= read -r IP; do
curl -w "%{url.host};%{time_total}\n" -k -H "Host: registry.npmjs.org" -o /dev/null -s "https://$IP"
done < "$FILE" >> "$TEMP_FILE"
done
# Sort the results by the second column (time in seconds), get top 3 results, and extract IPs
sort -t';' -k2 -n "$TEMP_FILE" | head -n 3 | cut -d';' -f1
# Remove the temporary file
rm "$TEMP_FILE"
104.16.27.34
104.16.0.35
104.16.28.34
104.16.29.34
104.16.25.34
104.16.1.35
104.16.31.34
104.16.30.34
104.16.26.34
104.16.2.35
104.16.24.34
104.16.3.35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment