synthetic/setup.go

92 lines
2.1 KiB
Go
Raw Normal View History

2024-02-09 01:44:51 +00:00
package synthetic
import (
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"net"
2024-02-21 06:26:54 +00:00
"strconv"
"strings"
2024-02-09 01:44:51 +00:00
)
// syntheticConfig holds the configuration options for the synthetic plugin.
type syntheticConfig struct {
net []*net.IPNet
forward string
2024-02-21 06:26:54 +00:00
ttl uint32
prefix string
2024-02-09 01:44:51 +00:00
}
// 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
}
2024-02-21 06:26:54 +00:00
// 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
}
2024-02-09 01:44:51 +00:00
default:
return c.Errf("unknown property '%s'", v)
}
}
}
2024-02-21 06:26:54 +00:00
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 + "-"
}
2024-02-09 01:44:51 +00:00
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return synthetic{Next: next, Config: config}
})
return nil
}