| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | package autopath
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-02-10 09:00:27 -05:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("autopath", setup) }
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 21:31:36 +01:00
										 |  |  | 	ap, mw, err := autoPathParse(c)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("autopath", err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Do this in OnStartup, so all plugin has been initialized.
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:21:42 +01:00
										 |  |  | 		m := dnsserver.GetConfig(c).Handler(mw)
 | 
					
						
							| 
									
										
										
										
											2017-08-10 21:31:36 +01:00
										 |  |  | 		if m == nil {
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:40:30 -05:00
										 |  |  | 		if x, ok := m.(AutoPather); ok {
 | 
					
						
							| 
									
										
										
										
											2017-08-18 12:57:23 +01:00
										 |  |  | 			ap.searchFunc = x.AutoPath
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:40:30 -05:00
										 |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return plugin.Error("autopath", fmt.Errorf("%s does not implement the AutoPather interface", mw))
 | 
					
						
							| 
									
										
										
										
											2017-08-10 21:31:36 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		ap.Next = next
 | 
					
						
							|  |  |  | 		return ap
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func autoPathParse(c *caddy.Controller) (*AutoPath, string, error) {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  | 	ap := &AutoPath{}
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	mw := ""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		zoneAndresolv := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(zoneAndresolv) < 1 {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  | 			return ap, "", fmt.Errorf("no resolv-conf specified")
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		resolv := zoneAndresolv[len(zoneAndresolv)-1]
 | 
					
						
							| 
									
										
										
										
											2022-02-10 09:00:27 -05:00
										 |  |  | 		if strings.HasPrefix(resolv, "@") {
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:40:30 -05:00
										 |  |  | 			mw = resolv[1:]
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		} else {
 | 
					
						
							|  |  |  | 			// assume file on disk
 | 
					
						
							|  |  |  | 			rc, err := dns.ClientConfigFromFile(resolv)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:27:54 +01:00
										 |  |  | 				return ap, "", fmt.Errorf("failed to parse %q: %v", resolv, err)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			ap.search = rc.Search
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			plugin.Zones(ap.search).Normalize()
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:55:55 +02:00
										 |  |  | 			ap.search = append(ap.search, "") // sentinel value as demanded.
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:19:54 +02:00
										 |  |  | 		zones := zoneAndresolv[:len(zoneAndresolv)-1]
 | 
					
						
							|  |  |  | 		ap.Zones = plugin.OriginsFromArgsOrServerBlock(zones, c.ServerBlockKeys)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 03:13:38 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return ap, mw, nil
 | 
					
						
							|  |  |  | }
 |