mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 18:53:43 -04:00
Implement chaos middleware
This allows for CH TXT queries that return some information about the server and/or the authors (or whatever you put in there).
This commit is contained in:
50
middleware/chaos/chaos.go
Normal file
50
middleware/chaos/chaos.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package chaos
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Chaos struct {
|
||||
Next middleware.Handler
|
||||
Version string
|
||||
Authors map[string]bool // get randomization for free \o/
|
||||
}
|
||||
|
||||
func (c Chaos) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
state := middleware.State{W: w, Req: r}
|
||||
if state.QClass() != dns.ClassINET || state.QType() != dns.TypeTXT {
|
||||
return c.Next.ServeDNS(ctx, w, r)
|
||||
}
|
||||
m := new(dns.Msg)
|
||||
hdr := dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeTXT, Class: dns.ClassCHAOS, Ttl: 0}
|
||||
switch state.Name() {
|
||||
default:
|
||||
return c.Next.ServeDNS(ctx, w, r)
|
||||
case "authors.bind.":
|
||||
for a, _ := range c.Authors {
|
||||
m.Answer = append(m.Answer, &dns.TXT{Hdr: hdr, Txt: []string{trim(a)}})
|
||||
}
|
||||
case "version.bind.", "version.server.":
|
||||
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(c.Version)}}}
|
||||
case "hostname.bind.", "id.server.":
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
hostname = "localhost"
|
||||
}
|
||||
m.Answer = []dns.RR{&dns.TXT{Hdr: hdr, Txt: []string{trim(hostname)}}}
|
||||
}
|
||||
w.WriteMsg(m)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func trim(s string) string {
|
||||
if len(s) < 256 {
|
||||
return s
|
||||
}
|
||||
return s[:255]
|
||||
}
|
||||
26
middleware/chaos/chaos.md
Normal file
26
middleware/chaos/chaos.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# chaos
|
||||
|
||||
`chaos`
|
||||
|
||||
## Syntax
|
||||
|
||||
~~~
|
||||
chaos [version] [authors...]
|
||||
~~~
|
||||
|
||||
* `version` the version to return, defaults to CoreDNS.
|
||||
* `authors` what authors to return. No default.
|
||||
|
||||
## Examples
|
||||
|
||||
~~~
|
||||
etcd {
|
||||
path /skydns
|
||||
endpoint endpoint...
|
||||
stubzones
|
||||
}
|
||||
~~~
|
||||
|
||||
* `path` /skydns
|
||||
* `endpoint` endpoints...
|
||||
* `stubzones`
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/miekg/coredns/middleware"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
|
||||
@@ -66,8 +66,8 @@ func (r *ResponseRecorder) Start() time.Time {
|
||||
return r.start
|
||||
}
|
||||
|
||||
// Reply returns the written message from the ResponseRecorder.
|
||||
func (r *ResponseRecorder) Reply() *dns.Msg {
|
||||
// Msg returns the written message from the ResponseRecorder.
|
||||
func (r *ResponseRecorder) Msg() *dns.Msg {
|
||||
return r.msg
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user