Add inline support for middleware/hosts (#1072)

This fix add inline support for middleware/hosts so that
it is possible to specify hosts file insides the Corefile:
```
hosts inline example.org {
    10.0.0.1 example.org
    fallthrough
}
```

This fix fixes 999.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2017-09-21 04:18:13 -07:00
committed by GitHub
parent aecf916377
commit 7109c6715c
5 changed files with 157 additions and 2 deletions

View File

@@ -53,6 +53,11 @@ type Hostsfile struct {
// We don't support old-classful IP address notation.
byAddr map[string][]string
// inline saves the hosts file is inlined in Corefile
// We need a copy here as we want to use inline to override
// the default /etc/hosts
inline []string
expire time.Time
path string
mtime time.Time
@@ -74,6 +79,10 @@ func (h *Hostsfile) ReadHosts() {
var file *os.File
if file, _ = os.Open(h.path); file == nil {
// If this is the first time then we will try to parse inline
if len(h.byAddr) == 0 && len(h.inline) > 0 {
h.Parse(nil)
}
return
}
defer file.Close()
@@ -92,7 +101,12 @@ func (h *Hostsfile) Parse(file io.Reader) {
hsv6 := make(map[string][]net.IP)
is := make(map[string][]string)
scanner := bufio.NewScanner(file)
var readers []io.Reader
if file != nil {
readers = append(readers, file)
}
readers = append(readers, strings.NewReader(strings.Join(h.inline, "\n")))
scanner := bufio.NewScanner(io.MultiReader(readers...))
for scanner.Scan() {
line := scanner.Bytes()
if i := bytes.Index(line, []byte{'#'}); i >= 0 {