This commit is contained in:
Magnus Åhall 2026-02-23 07:04:57 +01:00
parent 36baaf0caf
commit ad638acaf3
7 changed files with 120 additions and 37 deletions

82
dns.go
View file

@ -3,6 +3,7 @@ package main
import (
// Standard
"fmt"
"maps"
"slices"
"strings"
)
@ -17,6 +18,10 @@ type DNSRecord struct {
Type string
}
type DomainPart struct {
Subparts map[string]*DomainPart `json:",omitempty"`
}
type RecordsTree struct {
Record DNSRecord
Children []RecordsTree
@ -74,36 +79,63 @@ func (r DNSRecord) NameReversed() string {
return strings.Join(parts, ".")
}
func (rt *RecordsTree) BuildTree(records []DNSRecord, startAt, partsLevel int) (tree RecordsTree) {
tree = RecordsTree{}
func BuildRecordsTree(records []DNSRecord) *DomainPart {
topPart := new(DomainPart)
topPart.Subparts = make(map[string]*DomainPart)
for i := startAt; i < len(records); i++ {
// current: bar.foo.n44.se
current := records[i]
currentPart := current.Part(partsLevel)
for _, record := range records {
curPart := topPart
for j := i; j < len(records); j++ {
// next: baz.bar.foo.n44.se
next := records[j]
nextPart := next.Part(partsLevel)
fmt.Printf("%04d %04d: %s ?= %s\n", i, j, currentPart, nextPart)
currentDomainNameSplit := strings.Split(record.Name, ".")
slices.Reverse(currentDomainNameSplit)
if currentPart == nextPart {
fmt.Printf("FOUND SOMETHING!\n")
nextTree := RecordsTree{
Record: next,
}
tree.Children = append(tree.Children, nextTree)
}
// Doesn't match anymore, return to other loop after all that are processed
if currentPart != nextPart {
i = j
break
for _, part := range currentDomainNameSplit {
if nextDomainPart, found := curPart.Subparts[part]; !found {
newPart := new(DomainPart)
newPart.Subparts = make(map[string]*DomainPart)
curPart.Subparts[strings.ToLower(part)] = newPart
curPart = newPart
} else {
curPart = nextDomainPart
}
}
}
return tree
return topPart
}
func (dp *DomainPart) ToHTML(parts []string) string {
html := ""
sortedParts := slices.Sorted(maps.Keys(dp.Subparts))
for _, part := range sortedParts {
subpart := dp.Subparts[part]
newParts := append(parts, part)
reversedParts := make([]string, len(newParts))
copy(reversedParts, newParts)
slices.Reverse(reversedParts)
fqdn := strings.Join(reversedParts, ".")
var subHTML string
if len(subpart.Subparts) == 0 {
html += fmt.Sprintf(`<div class="record" data-record="%s">%s</div>`, fqdn, fqdn)
} else {
subHTML = subpart.ToHTML(newParts)
html += fmt.Sprintf(`
<div class="top" data-top="%s">
<div class="fqdn">%s</div>
<div class="records">%s</div>
</div>
`,
fqdn,
fqdn,
subHTML,
)
}
}
return html
}