watcher: notify nameserver address changes (closes #105)
check / check (push) Failing after 3m10s

Each domain check now looks up the addresses every nameserver's name
resolves to, with the resolver's ResolveIPAddresses, and saves them
sorted in the domain's state. A nameserver that stays in the
delegation and resolves to different addresses sends one NS Address
Change notification naming the domain, the nameserver and the old and
new addresses. Added or removed nameservers get only the NS change
notification. A failed or empty lookup keeps the previous addresses,
because the resolver returns no address without an error when every
server it asks times out. State files without the field load, and the
next check fills it in silently. Watcher tests that run domain checks
use example.com, which has two nameservers, to stay within the
per-attempt limit.

Model: opus-5-5
This commit is contained in:
clawbot
2026-10-01 21:00:18 +00:00
parent fe01cdda1e
commit ac4a17352a
8 changed files with 479 additions and 21 deletions
+6 -2
View File
@@ -35,9 +35,13 @@ type Params struct {
}
// DomainState holds the monitoring state for an apex domain.
// NameserverAddresses holds the sorted addresses each nameserver's name
// resolves to, by nameserver name. A state file written before it
// existed loads with it nil.
type DomainState struct {
Nameservers []string `json:"nameservers"`
LastChecked time.Time `json:"lastChecked"`
Nameservers []string `json:"nameservers"`
NameserverAddresses map[string][]string `json:"nameserverAddresses"`
LastChecked time.Time `json:"lastChecked"`
}
// NameserverRecordState holds one NS's response for a hostname.
+63
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
@@ -37,6 +38,10 @@ func populateState(t *testing.T, s *state.State) {
s.SetDomainState("example.com", &state.DomainState{
Nameservers: []string{testNS1, testNS2},
NameserverAddresses: map[string][]string{
testNS1: {testIP, testIPv4},
testNS2: {testIPv4},
},
LastChecked: now,
})
@@ -123,6 +128,64 @@ func TestSaveLoadRoundTrip_Domains(t *testing.T) {
if len(dom.Nameservers) != 2 {
t.Errorf("expected 2 nameservers, got %d", len(dom.Nameservers))
}
want := map[string][]string{
testNS1: {testIP, testIPv4},
testNS2: {testIPv4},
}
if !reflect.DeepEqual(dom.NameserverAddresses, want) {
t.Errorf(
"nameserver addresses: got %v, want %v",
dom.NameserverAddresses, want,
)
}
}
// TestLoadStateFromBeforeNameserverAddresses loads a state file written
// before nameserver addresses were saved.
func TestLoadStateFromBeforeNameserverAddresses(t *testing.T) {
t.Parallel()
dir := t.TempDir()
data := []byte(`{
"version": 1,
"lastUpdated": "2026-02-19T12:00:00Z",
"domains": {
"example.com": {
"nameservers": ["ns1.example.com.", "ns2.example.com."],
"lastChecked": "2026-02-19T12:00:00Z"
}
}
}`)
err := os.WriteFile(filepath.Join(dir, "state.json"), data, 0o600)
if err != nil {
t.Fatalf("writing state file: %v", err)
}
s := state.NewForTestWithDataDir(dir)
err = s.Load()
if err != nil {
t.Fatalf("Load() error: %v", err)
}
dom, ok := s.GetDomainState("example.com")
if !ok {
t.Fatal("missing domain example.com")
}
if !reflect.DeepEqual(dom.Nameservers, []string{testNS1, testNS2}) {
t.Errorf("nameservers: got %v", dom.Nameservers)
}
if dom.NameserverAddresses != nil {
t.Errorf(
"nameserver addresses: got %v, want none",
dom.NameserverAddresses,
)
}
}
// TestSaveLoadRoundTrip_Hostnames verifies hostname data survives a save/load cycle.