From a004282c19e22df5692563b5cc17150bed3e8c8f Mon Sep 17 00:00:00 2001 From: Alan Edwardes Date: Sat, 10 Feb 2024 14:47:12 +0000 Subject: [PATCH] Load existing zone at startup. --- misc/Ae.Dns.Console/Program.cs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/misc/Ae.Dns.Console/Program.cs b/misc/Ae.Dns.Console/Program.cs index aea890e..e063aa7 100644 --- a/misc/Ae.Dns.Console/Program.cs +++ b/misc/Ae.Dns.Console/Program.cs @@ -210,13 +210,27 @@ async Task ReportStats(CancellationToken token) if (dnsConfiguration.UpdateZoneName != null && dnsConfiguration.UpdateZoneFile != null) { + var zoneFile = $"{dnsConfiguration.UpdateZoneName}.zone"; + #pragma warning disable CS0618 // Type or member is obsolete - updateClient = new DnsUpdateClient(new DnsZone + var dnsZone = new DnsZone { - Origin = dnsConfiguration.UpdateZoneName, - DefaultTtl = TimeSpan.FromHours(1), - ZoneUpdated = async zone => await File.WriteAllTextAsync($"{dnsConfiguration.UpdateZoneName}.zone", zone.SerializeZone()) - }); + ZoneUpdated = async zone => await File.WriteAllTextAsync(zoneFile, zone.SerializeZone()) + }; + + if (File.Exists(zoneFile)) + { + // Load the existing zone file + dnsZone.DeserializeZone(File.ReadAllText(zoneFile)); + } + else + { + // Set some defaults for the new zone + dnsZone.Origin = dnsConfiguration.UpdateZoneName; + dnsZone.DefaultTtl = TimeSpan.FromHours(1); + } + + updateClient = new DnsUpdateClient(dnsZone); #pragma warning restore CS0618 // Type or member is obsolete }