Files
synthetic/setup.go
T

104 lines
2.5 KiB
Go
Raw Normal View History

2024-02-08 17:44:51 -08:00
package synthetic
import (
"fmt"
"net"
2024-02-20 22:26:54 -08:00
"strconv"
"strings"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
2024-02-08 17:44:51 -08:00
)
// syntheticConfig holds the parsed Corefile configuration for the synthetic plugin.
2024-02-08 17:44:51 -08:00
type syntheticConfig struct {
// net is the set of IP networks for which synthetic forward responses are
// generated. Only addresses falling within these CIDRs will be resolved.
net []*net.IPNet
// forward is the DNS zone appended to synthetic hostnames when generating
// PTR records (e.g., "example.com").
2024-02-08 17:44:51 -08:00
forward string
// ttl is the time-to-live value applied to all synthetic responses.
// Defaults to 0 (no caching).
ttl uint32
// prefix is the hostname label prefix used to identify synthetic queries
// (e.g., "ip-"). A trailing dash is appended automatically if absent.
prefix string
2024-02-08 17:44:51 -08:00
}
func init() { plugin.Register("synthetic", setup) }
// setup parses the Corefile configuration block for the synthetic plugin and
// registers it in the CoreDNS handler chain.
2024-02-08 17:44:51 -08:00
func setup(c *caddy.Controller) error {
config, err := parseConfig(c)
if err != nil {
return err
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return synthetic{Next: next, Config: config}
})
return nil
}
// parseConfig reads the synthetic { ... } block from the Corefile and returns
// a validated syntheticConfig.
func parseConfig(c *caddy.Controller) (syntheticConfig, error) {
2024-02-08 17:44:51 -08:00
var config syntheticConfig
for c.Next() {
for c.NextBlock() {
switch v := c.Val(); v {
case "net":
for _, arg := range c.RemainingArgs() {
2024-02-08 17:44:51 -08:00
_, cidr, err := net.ParseCIDR(arg)
if err != nil {
return config, fmt.Errorf("synthetic: invalid CIDR notation: %v", arg)
2024-02-08 17:44:51 -08:00
}
config.net = append(config.net, cidr)
2024-02-08 17:44:51 -08:00
}
case "forward":
args := c.RemainingArgs()
if len(args) > 0 {
config.forward = args[0]
2024-02-08 17:44:51 -08:00
}
2024-02-20 22:26:54 -08:00
case "ttl":
for _, arg := range c.RemainingArgs() {
2024-02-20 22:26:54 -08:00
ttl64, err := strconv.ParseUint(arg, 10, 32)
if err != nil {
return config, fmt.Errorf("synthetic: invalid ttl value: %v", arg)
2024-02-20 22:26:54 -08:00
}
config.ttl = uint32(ttl64)
}
case "prefix":
args := c.RemainingArgs()
if len(args) > 0 {
config.prefix = args[0]
2024-02-20 22:26:54 -08:00
}
2024-02-08 17:44:51 -08:00
default:
return config, c.Errf("unknown property '%s'", v)
2024-02-08 17:44:51 -08:00
}
}
}
// Apply defaults.
2024-02-20 22:26:54 -08:00
if config.prefix == "" {
config.prefix = "ip"
}
if !strings.HasSuffix(config.prefix, "-") {
config.prefix += "-"
2024-02-20 22:26:54 -08:00
}
return config, nil
2024-02-08 17:44:51 -08:00
}