2020-01-25 17:43:02 +00:00
|
|
|
package hosts
|
2013-07-26 04:06:16 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-15 03:42:24 +00:00
|
|
|
"github.com/miekg/dns"
|
2015-02-12 06:09:49 +00:00
|
|
|
"time"
|
2019-09-26 04:43:17 +00:00
|
|
|
)
|
2013-07-26 04:06:16 +00:00
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
var (
|
|
|
|
zeroDuration = time.Duration(0)
|
|
|
|
)
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
type Host struct {
|
|
|
|
Type uint16 `json:"type"`
|
|
|
|
TTL time.Duration `json:"ttl"`
|
|
|
|
Values []string `json:"values"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Host) TypeString() string {
|
|
|
|
return dns.TypeToString[h.Type]
|
|
|
|
}
|
|
|
|
|
2020-01-25 17:43:02 +00:00
|
|
|
type Hosts interface {
|
2021-04-15 03:42:24 +00:00
|
|
|
Get(queryType uint16, domain string) (*Host, error)
|
2020-01-25 17:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProviderList struct {
|
2020-01-25 18:48:26 +00:00
|
|
|
providers []Provider
|
2013-07-26 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
type Provider interface {
|
2021-04-15 03:42:24 +00:00
|
|
|
Get(queryType uint16, domain string) (*Host, error)
|
|
|
|
Set(domain string, host *Host) error
|
2018-07-01 03:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-25 18:48:26 +00:00
|
|
|
func NewHosts(providers []Provider) Hosts {
|
|
|
|
return &ProviderList{providers}
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
// Get Matches values to providers, loping each in order
|
|
|
|
func (h *ProviderList) Get(queryType uint16, domain string) (*Host, error) {
|
|
|
|
var host *Host
|
|
|
|
var err error
|
2015-02-12 06:54:02 +00:00
|
|
|
|
2018-07-01 03:08:29 +00:00
|
|
|
for _, provider := range h.providers {
|
2021-04-15 03:42:24 +00:00
|
|
|
host, err = provider.Get(queryType, domain)
|
2018-07-01 03:08:29 +00:00
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
if host != nil {
|
2018-07-01 03:08:29 +00:00
|
|
|
break
|
2015-02-12 06:54:02 +00:00
|
|
|
}
|
2013-07-26 10:54:19 +00:00
|
|
|
}
|
2015-02-03 12:32:18 +00:00
|
|
|
|
2021-04-15 03:42:24 +00:00
|
|
|
return host, err
|
2020-01-25 17:43:02 +00:00
|
|
|
}
|