Documentation
¶
Overview ¶
Package dns provides DNS-based endpoint discovery for go-iroh: publishing and resolving endpoint addressing information via DNS using the pkarr signed packet format.
It is a port of the Rust crate iroh-dns. The central types are EndpointData (a relay URL, direct addresses, and optional user data about an endpoint) and EndpointInfo (an key.EndpointID coupled with its EndpointData).
Records are published under the name "_iroh.<z32-endpoint-id>.<origin>" as TXT records of the form "key=value" (RFC 1464), with keys "relay", "addr", and "user-data". EndpointInfo converts to and from both TXT record sets and pkarr signed packets.
The Go API is not stable before v1 and may change in any v0 release.
Index ¶
- Constants
- Variables
- type DoHLookuper
- type DoTLookuper
- type EndpointData
- func (d *EndpointData) AddAddrs(addrs ...netaddr.TransportAddr)
- func (d *EndpointData) AddIPAddrs(addrs ...netip.AddrPort)
- func (d *EndpointData) AddRelayURL(u netaddr.RelayURL)
- func (d EndpointData) Addrs() []netaddr.TransportAddr
- func (d *EndpointData) ClearIPAddrs()
- func (d *EndpointData) ClearRelayURLs()
- func (d EndpointData) HasAddrs() bool
- func (d EndpointData) IPAddrs() []netip.AddrPort
- func (d EndpointData) RelayURLs() []netaddr.RelayURL
- func (d *EndpointData) SetUserData(u *UserData)
- func (d EndpointData) String() string
- func (d EndpointData) UserData() *UserData
- func (d EndpointData) WithAddrs(addrs ...netaddr.TransportAddr) EndpointData
- func (d EndpointData) WithIPAddrs(addrs ...netip.AddrPort) EndpointData
- func (d EndpointData) WithRelayURL(u netaddr.RelayURL) EndpointData
- func (d EndpointData) WithUserData(u *UserData) EndpointData
- func (d EndpointData) WithoutIPAddrs() EndpointData
- func (d EndpointData) WithoutRelayURLs() EndpointData
- type EndpointInfo
- type Resolver
- type SignedPacket
- type TXTLookuper
- type TXTLookuperFunc
- type UserData
Constants ¶
const ( // DNSTimeout is the default per-lookup timeout. DNSTimeout = 3 * time.Second // N0DNSEndpointOriginProd is the number0 production discovery origin. N0DNSEndpointOriginProd = "dns.iroh.link." // N0DNSEndpointOriginStaging is the number0 staging discovery origin. N0DNSEndpointOriginStaging = "staging-dns.iroh.link." )
DNS origin and timeout defaults, matching iroh-dns/src/dns.rs.
const IrohTXTName = "_iroh"
IrohTXTName is the DNS record name under which iroh TXT records are published.
const MaxSignedPacketSize = pkarr.MaxBytes
MaxSignedPacketSize is the maximum wire size of a signed packet, including its public key.
const UserDataMaxLength = 245
UserDataMaxLength is the maximum byte length of UserData. A DNS TXT character-string holds at most 255 bytes; subtracting the "user-data=" prefix leaves 245 bytes.
Variables ¶
var ( // ErrInvalidTXTAttr is returned for a TXT value not of the form key=value. ErrInvalidTXTAttr = errors.New("invalid TXT attribute") // ErrUnknownAttr is returned for an unrecognized attribute key. ErrUnknownAttr = errors.New("could not convert key to attr") // ErrNumLabels is returned when a DNS name has too few labels. ErrNumLabels = errors.New("expected at least 2 labels") // ErrNotIrohRecord is returned when the first label is not "_iroh". ErrNotIrohRecord = errors.New("not an iroh record, expected `_iroh`") )
Errors returned when parsing iroh DNS records.
var ErrUserDataTooLong = errors.New("user data: max length exceeded")
ErrUserDataTooLong is returned when a UserData value exceeds UserDataMaxLength.
Functions ¶
This section is empty.
Types ¶
type DoHLookuper ¶
type DoHLookuper struct {
// URL is the DNS-over-HTTPS endpoint.
URL string
// HTTPClient sends requests. If nil, [http.DefaultClient] is used.
HTTPClient *http.Client
}
DoHLookuper resolves TXT records using DNS-over-HTTPS.
It sends RFC 8484 POST requests with Content-Type application/dns-message. The zero value is not usable; set URL before calling LookupTXT.
type DoTLookuper ¶
DoTLookuper resolves TXT records using DNS-over-TLS.
Address is a host:port pair. TLSConfig may be nil to use the default TLS configuration.
type EndpointData ¶
type EndpointData struct {
// contains filtered or unexported fields
}
EndpointData is the addressing and metadata published about an endpoint: a set of transport addresses (in priority order) and optional UserData. It does not include the endpoint's id; see EndpointInfo.
The zero value is an empty EndpointData and is ready to use.
func EndpointDataFromAddr ¶
func EndpointDataFromAddr(addr netaddr.EndpointAddr) EndpointData
EndpointDataFromAddr builds EndpointData from an netaddr.EndpointAddr, taking its (already de-duplicated, sorted) addresses.
func NewEndpointData ¶
func NewEndpointData(addrs ...netaddr.TransportAddr) EndpointData
NewEndpointData returns an EndpointData with the given addresses. Order is preserved; duplicates are removed.
func (*EndpointData) AddAddrs ¶
func (d *EndpointData) AddAddrs(addrs ...netaddr.TransportAddr)
AddAddrs adds addresses in order, skipping duplicates and ones already present. Duplicate filtering preserves the existing order.
func (*EndpointData) AddIPAddrs ¶
func (d *EndpointData) AddIPAddrs(addrs ...netip.AddrPort)
AddIPAddrs adds IP addresses in order, skipping duplicates and existing ones.
func (*EndpointData) AddRelayURL ¶
func (d *EndpointData) AddRelayURL(u netaddr.RelayURL)
AddRelayURL adds a relay URL to the end of the address list, unless already present.
func (EndpointData) Addrs ¶
func (d EndpointData) Addrs() []netaddr.TransportAddr
Addrs returns the ordered transport addresses.
func (*EndpointData) ClearIPAddrs ¶
func (d *EndpointData) ClearIPAddrs()
ClearIPAddrs removes all direct IP addresses.
func (*EndpointData) ClearRelayURLs ¶
func (d *EndpointData) ClearRelayURLs()
ClearRelayURLs removes all relay addresses.
func (EndpointData) HasAddrs ¶
func (d EndpointData) HasAddrs() bool
HasAddrs reports whether any addresses are present.
func (EndpointData) IPAddrs ¶
func (d EndpointData) IPAddrs() []netip.AddrPort
IPAddrs returns the direct IP addresses in order.
func (EndpointData) RelayURLs ¶
func (d EndpointData) RelayURLs() []netaddr.RelayURL
RelayURLs returns the relay URLs in order.
func (*EndpointData) SetUserData ¶
func (d *EndpointData) SetUserData(u *UserData)
SetUserData sets or clears the user data.
func (EndpointData) String ¶
func (d EndpointData) String() string
String returns a diagnostic string for d.
func (EndpointData) UserData ¶
func (d EndpointData) UserData() *UserData
UserData returns the user data, or nil if unset.
func (EndpointData) WithAddrs ¶
func (d EndpointData) WithAddrs(addrs ...netaddr.TransportAddr) EndpointData
WithAddrs returns a copy of d with addrs added in order, skipping duplicates and existing ones.
func (EndpointData) WithIPAddrs ¶
func (d EndpointData) WithIPAddrs(addrs ...netip.AddrPort) EndpointData
WithIPAddrs returns a copy of d with addrs added in order, skipping duplicates and existing ones.
func (EndpointData) WithRelayURL ¶
func (d EndpointData) WithRelayURL(u netaddr.RelayURL) EndpointData
WithRelayURL returns a copy of d with u added to the end of the address list, unless already present.
func (EndpointData) WithUserData ¶
func (d EndpointData) WithUserData(u *UserData) EndpointData
WithUserData returns a copy of d with the user data set or cleared.
func (EndpointData) WithoutIPAddrs ¶
func (d EndpointData) WithoutIPAddrs() EndpointData
WithoutIPAddrs returns a copy of d with all direct IP addresses removed.
func (EndpointData) WithoutRelayURLs ¶
func (d EndpointData) WithoutRelayURLs() EndpointData
WithoutRelayURLs returns a copy of d with all relay addresses removed.
type EndpointInfo ¶
type EndpointInfo struct {
// ID is the endpoint this information is about.
ID key.EndpointID
// Data is the addressing and metadata.
Data EndpointData
}
EndpointInfo couples an key.EndpointID with the EndpointData published about it.
func EndpointInfoFromAddr ¶
func EndpointInfoFromAddr(addr netaddr.EndpointAddr) EndpointInfo
EndpointInfoFromAddr converts an netaddr.EndpointAddr into an EndpointInfo.
func EndpointInfoFromSignedPacket ¶
func EndpointInfoFromSignedPacket(packet *SignedPacket) (EndpointInfo, error)
EndpointInfoFromSignedPacket parses an EndpointInfo from a signed packet.
func EndpointInfoFromTXTLookup ¶
func EndpointInfoFromTXTLookup(domainName string, values []string) (EndpointInfo, error)
EndpointInfoFromTXTLookup parses an EndpointInfo from DNS TXT lookup results. domainName is the queried name ("_iroh.<z32>.<origin>") and values are the TXT record string values.
func (EndpointInfo) Addr ¶
func (e EndpointInfo) Addr() netaddr.EndpointAddr
Addr converts the info into an netaddr.EndpointAddr.
func (EndpointInfo) String ¶
func (e EndpointInfo) String() string
String returns a diagnostic string for e.
func (EndpointInfo) ToSignedPacket ¶
func (e EndpointInfo) ToSignedPacket(secretKey key.SecretKey, ttl uint32) (*SignedPacket, error)
ToSignedPacket builds a signed packet for this endpoint info, signed with secretKey and using the given record TTL in seconds.
func (EndpointInfo) ToTXTStrings ¶
func (e EndpointInfo) ToTXTStrings() []string
ToTXTStrings renders the endpoint info as "key=value" TXT record strings.
type Resolver ¶
type Resolver struct {
// Lookuper performs the underlying TXT lookups. If nil, a [net.Resolver]
// with the default configuration is used.
Lookuper TXTLookuper
}
Resolver resolves iroh endpoint information from DNS. The zero value uses the host's default DNS configuration; set Resolver.Lookuper to override.
func (*Resolver) LookupEndpointByDomainName ¶
func (r *Resolver) LookupEndpointByDomainName(ctx context.Context, name string) (EndpointInfo, error)
LookupEndpointByDomainName resolves the endpoint info from the TXT records at name, which must be of the form "_iroh.<z32-id>.<origin>".
func (*Resolver) LookupEndpointByID ¶
func (r *Resolver) LookupEndpointByID(ctx context.Context, id key.EndpointID, origin string) (EndpointInfo, error)
LookupEndpointByID resolves the endpoint info for id published under "_iroh.<z32-id>.<origin>". Pass N0DNSEndpointOriginProd for the number0 production service.
type SignedPacket ¶
type SignedPacket struct {
// contains filtered or unexported fields
}
SignedPacket is a signed DNS packet in the pkarr format used for iroh endpoint discovery.
A SignedPacket is immutable. Accessors return copies where mutation would otherwise affect the packet.
func SignedPacketFromBytes ¶
func SignedPacketFromBytes(b []byte) (*SignedPacket, error)
SignedPacketFromBytes parses and verifies a signed packet from its wire representation.
func SignedPacketFromBytesUnchecked ¶
func SignedPacketFromBytesUnchecked(b []byte) (*SignedPacket, error)
SignedPacketFromBytesUnchecked parses a signed packet without verifying its signature. It still validates the minimum length and DNS packet encoding.
func (*SignedPacket) Bytes ¶
func (p *SignedPacket) Bytes() []byte
Bytes returns the full serialized wire bytes.
func (*SignedPacket) MoreRecentThan ¶
func (p *SignedPacket) MoreRecentThan(other *SignedPacket) bool
MoreRecentThan reports whether p should replace other. Equal timestamps are ordered by their encoded DNS packets, matching pkarr's conflict rule.
func (*SignedPacket) PublicKey ¶
func (p *SignedPacket) PublicKey() key.PublicKey
PublicKey returns the signer's public key.
func (*SignedPacket) RelayPayload ¶
func (p *SignedPacket) RelayPayload() []byte
RelayPayload returns the relay payload: everything after the public key.
func (*SignedPacket) TXTRecords ¶
func (p *SignedPacket) TXTRecords(name string) []string
TXTRecords returns the TXT string values under name, normalized relative to the signer's z-base-32 public key.
func (*SignedPacket) TimestampMicros ¶
func (p *SignedPacket) TimestampMicros() uint64
TimestampMicros returns the packet timestamp in microseconds since the UNIX epoch.
type TXTLookuper ¶
type TXTLookuper interface {
// LookupTXT returns the TXT record string values for name.
LookupTXT(ctx context.Context, name string) ([]string, error)
}
TXTLookuper looks up the TXT records for a DNS name. It is the minimal resolver seam: any implementation (the stdlib-backed Resolver, a DoH/DoT client, or a test fake) satisfies it.
It is the Go analog of iroh's Resolver trait, narrowed to the TXT lookup that endpoint discovery needs.
type TXTLookuperFunc ¶
TXTLookuperFunc adapts a function to TXTLookuper.
type UserData ¶
type UserData struct {
// contains filtered or unexported fields
}
UserData is application-defined data published and resolved through endpoint discovery. It is a UTF-8 string of at most UserDataMaxLength bytes. iroh neither inspects nor uses it.
func NewUserData ¶
NewUserData returns a UserData wrapping s, or ErrUserDataTooLong if s exceeds UserDataMaxLength bytes.
func (UserData) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*UserData) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.