| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | package middleware | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | // Host represents a host from the Corefile, may contain port. | 
					
						
							|  |  |  | type ( | 
					
						
							|  |  |  | 	Host string | 
					
						
							|  |  |  | 	Addr string | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | // Normalize will return the host portion of host, stripping | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | // of any port. The host will also be fully qualified and lowercased. | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | func (h Host) Normalize() string { | 
					
						
							| 
									
										
										
										
											2016-03-18 20:57:35 +00:00
										 |  |  | 	// separate host and port | 
					
						
							|  |  |  | 	host, _, err := net.SplitHostPort(string(h)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		host, _, _ = net.SplitHostPort(string(h) + ":") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return strings.ToLower(dns.Fqdn(host)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | // Normalize will return a normalized address, if not port is specified | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | // port 53 is added, otherwise the port will be left as is. | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | func (a Addr) Normalize() string { | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	// separate host and port | 
					
						
							|  |  |  | 	addr, port, err := net.SplitHostPort(string(a)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		addr, port, _ = net.SplitHostPort(string(a) + ":53") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return net.JoinHostPort(addr, port) | 
					
						
							|  |  |  | } |