github.com/philhug/dnscontrol@v0.2.4-0.20180625181521-921fa9849001/models/dns.go (about)

     1  package models
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  )
     7  
     8  // DefaultTTL is applied to any DNS record without an explicit TTL.
     9  const DefaultTTL = uint32(300)
    10  
    11  // DNSConfig describes the desired DNS configuration, usually loaded from dnsconfig.js.
    12  type DNSConfig struct {
    13  	Registrars         []*RegistrarConfig            `json:"registrars"`
    14  	DNSProviders       []*DNSProviderConfig          `json:"dns_providers"`
    15  	Domains            []*DomainConfig               `json:"domains"`
    16  	RegistrarsByName   map[string]*RegistrarConfig   `json:"-"`
    17  	DNSProvidersByName map[string]*DNSProviderConfig `json:"-"`
    18  }
    19  
    20  // FindDomain returns the *DomainConfig for domain query in config.
    21  func (config *DNSConfig) FindDomain(query string) *DomainConfig {
    22  	for _, b := range config.Domains {
    23  		if b.Name == query {
    24  			return b
    25  		}
    26  	}
    27  	return nil
    28  }
    29  
    30  // RegistrarConfig describes a registrar.
    31  type RegistrarConfig struct {
    32  	Name     string          `json:"name"`
    33  	Type     string          `json:"type"`
    34  	Metadata json.RawMessage `json:"meta,omitempty"`
    35  }
    36  
    37  // DNSProviderConfig describes a DNS service provider.
    38  type DNSProviderConfig struct {
    39  	Name     string          `json:"name"`
    40  	Type     string          `json:"type"`
    41  	Metadata json.RawMessage `json:"meta,omitempty"`
    42  }
    43  
    44  // Nameserver describes a nameserver.
    45  type Nameserver struct {
    46  	Name string `json:"name"` // Normalized to a FQDN with NO trailing "."
    47  }
    48  
    49  func (n *Nameserver) String() string {
    50  	return n.Name
    51  }
    52  
    53  // StringsToNameservers constructs a list of *Nameserver structs using a list of FQDNs.
    54  func StringsToNameservers(nss []string) []*Nameserver {
    55  	nservers := []*Nameserver{}
    56  	for _, ns := range nss {
    57  		nservers = append(nservers, &Nameserver{Name: ns})
    58  	}
    59  	return nservers
    60  }
    61  
    62  // Correction is anything that can be run. Implementation is up to the specific provider.
    63  type Correction struct {
    64  	F   func() error `json:"-"`
    65  	Msg string
    66  }
    67  
    68  // DomainContainingFQDN finds the best domain from the dns config for the given record fqdn.
    69  // It will chose the domain whose name is the longest suffix match for the fqdn.
    70  func (config *DNSConfig) DomainContainingFQDN(fqdn string) *DomainConfig {
    71  	fqdn = strings.TrimSuffix(fqdn, ".")
    72  	longestLength := 0
    73  	var d *DomainConfig
    74  	for _, dom := range config.Domains {
    75  		if (dom.Name == fqdn || strings.HasSuffix(fqdn, "."+dom.Name)) && len(dom.Name) > longestLength {
    76  			longestLength = len(dom.Name)
    77  			d = dom
    78  		}
    79  	}
    80  	return d
    81  }