| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // Package proxy is middleware that proxies requests.
 | 
					
						
							|  |  |  | package proxy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:26:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ReverseProxy struct {
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	Host    string
 | 
					
						
							|  |  |  | 	Client  Client
 | 
					
						
							|  |  |  | 	Options Options
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (p ReverseProxy) ServeDNS(w dns.ResponseWriter, r *dns.Msg, extra []dns.RR) error {
 | 
					
						
							|  |  |  | 	var (
 | 
					
						
							|  |  |  | 		reply *dns.Msg
 | 
					
						
							|  |  |  | 		err   error
 | 
					
						
							|  |  |  | 	)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	switch {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	case request.Proto(w) == "tcp": // TODO(miek): keep this in request
 | 
					
						
							|  |  |  | 		reply, _, err = p.Client.TCP.Exchange(r, p.Host)
 | 
					
						
							| 
									
										
										
										
											2016-04-30 15:54:41 +01:00
										 |  |  | 	default:
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 		reply, _, err = p.Client.UDP.Exchange(r, p.Host)
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 12:22:52 +03:00
										 |  |  | 	if reply != nil && reply.Truncated {
 | 
					
						
							|  |  |  | 		// Suppress proxy error for truncated responses
 | 
					
						
							|  |  |  | 		err = nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-06-08 12:22:52 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	reply.Compress = true
 | 
					
						
							|  |  |  | 	reply.Id = r.Id
 | 
					
						
							|  |  |  | 	w.WriteMsg(reply)
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |