| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | package loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy" | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil" | 
					
						
							| 
									
										
										
										
											2022-03-16 09:24:58 -07:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/rand" | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("loop", setup) } | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							|  |  |  | 	l, err := parse(c) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return plugin.Error("loop", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { | 
					
						
							|  |  |  | 		l.Next = next | 
					
						
							|  |  |  | 		return l | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Send query to ourselves and see if it end up with us again. | 
					
						
							|  |  |  | 	c.OnStartup(func() error { | 
					
						
							|  |  |  | 		// Another Go function, otherwise we block startup and can't send the packet. | 
					
						
							|  |  |  | 		go func() { | 
					
						
							|  |  |  | 			deadline := time.Now().Add(30 * time.Second) | 
					
						
							|  |  |  | 			conf := dnsserver.GetConfig(c) | 
					
						
							| 
									
										
										
										
											2018-07-22 11:54:02 +01:00
										 |  |  | 			lh := conf.ListenHosts[0] | 
					
						
							|  |  |  | 			addr := net.JoinHostPort(lh, conf.Port) | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			for time.Now().Before(deadline) { | 
					
						
							| 
									
										
										
										
											2018-12-16 21:48:09 +00:00
										 |  |  | 				l.setAddress(addr) | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 				if _, err := l.exchange(addr); err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-31 18:20:43 +01:00
										 |  |  | 					l.reset() | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 					time.Sleep(1 * time.Second) | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				go func() { | 
					
						
							|  |  |  | 					time.Sleep(2 * time.Second) | 
					
						
							|  |  |  | 					l.setDisabled() | 
					
						
							|  |  |  | 				}() | 
					
						
							| 
									
										
										
										
											2018-07-22 11:54:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				break | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			l.setDisabled() | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func parse(c *caddy.Controller) (*Loop, error) { | 
					
						
							|  |  |  | 	i := 0 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	zones := []string{"."} | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 	for c.Next() { | 
					
						
							|  |  |  | 		if i > 0 { | 
					
						
							|  |  |  | 			return nil, plugin.ErrOnce | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		i++ | 
					
						
							|  |  |  | 		if c.NextArg() { | 
					
						
							|  |  |  | 			return nil, c.ArgErr() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(c.ServerBlockKeys) > 0 { | 
					
						
							| 
									
										
										
										
											2021-05-27 07:26:14 -04:00
										 |  |  | 			zones = plugin.Host(c.ServerBlockKeys[0]).NormalizeExact() | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 	return New(zones[0]), nil | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // qname returns a random name. <rand.Int()>.<rand.Int().<zone>. | 
					
						
							|  |  |  | func qname(zone string) string { | 
					
						
							|  |  |  | 	l1 := strconv.Itoa(r.Int()) | 
					
						
							|  |  |  | 	l2 := strconv.Itoa(r.Int()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-22 15:12:02 +01:00
										 |  |  | 	return dnsutil.Join(l1, l2, zone) | 
					
						
							| 
									
										
										
										
											2018-07-20 19:45:17 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 09:24:58 -07:00
										 |  |  | var r = rand.New(time.Now().UnixNano()) |