mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Remove the word middleware (#1067)
* Rename middleware to plugin first pass; mostly used 'sed', few spots where I manually changed text. This still builds a coredns binary. * fmt error * Rename AddMiddleware to AddPlugin * Readd AddMiddleware to remain backwards compat
This commit is contained in:
44
plugin/whoami/README.md
Normal file
44
plugin/whoami/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# whoami
|
||||
|
||||
*whoami* returns your resolver's local IP address, port and transport. Your IP address is returned
|
||||
in the additional section as either an A or AAAA record.
|
||||
|
||||
The reply always has an empty answer section. The port and transport are included in the additional
|
||||
section as a SRV record, transport can be "tcp" or "udp".
|
||||
|
||||
~~~ txt
|
||||
._<transport>.qname. 0 IN SRV 0 0 <port> .
|
||||
~~~
|
||||
|
||||
If CoreDNS can't find a Corefile on startup this is the *default* plugin that gets loaded. As
|
||||
such it can be used to check that CoreDNS is responding to queries. Other than that this plugin
|
||||
is of limited use in production.
|
||||
|
||||
The *whoami* plugin will respond to every A or AAAA query, regardless of the query name.
|
||||
|
||||
## Syntax
|
||||
|
||||
~~~ txt
|
||||
whoami
|
||||
~~~
|
||||
|
||||
## Examples
|
||||
|
||||
Start a server on the default port and load the *whoami* plugin.
|
||||
|
||||
~~~ corefile
|
||||
. {
|
||||
whoami
|
||||
}
|
||||
~~~
|
||||
|
||||
When queried for "example.org A", CoreDNS will respond with:
|
||||
|
||||
~~~ txt
|
||||
;; QUESTION SECTION:
|
||||
;example.org. IN A
|
||||
|
||||
;; ADDITIONAL SECTION:
|
||||
example.org. 0 IN A 10.240.0.1
|
||||
_udp.example.org. 0 IN SRV 0 0 40212
|
||||
~~~
|
||||
28
plugin/whoami/setup.go
Normal file
28
plugin/whoami/setup.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package whoami
|
||||
|
||||
import (
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("whoami", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setupWhoami,
|
||||
})
|
||||
}
|
||||
|
||||
func setupWhoami(c *caddy.Controller) error {
|
||||
c.Next() // 'whoami'
|
||||
if c.NextArg() {
|
||||
return plugin.Error("whoami", c.ArgErr())
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
||||
return Whoami{}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
19
plugin/whoami/setup_test.go
Normal file
19
plugin/whoami/setup_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package whoami
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func TestSetupWhoami(t *testing.T) {
|
||||
c := caddy.NewTestController("dns", `whoami`)
|
||||
if err := setupWhoami(c); err != nil {
|
||||
t.Fatalf("Expected no errors, but got: %v", err)
|
||||
}
|
||||
|
||||
c = caddy.NewTestController("dns", `whoami example.org`)
|
||||
if err := setupWhoami(c); err == nil {
|
||||
t.Fatalf("Expected errors, but got: %v", err)
|
||||
}
|
||||
}
|
||||
57
plugin/whoami/whoami.go
Normal file
57
plugin/whoami/whoami.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Package whoami implements a plugin that returns details about the resolving
|
||||
// querying it.
|
||||
package whoami
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Whoami is a plugin that returns your IP address, port and the protocol used for connecting
|
||||
// to CoreDNS.
|
||||
type Whoami struct{}
|
||||
|
||||
// ServeDNS implements the plugin.Handler interface.
|
||||
func (wh Whoami) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
state := request.Request{W: w, Req: r}
|
||||
|
||||
a := new(dns.Msg)
|
||||
a.SetReply(r)
|
||||
a.Compress = true
|
||||
a.Authoritative = true
|
||||
|
||||
ip := state.IP()
|
||||
var rr dns.RR
|
||||
|
||||
switch state.Family() {
|
||||
case 1:
|
||||
rr = new(dns.A)
|
||||
rr.(*dns.A).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeA, Class: state.QClass()}
|
||||
rr.(*dns.A).A = net.ParseIP(ip).To4()
|
||||
case 2:
|
||||
rr = new(dns.AAAA)
|
||||
rr.(*dns.AAAA).Hdr = dns.RR_Header{Name: state.QName(), Rrtype: dns.TypeAAAA, Class: state.QClass()}
|
||||
rr.(*dns.AAAA).AAAA = net.ParseIP(ip)
|
||||
}
|
||||
|
||||
srv := new(dns.SRV)
|
||||
srv.Hdr = dns.RR_Header{Name: "_" + state.Proto() + "." + state.QName(), Rrtype: dns.TypeSRV, Class: state.QClass()}
|
||||
port, _ := strconv.Atoi(state.Port())
|
||||
srv.Port = uint16(port)
|
||||
srv.Target = "."
|
||||
|
||||
a.Extra = []dns.RR{rr, srv}
|
||||
|
||||
state.SizeAndDo(a)
|
||||
w.WriteMsg(a)
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Name implements the Handler interface.
|
||||
func (wh Whoami) Name() string { return "whoami" }
|
||||
56
plugin/whoami/whoami_test.go
Normal file
56
plugin/whoami/whoami_test.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package whoami
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
|
||||
"github.com/coredns/coredns/plugin/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func TestWhoami(t *testing.T) {
|
||||
wh := Whoami{}
|
||||
|
||||
tests := []struct {
|
||||
qname string
|
||||
qtype uint16
|
||||
expectedCode int
|
||||
expectedReply []string // ownernames for the records in the additional section.
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
qname: "example.org",
|
||||
qtype: dns.TypeA,
|
||||
expectedCode: dns.RcodeSuccess,
|
||||
expectedReply: []string{"example.org.", "_udp.example.org."},
|
||||
expectedErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
for i, tc := range tests {
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
|
||||
|
||||
rec := dnsrecorder.New(&test.ResponseWriter{})
|
||||
code, err := wh.ServeDNS(ctx, rec, req)
|
||||
|
||||
if err != tc.expectedErr {
|
||||
t.Errorf("Test %d: Expected error %v, but got %v", i, tc.expectedErr, err)
|
||||
}
|
||||
if code != int(tc.expectedCode) {
|
||||
t.Errorf("Test %d: Expected status code %d, but got %d", i, tc.expectedCode, code)
|
||||
}
|
||||
if len(tc.expectedReply) != 0 {
|
||||
for i, expected := range tc.expectedReply {
|
||||
actual := rec.Msg.Extra[i].Header().Name
|
||||
if actual != expected {
|
||||
t.Errorf("Test %d: Expected answer %s, but got %s", i, expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user