From 8b9386898394bc749ea4e926f8b2e3f3e8e8e5c3 Mon Sep 17 00:00:00 2001 From: Johannes Kimmel Date: Fri, 17 Nov 2023 17:01:29 +0100 Subject: [PATCH] restrict available subnet sizes --- index.html | 12 +++--------- ipalloc/ipalloc.go | 14 +++++++++++--- main.go | 17 +++++++++++++++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index e691568..3ad4ca4 100644 --- a/index.html +++ b/index.html @@ -112,15 +112,9 @@
- - - - - - - - - + {{- range .Api.AllowedLengths }} + + {{- end }}
{{ block "update" .Update }}
{{ with .Content }}{{ . }}{{ end }}
{{ end }} diff --git a/ipalloc/ipalloc.go b/ipalloc/ipalloc.go index e7ab035..f03814a 100644 --- a/ipalloc/ipalloc.go +++ b/ipalloc/ipalloc.go @@ -266,13 +266,21 @@ func (t *tree) Dot() string { type DB struct { sync.Mutex - provisions *tree + provisions *tree + allowedLengths []int } -func NewDB(prefix string) *DB { - return &DB{provisions: &tree{prefix: netip.MustParsePrefix(prefix)}} +func NewDB(prefix string, allowedLengths ...int) *DB { + p := netip.MustParsePrefix(prefix) + if canonical := p.Masked(); p.Addr() != canonical.Addr() { + log.Fatalf("Prefix %q is not in canonical form, use: %q", p, canonical) + } + return &DB{provisions: &tree{prefix: p}, allowedLengths: allowedLengths} } +func (db *DB) AllowedLengths() []int { + return slices.Clip(db.allowedLengths) +} func (db *DB) Used() []netip.Prefix { db.Lock() defer db.Unlock() diff --git a/main.go b/main.go index e30c408..0e4e55c 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,10 @@ func (s *server) setup() { s.mux.HandleFunc("/api/print/", s.apiPrint) //s.mux.HandleFunc("/api/provision/", s.apiProvision) - s.ipdb = ipalloc.NewDB("10.83.46.0/23") + s.ipdb = ipalloc.NewDB("10.83.46.0/23", 32, 29, 28, 27, 26) + //s.ipdb = ipalloc.NewDB("fd43:5602:29bd:fffe::/64", 128, 127, 126) + //s.ipdb = ipalloc.NewDB("fd43:5602:29bd:ffff:ffff:ffff:0:0/96", 128, 127, 126) + //s.ipdb = ipalloc.NewDB("fd43:5602:29bd:ffff:ffff:ffff:ffff:0/112", 128) s.tmpl = template.Must(template.ParseFS(webcontent, "index.html")) } @@ -59,7 +62,17 @@ type UpdateMessage struct { } func (s *server) template(w http.ResponseWriter, r *http.Request) { - err := s.tmpl.Execute(w, PageContent{Api: s.ipdb, Treemap: s.ipdb.Treemap()}) + err := s.tmpl.Execute(w, + PageContent{ + Api: s.ipdb, + Update: UpdateMessage{ + Type: "hint", + Content: template.HTML(" "), + }, + Treemap: s.ipdb.Treemap(), + }, + ) + if err != nil { log.Println(err) }