| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | package header
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 10:10:16 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { plugin.Register("header", setup) }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 	queryRules, responseRules, err := parse(c)
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return plugin.Error("header", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							|  |  |  | 		return Header{
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 			QueryRules:    queryRules,
 | 
					
						
							|  |  |  | 			ResponseRules: responseRules,
 | 
					
						
							|  |  |  | 			Next:          next,
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | func parse(c *caddy.Controller) ([]Rule, []Rule, error) {
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 	for c.Next() {
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 		var queryRules []Rule
 | 
					
						
							|  |  |  | 		var responseRules []Rule
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 		for c.NextBlock() {
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 			selector := strings.ToLower(c.Val())
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var action string
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 			switch selector {
 | 
					
						
							|  |  |  | 			case "query", "response":
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				if c.NextArg() {
 | 
					
						
							|  |  |  | 					action = c.Val()
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2025-04-04 20:27:39 +02:00
										 |  |  | 			default:
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				return nil, nil, fmt.Errorf("setting up rule: invalid selector=%s should be query or response", selector)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			args := c.RemainingArgs()
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 			rules, err := newRules(action, args)
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 				return nil, nil, fmt.Errorf("setting up rule: %w", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if selector == "response" {
 | 
					
						
							|  |  |  | 				responseRules = append(responseRules, rules...)
 | 
					
						
							|  |  |  | 			} else {
 | 
					
						
							|  |  |  | 				queryRules = append(queryRules, rules...)
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 		if len(queryRules) > 0 || len(responseRules) > 0 {
 | 
					
						
							|  |  |  | 			return queryRules, responseRules, nil
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-08-12 13:46:06 +02:00
										 |  |  | 	return nil, nil, c.ArgErr()
 | 
					
						
							| 
									
										
										
										
											2021-07-15 09:32:39 +02:00
										 |  |  | }
 |