| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | // Package upstream abstracts a upstream lookups so that plugins can handle them in an unified way.
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | package upstream
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/nonwriter"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2020-01-30 09:19:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | // Upstream is used to resolve CNAME or other external targets via CoreDNS itself.
 | 
					
						
							|  |  |  | type Upstream struct{}
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-21 09:20:48 +07:00
										 |  |  | // New creates a new Upstream to resolve names using the coredns process.
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | func New() *Upstream { return &Upstream{} }
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 05:23:05 -07:00
										 |  |  | // Lookup routes lookups to our selves to make it follow the plugin chain *again*, but with a (possibly) new query. As
 | 
					
						
							|  |  |  | // we are doing the query against ourselves again, there is no actual new hop, as such RFC 6891 does not apply and we
 | 
					
						
							|  |  |  | // need the EDNS0 option present in the *original* query to be present here too.
 | 
					
						
							| 
									
										
										
										
											2019-03-26 14:37:30 +00:00
										 |  |  | func (u *Upstream) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
 | 
					
						
							|  |  |  | 	server, ok := ctx.Value(dnsserver.Key{}).(*dnsserver.Server)
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("no full server is running")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2021-08-26 09:39:44 -07:00
										 |  |  | 	req := state.NewWithQuestion(name, typ)
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	nw := nonwriter.New(state.W)
 | 
					
						
							| 
									
										
										
										
											2021-08-26 09:39:44 -07:00
										 |  |  | 	server.ServeDNS(ctx, nw, req.Req)
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	return nw.Msg, nil
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | }
 |