| 
									
										
										
										
											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-01-13 16:54:49 +00:00
										 |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 15:11:26 -05:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/nonwriter"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | // Lookup routes lookups to our selves or forward to a remote.
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | func (u *Upstream) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) {
 | 
					
						
							|  |  |  | 	server, ok := state.Context.Value(dnsserver.Key{}).(*dnsserver.Server)
 | 
					
						
							|  |  |  | 	if !ok {
 | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("no full server is running")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	req := new(dns.Msg)
 | 
					
						
							|  |  |  | 	req.SetQuestion(name, typ)
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	nw := nonwriter.New(state.W)
 | 
					
						
							| 
									
										
										
										
											2018-06-12 14:54:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 16:54:49 +00:00
										 |  |  | 	server.ServeDNS(state.Context, nw, 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
										 |  |  | }
 |