Go to bind DNS SRV programmatically

Can anyone learn how to set up SRV recording locally in Go?

This is for testing purposes. For example, I want to bind test.comto localhostonly during tests. I currently need to change the host/etc/bind/test.com.hosts

test.com.   IN  SOA bindhostname. admin.test.com. (
1452607488
10800
3600
604800
38400 )
test.com.   IN  NS  bindhostname.
my1.test.com.   300 IN  A   127.0.0.1
_etcd-client._tcp   300 IN  SRV 0 0 5000 my1.test.com.

I looked at https://github.com/miekg/dns but couldn't figure out where to start. Can anyone help?

Thank!

+4
source share
1 answer

First you need to add your local ip in /etc/resolv.conf

Then you can use the following code:

package main

import (
    "log"
    "net"

    "github.com/miekg/dns"
)

const (
    DOM    = "test.com."
    srvDom = "_etcd-client._tcp."
)

func handleSRV(w dns.ResponseWriter, r *dns.Msg) {
    var a net.IP
    m := new(dns.Msg)
    m.SetReply(r)
    if ip, ok := w.RemoteAddr().(*net.UDPAddr); ok {
        a = ip.IP
    }
    if ip, ok := w.RemoteAddr().(*net.TCPAddr); ok {
        a = ip.IP
    }

    // Add in case you are using IPv6 alongwith AAAA
    /*if a.To4() !=nil {
        a = a.To4()
        }
    */
    // Checking which type of query has come
    switch r.Question[0].Qtype {
    default:
        fallthrough
    case dns.TypeA:
        rr := new(dns.A)
        rr.Hdr = dns.RR_Header{Name: DOM, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
        rr.A = a.To4()
        m.Answer = append(m.Answer, rr)
    case dns.TypeSRV:
        rr := new(dns.SRV)
        rr.Hdr = dns.RR_Header{Name: srvDom, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: 0}
        rr.Priority = 0
        rr.Weight = 0
        rr.Port = 5000
        rr.Target = DOM
        m.Answer = append(m.Answer, rr)
    }
    w.WriteMsg(m)
}

func serve(net string) {
    server := &dns.Server{Addr: ":53", Net: net, TsigSecret: nil}
    err := server.ListenAndServe()
    if err != nil {
        log.Fatal("Server can't be started")
    }
}

func main() {
    dns.HandleFunc(DOM, handleSRV)
    dns.HandleFunc(srvDom, handleSRV)
    go serve("tcp")
    go serve("udp")
    for {
    }
}

You can check that this binding server gives the correct answer for dig

dig @"127.0.0.1"  _etcd-client._tcp. SRV

, IPv4 ( , , IPv6).

DOM SRV, const.

, dns . 53, root. - . , .

+3

All Articles