Debian Codegraph @

dnsutil/dnsutil.go

raw
1 // Package dnsutil contains function that are useful in the context of working with the DNS.
2 package dnsutil
3
4 import "crypto/rand"
5
6 // Trim removes the zone component from s. It returns the trimmed name or the original string if z is longer
7 // than s. The trimmed name will be returned without a trailing dot. s and z must be syntactically valid
8 // domain names, see [IsName] and [IsFqdn]. And also see [Prev] and [Next] to iterate over a domain name.
9 func Trim(s, z string) string {
10 offset, start := len(s), false
11 for range Labels(z) {
12 offset, start = Prev(s, offset)
13 if start {
14 return s
15 }
16 }
17 return s[:offset-1]
18 }
19
20 // IsBelow checks if child sits below parent in the DNS tree, i.e. check if the child is a sub-domain of
21 // parent. If child and parent are at the same level, true is returned as well.
22 func IsBelow(parent, child string) bool { return Common(parent, child) == Labels(parent) }
23
24 // Randomize randomizes the string s, it randomly upper or lower cases each letter. If a letter is already
25 // uppercase it is downcased.
26 func Randomize(s string) string {
27 b := []byte(s)
28 r := [1]byte{}
29 for i := range b {
30 rand.Read(r[:])
31 if r[0]%2 == 0 {
32 if b[i] >= 'A' && b[i] <= 'Z' {
33 b[i] += 0x20
34 continue
35 }
36 if b[i] >= 'a' && b[i] <= 'z' {
37 b[i] -= 0x20
38 }
39 }
40 }
41 return string(b)
42 }