package server import ( "context" "fmt" "net/url" "strconv" "strings" "git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns" ) // The EasyDNS API supports inclusion by one RR type, but no exclusion filter. // Signed zones can contain thousands of generated signatures. The panel pages // through the native API and applies the DNSSEC exclusion before constructing // a visible page, so a page is never filled with hidden rows. const ( zoneRecordBatch = 1000 // maximum per_page accepted by native EasyDNS zoneRecordPageSize = 50 ) var dnssecRecordTypes = map[string]bool{ "RRSIG": true, "NSEC": true, "NSEC3": true, "NSEC3PARAM": true, "DNSKEY": true, "DS": true, "CDS": true, "CDNSKEY": true, } func isDNSSECRecordType(typ string) bool { return dnssecRecordTypes[strings.ToUpper(typ)] } type visibleRecordPage struct { records []easydns.Record rawTotal int nextCursor int prevCursor int hasNext bool hasPrev bool etag string } type recordPager struct { ctx context.Context api *easydns.Client zone string filters url.Values page int batch easydns.Page[easydns.Record] total int etag string loaded bool } func (p *recordPager) at(index int) (*easydns.Record, error) { if index < 0 || p.loaded && index >= p.total { return nil, nil } page := index/zoneRecordBatch + 1 if !p.loaded || page != p.page { q := url.Values{"per_page": {strconv.Itoa(zoneRecordBatch)}, "page": {strconv.Itoa(page)}} for _, name := range []string{"name", "type"} { if v := strings.TrimSpace(p.filters.Get(name)); v != "" { q.Set(name, v) } } var b easydns.Page[easydns.Record] etag, err := p.api.Get(p.ctx, "/api/v1/zones/"+easydns.Escape(p.zone)+"/records?"+q.Encode(), &b) if err != nil { return nil, err } p.page, p.batch, p.loaded = page, b, true p.total = b.Pagination.Total if p.etag == "" { p.etag = etag } } if index >= p.total { return nil, nil } slot := index % zoneRecordBatch if slot >= len(p.batch.Items) { // Treat an incomplete API page as end of data rather than looping. return nil, nil } return &p.batch.Items[slot], nil } // listVisibleZoneRecords filters before page construction and uses raw-record // cursors rather than recalculating filtered counts for the whole zone. This // avoids repeatedly scanning large signed zones when moving to the next page. // The previous cursor is obtained by a reverse scan of just one visible page. func (s *Server) listVisibleZoneRecords(ctx context.Context, zone string, filters url.Values, cursor, displayPage int) (visibleRecordPage, error) { var result visibleRecordPage pager := &recordPager{ctx: ctx, api: s.api, zone: zone, filters: filters} if cursor < 0 { cursor = 0 } if displayPage < 1 { displayPage = 1 } for i := cursor; ; i++ { rec, err := pager.at(i) if err != nil { return visibleRecordPage{}, err } if rec == nil { break } if isDNSSECRecordType(rec.Type) { continue } if len(result.records) == zoneRecordPageSize { result.hasNext, result.nextCursor = true, i break } result.records = append(result.records, *rec) } // A forged or stale cursor must not cause an unbounded reverse walk. if cursor > pager.total { cursor = pager.total } // When users browse backwards, only the previous 50 visible records are // needed. A second page load may change the zone, so all cursors are best // effort and no stale ETag is used for any write action. if displayPage > 1 && cursor > 0 { visible := 0 for i := cursor - 1; i >= 0; i-- { rec, err := pager.at(i) if err != nil { return visibleRecordPage{}, err } if rec == nil { break } if !isDNSSECRecordType(rec.Type) { visible++ result.prevCursor = i if visible == zoneRecordPageSize { break } } } result.hasPrev = true } result.rawTotal, result.etag = pager.total, pager.etag return result, nil } func parseRecordCursor(raw string) int { if raw == "" { return 0 } cursor, err := strconv.Atoi(raw) if err != nil || cursor < 0 || cursor > 100_000_000 { return 0 } return cursor } func recordPageURL(zone string, values url.Values, page, cursor int, hide bool) string { q := url.Values{} for _, key := range []string{"name", "type"} { if v := strings.TrimSpace(values.Get(key)); v != "" { q.Set(key, v) } } q.Set("page", fmt.Sprint(page)) if hide { q.Set("hide_dnssec", "1") if cursor > 0 { q.Set("cursor", fmt.Sprint(cursor)) } } return "/zones/" + easydns.Escape(zone) + "/records?" + q.Encode() }