package dns
import (
"context"
"fmt"
"net"
"strings"
"time"
"github.com/tmc/go-iroh/key"
)
const (
DNSTimeout = 3 * time .Second
N0DNSEndpointOriginProd = "dns.iroh.link."
N0DNSEndpointOriginStaging = "staging-dns.iroh.link."
)
type TXTLookuper interface {
LookupTXT (ctx context .Context , name string ) ([]string , error )
}
type TXTLookuperFunc func (ctx context .Context , name string ) ([]string , error )
func (f TXTLookuperFunc ) LookupTXT (ctx context .Context , name string ) ([]string , error ) {
return f (ctx , name )
}
type Resolver struct {
Lookuper TXTLookuper
}
func (r *Resolver ) lookuper () TXTLookuper {
if r .Lookuper != nil {
return r .Lookuper
}
return netLookuper {}
}
func (r *Resolver ) LookupEndpointByID (ctx context .Context , id key .EndpointID , origin string ) (EndpointInfo , error ) {
name := IrohTXTName + "." + id .Z32 () + "." + ensureTrailingDot (origin )
return r .LookupEndpointByDomainName (ctx , name )
}
func (r *Resolver ) LookupEndpointByDomainName (ctx context .Context , name string ) (EndpointInfo , error ) {
ctx , cancel := context .WithTimeout (ctx , DNSTimeout )
defer cancel ()
values , err := r .lookuper ().LookupTXT (ctx , name )
if err != nil {
return EndpointInfo {}, fmt .Errorf ("lookup %q: %w" , name , err )
}
return EndpointInfoFromTXTLookup (name , values )
}
type netLookuper struct {}
func (netLookuper ) LookupTXT (ctx context .Context , name string ) ([]string , error ) {
return net .DefaultResolver .LookupTXT (ctx , strings .TrimSuffix (name , "." ))
}
func ensureTrailingDot(s string ) string {
if strings .HasSuffix (s , "." ) {
return s
}
return s + "."
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .