dns: pick the extra_records_path format by file extension

HuJSON and YAML records work too; an unknown extension fails instead of being
parsed as JSON.
This commit is contained in:
Kristoffer Dalby
2026-09-28 13:33:22 +00:00
committed by Kristoffer Dalby
parent dd8ce695cf
commit 906016beb4
4 changed files with 12 additions and 9 deletions
+7 -7
View File
@@ -3,7 +3,6 @@ package dns
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"os"
@@ -12,6 +11,7 @@ import (
"github.com/cenkalti/backoff/v5"
"github.com/fsnotify/fsnotify"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
"tailscale.com/tailcfg"
"tailscale.com/util/set"
@@ -222,8 +222,9 @@ func (e *ExtraRecordsMan) updateRecords() {
}
}
// readExtraRecordsFromPath reads a JSON file of [tailcfg.DNSRecord]
// and returns the records and the hash of the file.
// readExtraRecordsFromPath reads a file of [tailcfg.DNSRecord] in the format its
// extension names (see [util.UnmarshalByExt]) and returns the records and the
// hash of the file.
func readExtraRecordsFromPath(path string) ([]tailcfg.DNSRecord, [32]byte, error) {
var zero [32]byte
@@ -238,14 +239,13 @@ func readExtraRecordsFromPath(path string) ([]tailcfg.DNSRecord, [32]byte, error
return nil, zero, nil
}
var records []tailcfg.DNSRecord
// Hash first: decoding HuJSON may rewrite comments in b.
hash := sha256.Sum256(b)
err = json.Unmarshal(b, &records)
records, err := util.UnmarshalByExt[[]tailcfg.DNSRecord](path, b)
if err != nil {
return nil, zero, fmt.Errorf("unmarshalling records, content: %q: %w", string(b), err)
}
hash := sha256.Sum256(b)
return records, hash, nil
}