-
Notifications
You must be signed in to change notification settings - Fork 1
/
lookupSrv.go
80 lines (67 loc) · 1.74 KB
/
lookupSrv.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
package mydnslookup
import (
"bytes"
"context"
"net"
"text/template"
"github.com/miekg/dns"
)
var fqdnTemplate = template.Must(template.New("").Parse("_{{.Service}}._{{.Proto}}.{{.KubernetesService}}.{{.SearchPath}}."))
type fqdnSettings struct {
Service string
Proto string
KubernetesService string
SearchPath string
}
func LookupSRVMiekgGrpc(context context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error) {
return LookupSRVMiekg(service, proto, name)
}
func LookupSRVMiekg(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
config, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
panic(err)
}
//fmt.Printf("Servers: %#v\n", config.Servers)
c := new(dns.Client)
m := new(dns.Msg)
settings := fqdnSettings{
Service: service,
Proto: proto,
KubernetesService: name,
SearchPath: config.Search[0],
}
writer := new(bytes.Buffer)
if err := fqdnTemplate.Execute(writer, settings); err != nil {
panic(err)
}
usedAddress := writer.String()
//fmt.Println("UsedAddress: " + usedAddress)
m.SetQuestion(usedAddress, dns.TypeSRV)
//m.SetQuestion(usedAddress, dns.TypeCNAME)
m.RecursionDesired = true
r, _, err := c.Exchange(m, config.Servers[0]+":53")
//r, _, err := c.Exchange(m, "127.0.0.1:53")
if err != nil {
return
}
if r.Rcode != dns.RcodeSuccess {
return
}
for _, a := range r.Answer {
srv, ok := a.(*dns.SRV)
if !ok {
cnme, ok := a.(*dns.CNAME)
if !ok {
panic("record is not srv and not cname")
}
cname = cnme.Target
}
addrs = append(addrs, &net.SRV{
Target: srv.Target,
Port: srv.Port,
Priority: srv.Priority,
Weight: srv.Weight,
})
}
return
}