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

2
go.sum
View file

@ -1,6 +1,6 @@
git.gibonuddevalla.se/go/html_template v1.0.0 h1:0YoqKiWMxYUsYZomqFlXLR/h0UNBicqw+VOsPyotcD4= git.gibonuddevalla.se/go/html_template v1.0.0 h1:0YoqKiWMxYUsYZomqFlXLR/h0UNBicqw+VOsPyotcD4=
git.gibonuddevalla.se/go/html_template v1.0.0/go.mod h1:DGqGMulbZyGoRqWXInXISb6GQLswuUrTaX1WhH7jx/w= git.gibonuddevalla.se/go/html_template v1.0.0/go.mod h1:DGqGMulbZyGoRqWXInXISb6GQLswuUrTaX1WhH7jx/w=
git.gibonuddevalla.se/go/vlog v1.0.0 h1:6iu7Wy3V3vTSg1usLSZWX9ipA+SY3FV4pi7HrHqagyc= git.gibonuddevalla.se/go/vlog v1.0.0 h1:AlArMDt1Aw6poI8yDtU22d4NyFl3iQocZSswvlzy48o=
git.gibonuddevalla.se/go/vlog v1.0.0/go.mod h1:rjS9ZINhZF+Bhrb+fdD4aEKOnLxFYU3PJFMx7nOi4c0= git.gibonuddevalla.se/go/vlog v1.0.0/go.mod h1:rjS9ZINhZF+Bhrb+fdD4aEKOnLxFYU3PJFMx7nOi4c0=
git.gibonuddevalla.se/go/wrappederror v0.3.5 h1:/EzrdXETlZfNpS6TcK1Ix6BaV+Fl7qcGoxUM0GkrIN8= git.gibonuddevalla.se/go/wrappederror v0.3.5 h1:/EzrdXETlZfNpS6TcK1Ix6BaV+Fl7qcGoxUM0GkrIN8=
git.gibonuddevalla.se/go/wrappederror v0.3.5/go.mod h1:j4w320Hk1wvhOPjUaK4GgLvmtnjUUM5yVu6JFO1OCSc= git.gibonuddevalla.se/go/wrappederror v0.3.5/go.mod h1:j4w320Hk1wvhOPjUaK4GgLvmtnjUUM5yVu6JFO1OCSc=

View file

@ -2,7 +2,9 @@ html {
box-sizing: border-box; box-sizing: border-box;
} }
*, *:before, *:after { *,
*:before,
*:after {
box-sizing: inherit; box-sizing: inherit;
} }
@ -10,3 +12,23 @@ html {
cursor: pointer; cursor: pointer;
} }
body {
font-family: sans-serif;
}
.records-tree {
.top {
font-weight: bold;
margin-left: 16px;
}
&>.top {
margin-left: 0px;
}
.record {
display: none;
color: #444;
}
}

17
static/js/dns.mjs Normal file
View file

@ -0,0 +1,17 @@
export class Application {
constructor() {
this.addTopHandlers()
}
addTopHandlers() {
for (const top of document.querySelectorAll('.top .fqdn'))
top.addEventListener('click', event=>this.handlerTop(event))
}
handlerTop(event) {
const fqdn = event.target
const top = fqdn.closest('.top')
const records = top.querySelector('.records')
console.log(fqdn, top, records)
}
}

View file

@ -4,7 +4,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" type="text/css" href="/css/{{ .VERSION }}/main.css"> <link rel="stylesheet" type="text/css" href="/css/{{ .Data.VERSION }}/index.css">
<script type="importmap"> <script type="importmap">
{ {
@ -14,7 +14,7 @@
</script> </script>
<script> <script>
window._VERSION = "{{ .VERSION }}" window._VERSION = "{{ .Data.VERSION }}"
</script> </script>
</head> </head>

View file

@ -1,7 +1,13 @@
{{ define "page" }} {{ define "page" }}
<script type="module">
import { Application } from "/js/{{ .Data.VERSION }}/dns.mjs"
const app = new Application()
</script>
<h1>{{ .Data.Identity }}</h1> <h1>{{ .Data.Identity }}</h1>
{{ range .Data.DNSRecords }} <div class="records-tree">
<div class="dns-record">{{ .NameReversed }} {{ .Data.Tree }}
{{ end }} </div>
{{ end }} {{ end }}

View file

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"os" "os"
"slices" "slices"
"html/template"
) )
var ( var (
@ -65,12 +66,17 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
slices.SortFunc(entries, SortDNSRecord) slices.SortFunc(entries, SortDNSRecord)
data["DNSRecords"] = entries data["DNSRecords"] = entries
tree := RecordsTree{} fmt.Printf("\n\x1b[32;1mSTART\x1b[0m\n")
tree = tree.BuildTree(entries, 0, 1) tree := BuildRecordsTree(entries)
htmlTree := tree.ToHTML([]string{})
data["Tree"] = template.HTML(htmlTree)
fmt.Printf("\n\x1b[32;1mDONE\x1b[0m\n")
j, _ := json.Marshal(tree) j, _ := json.Marshal(tree)
os.WriteFile("/tmp/tree.json", j, 0644) os.WriteFile("/tmp/tree.json", j, 0644)
data["VERSION"] = VERSION
page.Data = data page.Data = data
err = htmlEngine.Render(page, w, r) err = htmlEngine.Render(page, w, r)