| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | package hosts
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"path"
 | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("hosts")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("hosts", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	h, err := hostsParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("hosts", err)
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 	parseChan := make(chan bool)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.OnShutdown(func() error {
 | 
					
						
							|  |  |  | 		close(parseChan)
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 		h.Next = next
 | 
					
						
							|  |  |  | 		return h
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func hostsParse(c *caddy.Controller) (Hosts, error) {
 | 
					
						
							|  |  |  | 	var h = Hosts{
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 		Hostsfile: &Hostsfile{
 | 
					
						
							|  |  |  | 			path: "/etc/hosts",
 | 
					
						
							|  |  |  | 			hmap: newHostsMap(),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	config := dnsserver.GetConfig(c)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 	inline := []string{}
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 	i := 0
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2018-02-28 18:16:05 -08:00
										 |  |  | 		if i > 0 {
 | 
					
						
							|  |  |  | 			return h, plugin.ErrOnce
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(args) >= 1 {
 | 
					
						
							|  |  |  | 			h.path = args[0]
 | 
					
						
							|  |  |  | 			args = args[1:]
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			if !path.IsAbs(h.path) && config.Root != "" {
 | 
					
						
							|  |  |  | 				h.path = path.Join(config.Root, h.path)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 17:28:37 -04:00
										 |  |  | 			s, err := os.Stat(h.path)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				if os.IsNotExist(err) {
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 					log.Warningf("File does not exist: %s", h.path)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 				} else {
 | 
					
						
							|  |  |  | 					return h, c.Errf("unable to access hosts file '%s': %v", h.path, err)
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-09-29 17:28:37 -04:00
										 |  |  | 			if s != nil && s.IsDir() {
 | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 				log.Warningf("Hosts file %q is a directory", h.path)
 | 
					
						
							| 
									
										
										
										
											2017-09-29 17:28:37 -04:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		origins := make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 		copy(origins, c.ServerBlockKeys)
 | 
					
						
							|  |  |  | 		if len(args) > 0 {
 | 
					
						
							|  |  |  | 			origins = args
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		for i := range origins {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			origins[i] = plugin.Host(origins[i]).Normalize()
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		h.Origins = origins
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "fallthrough":
 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 |  |  | 				h.Fall.SetZonesFromArgs(c.RemainingArgs())
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:49:26 +01:00
										 |  |  | 			default:
 | 
					
						
							| 
									
										
										
										
											2018-01-07 14:51:32 -05:00
										 |  |  | 				if len(h.Fall.Zones) == 0 {
 | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 					line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ")
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 					inline = append(inline, line)
 | 
					
						
							| 
									
										
										
										
											2017-09-21 04:18:13 -07:00
										 |  |  | 					continue
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:49:26 +01:00
										 |  |  | 				return h, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	h.initInline(inline)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	return h, nil
 | 
					
						
							|  |  |  | }
 |