-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (114 loc) · 3.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"time"
"github.com/chekt/dns"
)
const resolvConfigFilePath = "/etc/resolv.conf"
type serverInfo struct {
ServerStringForConfig string // e.g. nameserver 192.168.0.1
Server string // e.g. 192.168.0.1:53
Latency time.Duration
Error error
}
type serverInfoList []serverInfo
func getNameServers() (serverInfoList serverInfoList, err error) {
config, _ := dns.ClientConfigFromFile(resolvConfigFilePath)
port := "53"
if config.Port != "" {
port = config.Port
}
if len(config.Servers) == 0 {
errMessage := fmt.Sprintf("No nameservers in %s", resolvConfigFilePath)
err = errors.New(errMessage)
} else {
servers := config.Servers
for _, v := range servers {
var serverInfo serverInfo
server := v + ":" + port
serverInfo.Server = server
serverInfo.ServerStringForConfig = "nameserver " + v
serverInfoList = append(serverInfoList, serverInfo)
}
}
return serverInfoList, err
}
func checkNameServerSpeed(server string, target string) (duration time.Duration, records []string, err error) {
c := dns.Client{}
m := dns.Msg{}
m.SetQuestion(target+".", dns.TypeA)
res, duration, err := c.Exchange(&m, server)
if err != nil {
return
}
for _, answer := range res.Answer {
records = append(records, strings.Replace(answer.String(), "\t", " ", -1))
}
totalRecord := len(records)
if totalRecord == 0 {
errMessage := fmt.Sprintf("No IP address for %s", target)
err = errors.New(errMessage)
return
}
return
}
func updateResolvconf(serverInfoList serverInfoList) (err error) {
context := "# Generated by dnsResolverOptimizer\n"
contextServerWithErr := ""
for _, serverInfo := range serverInfoList {
if serverInfo.Error != nil {
contextServerWithErr += serverInfo.ServerStringForConfig + "\n"
continue
}
context += serverInfo.ServerStringForConfig + "\n"
}
context = context + contextServerWithErr
textToWrite := []byte(context)
err = ioutil.WriteFile(resolvConfigFilePath, textToWrite, 0644)
return err
}
func main() {
// 1. Get target hostname
target := "google.com"
// server := "203.248.252.2"
// server = "164.124.250.84"
// 2. UPDATE argument as target
if len(os.Args) > 1 {
target = os.Args[1]
}
log.Println(target)
// 3. Get nameserver from resolv.conf
serverInfoList, err := getNameServers()
if err != nil {
log.Fatal(err)
}
// 4. Check nameserver latency
for i, serverInfo := range serverInfoList {
server := serverInfo.Server
duration, records, err := checkNameServerSpeed(server, target)
if err != nil {
message := fmt.Sprintf("server: %v, records: %+v, speed: %v, error: %v", server, records, duration, err)
log.Println(message)
} else {
message := fmt.Sprintf("server: %v, records: %+v, speed: %v, error: %v", server, records, duration, err)
log.Println(message)
}
serverInfoList[i].Error = err
serverInfoList[i].Latency = duration
}
// 5. Sort nameserver by latency
sort.SliceStable(serverInfoList, func(i, j int) bool {
return serverInfoList[i].Latency < serverInfoList[j].Latency
})
// 6. Update resolv.conf
err = updateResolvconf(serverInfoList)
if err != nil {
log.Fatal(err)
}
}