plugin/hosts provide more configuration flexibility (#2535)

* plugin/hosts provide more configuration flexibility

This patch adds few features to the host plugin
 * no-reverse (both as first argument on the plugin line and inline)
   disable the automatic generation of reserve entries for hosts
 * ttl <duration> (inline only atm)
   allows to change the default ttl (default 5 minutes)
 * reload <duration> (inline only atm)
   allows to change the reloading interval (default 5s)

* plugin/hosts remove superfluous parameters to parse
This commit is contained in:
Thomas Mangin
2019-02-12 16:09:33 +00:00
committed by Pat Moroney
parent e47d881461
commit 4b402e000d
6 changed files with 163 additions and 70 deletions

View File

@@ -3,6 +3,7 @@ package hosts
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
@@ -22,28 +23,37 @@ func init() {
})
}
func periodicHostsUpdate(h *Hosts) chan bool {
parseChan := make(chan bool)
if h.options.reload == durationOf0s {
return parseChan
}
go func() {
ticker := time.NewTicker(h.options.reload)
for {
select {
case <-parseChan:
return
case <-ticker.C:
h.readHosts()
}
}
}()
return parseChan
}
func setup(c *caddy.Controller) error {
h, err := hostsParse(c)
if err != nil {
return plugin.Error("hosts", err)
}
parseChan := make(chan bool)
parseChan := periodicHostsUpdate(&h)
c.OnStartup(func() error {
h.readHosts()
go func() {
ticker := time.NewTicker(5 * time.Second)
for {
select {
case <-parseChan:
return
case <-ticker.C:
h.readHosts()
}
}
}()
return nil
})
@@ -61,15 +71,18 @@ func setup(c *caddy.Controller) error {
}
func hostsParse(c *caddy.Controller) (Hosts, error) {
var h = Hosts{
config := dnsserver.GetConfig(c)
options := newOptions()
h := Hosts{
Hostsfile: &Hostsfile{
path: "/etc/hosts",
hmap: newHostsMap(),
path: "/etc/hosts",
hmap: newHostsMap(),
options: options,
},
}
config := dnsserver.GetConfig(c)
inline := []string{}
i := 0
for c.Next() {
@@ -79,6 +92,7 @@ func hostsParse(c *caddy.Controller) (Hosts, error) {
i++
args := c.RemainingArgs()
if len(args) >= 1 {
h.path = args[0]
args = args[1:]
@@ -114,6 +128,34 @@ func hostsParse(c *caddy.Controller) (Hosts, error) {
switch c.Val() {
case "fallthrough":
h.Fall.SetZonesFromArgs(c.RemainingArgs())
case "no_reverse":
options.autoReverse = false
case "ttl":
remaining := c.RemainingArgs()
if len(remaining) < 1 {
return h, c.Errf("ttl needs a time in second")
}
ttl, err := strconv.Atoi(remaining[0])
if err != nil {
return h, c.Errf("ttl needs a number of second")
}
if ttl <= 0 || ttl > 65535 {
return h, c.Errf("ttl provided is invalid")
}
options.ttl = uint32(ttl)
case "reload":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return h, c.Errf("reload needs a duration (zero seconds to disable)")
}
reload, err := time.ParseDuration(remaining[0])
if err != nil {
return h, c.Errf("invalid duration for reload '%s'", remaining[0])
}
if reload < durationOf0s {
return h, c.Errf("invalid negative duration for reload '%s'", remaining[0])
}
options.reload = reload
default:
if len(h.Fall.Zones) == 0 {
line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ")