| 1 |
package dbfile |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"log/slog" |
| 7 |
"maps" |
| 8 |
"math/rand/v2" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"strings" |
| 12 |
"time" |
| 13 |
|
| 14 |
"codeberg.org/miekg/dns" |
| 15 |
"codeberg.org/miekg/dns/cmd/atomdns/handlers/dbfile/zone" |
| 16 |
"codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver" |
| 17 |
"codeberg.org/miekg/dns/dnsutil" |
| 18 |
) |
| 19 |
|
| 20 |
func (d *Dbfile) Setup(co *dnsserver.Controller) error { |
| 21 |
d.Zones = map[string]*zone.Zone{} |
| 22 |
d.ctx, d.cancel = context.WithCancel(context.Background()) |
| 23 |
|
| 24 |
if co.Next() { |
| 25 |
if !co.Next() { |
| 26 |
return co.ArgErr() |
| 27 |
} |
| 28 |
d.Path = co.Path() |
| 29 |
for co.NextBlock(0) { |
| 30 |
switch co.Val() { |
| 31 |
case "transfer": |
| 32 |
if err := d.SetupTransfer(co); err != nil { |
| 33 |
return err |
| 34 |
} |
| 35 |
default: |
| 36 |
return co.ArgErr() |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
if len(co.Keys()) > 1 && len(d.From.IPs) > 0 { |
| 41 |
return co.Errf("when transferring from, there can only be a single origin, got: %s", strings.Join(co.Keys(), ", ")) |
| 42 |
} |
| 43 |
|
| 44 |
for _, z := range co.Keys() { |
| 45 |
d.Zones[dnsutil.Canonical(z)] = zone.New(z, d.Path) |
| 46 |
} |
| 47 |
co.OnStartup(func() error { |
| 48 |
log().Info("Startup", "reload", filepath.Base(d.Path)) |
| 49 |
d.RLock() |
| 50 |
zones := maps.Values(d.Zones) |
| 51 |
d.RUnlock() |
| 52 |
for z := range zones { |
| 53 |
alog := log().With(slog.String("zone", z.Origin()), slog.String("file", filepath.Base(z.Path))) |
| 54 |
_, err := os.Stat(z.Path) |
| 55 |
if errors.Is(err, os.ErrNotExist) { |
| 56 |
alog.Warn("Waiting for zone to appear") |
| 57 |
continue |
| 58 |
} |
| 59 |
if err := z.Load(); err != nil { |
| 60 |
return d.Err(err) |
| 61 |
} |
| 62 |
} |
| 63 |
return d.Reload() |
| 64 |
}) |
| 65 |
if d.From != nil && len(d.From.IPs) > 0 { |
| 66 |
co.OnStartup(func() error { |
| 67 |
d.RLock() |
| 68 |
zones := maps.Values(d.Zones) |
| 69 |
d.RUnlock() |
| 70 |
for z := range zones { |
| 71 |
log().Info("Startup", "retransfer", z.Origin(), "file", filepath.Base(z.Path)) |
| 72 |
|
| 73 |
apex := z.Apex() |
| 74 |
serial := uint32(0) |
| 75 |
if apex != nil { |
| 76 |
for _, rr := range apex.RRs { |
| 77 |
if s, ok := rr.(*dns.SOA); ok { |
| 78 |
serial = s.Serial |
| 79 |
break |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
if !d.From.AvailableFrom(z.Origin(), serial) { |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
err := d.TransferIn(z.Origin()) |
| 88 |
if err != nil { |
| 89 |
alog := log().With(slog.String("zone", z.Origin()), slog.String("file", filepath.Base(z.Path))) |
| 90 |
alog.Error("Failed to transfer", Err(err)) |
| 91 |
} |
| 92 |
break |
| 93 |
} |
| 94 |
return d.Retransfer() |
| 95 |
}) |
| 96 |
} |
| 97 |
if d.To != nil && len(d.To.IPs) > 0 { |
| 98 |
co.OnStartup(func() error { |
| 99 |
d.RLock() |
| 100 |
zones := maps.Values(d.Zones) |
| 101 |
d.RUnlock() |
| 102 |
for z := range zones { |
| 103 |
go func() { |
| 104 |
N := time.Duration(rand.IntN(20)) + 10 |
| 105 |
log().Info("Startup", "notifying", z.Origin(), "file", filepath.Base(d.Path), slog.Duration("after", N*time.Second)) |
| 106 |
time.Sleep(N * time.Second) |
| 107 |
d.To.Notify(z.Origin()) |
| 108 |
}() |
| 109 |
} |
| 110 |
return nil |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
co.OnShutdown(func() error { |
| 115 |
log().Info("Shutdown", "reload", filepath.Base(d.Path)) |
| 116 |
d.cancel() |
| 117 |
return nil |
| 118 |
}) |
| 119 |
if d.From != nil && len(d.From.IPs) > 0 { |
| 120 |
co.OnShutdown(func() error { |
| 121 |
log().Info("Shutdown", "retransfer", filepath.Base(d.Path)) |
| 122 |
d.cancel() |
| 123 |
return nil |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
return nil |
| 128 |
} |