Skip to content

Commit e7ec8d3

Browse files
committed
explicitly set zone id and name in zone records since Cloudflare API does not consistently populate them
1 parent 892b1b5 commit e7ec8d3

File tree

4 files changed

+652
-343
lines changed

4 files changed

+652
-343
lines changed

DnsTube.Core/DnsTube.Core.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1111
<DebugSymbols>False</DebugSymbols>
1212
<DebugType>None</DebugType>
13+
<ShouldCreateLogs>True</ShouldCreateLogs>
14+
<AdvancedSettingsExpanded>True</AdvancedSettingsExpanded>
15+
<UpdateAssemblyVersion>False</UpdateAssemblyVersion>
16+
<UpdateAssemblyFileVersion>False</UpdateAssemblyFileVersion>
17+
<UpdateAssemblyInfoVersion>False</UpdateAssemblyInfoVersion>
18+
<UpdatePackageVersion>False</UpdatePackageVersion>
19+
<AssemblyInfoVersionType>SettingsVersion</AssemblyInfoVersionType>
20+
<InheritWinAppVersionFrom>None</InheritWinAppVersionFrom>
1321
</PropertyGroup>
1422
<ItemGroup>
1523
<PackageReference Include="Dapper" Version="2.1.35" />

DnsTube.Core/Models/Zone.cs

+62-62
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
namespace DnsTube.Core.Models.Zone
22
{
3-
public class ListZonesResponse
4-
{
5-
public bool success { get; set; }
6-
public object[] errors { get; set; }
7-
public object[] messages { get; set; }
8-
public Result[] result { get; set; }
9-
public Result_Info result_info { get; set; }
10-
}
3+
public class ListZonesResponse
4+
{
5+
public bool success { get; set; }
6+
public object[] errors { get; set; }
7+
public object[] messages { get; set; }
8+
public Zone[] result { get; set; }
9+
public Result_Info result_info { get; set; }
10+
}
1111

12-
public class Result_Info
13-
{
14-
public int page { get; set; }
15-
public int per_page { get; set; }
16-
public int count { get; set; }
17-
public int total_count { get; set; }
18-
}
12+
public class Result_Info
13+
{
14+
public int page { get; set; }
15+
public int per_page { get; set; }
16+
public int count { get; set; }
17+
public int total_count { get; set; }
18+
}
1919

20-
public class Result
21-
{
22-
public string id { get; set; }
23-
public string name { get; set; }
24-
public int development_mode { get; set; }
25-
public string[] original_name_servers { get; set; }
26-
public string original_registrar { get; set; }
27-
public string original_dnshost { get; set; }
28-
public DateTime created_on { get; set; }
29-
public DateTime modified_on { get; set; }
30-
public Owner owner { get; set; }
31-
public string[] permissions { get; set; }
32-
public Plan plan { get; set; }
33-
public Plan_Pending plan_pending { get; set; }
34-
public string status { get; set; }
35-
public bool paused { get; set; }
36-
public string type { get; set; }
37-
public string[] name_servers { get; set; }
38-
}
20+
public class Zone
21+
{
22+
public string id { get; set; }
23+
public string name { get; set; }
24+
public int development_mode { get; set; }
25+
public string[] original_name_servers { get; set; }
26+
public string original_registrar { get; set; }
27+
public string original_dnshost { get; set; }
28+
public DateTime created_on { get; set; }
29+
public DateTime modified_on { get; set; }
30+
public Owner owner { get; set; }
31+
public string[] permissions { get; set; }
32+
public Plan plan { get; set; }
33+
public Plan_Pending plan_pending { get; set; }
34+
public string status { get; set; }
35+
public bool paused { get; set; }
36+
public string type { get; set; }
37+
public string[] name_servers { get; set; }
38+
}
3939

40-
public class Owner
41-
{
42-
public string id { get; set; }
43-
public string email { get; set; }
44-
public string owner_type { get; set; }
45-
}
40+
public class Owner
41+
{
42+
public string id { get; set; }
43+
public string email { get; set; }
44+
public string owner_type { get; set; }
45+
}
4646

47-
public class Plan
48-
{
49-
public string id { get; set; }
50-
public string name { get; set; }
51-
public int price { get; set; }
52-
public string currency { get; set; }
53-
public string frequency { get; set; }
54-
public string legacy_id { get; set; }
55-
public bool is_subscribed { get; set; }
56-
public bool can_subscribe { get; set; }
57-
}
47+
public class Plan
48+
{
49+
public string id { get; set; }
50+
public string name { get; set; }
51+
public int price { get; set; }
52+
public string currency { get; set; }
53+
public string frequency { get; set; }
54+
public string legacy_id { get; set; }
55+
public bool is_subscribed { get; set; }
56+
public bool can_subscribe { get; set; }
57+
}
5858

59-
public class Plan_Pending
60-
{
61-
public string id { get; set; }
62-
public string name { get; set; }
63-
public int price { get; set; }
64-
public string currency { get; set; }
65-
public string frequency { get; set; }
66-
public string legacy_id { get; set; }
67-
public bool is_subscribed { get; set; }
68-
public bool can_subscribe { get; set; }
69-
}
59+
public class Plan_Pending
60+
{
61+
public string id { get; set; }
62+
public string name { get; set; }
63+
public int price { get; set; }
64+
public string currency { get; set; }
65+
public string frequency { get; set; }
66+
public string legacy_id { get; set; }
67+
public bool is_subscribed { get; set; }
68+
public bool can_subscribe { get; set; }
69+
}
7070
}

DnsTube.Core/Services/CloudflareService.cs

+48-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using DnsTube.Core.Interfaces;
77
using DnsTube.Core.Models;
88
using DnsTube.Core.Models.Dns;
9+
using DnsTube.Core.Models.Zone;
910

1011
using Microsoft.Extensions.Logging;
1112

@@ -143,8 +144,38 @@ public async Task<List<string>> ListZoneIDsAsync()
143144
return ret;
144145
}
145146

147+
public async Task<List<Zone>> ListZoneesAsync()
148+
{
149+
List<Zone> ret = new();
150+
int pageSize = 50;
151+
int pageNumber = 1;
152+
int totalPages;
153+
154+
var httpClient = _httpClientFactory.CreateClient(HttpClientName.Cloudflare.ToString());
155+
do
156+
{
157+
var req = await GetRequestMessageAsync(HttpMethod.Get, $"zones?status=active&page={pageNumber}&per_page={pageSize}&order=name&direction=asc&match=all");
158+
159+
var response = await httpClient.SendAsync(req);
160+
var result = await response.Content.ReadAsStringAsync();
161+
162+
await ValidateCloudflareResultAsync(response, result, "list zones");
163+
164+
var zoneListResponse = JsonSerializer.Deserialize<ListZonesResponse>(result);
165+
166+
int totalRecords = zoneListResponse.result_info.total_count;
167+
totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
168+
169+
ret.AddRange(zoneListResponse.result);
170+
171+
pageNumber++;
172+
} while (pageNumber <= totalPages);
173+
174+
return ret;
175+
}
176+
146177
// Ref: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
147-
private async Task<List<Result>> GetRecordsByTypeAsync(string zoneIdentifier, string recordType)
178+
private async Task<List<Result>> GetRecordsByTypeAsync(string zoneIdentifier, string zoneName, string recordType)
148179
{
149180
int pageSize = 100;
150181
int pageNumber = 1;
@@ -174,6 +205,12 @@ private async Task<List<Result>> GetRecordsByTypeAsync(string zoneIdentifier, st
174205
pageNumber++;
175206
} while (pageNumber <= totalPages);
176207

208+
foreach (var record in ret)
209+
{
210+
record.zone_id = zoneIdentifier;
211+
record.zone_name = zoneName;
212+
}
213+
177214
return ret;
178215
}
179216

@@ -224,26 +261,32 @@ public async Task<List<Result>> GetAllDnsRecordsByZoneAsync()
224261
if (zoneIDs is null || !zoneIDs.Any())
225262
zoneIDs = await ListZoneIDsAsync();
226263

264+
// The Cloudflare API we call in GetRecordsByTypeAsync does not always return zone info properly,
265+
// so we need to get the zone info here and add it to the results
266+
var cloudflareZones = await ListZoneesAsync();
267+
227268
var allDnsEntries = new List<Result>();
228269

229270
foreach (var zoneID in zoneIDs)
230271
{
272+
var zoneName = cloudflareZones.First(z => z.id == zoneID).name;
273+
231274
if (settings.ProtocolSupport != IpSupport.IPv6)
232275
{
233-
var aRecords = await GetRecordsByTypeAsync(zoneID, "A");
276+
var aRecords = await GetRecordsByTypeAsync(zoneID, zoneName, "A");
234277
allDnsEntries.AddRange(aRecords);
235278
}
236279

237280
if (settings.ProtocolSupport != IpSupport.IPv4)
238281
{
239-
var aaaaRecords = await GetRecordsByTypeAsync(zoneID, "AAAA");
282+
var aaaaRecords = await GetRecordsByTypeAsync(zoneID, zoneName, "AAAA");
240283
allDnsEntries.AddRange(aaaaRecords);
241284
}
242285

243-
var txtRecords = await GetRecordsByTypeAsync(zoneID, "TXT");
286+
var txtRecords = await GetRecordsByTypeAsync(zoneID, zoneName, "TXT");
244287
allDnsEntries.AddRange(txtRecords);
245288

246-
var spfRecords = await GetRecordsByTypeAsync(zoneID, "SPF");
289+
var spfRecords = await GetRecordsByTypeAsync(zoneID, zoneName, "SPF");
247290
allDnsEntries.AddRange(spfRecords);
248291
}
249292

0 commit comments

Comments
 (0)