本文整理汇总了Golang中github.com/miekg/dns.Msg.SetTsig方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.SetTsig方法的具体用法?Golang Msg.SetTsig怎么用?Golang Msg.SetTsig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.Msg
的用法示例。
在下文中一共展示了Msg.SetTsig方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: changeRecord
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
// Find the zone for the given fqdn
zone, err := acme.FindZoneByFqdn(fqdn, []string{r.nameserver})
if err != nil {
return err
}
// Create RR
rr := new(dns.TXT)
rr.Hdr = dns.RR_Header{Name: fqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: uint32(ttl)}
rr.Txt = []string{value}
rrs := []dns.RR{rr}
// Create dynamic update packet
m := new(dns.Msg)
m.SetUpdate(zone)
switch action {
case "INSERT":
// Always remove old challenge left over from who knows what.
m.RemoveRRset(rrs)
m.Insert(rrs)
case "REMOVE":
m.Remove(rrs)
default:
return fmt.Errorf("Unexpected action: %s", action)
}
// Setup client
c := new(dns.Client)
c.SingleInflight = true
// TSIG authentication / msg signing
if len(r.tsigKey) > 0 && len(r.tsigSecret) > 0 {
m.SetTsig(dns.Fqdn(r.tsigKey), r.tsigAlgorithm, 300, time.Now().Unix())
c.TsigSecret = map[string]string{dns.Fqdn(r.tsigKey): r.tsigSecret}
}
// Send the query
reply, _, err := c.Exchange(m, r.nameserver)
if err != nil {
return fmt.Errorf("DNS update failed: %v", err)
}
if reply != nil && reply.Rcode != dns.RcodeSuccess {
return fmt.Errorf("DNS update failed. Server replied: %s", dns.RcodeToString[reply.Rcode])
}
return nil
}
示例2: RemoveName
// Dynamically remove a full set of RR records stored in DNS
func (s *Service) RemoveName(zone string, rr []dns.RR) error {
m := new(dns.Msg)
m.SetUpdate(zone)
m.SetTsig(dns.Fqdn(s.Key), dns.HmacMD5, 300, time.Now().Unix())
m.RemoveName(rr)
h, err := s.ServerPort()
if err != nil {
return err
}
c := new(dns.Client)
c.TsigSecret = map[string]string{dns.Fqdn(s.Key): s.Secret}
r, _, err := c.Exchange(m, h)
if err != nil {
return err
}
if r.Rcode != dns.RcodeSuccess {
return errors.New(fmt.Sprintf("invalid exchange answer"))
}
return nil
}
示例3: list
func (r *RFC2136Provider) list() ([]dns.RR, error) {
logrus.Debugf("Fetching records for '%s'", r.zoneName)
t := new(dns.Transfer)
t.TsigSecret = map[string]string{r.tsigKeyName: r.tsigSecret}
m := new(dns.Msg)
m.SetAxfr(r.zoneName)
m.SetTsig(r.tsigKeyName, dns.HmacMD5, 300, time.Now().Unix())
env, err := t.In(m, r.nameserver)
if err != nil {
return nil, fmt.Errorf("Failed to fetch records via AXFR: %v", err)
}
records := make([]dns.RR, 0)
for e := range env {
if e.Error != nil {
logrus.Errorf("AXFR envelope error: %v", e.Error)
continue
}
records = append(records, e.RR...)
}
return records, nil
}
示例4: handleDNSRequest
func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
if *debug {
Log.Printf("handleRequest: message: %+v\n", r)
}
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m)
case dns.OpcodeUpdate:
for _, question := range r.Question {
for _, rr := range r.Ns {
updateRecord(rr, &question)
}
}
}
if r.IsTsig() != nil {
if w.TsigStatus() == nil {
m.SetTsig(r.Extra[len(r.Extra)-1].(*dns.TSIG).Hdr.Name,
dns.HmacMD5, 300, time.Now().Unix())
} else {
Log.Println("Status", w.TsigStatus().Error())
}
}
w.WriteMsg(m)
}
示例5: formerr
func formerr(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.MsgHdr.Opcode = dns.OpcodeUpdate
if req.IsTsig() != nil {
m.SetTsig(userFromTsig(req), dns.HmacMD5, 300, time.Now().Unix())
}
w.WriteMsg(m.SetRcode(req, dns.RcodeFormatError))
}
示例6: configZONE
// Deal with the zone options
func configZONE(w dns.ResponseWriter, req *dns.Msg, t *dns.TXT, c *Config) error {
sx := strings.Split(t.Txt[0], " ")
if len(sx) == 0 {
return nil
}
switch strings.ToUpper(sx[0]) {
case "READ":
if len(sx) != 3 {
return nil
}
logPrintf("config READ %s %s\n", dns.Fqdn(sx[1]), sx[2])
if e := c.ReadZoneFile(dns.Fqdn(sx[1]), sx[2]); e != nil {
logPrintf("failed to read %s: %s\n", sx[2], e.Error())
return e
}
logPrintf("config added: READ %s %s\n", dns.Fqdn(sx[1]), sx[2])
noerr(w, req)
case "READXFR":
if len(sx) != 3 {
return nil
}
logPrintf("config READXFR %s %s\n", dns.Fqdn(sx[1]), sx[2])
if e := c.ReadZoneXfr(dns.Fqdn(sx[1]), sx[2]); e != nil {
logPrintf("failed to axfr %s: %s\n", sx[2], e.Error())
return e
}
logPrintf("config added: READXFR %s %s\n", dns.Fqdn(sx[1]), sx[2])
noerr(w, req)
case "DROP":
if len(sx) != 2 {
return nil
}
logPrintf("config DROP %s\n", dns.Fqdn(sx[1]))
if e := c.DropZone(dns.Fqdn(sx[1])); e != nil {
logPrintf("Failed to drop %s: %s\n", dns.Fqdn(sx[1]), e.Error())
return e
}
logPrintf("config dropped: DROP %s\n", dns.Fqdn(sx[1]))
noerr(w, req)
case "LIST":
logPrintf("config LIST\n")
m := new(dns.Msg)
m.SetReply(req)
// Add the zones to the additional section
for zone, _ := range c.Zones {
a, _ := dns.NewRR("ZONE. TXT \"" + zone + "\"")
m.Extra = append(m.Extra, a)
}
m.SetTsig(userFromTsig(req), dns.HmacMD5, 300, time.Now().Unix())
w.WriteMsg(m)
}
return nil
}
示例7: serverHandlerReturnSuccess
func serverHandlerReturnSuccess(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
if t := req.IsTsig(); t != nil {
if w.TsigStatus() == nil {
// Validated
m.SetTsig(rfc2136TestZone, dns.HmacMD5, 300, time.Now().Unix())
}
}
w.WriteMsg(m)
}
示例8: sendMessage
func (r *RFC2136Provider) sendMessage(msg *dns.Msg) error {
c := new(dns.Client)
c.TsigSecret = map[string]string{r.tsigKeyName: r.tsigSecret}
c.SingleInflight = true
msg.SetTsig(r.tsigKeyName, dns.HmacMD5, 300, time.Now().Unix())
resp, _, err := c.Exchange(msg, r.nameserver)
if err != nil {
return err
}
if resp != nil && resp.Rcode != dns.RcodeSuccess {
return fmt.Errorf("Bad return code: %s", dns.RcodeToString[resp.Rcode])
}
return nil
}
示例9: serverHandlerReturnSuccess
func serverHandlerReturnSuccess(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
if req.Opcode == dns.OpcodeQuery && req.Question[0].Qtype == dns.TypeSOA && req.Question[0].Qclass == dns.ClassINET {
// Return SOA to appease findZoneByFqdn()
soaRR, _ := dns.NewRR(fmt.Sprintf("%s %d IN SOA ns1.%s admin.%s 2016022801 28800 7200 2419200 1200", rfc2136TestZone, rfc2136TestTTL, rfc2136TestZone, rfc2136TestZone))
m.Answer = []dns.RR{soaRR}
}
if t := req.IsTsig(); t != nil {
if w.TsigStatus() == nil {
// Validated
m.SetTsig(rfc2136TestZone, dns.HmacMD5, 300, time.Now().Unix())
}
}
w.WriteMsg(m)
}
示例10: handleDNS
func (d *DNSResponder) handleDNS(w dns.ResponseWriter, r *dns.Msg) {
var (
v4 bool
rr dns.RR
a net.IP
)
dom := r.Question[0].Name
m := new(dns.Msg)
m.SetReply(r)
if ip, ok := w.RemoteAddr().(*net.UDPAddr); ok {
a = ip.IP
v4 = a.To4() != nil
}
if ip, ok := w.RemoteAddr().(*net.TCPAddr); ok {
a = ip.IP
v4 = a.To4() != nil
}
if v4 {
rr = new(dns.A)
rr.(*dns.A).Hdr = dns.RR_Header{Name: dom, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.A).A = a.To4()
} else {
rr = new(dns.AAAA)
rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: dom, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.AAAA).AAAA = a
}
switch r.Question[0].Qtype {
case dns.TypeAAAA, dns.TypeA:
m.Answer = append(m.Answer, rr)
}
if r.IsTsig() != nil {
if w.TsigStatus() == nil {
m.SetTsig(r.Extra[len(r.Extra)-1].(*dns.TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix())
}
}
w.WriteMsg(m)
}
示例11: changeRecord
func (r *DNSProviderRFC2136) changeRecord(action, fqdn, value string, ttl int) error {
// Create RR
rr := new(dns.TXT)
rr.Hdr = dns.RR_Header{Name: fqdn, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: uint32(ttl)}
rr.Txt = []string{value}
rrs := make([]dns.RR, 1)
rrs[0] = rr
// Create dynamic update packet
m := new(dns.Msg)
m.SetUpdate(dns.Fqdn(r.zone))
switch action {
case "INSERT":
m.Insert(rrs)
case "REMOVE":
m.Remove(rrs)
default:
return fmt.Errorf("Unexpected action: %s", action)
}
// Setup client
c := new(dns.Client)
c.SingleInflight = true
// TSIG authentication / msg signing
if len(r.tsigKey) > 0 && len(r.tsigSecret) > 0 {
m.SetTsig(dns.Fqdn(r.tsigKey), dns.HmacMD5, 300, time.Now().Unix())
c.TsigSecret = map[string]string{dns.Fqdn(r.tsigKey): r.tsigSecret}
}
// Send the query
reply, _, err := c.Exchange(m, r.nameserver)
if err != nil {
return fmt.Errorf("DNS update failed: %v", err)
}
if reply != nil && reply.Rcode != dns.RcodeSuccess {
return fmt.Errorf("DNS update failed. Server replied: %s", dns.RcodeToString[reply.Rcode])
}
return nil
}
示例12: dnsHandler
// base handler for dns server
func dnsHandler(w dns.ResponseWriter, request *dns.Msg) {
response := new(dns.Msg)
response.SetReply(request)
response.Compress = false
switch request.Opcode {
case dns.OpcodeQuery:
for _, q := range response.Question {
if readRR, e := getRecord(q.Name, q.Qtype); e == nil {
rr := readRR.(dns.RR)
if rr.Header().Name == q.Name {
response.Answer = append(response.Answer, rr)
}
}
}
case dns.OpcodeUpdate:
if request.IsTsig() != nil && w.TsigStatus() == nil {
for _, question := range request.Question {
for _, rr := range request.Ns {
updateRecord(rr, &question)
}
}
} else {
log.Println("droping update without tsig or with bad sig")
}
}
if request.IsTsig() != nil {
if w.TsigStatus() == nil {
response.SetTsig(request.Extra[len(request.Extra)-1].(*dns.TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix())
} else {
log.Println("Status: ", w.TsigStatus().Error())
}
}
w.WriteMsg(response)
}
示例13: handleReflect
func handleReflect(w dns.ResponseWriter, r *dns.Msg) {
var (
v4 bool
rr dns.RR
str string
a net.IP
)
// TC must be done here
m := new(dns.Msg)
m.SetReply(r)
m.Compress = *compress
if ip, ok := w.RemoteAddr().(*net.UDPAddr); ok {
str = "Port: " + strconv.Itoa(ip.Port) + " (udp)"
a = ip.IP
v4 = a.To4() != nil
}
if ip, ok := w.RemoteAddr().(*net.TCPAddr); ok {
str = "Port: " + strconv.Itoa(ip.Port) + " (tcp)"
a = ip.IP
v4 = a.To4() != nil
}
if v4 {
rr = new(dns.RR_A)
rr.(*dns.RR_A).Hdr = dns.RR_Header{Name: dom, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.RR_A).A = a.To4()
} else {
rr = new(dns.RR_AAAA)
rr.(*dns.RR_AAAA).Hdr = dns.RR_Header{Name: dom, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 0}
rr.(*dns.RR_AAAA).AAAA = a
}
t := new(dns.RR_TXT)
t.Hdr = dns.RR_Header{Name: dom, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: 0}
t.Txt = []string{str}
switch r.Question[0].Qtype {
case dns.TypeAXFR:
c := make(chan *dns.XfrToken)
var e *error
if err := dns.XfrSend(w, r, c, e); err != nil {
close(c)
return
}
soa, _ := dns.NewRR(`whoami.miek.nl. IN SOA elektron.atoom.net. miekg.atoom.net. (
2009032802
21600
7200
604800
3600)`)
c <- &dns.XfrToken{RR: []dns.RR{soa, t, rr, soa}}
close(c)
w.Hijack()
// w.Close() // Client closes
return
case dns.TypeTXT:
m.Answer = append(m.Answer, t)
m.Extra = append(m.Extra, rr)
default:
fallthrough
case dns.TypeAAAA, dns.TypeA:
m.Answer = append(m.Answer, rr)
m.Extra = append(m.Extra, t)
}
if r.IsTsig() != nil {
if w.TsigStatus() == nil {
m.SetTsig(r.Extra[len(r.Extra)-1].(*dns.RR_TSIG).Hdr.Name, dns.HmacMD5, 300, time.Now().Unix())
} else {
println("Status", w.TsigStatus().Error())
}
}
if *printf {
fmt.Printf("%v\n", m.String())
}
w.Write(m)
}
示例14: noerr
func noerr(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.MsgHdr.Opcode = dns.OpcodeUpdate
m.SetTsig(userFromTsig(req), dns.HmacMD5, 300, time.Now().Unix())
w.WriteMsg(m.SetReply(req))
}
示例15: main
//.........这里部分代码省略.........
if e.Address.To4() == nil {
e.Family = 2 // IP6
e.SourceNetmask = net.IPv6len * 8
}
o.Option = append(o.Option, e)
}
m.Extra = append(m.Extra, o)
}
if *tcp {
co := new(dns.Conn)
tcp := "tcp"
if *six {
tcp = "tcp6"
}
var err error
if co.Conn, err = net.DialTimeout(tcp, nameserver, 2*time.Second); err != nil {
fmt.Fprintf(os.Stderr, "Dialing "+nameserver+" failed: "+err.Error()+"\n")
return
}
defer co.Close()
qt := dns.TypeA
qc := uint16(dns.ClassINET)
for i, v := range qname {
if i < len(qtype) {
qt = qtype[i]
}
if i < len(qclass) {
qc = qclass[i]
}
m.Question[0] = dns.Question{dns.Fqdn(v), qt, qc}
m.Id = dns.Id()
if *tsig != "" {
if algo, name, secret, ok := tsigKeyParse(*tsig); ok {
m.SetTsig(name, algo, 300, time.Now().Unix())
c.TsigSecret = map[string]string{name: secret}
t.TsigSecret = map[string]string{name: secret}
} else {
fmt.Fprintf(os.Stderr, ";; TSIG key data error\n")
continue
}
}
co.SetReadDeadline(time.Now().Add(2 * time.Second))
co.SetWriteDeadline(time.Now().Add(2 * time.Second))
if *query {
fmt.Printf("%s", m.String())
fmt.Printf("\n;; size: %d bytes\n\n", m.Len())
}
then := time.Now()
if e := co.WriteMsg(m); e != nil {
fmt.Fprintf(os.Stderr, ";; %s\n", e.Error())
continue
}
r, e := co.ReadMsg()
if e != nil {
fmt.Fprintf(os.Stderr, ";; %s\n", e.Error())
continue
}
rtt := time.Since(then)
if r.Id != m.Id {
fmt.Fprintf(os.Stderr, "Id mismatch\n")
continue
}
if *check {
sigCheck(r, nameserver, true)