util

package
v0.26.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IPv4PtrSuffix = ".in-addr.arpa."
	IPv6PtrSuffix = ".ip6.arpa."
)

Variables

View Source
var (
	// Version current version number
	Version = "undefined"
	// BuildTime build time of the binary
	BuildTime = "undefined"
	// Architecture current CPU architecture
	Architecture = "undefined"
)
View Source
var ErrInvalidArpaAddrLen = errors.New("arpa hostname is not of expected length")
View Source
var (
	// To avoid making this package depend on config, we use a global
	// that is set at config load.
	// Ideally we'd move the obfuscate code somewhere else (maybe into `log`),
	// but that would require also moving all its dependencies.
	// This is good enough for now.
	LogPrivacy atomic.Bool
)

Functions

func AnswerToString

func AnswerToString(answer []dns.RR) string

AnswerToString creates a user-friendly representation of an answer

func CidrContainsIP

func CidrContainsIP(cidr string, ip net.IP) bool

CidrContainsIP checks if CIDR contains a single IP

func ClientNameMatchesGroupName

func ClientNameMatchesGroupName(group, clientName string) bool

ClientNameMatchesGroupName checks if a group with optional wildcards contains a client name

func CreateAnswerFromQuestion

func CreateAnswerFromQuestion(question dns.Question, ip net.IP, remainingTTL uint32) (dns.RR, error)

CreateAnswerFromQuestion creates new answer from a question

func CreateHeader

func CreateHeader(question dns.Question, remainingTTL uint32) dns.RR_Header

CreateHeader creates DNS header for passed question

func CtxSend

func CtxSend[T any](ctx context.Context, ch chan T, val T) (ok bool)

CtxSend sends a value to a channel while the context isn't done. If the message is sent, it returns true. If the context is done or the channel is closed, it returns false.

func DefaultHTTPTransport

func DefaultHTTPTransport() *http.Transport

DefaultHTTPTransport returns a new Transport with the same defaults as net/http.

func ExtractCacheKey

func ExtractCacheKey(key string) (qType dns.Type, qName string)

ExtractCacheKey return query type/domain from cacheKey

func ExtractDomain

func ExtractDomain(question dns.Question) string

ExtractDomain returns domain string from the question

func ExtractDomainOnly

func ExtractDomainOnly(in string) string

ExtractDomainOnly extracts domain from the DNS query

func FatalOnError

func FatalOnError(message string, err error)

FatalOnError logs the message only if error is not nil and exits the program execution

func GenerateCacheKey

func GenerateCacheKey(qType dns.Type, qName string) string

GenerateCacheKey return cacheKey by query type/domain

func GetEdns0Option

func GetEdns0Option[T EDNS0Option](msg *dns.Msg) T

GetEdns0Option returns the option with the given code from the OPT record in the Extra section of the given message. If the option is not found, nil will be returned.

func HTTPClientIP

func HTTPClientIP(r *http.Request) net.IP

func IterateValueSorted

func IterateValueSorted(in map[string]int, fn func(string, int))

IterateValueSorted iterates over maps value in a sorted order and applies the passed function

func LogOnError

func LogOnError(ctx context.Context, message string, err error)

LogOnError logs the message only if error is not nil

func LogOnErrorWithEntry

func LogOnErrorWithEntry(logEntry *logrus.Entry, message string, err error)

LogOnErrorWithEntry logs the message only if error is not nil

func NewMsgWithAnswer

func NewMsgWithAnswer(domain string, ttl uint, dnsType dns.Type, address string) (*dns.Msg, error)

NewMsgWithAnswer creates new DNS message with answer

func NewMsgWithQuestion

func NewMsgWithQuestion(question string, qType dns.Type) *dns.Msg

NewMsgWithQuestion creates new DNS message with question

func Obfuscate

func Obfuscate(in string) string

Obfuscate replaces all alphanumeric characters with * to obfuscate user sensitive data if LogPrivacy is enabled

func ParseIPFromArpaAddr

func ParseIPFromArpaAddr(arpa string) (net.IP, error)

func QuestionToString

func QuestionToString(questions []dns.Question) string

QuestionToString creates a user-friendly representation of a question

func RemoveEdns0Option

func RemoveEdns0Option[T EDNS0Option](msg *dns.Msg) bool

RemoveEdns0Option removes the option according to the given type from the OPT record in the Extra section of the given message. If there are no more options in the OPT record, the OPT record will be removed. If the option is successfully removed, true will be returned.

func RemoveEdns0Record

func RemoveEdns0Record(msg *dns.Msg) bool

RemoveEdns0Record removes the OPT record from the Extra section of the given message. If the OPT record is removed, true will be returned.

func SetEdns0Option

func SetEdns0Option(msg *dns.Msg, opt dns.EDNS0) bool

SetEdns0Option adds the given option to the OPT record in the Extra section of the given message. If the option already exists, it will be replaced. If the option is successfully set, true will be returned.

func TLSGenerateSelfSignedCert

func TLSGenerateSelfSignedCert(domains []string) (tls.Certificate, error)

TLSGenerateSelfSignedCert returns a new self-signed cert for the given domains.

Being self-signed, no client will trust this certificate.

Types

type EDNS0Option

type EDNS0Option interface {
	*dns.EDNS0_SUBNET | *dns.EDNS0_EDE | *dns.EDNS0_LOCAL | *dns.EDNS0_NSID | *dns.EDNS0_COOKIE | *dns.EDNS0_UL
	Option() uint16
}

EDNS0Option is an interface for all EDNS0 options as type constraint for generics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL