synthetic/setup.go
2024-02-20 22:26:54 -08:00

92 lines
2.1 KiB
Go

package synthetic
import (
"fmt"
"github.com/coredns/caddy"
"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.
func init() { plugin.Register("synthetic", setup) }
// setup is the function that gets called when the config parser see the token "synthetic".
func setup(c *caddy.Controller) error {
var config syntheticConfig
// Parse the configuration file
for c.Next() {
for c.NextBlock() {
switch v := c.Val(); v {
// Configuration for forward lookup zones for which to do resolution
case "net":
args := c.RemainingArgs()
for _, arg := range args {
_, cidr, err := net.ParseCIDR(arg)
if err == nil {
config.net = append(config.net, cidr)
} else {
return fmt.Errorf("synthetic: invalid reverse lookup cidr: %v", arg)
}
}
// Configuration for reverse lookup zones for the forward lookup zone name
case "forward":
args := c.RemainingArgs()
for _, arg := range args {
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}
})
return nil
}