| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | package loadbalance
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | type RoundRobinResponseWriter struct {
 | 
					
						
							|  |  |  | 	dns.ResponseWriter
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewRoundRobinResponseWriter(w dns.ResponseWriter) *RoundRobinResponseWriter {
 | 
					
						
							|  |  |  | 	return &RoundRobinResponseWriter{w}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r *RoundRobinResponseWriter) WriteMsg(res *dns.Msg) error {
 | 
					
						
							|  |  |  | 	if res.Rcode != dns.RcodeSuccess {
 | 
					
						
							|  |  |  | 		return r.ResponseWriter.WriteMsg(res)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 	res.Answer = roundRobin(res.Answer)
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 	res.Ns = roundRobin(res.Ns)
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 	res.Extra = roundRobin(res.Extra)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return r.ResponseWriter.WriteMsg(res)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func roundRobin(in []dns.RR) []dns.RR {
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 	cname := []dns.RR{}
 | 
					
						
							|  |  |  | 	address := []dns.RR{}
 | 
					
						
							|  |  |  | 	rest := []dns.RR{}
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 	for _, r := range in {
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 		switch r.Header().Rrtype {
 | 
					
						
							|  |  |  | 		case dns.TypeCNAME:
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 			// d d d d DNAME and friends here as well?
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 			cname = append(cname, r)
 | 
					
						
							|  |  |  | 		case dns.TypeA, dns.TypeAAAA:
 | 
					
						
							|  |  |  | 			address = append(address, r)
 | 
					
						
							|  |  |  | 		default:
 | 
					
						
							|  |  |  | 			rest = append(rest, r)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch l := len(address); l {
 | 
					
						
							|  |  |  | 	case 0, 1:
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 		break
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 	case 2:
 | 
					
						
							|  |  |  | 		if dns.Id()%2 == 0 {
 | 
					
						
							|  |  |  | 			address[0], address[1] = address[1], address[0]
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	default:
 | 
					
						
							|  |  |  | 		for j := 0; j < l*(int(dns.Id())%4+1); j++ {
 | 
					
						
							|  |  |  | 			q := int(dns.Id()) % l
 | 
					
						
							|  |  |  | 			p := int(dns.Id()) % l
 | 
					
						
							|  |  |  | 			if q == p {
 | 
					
						
							|  |  |  | 				p = (p + 1) % l
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			address[q], address[p] = address[p], address[q]
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-26 09:53:40 +00:00
										 |  |  | 	out := append(cname, rest...)
 | 
					
						
							|  |  |  | 	out = append(out, address...)
 | 
					
						
							|  |  |  | 	return out
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 17:51:28 +00:00
										 |  |  | // Should we pack and unpack here to fiddle with the packet... Not likely.
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | func (r *RoundRobinResponseWriter) Write(buf []byte) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 	log.Printf("[WARNING] RoundRobin called with Write: no shuffling records")
 | 
					
						
							| 
									
										
										
										
											2016-03-23 15:57:48 +00:00
										 |  |  | 	n, err := r.ResponseWriter.Write(buf)
 | 
					
						
							|  |  |  | 	return n, err
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (r *RoundRobinResponseWriter) Hijack() {
 | 
					
						
							|  |  |  | 	r.ResponseWriter.Hijack()
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 |