initial commit
This commit is contained in:
commit
50b412f5e5
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
go.mod
|
||||
go.sum
|
45
ResponseWrapper.go
Normal file
45
ResponseWrapper.go
Normal file
@ -0,0 +1,45 @@
|
||||
package overide
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
var log = clog.NewWithPlugin("overide")
|
||||
|
||||
type Rule struct {
|
||||
Search net.IP
|
||||
Override net.IP
|
||||
}
|
||||
|
||||
type ResponseWrapper struct {
|
||||
dns.ResponseWriter
|
||||
Rules []Rule
|
||||
}
|
||||
|
||||
func (r *ResponseWrapper) WriteMsg(res *dns.Msg) error {
|
||||
for _, r := range r.Rules {
|
||||
for _, rr := range res.Answer {
|
||||
if rr.Header().Rrtype == dns.TypeA {
|
||||
a := rr.(*dns.A)
|
||||
if a.A.Equal(r.Search) {
|
||||
a.A = r.Override
|
||||
}
|
||||
} else if rr.Header().Rrtype == dns.TypeAAAA {
|
||||
a := rr.(*dns.AAAA)
|
||||
if a.AAAA.Equal(r.Search) {
|
||||
a.AAAA = r.Override
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r.ResponseWriter.WriteMsg(res)
|
||||
}
|
||||
|
||||
func (r *ResponseWrapper) Write(buf []byte) (int, error) {
|
||||
log.Warning("ResponseWrapper called with Write: not ensuring overide")
|
||||
return r.ResponseWriter.Write(buf)
|
||||
}
|
20
override.go
Normal file
20
override.go
Normal file
@ -0,0 +1,20 @@
|
||||
package overide
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
type Override struct {
|
||||
Next plugin.Handler
|
||||
Rules []Rule
|
||||
}
|
||||
|
||||
func (o Override) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
rw := &ResponseWrapper{ResponseWriter: w, Rules: o.Rules}
|
||||
return plugin.NextOrFailure(o.Name(), o.Next, ctx, rw, r)
|
||||
}
|
||||
|
||||
func (o Override) Name() string { return "override" }
|
40
setup.go
Normal file
40
setup.go
Normal file
@ -0,0 +1,40 @@
|
||||
package overide
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/plugin"
|
||||
)
|
||||
|
||||
func init() {
|
||||
plugin.Register("override", setup)
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
c.Next()
|
||||
|
||||
rules, err := parseRules(c)
|
||||
|
||||
if err != nil {
|
||||
return plugin.Error("override", err)
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
||||
return Override{Next: next, Rules: rules}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseRules(c *caddy.Controller) ([]Rule, error) {
|
||||
var rules []Rule
|
||||
|
||||
rules = append(rules, Rule{
|
||||
Search: net.ParseIP("45.9.63.244"),
|
||||
Override: net.ParseIP("10.0.0.1"),
|
||||
})
|
||||
|
||||
return rules, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user