手写DNS服务器测速程序(工具分享)
- 目的
- 特性
- 下载地址
- 使用方法
- Golang代码
目的
为路由器设置dns时往往不知道如何设置dns为最佳,网上搜的脚本都不好用,于是手写了这个程序。
特性
- 多DNS服务器并发测试
- 每个DNS服务器测试多次测试取速度最快的结果
- 支持默认DNS测试测试或者文件自定义DNS服务器测试
- 结果按照DNS速度排序。
下载地址
Github下载
使用方法
>> race-dns-speed.exe
Enter a domain: www.baidu.com
DNS resolution for www.baidu.com at 114.114.114.114 took 364.41 ms
DNS resolution for www.baidu.com at 208.67.220.220 took 395.63 ms
DNS resolution for www.baidu.com at 101.226.4.6 took 470.03 ms
DNS resolution for www.baidu.com at 223.6.6.6 took 470.03 ms
DNS resolution for www.baidu.com at 180.76.76.76 took 470.54 ms
DNS resolution for www.baidu.com at 119.29.29.29 took 473.33 ms
DNS resolution for www.baidu.com at 208.67.222.222 took 475.09 ms
DNS resolution for www.baidu.com at 223.5.5.5 took 477.62 ms
DNS resolution for www.baidu.com at 101.101.101.101 took 478.43 ms
DNS resolution for www.baidu.com at 8.8.8.8 took 480.65 ms
DNS resolution for www.baidu.com at 112.124.47.27 took 481.81 ms
DNS resolution for www.baidu.com at 114.215.126.16 took 487.02 ms
DNS resolution for www.baidu.com at 8.8.4.4 took 487.75 ms
DNS resolution for www.baidu.com at 123.125.81.6 took 490.54 ms
Enter a domain: www.sohu.com
DNS resolution for www.sohu.com at 223.6.6.6 took 362.69 ms
DNS resolution for www.sohu.com at 223.5.5.5 took 364.30 ms
DNS resolution for www.sohu.com at 8.8.4.4 took 367.48 ms
DNS resolution for www.sohu.com at 208.67.222.222 took 370.11 ms
DNS resolution for www.sohu.com at 208.67.220.220 took 370.11 ms
DNS resolution for www.sohu.com at 101.101.101.101 took 371.72 ms
DNS resolution for www.sohu.com at 119.29.29.29 took 372.36 ms
DNS resolution for www.sohu.com at 123.125.81.6 took 459.76 ms
DNS resolution for www.sohu.com at 114.215.126.16 took 461.91 ms
DNS resolution for www.sohu.com at 112.124.47.27 took 462.23 ms
DNS resolution for www.sohu.com at 101.226.4.6 took 462.46 ms
DNS resolution for www.sohu.com at 180.76.76.76 took 469.42 ms
DNS resolution for www.sohu.com at 114.114.114.114 took 469.48 ms
DNS resolution for www.sohu.com at 8.8.8.8 took 490.74 ms
Enter a domain: www.google.com
DNS resolution for www.google.com at 223.6.6.6 took 0.50 ms
DNS resolution for www.google.com at 180.76.76.76 took 0.50 ms
DNS resolution for www.google.com at 101.226.4.6 took 0.50 ms
DNS resolution for www.google.com at 123.125.81.6 took 0.50 ms
...
...
...
Golang代码
package mainimport ("bufio""context""fmt""net""os""sort""time"
)var dnsServers = []string{"114.114.114.114","119.29.29.29","223.5.5.5","223.6.6.6","180.76.76.76","101.226.4.6","123.125.81.6","101.226.4.6","101.226.4.6","112.124.47.27","114.215.126.16","8.8.8.8","8.8.4.4","208.67.222.222","208.67.220.220","101.101.101.101",
}type DnsResult struct {Server stringTime float64
}func resolve(domain string, dnsServer string, results chan<- DnsResult) {defer func() {if r := recover(); r != nil {fmt.Println("Recovered from panic in resolve:", r)}}()r := net.Resolver{PreferGo: true,Dial: func(ctx context.Context, network, address string) (net.Conn, error) {d := net.Dialer{Timeout: time.Millisecond * 5000,}return d.DialContext(ctx, network, dnsServer+":53")},}minTime := float64(1<<63 - 1) // max float64 valuefor i := 0; i < 10; i++ {start := time.Now()_, err := r.LookupHost(context.Background(), domain)elapsed := time.Since(start)if err != nil {fmt.Printf("DNS resolution for %s at %s failed: %v\n", domain, dnsServer, err)return} else {ms := elapsed.Seconds() * 1000if ms < minTime {minTime = ms}}}results <- DnsResult{Server: dnsServer, Time: minTime}
}func main() {if _, err := os.Stat("dns-servers.txt"); err == nil {file, err := os.Open("dns-servers.txt")if err != nil {fmt.Println("Error opening dns-servers.txt:", err)os.Exit(1)}defer file.Close()scanner := bufio.NewScanner(file)dnsServers = nilfor scanner.Scan() {dnsServers = append(dnsServers, scanner.Text())}if err := scanner.Err(); err != nil {fmt.Println("Error reading dns-servers.txt:", err)os.Exit(1)}}seen := make(map[string]bool)var uniqueDnsServers []stringfor _, server := range dnsServers {if !seen[server] {seen[server] = trueuniqueDnsServers = append(uniqueDnsServers, server)}}dnsServers = uniqueDnsServersfor {fmt.Print("Enter a domain: ")var domain stringfmt.Scanln(&domain)results := make(chan DnsResult, len(dnsServers))for _, dnsServer := range dnsServers {go resolve(domain, dnsServer, results)}var dnsResults []DnsResultfor range dnsServers {dnsResults = append(dnsResults, <-results)}close(results)sort.Slice(dnsResults, func(i, j int) bool {return dnsResults[i].Time < dnsResults[j].Time})for _, result := range dnsResults {fmt.Printf("DNS resolution for %s at %s took %.2f ms\n", domain, result.Server, result.Time)}}
}