2024-02-08 17:44:51 -08:00
|
|
|
package synthetic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
2024-02-20 22:26:54 -08:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2026-04-05 13:33:25 -05:00
|
|
|
|
|
|
|
|
"github.com/coredns/caddy"
|
|
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
|
"github.com/coredns/coredns/plugin"
|
2024-02-08 17:44:51 -08:00
|
|
|
)
|
|
|
|
|
|
2026-04-05 13:33:25 -05:00
|
|
|
// syntheticConfig holds the parsed Corefile configuration for the synthetic plugin.
|
2024-02-08 17:44:51 -08:00
|
|
|
type syntheticConfig struct {
|
2026-04-05 13:33:25 -05:00
|
|
|
// 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
|
2026-04-05 13:33:25 -05:00
|
|
|
|
|
|
|
|
// 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) }
|
|
|
|
|
|
2026-04-05 13:33:25 -05:00
|
|
|
// 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 {
|
2026-04-05 13:33:25 -05:00
|
|
|
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":
|
2026-04-05 13:33:25 -05:00
|
|
|
for _, arg := range c.RemainingArgs() {
|
2024-02-08 17:44:51 -08:00
|
|
|
_, cidr, err := net.ParseCIDR(arg)
|
2026-04-05 13:33:25 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return config, fmt.Errorf("synthetic: invalid CIDR notation: %v", arg)
|
2024-02-08 17:44:51 -08:00
|
|
|
}
|
2026-04-05 13:33:25 -05:00
|
|
|
config.net = append(config.net, cidr)
|
2024-02-08 17:44:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "forward":
|
|
|
|
|
args := c.RemainingArgs()
|
2026-04-05 13:33:25 -05:00
|
|
|
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":
|
2026-04-05 13:33:25 -05:00
|
|
|
for _, arg := range c.RemainingArgs() {
|
2024-02-20 22:26:54 -08:00
|
|
|
ttl64, err := strconv.ParseUint(arg, 10, 32)
|
|
|
|
|
if err != nil {
|
2026-04-05 13:33:25 -05:00
|
|
|
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()
|
2026-04-05 13:33:25 -05:00
|
|
|
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:
|
2026-04-05 13:33:25 -05:00
|
|
|
return config, c.Errf("unknown property '%s'", v)
|
2024-02-08 17:44:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 13:33:25 -05:00
|
|
|
// Apply defaults.
|
2024-02-20 22:26:54 -08:00
|
|
|
if config.prefix == "" {
|
|
|
|
|
config.prefix = "ip"
|
|
|
|
|
}
|
|
|
|
|
if !strings.HasSuffix(config.prefix, "-") {
|
2026-04-05 13:33:25 -05:00
|
|
|
config.prefix += "-"
|
2024-02-20 22:26:54 -08:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 13:33:25 -05:00
|
|
|
return config, nil
|
2024-02-08 17:44:51 -08:00
|
|
|
}
|