mirror of
https://github.com/coredns/coredns.git
synced 2025-12-19 00:25:11 -05:00
middleware/hosts for /etc/hosts parsing (#695)
* add hosts middleware * forgot pointer receiver * add appropriately modified hostsfile tests from golang repo * remove test artifacts, separate hostsfile parsing from caching and opening, remove unused metrics references, move middleware up the chain * refactored the logic for creating records and filtering ip address versions. also got PTR lookups working * Add README.md. Modify config to be more concise. Add zones list to config. Filter PTR responses based on zones list. * add Fallthrough and return correct dns response code otherwise * Simplified Hostsfile to only store hosts in the zones we care about, and by ip version. Added handler tests and improved other tests. * oops, goimports loaded a package from a different repo
This commit is contained in:
88
middleware/hosts/setup.go
Normal file
88
middleware/hosts/setup.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/middleware"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("hosts", caddy.Plugin{
|
||||
ServerType: "dns",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
func setup(c *caddy.Controller) error {
|
||||
h, err := hostsParse(c)
|
||||
if err != nil {
|
||||
return middleware.Error("hosts", err)
|
||||
}
|
||||
|
||||
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
|
||||
h.Next = next
|
||||
return h
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func hostsParse(c *caddy.Controller) (Hosts, error) {
|
||||
var h = Hosts{
|
||||
Hostsfile: &Hostsfile{path: "/etc/hosts"},
|
||||
}
|
||||
defer h.ReadHosts()
|
||||
|
||||
config := dnsserver.GetConfig(c)
|
||||
|
||||
for c.Next() {
|
||||
if c.Val() == "hosts" { // hosts [FILE] [ZONES...]
|
||||
args := c.RemainingArgs()
|
||||
if len(args) >= 1 {
|
||||
h.path = args[0]
|
||||
args = args[1:]
|
||||
|
||||
if !path.IsAbs(h.path) && config.Root != "" {
|
||||
h.path = path.Join(config.Root, h.path)
|
||||
}
|
||||
_, err := os.Stat(h.path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("[WARNING] File does not exist: %s", h.path)
|
||||
} else {
|
||||
return h, c.Errf("unable to access hosts file '%s': %v", h.path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
origins := make([]string, len(c.ServerBlockKeys))
|
||||
copy(origins, c.ServerBlockKeys)
|
||||
if len(args) > 0 {
|
||||
origins = args
|
||||
}
|
||||
|
||||
for i := range origins {
|
||||
origins[i] = middleware.Host(origins[i]).Normalize()
|
||||
}
|
||||
h.Origins = origins
|
||||
|
||||
for c.NextBlock() {
|
||||
switch c.Val() {
|
||||
case "fallthrough":
|
||||
args := c.RemainingArgs()
|
||||
if len(args) == 0 {
|
||||
h.Fallthrough = true
|
||||
continue
|
||||
}
|
||||
return h, c.ArgErr()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
Reference in New Issue
Block a user