diff --git a/JoshHeaps.Net/JoshHeaps.Net.csproj b/JoshHeaps.Net/JoshHeaps.Net.csproj
index fdf2f26..765343c 100644
--- a/JoshHeaps.Net/JoshHeaps.Net.csproj
+++ b/JoshHeaps.Net/JoshHeaps.Net.csproj
@@ -6,6 +6,10 @@
enable
+
+
+
+
diff --git a/JoshHeaps.Net/Program.cs b/JoshHeaps.Net/Program.cs
index 7a794f5..4f34166 100644
--- a/JoshHeaps.Net/Program.cs
+++ b/JoshHeaps.Net/Program.cs
@@ -6,6 +6,8 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
+var configuration = builder.Configuration;
+_ = Run(configuration);
builder.Services.AddControllers();
@@ -46,3 +48,98 @@ app.MapControllers();
app.MapHub("/chessHub");
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 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 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(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 result);
\ No newline at end of file
diff --git a/JoshHeaps.Net/Properties/launchSettings.json b/JoshHeaps.Net/Properties/launchSettings.json
index 56f9bfc..99ea902 100644
--- a/JoshHeaps.Net/Properties/launchSettings.json
+++ b/JoshHeaps.Net/Properties/launchSettings.json
@@ -1,38 +1,38 @@
{
- "$schema": "http://json.schemastore.org/launchsettings.json",
- "iisSettings": {
- "windowsAuthentication": false,
- "anonymousAuthentication": true,
- "iisExpress": {
- "applicationUrl": "http://localhost:4706",
- "sslPort": 44345
- }
- },
- "profiles": {
- "http": {
- "commandName": "Project",
- "dotnetRunMessages": true,
- "launchBrowser": true,
- "applicationUrl": "http://localhost:5200",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
+ "$schema": "http://json.schemastore.org/launchsettings.json",
+ "iisSettings": {
+ "windowsAuthentication": false,
+ "anonymousAuthentication": true,
+ "iisExpress": {
+ "applicationUrl": "http://localhost:4706",
+ "sslPort": 44345
+ }
},
- "https": {
- "commandName": "Project",
- "dotnetRunMessages": true,
- "launchBrowser": true,
- "applicationUrl": "https://localhost:7118;http://localhost:5200",
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
- },
- "IIS Express": {
- "commandName": "IISExpress",
- "launchBrowser": true,
- "environmentVariables": {
- "ASPNETCORE_ENVIRONMENT": "Development"
- }
+ "profiles": {
+ "http": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "http://localhost:5200",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "https": {
+ "commandName": "Project",
+ "dotnetRunMessages": true,
+ "launchBrowser": true,
+ "applicationUrl": "https://localhost:7118;http://localhost:5200",
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ },
+ "IIS Express": {
+ "commandName": "IISExpress",
+ "launchBrowser": true,
+ "environmentVariables": {
+ "ASPNETCORE_ENVIRONMENT": "Development"
+ }
+ }
}
- }
}