| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package chaos
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("chaos", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							|  |  |  | 		Action:     setup,
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	version, authors, err := chaosParse(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("chaos", err)
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		return Chaos{Next: next, Version: version, Authors: authors}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func chaosParse(c *caddy.Controller) (string, map[string]bool, error) {
 | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:35 +01:00
										 |  |  | 	// Set here so we pick up AppName and AppVersion that get set in coremain's init().
 | 
					
						
							|  |  |  | 	chaosVersion = caddy.AppName + "-" + caddy.AppVersion
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	version := ""
 | 
					
						
							|  |  |  | 	authors := make(map[string]bool)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for c.Next() {
 | 
					
						
							|  |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 		if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:35 +01:00
										 |  |  | 			return chaosVersion, nil, nil
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		if len(args) == 1 {
 | 
					
						
							|  |  |  | 			return args[0], nil, nil
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		version = args[0]
 | 
					
						
							|  |  |  | 		for _, a := range args[1:] {
 | 
					
						
							|  |  |  | 			authors[a] = true
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return version, authors, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return version, authors, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:35 +01:00
										 |  |  | var chaosVersion string
 |