From 9d6888a24ac7a03997b1ab543801f461bf1eabf8 Mon Sep 17 00:00:00 2001 From: ptillemans Date: Thu, 21 Sep 2023 23:58:08 +0200 Subject: [PATCH] initial commit --- go.mod | 12 ++++++++++++ go.sum | 12 ++++++++++++ main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..92f3825 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module gitea.snamellit.com/pti/godns + +go 1.21.1 + +require github.com/miekg/dns v1.1.56 + +require ( + golang.org/x/mod v0.12.0 // indirect + golang.org/x/net v0.15.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/tools v0.13.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..92d0efe --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE= +github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= diff --git a/main.go b/main.go new file mode 100644 index 0000000..d9cb343 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "errors" + "fmt" + "net" + "time" + + "github.com/miekg/dns" +) + + +type DnsTest struct { + server net.IP + testHost string + rtt time.Duration + answer net.IP +} + +func RunTest(srv net.IP, host string) (*DnsTest, error) { + m := new(dns.Msg) + m.SetQuestion(host, dns.TypeA) + c := new(dns.Client) + in, rtt, err := c.Exchange(m, fmt.Sprintf("%s:53", srv)) + if err != nil { + return nil, err + } + for _, answer := range in.Answer { + if t, ok := answer.(*dns.A); ok { + return &DnsTest{server: srv, testHost: host, rtt: rtt, answer: t.A}, nil + } + } + return nil, errors.New("No answer found") +} + + +func main() { + testHosts := []string{ "gitea.snamellit.com", "www.snamellit.com", "home.snamellit.com", "nas.snamellit.com" , "gitlab.melexis.com", "dsl.melexis.com", "cvs.tess.elex.be", "cvs.sofia.elex.be", "cvs.sensors.elex.be", "cvs.erfurt.elex.be", "www.google.com" } + for _, host := range testHosts { + t, err := RunTest(net.ParseIP("192.168.1.1"), host + ".") + if err != nil { + fmt.Println("Error for ", host, ":", err.Error()) + continue + } + fmt.Println("Answer ", t.server, t.testHost, t.answer, t.rtt) + } +}