From 50b412f5e5b4886f9c0709413c866fbc09ba74db Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 31 Mar 2022 21:46:56 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ README.md | 0 ResponseWrapper.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ override.go | 20 ++++++++++++++++++++ setup.go | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 ResponseWrapper.go create mode 100644 override.go create mode 100644 setup.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f117bc7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +go.mod +go.sum \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/ResponseWrapper.go b/ResponseWrapper.go new file mode 100644 index 0000000..11c8a3a --- /dev/null +++ b/ResponseWrapper.go @@ -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) +} diff --git a/override.go b/override.go new file mode 100644 index 0000000..6b2ec82 --- /dev/null +++ b/override.go @@ -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" } diff --git a/setup.go b/setup.go new file mode 100644 index 0000000..8619985 --- /dev/null +++ b/setup.go @@ -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 +}