| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | package hosts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("hosts") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("hosts", setup) } | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | func periodicHostsUpdate(h *Hosts) chan bool { | 
					
						
							|  |  |  | 	parseChan := make(chan bool) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 |  |  | 	if h.options.reload == 0 { | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 		return parseChan | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		ticker := time.NewTicker(h.options.reload) | 
					
						
							| 
									
										
										
										
											2022-10-11 09:26:45 -04:00
										 |  |  | 		defer ticker.Stop() | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 		for { | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case <-parseChan: | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			case <-ticker.C: | 
					
						
							|  |  |  | 				h.readHosts() | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 	return parseChan | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 	parseChan := periodicHostsUpdate(&h) | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	c.OnStartup(func() error { | 
					
						
							|  |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 	config := dnsserver.GetConfig(c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	h := Hosts{ | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 		Hostsfile: &Hostsfile{ | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 			path:    "/etc/hosts", | 
					
						
							| 
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 |  |  | 			hmap:    newMap(), | 
					
						
							| 
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 |  |  | 			inline:  newMap(), | 
					
						
							|  |  |  | 			options: newOptions(), | 
					
						
							| 
									
										
										
										
											2017-10-31 01:40:47 -06:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 		if len(args) >= 1 { | 
					
						
							|  |  |  | 			h.path = args[0] | 
					
						
							|  |  |  | 			args = args[1:] | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 15:59:37 +02:00
										 |  |  | 			if !filepath.IsAbs(h.path) && config.Root != "" { | 
					
						
							|  |  |  | 				h.path = filepath.Join(config.Root, h.path) | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2025-06-05 09:10:58 +02:00
										 |  |  | 				if !os.IsNotExist(err) { | 
					
						
							| 
									
										
										
										
											2017-08-10 05:30:18 -07:00
										 |  |  | 					return h, c.Errf("unable to access hosts file '%s': %v", h.path, err) | 
					
						
							| 
									
										
										
										
											2017-06-08 13:48:04 -06:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2025-06-05 09:10:58 +02:00
										 |  |  | 				log.Warningf("File does not exist: %s", h.path) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 		h.Origins = plugin.OriginsFromArgsOrServerBlock(args, c.ServerBlockKeys) | 
					
						
							| 
									
										
										
										
											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()) | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 			case "no_reverse": | 
					
						
							| 
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 |  |  | 				h.options.autoReverse = false | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 			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") | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 |  |  | 				h.options.ttl = uint32(ttl) | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 			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]) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-25 18:53:07 +00:00
										 |  |  | 				if reload < 0 { | 
					
						
							| 
									
										
										
										
											2019-02-12 16:09:33 +00:00
										 |  |  | 					return h, c.Errf("invalid negative duration for reload '%s'", remaining[0]) | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-07-31 19:37:09 +00:00
										 |  |  | 				h.options.reload = reload | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | } |