MijnHost / MijnHost / CheckAllHelper.cs
Code · 35 lines · 1102 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35using System;
using System.Collections.Generic;
using System.Text;

namespace MijnHost;

public class CheckAllHelper
{
	private static readonly string ACME = "acme.certservice.net."; // must end in .
	public static async Task CheckAlAsync(DnsApiClient client)
	{
		if (!ACME.EndsWith('.'))
			return;
		DnsResponse<DnsDomains> dnsResponse = await client.GetDomainsAsync();
		foreach (DnsDomain dom in dnsResponse.Data.Domains)
		{
			DnsResponse<DnsRecords> dnsRecords = await client.GetDomainRecordsAsync(dom.Domain);

			DnsRecord? challenge = dnsRecords.Data.Records.FirstOrDefault(x => x.Name.StartsWith("_acme-challenge"));
			if (challenge != null && challenge.Value.EndsWith(ACME))
				continue;

			DnsApiResponse? result = await client.PatchDnsRecordAsync(
				dom.Domain, new DnsRecord($"_acme-challenge", "CNAME", $"{dom.Domain}.{ACME}", 60));
			if (result.Status == 200)
			{
				Console.WriteLine($"Added _acme-challenge CNAME {dom.Domain}.{ACME}");
			}
			else
			{
				Console.WriteLine($"Failed to add _acme-challenge for {dom.Domain}: {result.StatusDescription}");
			}
		}
	}
}