2016-08-19 17:14:17 -07:00
|
|
|
package cache
|
2016-04-19 11:13:24 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
"github.com/miekg/coredns/core/dnsserver"
|
2016-04-19 11:13:24 +01:00
|
|
|
"github.com/miekg/coredns/middleware"
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
2016-04-19 11:13:24 +01:00
|
|
|
)
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func init() {
|
|
|
|
|
caddy.RegisterPlugin("cache", caddy.Plugin{
|
|
|
|
|
ServerType: "dns",
|
|
|
|
|
Action: setup,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-19 11:13:24 +01:00
|
|
|
// Cache sets up the root file path of the server.
|
2016-08-19 17:14:17 -07:00
|
|
|
func setup(c *caddy.Controller) error {
|
2016-04-19 11:13:24 +01:00
|
|
|
ttl, zones, err := cacheParse(c)
|
|
|
|
|
if err != nil {
|
2016-09-10 09:16:25 +01:00
|
|
|
return middleware.Error("cache", err)
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
2016-09-19 11:26:00 +01:00
|
|
|
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
|
2016-08-19 17:14:17 -07:00
|
|
|
return NewCache(ttl, zones, next)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return nil
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 17:14:17 -07:00
|
|
|
func cacheParse(c *caddy.Controller) (int, []string, error) {
|
2016-04-19 11:13:24 +01:00
|
|
|
var (
|
2016-08-19 17:14:17 -07:00
|
|
|
err error
|
|
|
|
|
ttl int
|
|
|
|
|
origins []string
|
2016-04-19 11:13:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
|
if c.Val() == "cache" {
|
|
|
|
|
// cache [ttl] [zones..]
|
2016-08-19 17:14:17 -07:00
|
|
|
origins = make([]string, len(c.ServerBlockKeys))
|
|
|
|
|
copy(origins, c.ServerBlockKeys)
|
2016-04-19 11:13:24 +01:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
|
origins = args
|
|
|
|
|
// first args may be just a number, then it is the ttl, if not it is a zone
|
|
|
|
|
t := origins[0]
|
|
|
|
|
ttl, err = strconv.Atoi(t)
|
|
|
|
|
if err == nil {
|
|
|
|
|
origins = origins[1:]
|
|
|
|
|
if len(origins) == 0 {
|
|
|
|
|
// There was *only* the ttl, revert back to server block
|
2016-08-19 17:14:17 -07:00
|
|
|
copy(origins, c.ServerBlockKeys)
|
2016-04-19 11:13:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 17:01:19 +01:00
|
|
|
for i := range origins {
|
2016-04-19 11:13:24 +01:00
|
|
|
origins[i] = middleware.Host(origins[i]).Normalize()
|
|
|
|
|
}
|
|
|
|
|
return ttl, origins, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0, nil, nil
|
|
|
|
|
}
|