| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | package erratic
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("erratic", caddy.Plugin{
 | 
					
						
							|  |  |  | 		ServerType: "dns",
 | 
					
						
							| 
									
										
										
										
											2018-02-28 19:56:14 -08:00
										 |  |  | 		Action:     setup,
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 	})
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 19:56:14 -08:00
										 |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 	e, err := parseErratic(c)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("erratic", err)
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 		return e
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func parseErratic(c *caddy.Controller) (*Erratic, error) {
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 	e := &Erratic{drop: 2}
 | 
					
						
							| 
									
										
										
										
											2017-04-16 07:49:13 +01:00
										 |  |  | 	drop := false // true if we've seen the drop keyword
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 	for c.Next() { // 'erratic'
 | 
					
						
							|  |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							|  |  |  | 			case "drop":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) > 1 {
 | 
					
						
							|  |  |  | 					return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if len(args) == 0 {
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 					continue
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				amount, err := strconv.ParseInt(args[0], 10, 32)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return nil, err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if amount < 0 {
 | 
					
						
							|  |  |  | 					return nil, fmt.Errorf("illegal amount value given %q", args[0])
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				e.drop = uint64(amount)
 | 
					
						
							| 
									
										
										
										
											2017-04-16 07:49:13 +01:00
										 |  |  | 				drop = true
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 			case "delay":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) > 2 {
 | 
					
						
							|  |  |  | 					return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// Defaults.
 | 
					
						
							|  |  |  | 				e.delay = 2
 | 
					
						
							| 
									
										
										
										
											2017-08-06 05:54:24 -07:00
										 |  |  | 				e.duration = 100 * time.Millisecond
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 				if len(args) == 0 {
 | 
					
						
							|  |  |  | 					continue
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 				amount, err := strconv.ParseInt(args[0], 10, 32)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return nil, err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if amount < 0 {
 | 
					
						
							|  |  |  | 					return nil, fmt.Errorf("illegal amount value given %q", args[0])
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-04-13 16:26:17 +01:00
										 |  |  | 				e.delay = uint64(amount)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if len(args) > 1 {
 | 
					
						
							|  |  |  | 					duration, err := time.ParseDuration(args[1])
 | 
					
						
							|  |  |  | 					if err != nil {
 | 
					
						
							|  |  |  | 						return nil, err
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					e.duration = duration
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2017-04-16 07:49:13 +01:00
										 |  |  | 			case "truncate":
 | 
					
						
							|  |  |  | 				args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 				if len(args) > 1 {
 | 
					
						
							|  |  |  | 					return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if len(args) == 0 {
 | 
					
						
							|  |  |  | 					continue
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				amount, err := strconv.ParseInt(args[0], 10, 32)
 | 
					
						
							|  |  |  | 				if err != nil {
 | 
					
						
							|  |  |  | 					return nil, err
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				if amount < 0 {
 | 
					
						
							|  |  |  | 					return nil, fmt.Errorf("illegal amount value given %q", args[0])
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				e.truncate = uint64(amount)
 | 
					
						
							| 
									
										
										
										
											2018-10-23 17:55:40 +01:00
										 |  |  | 			case "large":
 | 
					
						
							|  |  |  | 				e.large = true
 | 
					
						
							| 
									
										
										
										
											2017-08-14 08:49:26 +01:00
										 |  |  | 			default:
 | 
					
						
							|  |  |  | 				return nil, c.Errf("unknown property '%s'", c.Val())
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-04-16 07:49:13 +01:00
										 |  |  | 	if (e.delay > 0 || e.truncate > 0) && !drop { // delay is set, but we've haven't seen a drop keyword, remove default drop stuff
 | 
					
						
							|  |  |  | 		e.drop = 0
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-06 09:42:30 +00:00
										 |  |  | 	return e, nil
 | 
					
						
							|  |  |  | }
 |