rio/dns.go

65 lines
1.1 KiB
Go
Raw Normal View History

2023-12-31 12:26:56 +00:00
package main
import (
2023-12-31 21:41:51 +00:00
"errors"
2023-12-31 12:26:56 +00:00
"net"
2023-12-31 21:41:51 +00:00
"strings"
2023-12-31 12:26:56 +00:00
"time"
"github.com/patrickmn/go-cache"
)
2023-12-31 21:41:51 +00:00
const (
TxtRepoKey = "repo="
)
2023-12-31 12:26:56 +00:00
var (
cnameCache = cache.New(1 * time.Hour, 1 * time.Hour)
2023-12-31 21:41:51 +00:00
txtRepoCache = cache.New(1 * time.Hour, 1 * time.Hour)
2023-12-31 12:26:56 +00:00
)
2023-12-31 21:41:51 +00:00
func lookupRepoTXT(domain string) (string, error) {
repoLookup, found := txtRepoCache.Get(domain)
if found {
return repoLookup.(string), nil
}
txts, err := net.LookupTXT("_rio-pages." + domain)
if err != nil {
return "", err
}
repo := ""
for _, txt := range txts {
if !strings.HasPrefix(txt, TxtRepoKey) {
continue
}
repo = strings.TrimPrefix(txt, TxtRepoKey)
break
}
txtRepoCache.Set(domain, repo, cache.DefaultExpiration)
return repo, nil
}
2023-12-31 12:26:56 +00:00
func lookupCNAME(domain string) (string, error) {
cname, found := cnameCache.Get(domain)
if found {
2023-12-31 21:41:51 +00:00
if cname == "" {
return "", errors.New("Previous request failure")
}
2023-12-31 12:26:56 +00:00
return cname.(string), nil
}
cname, err := net.LookupCNAME(domain)
if err == nil {
2023-12-31 21:41:51 +00:00
cnameCache.Set(domain, cname, cache.DefaultExpiration)
2023-12-31 12:26:56 +00:00
return cname.(string), nil
}
2023-12-31 21:41:51 +00:00
cnameCache.Set(domain, "", cache.DefaultExpiration)
2023-12-31 12:26:56 +00:00
return "", err
}