restrict available subnet sizes

This commit is contained in:
Johannes Kimmel 2023-11-17 17:01:29 +01:00
parent a5a29a18b1
commit 8b93868983
3 changed files with 29 additions and 14 deletions

View File

@ -112,15 +112,9 @@
</details>
<div id="control" hx-swap="none">
<button hx-get="/api/alloc/32">/32</button>
<button hx-get="/api/alloc/31">/31</button>
<button hx-get="/api/alloc/30">/30</button>
<button hx-get="/api/alloc/29">/29</button>
<button hx-get="/api/alloc/28">/28</button>
<button hx-get="/api/alloc/27">/27</button>
<button hx-get="/api/alloc/26">/26</button>
<button hx-get="/api/alloc/25">/25</button>
<button hx-get="/api/alloc/24">/24</button>
{{- range .Api.AllowedLengths }}
<button hx-get="/api/alloc/{{ . }}">/{{ . }}</button>
{{- end }}
</div>
{{ block "update" .Update }}<div id="updatemsg" class="card {{ .Type }}" hx-swap-oob="morph">{{ with .Content }}{{ . }}{{ end }}</div>{{ end }}

View File

@ -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()

17
main.go
View File

@ -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("&nbsp;"),
},
Treemap: s.ipdb.Treemap(),
},
)
if err != nil {
log.Println(err)
}