diff options
Diffstat (limited to 'plugin/hosts/setup.go')
-rw-r--r-- | plugin/hosts/setup.go | 78 |
1 files changed, 60 insertions, 18 deletions
diff --git a/plugin/hosts/setup.go b/plugin/hosts/setup.go index 3dafb5be7..945a53dfd 100644 --- a/plugin/hosts/setup.go +++ b/plugin/hosts/setup.go @@ -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()...), " ") |