| 
									
										
										
										
											2016-04-11 07:56:38 +01:00
										 |  |  | package test
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 17:09:05 +01:00
										 |  |  | // ResponseWriter is useful for writing tests. It uses some fixed values for the client. The
 | 
					
						
							|  |  |  | // remote will always be 10.240.0.1 and port 40212. The local address is always 127.0.0.1 and
 | 
					
						
							|  |  |  | // port 53.
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | type ResponseWriter struct {
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	TCP      bool // if TCP is true we return an TCP connection instead of an UDP one.
 | 
					
						
							|  |  |  | 	RemoteIP string
 | 
					
						
							| 
									
										
										
										
											2022-05-20 10:22:30 +05:30
										 |  |  | 	Zone     string
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:30:57 +01:00
										 |  |  | // LocalAddr returns the local address, 127.0.0.1:53 (UDP, TCP if t.TCP is true).
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (t *ResponseWriter) LocalAddr() net.Addr {
 | 
					
						
							|  |  |  | 	ip := net.ParseIP("127.0.0.1")
 | 
					
						
							|  |  |  | 	port := 53
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	if t.TCP {
 | 
					
						
							|  |  |  | 		return &net.TCPAddr{IP: ip, Port: port, Zone: ""}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	return &net.UDPAddr{IP: ip, Port: port, Zone: ""}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | // RemoteAddr returns the remote address, defaults to 10.240.0.1:40212 (UDP, TCP is t.TCP is true).
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (t *ResponseWriter) RemoteAddr() net.Addr {
 | 
					
						
							| 
									
										
										
										
											2019-06-09 00:10:57 -07:00
										 |  |  | 	remoteIP := "10.240.0.1"
 | 
					
						
							|  |  |  | 	if t.RemoteIP != "" {
 | 
					
						
							|  |  |  | 		remoteIP = t.RemoteIP
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	ip := net.ParseIP(remoteIP)
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | 	port := 40212
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	if t.TCP {
 | 
					
						
							| 
									
										
										
										
											2022-05-20 10:22:30 +05:30
										 |  |  | 		return &net.TCPAddr{IP: ip, Port: port, Zone: t.Zone}
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-05-20 10:22:30 +05:30
										 |  |  | 	return &net.UDPAddr{IP: ip, Port: port, Zone: t.Zone}
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // WriteMsg implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | func (t *ResponseWriter) WriteMsg(m *dns.Msg) error { return nil }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // Write implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2016-04-04 08:19:06 +01:00
										 |  |  | func (t *ResponseWriter) Write(buf []byte) (int, error) { return len(buf), nil }
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // Close implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | func (t *ResponseWriter) Close() error { return nil }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // TsigStatus implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | func (t *ResponseWriter) TsigStatus() error { return nil }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // TsigTimersOnly implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2019-09-23 21:40:14 +08:00
										 |  |  | func (t *ResponseWriter) TsigTimersOnly(bool) {}
 | 
					
						
							| 
									
										
										
										
											2016-09-23 09:14:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // Hijack implements dns.ResponseWriter interface.
 | 
					
						
							| 
									
										
										
										
											2019-09-23 21:40:14 +08:00
										 |  |  | func (t *ResponseWriter) Hijack() {}
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-15 15:34:02 -07:00
										 |  |  | // ResponseWriter6 returns fixed client and remote address in IPv6.  The remote
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:30:57 +01:00
										 |  |  | // address is always fe80::42:ff:feca:4c65 and port 40212. The local address is always ::1 and port 53.
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | type ResponseWriter6 struct {
 | 
					
						
							|  |  |  | 	ResponseWriter
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:30:57 +01:00
										 |  |  | // LocalAddr returns the local address, always ::1, port 53 (UDP, TCP is t.TCP is true).
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | func (t *ResponseWriter6) LocalAddr() net.Addr {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	if t.TCP {
 | 
					
						
							|  |  |  | 		return &net.TCPAddr{IP: net.ParseIP("::1"), Port: 53, Zone: ""}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 	return &net.UDPAddr{IP: net.ParseIP("::1"), Port: 53, Zone: ""}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:30:57 +01:00
										 |  |  | // RemoteAddr returns the remote address, always fe80::42:ff:feca:4c65 port 40212 (UDP, TCP is t.TCP is true).
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | func (t *ResponseWriter6) RemoteAddr() net.Addr {
 | 
					
						
							| 
									
										
										
										
											2018-07-07 10:14:21 +03:00
										 |  |  | 	if t.TCP {
 | 
					
						
							|  |  |  | 		return &net.TCPAddr{IP: net.ParseIP("fe80::42:ff:feca:4c65"), Port: 40212, Zone: ""}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-09-08 13:36:09 -07:00
										 |  |  | 	return &net.UDPAddr{IP: net.ParseIP("fe80::42:ff:feca:4c65"), Port: 40212, Zone: ""}
 | 
					
						
							|  |  |  | }
 |