2016-08-19 17:14:17 -07:00
|
|
|
package rewrite
|
2016-03-18 20:57:35 +00:00
|
|
|
|
|
|
|
|
import (
|
2020-09-24 18:14:41 +02:00
|
|
|
"github.com/coredns/caddy"
|
2017-02-21 22:51:47 -08:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2016-03-18 20:57:35 +00:00
|
|
|
)
|
|
|
|
|
|
2019-09-20 08:02:30 +01:00
|
|
|
func init() { plugin.Register("rewrite", setup) }
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
2016-03-18 20:57:35 +00:00
|
|
|
rewrites, err := rewriteParse(c)
|
|
|
|
|
if err != nil {
|
2017-09-14 09:36:06 +01:00
|
|
|
return plugin.Error("rewrite", err)
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2016-08-19 17:14:17 -07:00
|
|
|
return Rewrite{Next: next, Rules: rewrites}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return nil
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func rewriteParse(c *caddy.Controller) ([]Rule, error) {
|
2017-03-06 16:32:17 -05:00
|
|
|
var rules []Rule
|
2016-03-18 20:57:35 +00:00
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
|
args := c.RemainingArgs()
|
2018-01-18 10:41:14 -05:00
|
|
|
if len(args) < 2 {
|
|
|
|
|
// Handles rules out of nested instructions, i.e. the ones enclosed in curly brackets
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
|
args = append(args, c.Val())
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-06 16:32:17 -05:00
|
|
|
rule, err := newRule(args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
2017-03-06 16:32:17 -05:00
|
|
|
rules = append(rules, rule)
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|
2017-03-06 16:32:17 -05:00
|
|
|
return rules, nil
|
2016-03-18 20:57:35 +00:00
|
|
|
}
|