Add ip update capabilities

This commit is contained in:
2025-03-14 10:27:45 -06:00
parent 2edc40fb99
commit aa2084f3ee
3 changed files with 135 additions and 34 deletions
+4
View File
@@ -6,4 +6,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="9.0.3" />
</ItemGroup>
</Project> </Project>
+97
View File
@@ -2,6 +2,8 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
var configuration = builder.Configuration;
_ = Run(configuration);
var app = builder.Build(); var app = builder.Build();
@@ -23,3 +25,98 @@ app.UseAuthorization();
app.MapRazorPages(); app.MapRazorPages();
app.Run(); app.Run();
static async Task Run(IConfiguration config)
{
HttpClient httpClient = new();
AAAARecord dnsRecord = await GetDnsRecordAsync(config);
string lastKnownIp = dnsRecord.content;
TimeSpan checkInterval = TimeSpan.FromMinutes(1);
while (true)
{
try
{
string currentIp = await GetPublicIpAsync(httpClient) ?? "";
if (lastKnownIp != currentIp)
{
await UpdateDnsIpAsync(config, dnsRecord, currentIp);
lastKnownIp = currentIp;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
await Task.Delay(checkInterval);
}
}
static async Task<string> GetPublicIpAsync(HttpClient client)
{
try
{
return await client.GetStringAsync(@"https://api.ipify.org/");
}
catch
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to grab public ip");
return await GetPublicIpAsync(client);
}
}
static async Task<AAAARecord> GetDnsRecordAsync(IConfiguration config)
{
try
{
HttpClient cfClient = new();
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
var result = await cfClient.GetAsync(@$"https://api.cloudflare.com/client/v4/zones/{config["zoneId"]}/dns_records");
Console.WriteLine(await result.Content.ReadAsStringAsync());
var records = System.Text.Json.JsonSerializer.Deserialize<RecordList>(await result.Content.ReadAsStringAsync());
return records!.result[0];
}
catch
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to grab current ip");
return await GetDnsRecordAsync(config);
}
}
static async Task UpdateDnsIpAsync(IConfiguration config, AAAARecord record, string ip)
{
HttpClient cfClient = new();
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
object content = new
{
comment = "Update as needed",
content = ip,
name = "@",
proxied = true,
ttl = 3600,
type = "AAAA"
};
var result = await cfClient.PutAsJsonAsync(@$"https://api.cloudflare.com/client/v4/zones/{config["zoneId"]}/dns_records{record.id}", content);
if (!result.IsSuccessStatusCode)
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("Reattempting to update ip");
await UpdateDnsIpAsync(config, record, ip);
}
}
record AAAARecord(string comment, string content, string name, string id);
record RecordList(List<AAAARecord> result);