WIP
This commit is contained in:
parent
36baaf0caf
commit
ad638acaf3
7 changed files with 120 additions and 37 deletions
82
dns.go
82
dns.go
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -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/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/wrappederror v0.3.5 h1:/EzrdXETlZfNpS6TcK1Ix6BaV+Fl7qcGoxUM0GkrIN8=
|
||||
git.gibonuddevalla.se/go/wrappederror v0.3.5/go.mod h1:j4w320Hk1wvhOPjUaK4GgLvmtnjUUM5yVu6JFO1OCSc=
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ html {
|
|||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
*, *:before, *:after {
|
||||
*,
|
||||
*:before,
|
||||
*:after {
|
||||
box-sizing: inherit;
|
||||
}
|
||||
|
||||
|
|
@ -10,3 +12,23 @@ html {
|
|||
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
17
static/js/dns.mjs
Normal 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@
|
|||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<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">
|
||||
{
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
</script>
|
||||
|
||||
<script>
|
||||
window._VERSION = "{{ .VERSION }}"
|
||||
window._VERSION = "{{ .Data.VERSION }}"
|
||||
</script>
|
||||
</head>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
{{ define "page" }}
|
||||
<script type="module">
|
||||
import { Application } from "/js/{{ .Data.VERSION }}/dns.mjs"
|
||||
const app = new Application()
|
||||
</script>
|
||||
|
||||
<h1>{{ .Data.Identity }}</h1>
|
||||
|
||||
{{ range .Data.DNSRecords }}
|
||||
<div class="dns-record">{{ .NameReversed }}
|
||||
{{ end }}
|
||||
<div class="records-tree">
|
||||
{{ .Data.Tree }}
|
||||
</div>
|
||||
|
||||
{{ end }}
|
||||
|
|
|
|||
10
webserver.go
10
webserver.go
|
|
@ -11,6 +11,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -65,12 +66,17 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|||
slices.SortFunc(entries, SortDNSRecord)
|
||||
data["DNSRecords"] = entries
|
||||
|
||||
tree := RecordsTree{}
|
||||
tree = tree.BuildTree(entries, 0, 1)
|
||||
fmt.Printf("\n\x1b[32;1mSTART\x1b[0m\n")
|
||||
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)
|
||||
os.WriteFile("/tmp/tree.json", j, 0644)
|
||||
|
||||
data["VERSION"] = VERSION
|
||||
page.Data = data
|
||||
|
||||
err = htmlEngine.Render(page, w, r)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue