2022-03-31 20:00:17 +00:00
|
|
|
package override
|
2022-03-31 19:46:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/coredns/caddy"
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
2022-04-02 23:33:41 +00:00
|
|
|
clog "github.com/coredns/coredns/plugin/pkg/log"
|
2022-03-31 19:46:56 +00:00
|
|
|
)
|
|
|
|
|
2022-04-02 23:33:41 +00:00
|
|
|
var log = clog.NewWithPlugin("overide")
|
|
|
|
|
2022-03-31 19:46:56 +00:00
|
|
|
func init() {
|
|
|
|
plugin.Register("override", setup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
rules, err := parseRules(c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return plugin.Error("override", err)
|
|
|
|
}
|
|
|
|
|
2022-04-02 23:44:37 +00:00
|
|
|
log.Debug(rules)
|
|
|
|
|
2022-03-31 19:46:56 +00:00
|
|
|
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
|
|
|
|
|
2022-04-02 23:33:41 +00:00
|
|
|
for c.Next() {
|
|
|
|
for c.NextBlock() {
|
|
|
|
searchIP := net.ParseIP(c.Val())
|
|
|
|
if searchIP == nil {
|
|
|
|
return nil, c.Errf("invalid search IP: %s", c.Val())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
|
|
|
|
overrideIP := net.ParseIP(c.Val())
|
|
|
|
if overrideIP == nil {
|
|
|
|
return nil, c.Errf("invalid override IP: %s", c.Val())
|
|
|
|
}
|
|
|
|
|
|
|
|
rules = append(rules, Rule{Search: searchIP, Override: overrideIP})
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 19:46:56 +00:00
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
}
|