| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | package dnsserver
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 09:50:18 +02:00
										 |  |  | import (
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:20:45 +05:30
										 |  |  | 	"regexp"
 | 
					
						
							| 
									
										
										
										
											2021-05-11 09:50:18 +02:00
										 |  |  | 	"sort"
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:20:45 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil"
 | 
					
						
							| 
									
										
										
										
											2021-05-11 09:50:18 +02:00
										 |  |  | )
 | 
					
						
							| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:20:45 +05:30
										 |  |  | // checkZoneSyntax() checks whether the given string match 1035 Preferred Syntax or not.
 | 
					
						
							|  |  |  | // The root zone, and all reverse zones always return true even though they technically don't meet 1035 Preferred Syntax
 | 
					
						
							|  |  |  | func checkZoneSyntax(zone string) bool {
 | 
					
						
							|  |  |  | 	if zone == "." || dnsutil.IsReverse(zone) != 0 {
 | 
					
						
							|  |  |  | 		return true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	regex1035PreferredSyntax, _ := regexp.MatchString(`^(([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])?)\.)+$`, zone)
 | 
					
						
							|  |  |  | 	return regex1035PreferredSyntax
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | // startUpZones creates the text that we show when starting up:
 | 
					
						
							| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | // grpc://example.com.:1055
 | 
					
						
							|  |  |  | // example.com.:1053 on 127.0.0.1
 | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | func startUpZones(protocol, addr string, zones map[string][]*Config) string {
 | 
					
						
							| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | 	s := ""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 09:50:18 +02:00
										 |  |  | 	keys := make([]string, len(zones))
 | 
					
						
							|  |  |  | 	i := 0
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:20:45 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 09:50:18 +02:00
										 |  |  | 	for k := range zones {
 | 
					
						
							|  |  |  | 		keys[i] = k
 | 
					
						
							|  |  |  | 		i++
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	sort.Strings(keys)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, zone := range keys {
 | 
					
						
							| 
									
										
										
										
											2022-07-06 23:20:45 +05:30
										 |  |  | 		if !checkZoneSyntax(zone) {
 | 
					
						
							|  |  |  | 			s += fmt.Sprintf("Warning: Domain %q does not follow RFC1035 preferred syntax\n", zone)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | 		// split addr into protocol, IP and Port
 | 
					
						
							|  |  |  | 		_, ip, port, err := SplitProtocolHostPort(addr)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			// this should not happen, but we need to take care of it anyway
 | 
					
						
							|  |  |  | 			s += fmt.Sprintln(protocol + zone + ":" + addr)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if ip == "" {
 | 
					
						
							|  |  |  | 			s += fmt.Sprintln(protocol + zone + ":" + port)
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		// if the server is listening on a specific address let's make it visible in the log,
 | 
					
						
							|  |  |  | 		// so one can differentiate between all active listeners
 | 
					
						
							|  |  |  | 		s += fmt.Sprintln(protocol + zone + ":" + port + " on " + ip)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return s
 | 
					
						
							|  |  |  | }
 |