| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package secondary
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/middleware/file"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("secondary", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	zones, err := secondaryParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-09-10 09:16:25 +01:00
										 |  |  | 		return middleware.Error("secondary", err)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	// Add startup functions to retrieve the zone and keep it up to date.
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	for _, n := range zones.Names {
 | 
					
						
							|  |  |  | 		if len(zones.Z[n].TransferFrom) > 0 {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2016-04-13 20:14:03 +01:00
										 |  |  | 				zones.Z[n].StartupOnce.Do(func() {
 | 
					
						
							|  |  |  | 					zones.Z[n].TransferIn()
 | 
					
						
							|  |  |  | 					go func() {
 | 
					
						
							|  |  |  | 						zones.Z[n].Update()
 | 
					
						
							|  |  |  | 					}()
 | 
					
						
							|  |  |  | 				})
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 				return nil
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		return Secondary{file.File{Next: next, Zones: zones}}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return nil
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | func secondaryParse(c *caddy.Controller) (file.Zones, error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	z := make(map[string]*file.Zone)
 | 
					
						
							|  |  |  | 	names := []string{}
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	origins := []string{}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		if c.Val() == "secondary" {
 | 
					
						
							|  |  |  | 			// secondary [origin]
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 			origins = make([]string, len(c.ServerBlockKeys))
 | 
					
						
							|  |  |  | 			copy(origins, c.ServerBlockKeys)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 			args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 			if len(args) > 0 {
 | 
					
						
							|  |  |  | 				origins = args
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2016-09-21 17:01:19 +01:00
										 |  |  | 			for i := range origins {
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				origins[i] = middleware.Host(origins[i]).Normalize()
 | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 				z[origins[i]] = file.NewZone(origins[i], "stdin")
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				names = append(names, origins[i])
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for c.NextBlock() {
 | 
					
						
							| 
									
										
										
										
											2016-10-02 19:41:00 +01:00
										 |  |  | 				t, f, e := file.TransferParse(c, true)
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 				if e != nil {
 | 
					
						
							|  |  |  | 					return file.Zones{}, e
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				for _, origin := range origins {
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					if t != nil {
 | 
					
						
							|  |  |  | 						z[origin].TransferTo = append(z[origin].TransferTo, t...)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-14 19:57:39 +01:00
										 |  |  | 					if f != nil {
 | 
					
						
							|  |  |  | 						z[origin].TransferFrom = append(z[origin].TransferFrom, f...)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 					}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 15:52:23 +01:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return file.Zones{Z: z, Names: names}, nil
 | 
					
						
							|  |  |  | }
 |