-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDnsUpdater.cs
111 lines (85 loc) · 3.27 KB
/
DnsUpdater.cs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace CloudflareDDNSUpdater;
public class DnsUpdater {
private readonly ILogger<DnsUpdater> _logger;
private readonly IConfiguration _configuration;
private static string _lastIpAddress;
public DnsUpdater(
ILogger<DnsUpdater> logger,
IConfiguration configuration) {
_logger = logger;
_configuration = configuration;
}
public async Task Update() {
var ipAddress = await GetPublicIpAddress();
if (string.IsNullOrEmpty(ipAddress)) {
return;
}
_logger.LogTrace("IP address is {IpAddress}", ipAddress);
if (_lastIpAddress != null && _lastIpAddress.Equals(ipAddress, StringComparison.OrdinalIgnoreCase)) {
_logger.LogTrace("IP has not changed, update not required");
return;
}
await UpdateDns(ipAddress);
_lastIpAddress = ipAddress;
_logger.LogInformation("DNS updated to use IP: {IpAddress}", ipAddress);
}
private async Task<string> GetPublicIpAddress() {
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(15);
try {
var response = await client.GetAsync("https://api.ipify.org/");
response.EnsureSuccessStatusCode();
return (await response.Content.ReadAsStringAsync()).Trim();
}
catch (OperationCanceledException) {
_logger.LogError("Operation canceled or timeout getting public IP address");
return null;
}
}
private async Task UpdateDns(string ipAddress)
{
var apiToken = _configuration.GetValue<string>("Cloudflare:ApiToken");
var recordConfigs = _configuration.GetSection("Cloudflare:Records").GetChildren();
foreach (var recordConfig in recordConfigs)
{
var config = recordConfig.Get<RecordConfiguration>();
try
{
await UpdateDnsForZone(ipAddress, config, apiToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating zone {ZoneId}, record {RecordId}", config.ZoneId, config.RecordId);
}
}
}
private static async Task UpdateDnsForZone(string ipAddress, RecordConfiguration config, string apiToken)
{
var url = $"https://api.cloudflare.com/client/v4/zones/{config.ZoneId}/dns_records/{config.RecordId}";
using var client = new HttpClient();
var json = JsonSerializer.Serialize(new {
content = ipAddress,
name = config.RecordName,
ttl = config.RecordTtl,
type = config.RecordType
});
var content = new StringContent(json, Encoding.UTF8, "application/json");
var message = new HttpRequestMessage {
Method = HttpMethod.Put,
RequestUri = new Uri(url),
Content = content,
Headers = {
{ "Authorization", $"Bearer {apiToken}" }
}
};
var response = await client.SendAsync(message);
response.EnsureSuccessStatusCode();
}
}