godns/hosts/hosts.go

49 lines
872 B
Go

package hosts
import (
"time"
)
var (
zeroDuration = time.Duration(0)
)
type Hosts interface {
Get(queryType uint16, domain string) ([]string, time.Duration, bool)
}
type ProviderList struct {
providers []Provider
}
type Provider interface {
Get(queryType uint16, domain string) ([]string, time.Duration, bool)
Set(t, domain, value string) (bool, error)
}
func NewHosts(providers []Provider) Hosts {
return &ProviderList{providers}
}
/*
Match local /etc/hosts file first, remote redis records second
*/
func (h *ProviderList) Get(queryType uint16, domain string) ([]string, time.Duration, bool) {
var vals []string
var ok bool
var ttl time.Duration
for _, provider := range h.providers {
vals, ttl, ok = provider.Get(queryType, domain)
if ok {
break
}
}
if vals == nil {
return nil, zeroDuration, false
}
return vals, ttl, true
}