diff --git a/README.md b/README.md index 2d6df2f..40fd49e 100644 --- a/README.md +++ b/README.md @@ -79,4 +79,10 @@ This plugin provides an easier experience by just providing an IP prefix in CIDR ## Development -TODO: Add development instructions +Standard Go development practices apply. + +### Tests + +``` +$ go test -v +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5ae6ce7 --- /dev/null +++ b/go.mod @@ -0,0 +1,40 @@ +module code.fritzlab.net/dns/synthetic + +go 1.21.6 + +require ( + github.com/coredns/caddy v1.1.1 + github.com/coredns/coredns v1.11.1 + github.com/miekg/dns v1.1.58 +) + +require ( + github.com/apparentlymart/go-cidr v1.1.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect + github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect + github.com/golang/mock v1.6.0 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/pprof v0.0.0-20230509042627-b1315fad0c5a // indirect + github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect + github.com/onsi/ginkgo/v2 v2.11.0 // indirect + github.com/opentracing/opentracing-go v1.2.0 // indirect + github.com/prometheus/client_golang v1.16.0 // indirect + github.com/prometheus/client_model v0.4.0 // indirect + github.com/prometheus/common v0.44.0 // indirect + github.com/prometheus/procfs v0.10.1 // indirect + github.com/quic-go/qtls-go1-20 v0.3.1 // indirect + github.com/quic-go/quic-go v0.37.4 // indirect + golang.org/x/crypto v0.18.0 // indirect + golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect + golang.org/x/mod v0.14.0 // indirect + golang.org/x/net v0.20.0 // indirect + golang.org/x/sys v0.16.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.17.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect + google.golang.org/grpc v1.57.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..566da60 --- /dev/null +++ b/go.sum @@ -0,0 +1,114 @@ +github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU= +github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/coredns/caddy v1.1.1 h1:2eYKZT7i6yxIfGP3qLJoJ7HAsDJqYB+X68g4NYjSrE0= +github.com/coredns/caddy v1.1.1/go.mod h1:A6ntJQlAWuQfFlsd9hvigKbo2WS0VUs2l1e2F+BawD4= +github.com/coredns/coredns v1.11.1 h1:IYBM+j/Xx3nTV4HE1s626G9msmJZSdKL9k0ZagYcZFQ= +github.com/coredns/coredns v1.11.1/go.mod h1:X0ac9RLzd/WAxKuEe3A52miPSm6XjfoxVNAjEQgjphk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= +github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= +github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20230509042627-b1315fad0c5a h1:PEOGDI1kkyW37YqPWHLHc+D20D9+87Wt12TCcfTUo5Q= +github.com/google/pprof v0.0.0-20230509042627-b1315fad0c5a/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 h1:MJG/KsmcqMwFAkh8mTnAwhyKoB+sTAnY4CACC110tbU= +github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645/go.mod h1:6iZfnjpejD4L/4DwD7NryNaJyCQdzwWwH2MWhCA90Kw= +github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= +github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4= +github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY= +github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU= +github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM= +github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc= +github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ= +github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= +github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8= +github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc= +github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= +github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= +github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdOOfY= +github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY= +github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+PymziUAg= +github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM= +github.com/quic-go/qtls-go1-20 v0.3.1 h1:O4BLOM3hwfVF3AcktIylQXyl7Yi2iBNVy5QsV+ySxbg= +github.com/quic-go/qtls-go1-20 v0.3.1/go.mod h1:X9Nh97ZL80Z+bX/gUXMbipO6OxdiDi58b/fMC9mAL+k= +github.com/quic-go/quic-go v0.37.4 h1:ke8B73yMCWGq9MfrCCAw0Uzdm7GaViC3i39dsIdDlH4= +github.com/quic-go/quic-go v0.37.4/go.mod h1:YsbH1r4mSHPJcLF4k4zruUkLBqctEMBDR6VPvcYjIsU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o= +golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= +golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= +golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw= +google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/setup.go b/setup.go index 23beceb..f1e9a1f 100644 --- a/setup.go +++ b/setup.go @@ -6,12 +6,16 @@ import ( "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" "net" + "strconv" + "strings" ) // syntheticConfig holds the configuration options for the synthetic plugin. type syntheticConfig struct { net []*net.IPNet forward string + ttl uint32 + prefix string } // init registers this plugin. @@ -45,12 +49,40 @@ func setup(c *caddy.Controller) error { config.forward = arg } + // Configuration for the TTL value + case "ttl": + args := c.RemainingArgs() + for _, arg := range args { + ttl64, err := strconv.ParseUint(arg, 10, 32) + if err != nil { + return fmt.Errorf("synthetic: invalid ttl value: %v", arg) + } + config.ttl = uint32(ttl64) + } + + // configuration for the prefix value (defaults to `ip`) + case "prefix": + args := c.RemainingArgs() + for _, arg := range args { + config.prefix = arg + } + default: return c.Errf("unknown property '%s'", v) } } } + if config.forward == "" { + return c.Err("synthetic: forward zone must be specified") + } + if config.prefix == "" { + config.prefix = "ip" + } + if !strings.HasSuffix(config.prefix, "-") { + config.prefix = config.prefix + "-" + } + dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { return synthetic{Next: next, Config: config} }) diff --git a/synthetic.go b/synthetic.go index 5f0abe4..169c681 100644 --- a/synthetic.go +++ b/synthetic.go @@ -13,22 +13,15 @@ import ( "strings" ) -// Define log to be a logger with the plugin name in it. This way we can just use log.Info and -// friends to log. var log = clog.NewWithPlugin("synthetic") -// Name implements the Handler interface. func (s synthetic) Name() string { return "synthetic" } -// synthetic is an synthetic plugin to show how to write a plugin. type synthetic struct { Next plugin.Handler Config syntheticConfig } -// ServeDNS implements the plugin.Handler interface. -// -// This method gets called when synthetic is used in a Server func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { // Create a new state for this request. This is used to store state and allows us to pass this @@ -39,12 +32,21 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms rec := dnstest.NewRecorder(&test.ResponseWriter{}) rc, err := plugin.NextOrFailure(s.Name(), s.Next, ctx, rec, r) + log.Debug("Next plugin in chain responded with rcode ", + rc, + " and ", + len(r.Answer), + " answer(s) for ", + state.QName(), + ) + // If the next plugin in the chain's recorded response is success, we go with that. - if rc == dns.RcodeNameError { - log.Debug("Next plugin in chain responded with acceptable response ", rc, " for ", state.QName()) + if rc == dns.RcodeSuccess && len(rec.Msg.Answer) > 0 { + log.Info("Next Plugin's answers are acceptable") w.WriteMsg(rec.Msg) return rc, err } + log.Debug("Injecting Synthetic response") switch state.QType() { @@ -54,26 +56,26 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms // case dns.TypeA, dns.TypeAAAA: - // only continue if the first label starts with "ip-" (fall back to recorded response) - if !strings.HasPrefix(state.Name(), "ip-") { + // only continue if the first label starts with the prefix (fall back to recorded response) + if !strings.HasPrefix(state.Name(), s.Config.prefix) { w.WriteMsg(rec.Msg) return rc, err } - // parse first dns label (now we have: "ip-") + // parse first dns label (now we have: "") firstLabel := strings.Split(state.Name(), ".")[0] - // trim "ip-" prefix (now we have: "") - ipStr := strings.TrimPrefix(firstLabel, "ip-") + // trim the prefix (now we have: "") + ipStr := strings.TrimPrefix(firstLabel, s.Config.prefix) - // attempt to parse as IPv4 or IPv6 + // attempt to parse as IPv6 or IPv4 ip := net.ParseIP(strings.ReplaceAll(ipStr, "-", ".")) if ip == nil { ip = net.ParseIP(strings.ReplaceAll(ipStr, "-", ":")) } log.Debug(state.Name(), " parsed to ", ip) - // don't continue if we couldn't parse the IP (fall back to recorded respones) + // don't continue if we couldn't parse the IP (fall back to recorded responses) if ip == nil { log.Debug("Failed to parse IP from: ", state.QName()) w.WriteMsg(rec.Msg) @@ -100,7 +102,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms log.Debug("Responding to AAAA request for ", state.QName()) m := new(dns.Msg) m.SetReply(r) - hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()} + hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl} m.Answer = append(m.Answer, &dns.AAAA{Hdr: hdr, AAAA: ip.To16()}) w.WriteMsg(m) return dns.RcodeSuccess, nil @@ -111,7 +113,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms log.Debug("Responding to A request for ", state.QName()) m := new(dns.Msg) m.SetReply(r) - hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()} + hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl} m.Answer = append(m.Answer, &dns.A{Hdr: hdr, A: ip.To4()}) w.WriteMsg(m) return dns.RcodeSuccess, nil @@ -142,7 +144,7 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms // // Handle PTR requests (Reverse Lookup) - // Note: on the forward lookup, we're guaranteed to have a match on the network + // Note: on the reverse lookup, we're guaranteed to have a match on the network based on the zone file // case dns.TypePTR: ip := inArpaToIp(state.QName()) @@ -155,11 +157,11 @@ func (s synthetic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms } // successful reverse lookup - forward := ipToDomainName(ip, s.Config.forward) + forward := ipToDomainName(s.Config.prefix, ip, s.Config.forward) log.Debug("Responding to PTR request for ", state.QName(), " with ", forward) m := new(dns.Msg) m.SetReply(r) - hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass()} + hdr := dns.RR_Header{Name: state.QName(), Rrtype: state.QType(), Class: state.QClass(), Ttl: s.Config.ttl} m.Answer = append(m.Answer, &dns.PTR{Hdr: hdr, Ptr: forward}) w.WriteMsg(m) return dns.RcodeSuccess, nil @@ -209,7 +211,7 @@ func inArpaToIp(name string) net.IP { return nil } -func ipToDomainName(ip net.IP, zone string) string { +func ipToDomainName(prefix string, ip net.IP, zone string) string { if ip == nil { return "" } @@ -228,5 +230,5 @@ func ipToDomainName(ip net.IP, zone string) string { } response := strings.Join(strings.Split(ip.String(), sep), "-") - return "ip-" + response + zone + return prefix + response + zone } diff --git a/synthetic_test.go b/synthetic_test.go new file mode 100644 index 0000000..2764065 --- /dev/null +++ b/synthetic_test.go @@ -0,0 +1,231 @@ +package synthetic + +import ( + "context" + "fmt" + "github.com/coredns/coredns/plugin/pkg/dnstest" + "github.com/coredns/coredns/plugin/test" + "github.com/miekg/dns" + "net" + "testing" +) + +// todo: +// 1. A lookup for v6 address +func TestServeDNSv4(t *testing.T) { + ip, ipNet, err := net.ParseCIDR("192.0.2.0/24") + if err != nil { + log.Fatal(err) + } + + s := synthetic{ + Next: test.ErrorHandler(), + Config: syntheticConfig{ + net: []*net.IPNet{{IP: ip, Mask: ipNet.Mask}}, + forward: "example.com", + prefix: "ip-", + }, + } + + testCases := []struct { + qname string + qtype uint16 + wantrcode int + wantAnswer []string + wantTTL uint32 + }{ + { + qname: "ip-192-0-2-0.example.com", + qtype: dns.TypeA, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"192.0.2.0"}, + wantTTL: 0, + }, + { + qname: "ip-192-0-2-255.example.com", + qtype: dns.TypeA, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"192.0.2.255"}, + wantTTL: 0, + }, + { + qname: "ip-203-0-113-100.example.com", + qtype: dns.TypeA, + wantrcode: dns.RcodeServerFailure, + wantAnswer: nil, + wantTTL: 0, + }, + { + qname: "123.2.0.192.in-addr.arpa.", + qtype: dns.TypePTR, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"ip-192-0-2-123.example.com."}, + wantTTL: 0, + }, + { + qname: "ip-2001-db8-abcd--.example.com", + qtype: dns.TypeA, + wantrcode: dns.RcodeServerFailure, + wantAnswer: nil, + wantTTL: 0, + }, + } + + for i, tc := range testCases { + errorMsgPrefix := fmt.Sprintf("Test case %v for '%v' failed. Expected", i, tc.qname) + ctx := context.TODO() + w := dnstest.NewRecorder(&test.ResponseWriter{}) + r := new(dns.Msg) + r.SetQuestion(tc.qname, tc.qtype) + + rc, err := s.ServeDNS(ctx, w, r) + + if err != nil { + t.Errorf("%v no error, but got %v", errorMsgPrefix, err) + } + if rc != tc.wantrcode { + t.Errorf("%v rcode %v, but got %v", errorMsgPrefix, tc.wantrcode, rc) + } + if len(w.Msg.Answer) == 0 && tc.wantAnswer != nil { + t.Errorf("%v an answer, but got none", errorMsgPrefix) + continue + } + if tc.wantAnswer == nil { + if len(w.Msg.Answer) > 0 { + t.Errorf("%v no answer, but got %v", errorMsgPrefix, w.Msg.Answer[0]) + } + continue + } + if w.Msg.Answer[0].Header().Ttl != 0 { + t.Errorf("%v TTL to be 0, but got %v", errorMsgPrefix, w.Msg.Answer[0].Header().Ttl) + } + if w.Msg.Answer[0].Header().Name != tc.qname { + t.Errorf("%v Name to be %s, but got %s", errorMsgPrefix, tc.qname, w.Msg.Answer[0].Header().Name) + } + if w.Msg.Answer[0].Header().Rrtype != tc.qtype { + t.Errorf("%v Type to be %d, but got %d", errorMsgPrefix, tc.qtype, w.Msg.Answer[0].Header().Rrtype) + } + if w.Msg.Answer[0].Header().Ttl != tc.wantTTL { + t.Errorf("%v TTL to be %d, but got %d", errorMsgPrefix, tc.wantTTL, w.Msg.Answer[0].Header().Ttl) + } + switch tc.qtype { + case dns.TypeA: + if w.Msg.Answer[0].(*dns.A).A.String() != tc.wantAnswer[0] { + t.Errorf("%v answer %s, but got %s", errorMsgPrefix, tc.wantAnswer[0], w.Msg.Answer[0].(*dns.A).A.String()) + } + case dns.TypePTR: + if w.Msg.Answer[0].(*dns.PTR).Ptr != tc.wantAnswer[0] { + t.Errorf("%v answer %s, but got %s", errorMsgPrefix, tc.wantAnswer[0], w.Msg.Answer[0].(*dns.PTR).Ptr) + } + } + } +} + +// todo: +// 2. AAAA lookup for v4 address +func TestServeDNSv6(t *testing.T) { + ip, ipNet, err := net.ParseCIDR("2001:db8:abcd::/48") + if err != nil { + log.Fatal(err) + } + + s := synthetic{ + Next: test.ErrorHandler(), + Config: syntheticConfig{ + net: []*net.IPNet{{IP: ip, Mask: ipNet.Mask}}, + forward: "example.com.", + ttl: 1800, + prefix: "ip6-", + }, + } + + testCases := []struct { + qname string + qtype uint16 + wantrcode int + wantAnswer []string + wantTTL uint32 + }{ + { + qname: "ip6-2001-db8-abcd--.example.com", + qtype: dns.TypeAAAA, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"2001:db8:abcd::"}, + wantTTL: 1800, + }, + { + qname: "ip6-2001-db8-abcd-1234-4567-890a-bcde-f123.example.com", + qtype: dns.TypeAAAA, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"2001:db8:abcd:1234:4567:890a:bcde:f123"}, + wantTTL: 1800, + }, + { + qname: "ip6-2001-db8-1234--.example.com", + qtype: dns.TypeAAAA, + wantrcode: dns.RcodeServerFailure, + wantAnswer: nil, + wantTTL: 1800, + }, + { + qname: "3.2.1.f.e.d.c.b.a.0.9.8.7.6.5.4.4.3.2.1.d.c.b.a.8.b.d.0.1.0.0.2.ip6.arpa.", + qtype: dns.TypePTR, + wantrcode: dns.RcodeSuccess, + wantAnswer: []string{"ip6-2001-db8-abcd-1234-4567-890a-bcde-f123.example.com."}, + wantTTL: 1800, + }, + { + qname: "ip6-192-0-2-0.example.com", + qtype: dns.TypeAAAA, + wantrcode: dns.RcodeServerFailure, + wantAnswer: nil, + wantTTL: 0, + }, + } + + for i, tc := range testCases { + errorMsgPrefix := fmt.Sprintf("Test case %v for '%v' failed. Expected", i, tc.qname) + ctx := context.TODO() + w := dnstest.NewRecorder(&test.ResponseWriter{}) + r := new(dns.Msg) + r.SetQuestion(tc.qname, tc.qtype) + + rc, err := s.ServeDNS(ctx, w, r) + + if err != nil { + t.Errorf("%v no error, but got %v", errorMsgPrefix, err) + } + if rc != tc.wantrcode { + t.Errorf("%v rcode %v, but got %v", errorMsgPrefix, tc.wantrcode, rc) + } + if len(w.Msg.Answer) == 0 && tc.wantAnswer != nil { + t.Errorf("%v an answer, but got none", errorMsgPrefix) + continue + } + if tc.wantAnswer == nil { + if len(w.Msg.Answer) > 0 { + t.Errorf("%v no answer, but got %v", errorMsgPrefix, w.Msg.Answer[0]) + } + continue + } + if w.Msg.Answer[0].Header().Name != tc.qname { + t.Errorf("%v Name to be %s, but got %s", errorMsgPrefix, tc.qname, w.Msg.Answer[0].Header().Name) + } + if w.Msg.Answer[0].Header().Rrtype != tc.qtype { + t.Errorf("%v Type to be %d, but got %d", errorMsgPrefix, tc.qtype, w.Msg.Answer[0].Header().Rrtype) + } + if w.Msg.Answer[0].Header().Ttl != tc.wantTTL { + t.Errorf("%v TTL to be %d, but got %d", errorMsgPrefix, tc.wantTTL, w.Msg.Answer[0].Header().Ttl) + } + switch tc.qtype { + case dns.TypeA: + if w.Msg.Answer[0].(*dns.A).A.String() != tc.wantAnswer[0] { + t.Errorf("%v answer %s, but got %s", errorMsgPrefix, tc.wantAnswer[0], w.Msg.Answer[0].(*dns.A).A.String()) + } + case dns.TypePTR: + if w.Msg.Answer[0].(*dns.PTR).Ptr != tc.wantAnswer[0] { + t.Errorf("%v answer %s, but got %s", errorMsgPrefix, tc.wantAnswer[0], w.Msg.Answer[0].(*dns.PTR).Ptr) + } + } + } +}