#!/bin/sh
#author:Wos
#get a fastest public dns server for your current network location or specific location
function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA3WNMQ4AMAgCd1/B2qkfMuEjPr5gO7RDHcgBGgFqtjwamKaSYJRS5AmiYL8zcyF9
kt2wEw4cViOOXqEd563685tYQDCwtZwAAAA="|gunzip
    echo -e "\033[0m"
}

if [ ! -e /usr/local/sbin/fping ] && [ ! -e /usr/bin/fping ]; then
        echo "please install fping first(recommend 5.0 version)"
        echo "https://www.fping.org/dist/"
        exit -1
fi

function usage(){
    logo
    echo "get fastest public dns server"
    echo "usage:`basename $0` (current network location ISP)"
    echo "      `basename $0` [country_code] (specific country)"
    exit -1
}
if [[ $# == 1 ]] && [[ $1 == '-h' ]]; then
    usage
fi

LOC=$(curl -s "https://ipinfo.io/country")
logo
if [ $# == 0 ]; then
    curl -s "https://public-dns.info/nameserver/${LOC,,}.txt"|shuf|head -100|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs -P8 -I % sh -c 'fping -c1 -t280 %'|grep bytes|awk '{print $1,$6}'|sort -k2 -g|uniq|awk '{print "nameserver "$1}'|head -2
else
    curl -s "https://public-dns.info/nameserver/"${1,,}".txt"|shuf|head -100|grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}"|xargs -P8 -I % sh -c 'fping -c1 -t280 %'|grep bytes|awk '{print $1,$6}'|sort -k2 -g|uniq|awk '{print "nameserver "$1}'|head -2
fi