1
0
Fork 0

Merge pull request #4 from Nordix/issue-3

Add "translateAll" parameter. Fix for #3
pull/5/head
Ambrose Chua 2018-10-31 16:12:54 +08:00 committed by GitHub
commit 4efeb9f3bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 15 deletions

View File

@ -20,6 +20,7 @@ type DNS64 struct {
Next plugin.Handler Next plugin.Handler
Proxy proxy.Proxy Proxy proxy.Proxy
Prefix *net.IPNet Prefix *net.IPNet
translateAll bool
} }
// ServeDNS implements the plugin.Handler interface. // ServeDNS implements the plugin.Handler interface.
@ -55,7 +56,7 @@ func (r *ResponseWriter) WriteMsg(res *dns.Msg) error {
// do not modify if there are AAAA records or NameError. continue if NoData or any other error. // do not modify if there are AAAA records or NameError. continue if NoData or any other error.
ty, _ := response.Typify(res, time.Now().UTC()) ty, _ := response.Typify(res, time.Now().UTC())
if ty == response.NoError || ty == response.NameError { if ty == response.NoError || ty == response.NameError {
if hasAAAA(res) { if hasAAAA(res) && ! r.translateAll {
return r.ResponseWriter.WriteMsg(res) return r.ResponseWriter.WriteMsg(res)
} }
} }

View File

@ -2,7 +2,7 @@ package dns64
import ( import (
"net" "net"
"strconv"
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil" "github.com/coredns/coredns/plugin/pkg/dnsutil"
@ -22,26 +22,27 @@ func init() {
} }
func setup(c *caddy.Controller) error { func setup(c *caddy.Controller) error {
prxy, pref, err := dns64Parse(c) prxy, pref, translateAll, err := dns64Parse(c)
if err != nil { if err != nil {
return plugin.Error("dns64", err) return plugin.Error("dns64", err)
} }
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return DNS64{Next: next, Proxy: prxy, Prefix: pref} return DNS64{Next: next, Proxy: prxy, Prefix: pref, translateAll: translateAll}
}) })
return nil return nil
} }
func dns64Parse(c *caddy.Controller) (proxy.Proxy, *net.IPNet, error) { func dns64Parse(c *caddy.Controller) (proxy.Proxy, *net.IPNet, bool, error) {
prxy := proxy.Proxy{} prxy := proxy.Proxy{}
_, pref, _ := net.ParseCIDR("64:ff9b::/96") _, pref, _ := net.ParseCIDR("64:ff9b::/96")
translateAll := false
for c.Next() { for c.Next() {
args := c.RemainingArgs() args := c.RemainingArgs()
if len(args) > 0 { if len(args) > 0 {
return prxy, pref, c.ArgErr() return prxy, pref, translateAll, c.ArgErr()
} }
for c.NextBlock() { for c.NextBlock() {
@ -49,16 +50,17 @@ func dns64Parse(c *caddy.Controller) (proxy.Proxy, *net.IPNet, error) {
case "upstream": case "upstream":
args := c.RemainingArgs() args := c.RemainingArgs()
if len(args) == 0 { if len(args) == 0 {
return prxy, pref, c.ArgErr() return prxy, pref, translateAll, c.ArgErr()
} }
ups, err := dnsutil.ParseHostPortOrFile(args...) ups, err := dnsutil.ParseHostPortOrFile(args...)
if err != nil { if err != nil {
return prxy, pref, err return prxy, pref, translateAll, err
} }
prxy = proxy.NewLookup(ups) prxy = proxy.NewLookup(ups)
log.Infof("Upstream %v", ups)
case "prefix": case "prefix":
if !c.NextArg() { if !c.NextArg() {
return prxy, pref, c.ArgErr() return prxy, pref, translateAll, c.ArgErr()
} }
var err error var err error
_, pref, err = net.ParseCIDR(c.Val()) _, pref, err = net.ParseCIDR(c.Val())
@ -66,20 +68,25 @@ func dns64Parse(c *caddy.Controller) (proxy.Proxy, *net.IPNet, error) {
// test for valid prefix // test for valid prefix
n, total := pref.Mask.Size() n, total := pref.Mask.Size()
if total != 128 { if total != 128 {
return prxy, pref, c.Errf("'%s' not a valid IPv6 address", pref) return prxy, pref, translateAll, c.Errf("'%s' not a valid IPv6 address", pref)
} }
if n%8 != 0 || n < 32 || n > 96 { if n%8 != 0 || n < 32 || n > 96 {
return prxy, pref, c.Errf("'%s' not a valid prefix length", pref) return prxy, pref, translateAll, c.Errf("'%s' not a valid prefix length", pref)
} }
if err != nil { if err != nil {
return prxy, pref, err return prxy, pref, translateAll, err
}
log.Infof("Prefix %v", pref)
case "translateAll":
args := c.RemainingArgs()
if len(args) > 0 {
translateAll, _ = strconv.ParseBool(args[0])
} }
default: default:
return prxy, pref, c.Errf("unknown property '%s'", c.Val()) return prxy, pref, translateAll, c.Errf("unknown property '%s'", c.Val())
} }
} }
} }
return prxy, pref, translateAll, nil
return prxy, pref, nil
} }