fqdn.go raw

   1  package dns01
   2  
   3  import (
   4  	"iter"
   5  
   6  	"github.com/miekg/dns"
   7  )
   8  
   9  // ToFqdn converts the name into a fqdn appending a trailing dot.
  10  //
  11  // Deprecated: Use [github.com/miekg/dns.Fqdn] directly.
  12  func ToFqdn(name string) string {
  13  	return dns.Fqdn(name)
  14  }
  15  
  16  // UnFqdn converts the fqdn into a name removing the trailing dot.
  17  func UnFqdn(name string) string {
  18  	n := len(name)
  19  	if n != 0 && name[n-1] == '.' {
  20  		return name[:n-1]
  21  	}
  22  
  23  	return name
  24  }
  25  
  26  // UnFqdnDomainsSeq generates a sequence of "unFQDNed" domain names derived from a domain (FQDN or not) in descending order.
  27  func UnFqdnDomainsSeq(fqdn string) iter.Seq[string] {
  28  	return func(yield func(string) bool) {
  29  		if fqdn == "" {
  30  			return
  31  		}
  32  
  33  		for _, index := range dns.Split(fqdn) {
  34  			if !yield(UnFqdn(fqdn[index:])) {
  35  				return
  36  			}
  37  		}
  38  	}
  39  }
  40  
  41  // DomainsSeq generates a sequence of domain names derived from a domain (FQDN or not) in descending order.
  42  func DomainsSeq(fqdn string) iter.Seq[string] {
  43  	return func(yield func(string) bool) {
  44  		if fqdn == "" {
  45  			return
  46  		}
  47  
  48  		for _, index := range dns.Split(fqdn) {
  49  			if !yield(fqdn[index:]) {
  50  				return
  51  			}
  52  		}
  53  	}
  54  }
  55