From 824a1d6eab21f6fa49ae600e23a6a68b033f9c5a Mon Sep 17 00:00:00 2001 From: jheaps Date: Tue, 27 May 2025 10:40:27 -0600 Subject: [PATCH] Add debug --- JoshHeaps.Net/Controllers/DebugController.cs | 14 ++ JoshHeaps.Net/JoshHeaps.Net.csproj | 4 +- JoshHeaps.Net/Pages/Debug.cshtml | 15 ++ JoshHeaps.Net/Pages/Debug.cshtml.cs | 11 + JoshHeaps.Net/Program.cs | 240 ++++++++++--------- 5 files changed, 169 insertions(+), 115 deletions(-) create mode 100644 JoshHeaps.Net/Controllers/DebugController.cs create mode 100644 JoshHeaps.Net/Pages/Debug.cshtml create mode 100644 JoshHeaps.Net/Pages/Debug.cshtml.cs diff --git a/JoshHeaps.Net/Controllers/DebugController.cs b/JoshHeaps.Net/Controllers/DebugController.cs new file mode 100644 index 0000000..9ecd387 --- /dev/null +++ b/JoshHeaps.Net/Controllers/DebugController.cs @@ -0,0 +1,14 @@ +using Microsoft.AspNetCore.Mvc; + +namespace JoshHeaps.Net.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class DebugController : ControllerBase +{ + [HttpGet("IpCheck")] + public ActionResult GetIpCheckingStatus() + { + return Ok(Program.CheckingForIpUpdates.ToString()); + } +} diff --git a/JoshHeaps.Net/JoshHeaps.Net.csproj b/JoshHeaps.Net/JoshHeaps.Net.csproj index 765343c..7620e40 100644 --- a/JoshHeaps.Net/JoshHeaps.Net.csproj +++ b/JoshHeaps.Net/JoshHeaps.Net.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -11,7 +11,9 @@ + + diff --git a/JoshHeaps.Net/Pages/Debug.cshtml b/JoshHeaps.Net/Pages/Debug.cshtml new file mode 100644 index 0000000..cadedb5 --- /dev/null +++ b/JoshHeaps.Net/Pages/Debug.cshtml @@ -0,0 +1,15 @@ +@page +@model JoshHeaps.Net.Pages.GameModel +@{ + ViewData["Title"] = "Game"; + Layout = "_Layout"; +} + +
+ +
...
+
+ +@section Scripts { + +} \ No newline at end of file diff --git a/JoshHeaps.Net/Pages/Debug.cshtml.cs b/JoshHeaps.Net/Pages/Debug.cshtml.cs new file mode 100644 index 0000000..20f2078 --- /dev/null +++ b/JoshHeaps.Net/Pages/Debug.cshtml.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace JoshHeaps.Net.Pages +{ + public class DebugModel : PageModel + { + public void OnGet() + { + } + } +} diff --git a/JoshHeaps.Net/Program.cs b/JoshHeaps.Net/Program.cs index 8829e08..9142455 100644 --- a/JoshHeaps.Net/Program.cs +++ b/JoshHeaps.Net/Program.cs @@ -2,147 +2,159 @@ using JoshHeaps.Net.Hubs; using JoshHeaps.Net.Services.Implementations; using JoshHeaps.Net.Services.Interfaces; -var builder = WebApplication.CreateBuilder(args); +namespace JoshHeaps.Net; -// Add services to the container. -builder.Services.AddRazorPages(); -var configuration = builder.Configuration; -Task updateIpTask; - -if (!builder.Environment.IsDevelopment()) - updateIpTask = Run(configuration); - -builder.Services.AddControllers(); - -builder.Services.AddSignalR(); - -builder.Services.AddSingleton(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) +// need to declare the class so I can add the public static bool +public class Program { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); -} + public static bool CheckingForIpUpdates { get; private set; } = false; -app.UseHttpsRedirection(); - -app.UseStaticFiles(new StaticFileOptions -{ - OnPrepareResponse = ctx => + public static void Main(string[] args) { - ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate"); - ctx.Context.Response.Headers.Append("Pragma", "no-cache"); - ctx.Context.Response.Headers.Append("Expires", "0"); + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + builder.Services.AddRazorPages(); + var configuration = builder.Configuration; + Task updateIpTask; + + if (!builder.Environment.IsDevelopment()) + updateIpTask = Run(configuration); + + builder.Services.AddControllers(); + + builder.Services.AddSignalR(); + + builder.Services.AddSingleton(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (!app.Environment.IsDevelopment()) + { + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + app.UseHsts(); + } + + app.UseHttpsRedirection(); + + app.UseStaticFiles(new StaticFileOptions + { + OnPrepareResponse = ctx => + { + ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate"); + ctx.Context.Response.Headers.Append("Pragma", "no-cache"); + ctx.Context.Response.Headers.Append("Expires", "0"); + } + }); + + app.UseRouting(); + + app.UseAuthorization(); + + app.MapRazorPages(); + + app.MapControllers(); + + app.MapHub("/chessHub"); + + app.Run(); } -}); -app.UseRouting(); + static async Task Run(IConfiguration config) + { + CheckingForIpUpdates = true; + HttpClient httpClient = new(); + AAAARecord dnsRecord = await GetDnsRecordAsync(config); + string lastKnownIp = dnsRecord.content; + TimeSpan checkInterval = TimeSpan.FromMinutes(1); -app.UseAuthorization(); + while (true) + { + try + { + string currentIp = await GetPublicIpAsync(httpClient) ?? ""; -app.MapRazorPages(); + if (lastKnownIp != currentIp) + { + await UpdateDnsIpAsync(config, dnsRecord, currentIp); -app.MapControllers(); + lastKnownIp = currentIp; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } -app.MapHub("/chessHub"); + await Task.Delay(checkInterval); + } + } -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) + static async Task GetPublicIpAsync(HttpClient client) { try { - string currentIp = await GetPublicIpAsync(httpClient) ?? ""; - - if (lastKnownIp != currentIp) - { - await UpdateDnsIpAsync(config, dnsRecord, currentIp); - - lastKnownIp = currentIp; - } + return await client.GetStringAsync(@"https://api.ipify.org/"); } - catch (Exception ex) + catch { - Console.WriteLine(ex.Message); + await Task.Delay(TimeSpan.FromSeconds(10)); + Console.WriteLine("Reattempting to grab public ip"); + + return await GetPublicIpAsync(client); } - - await Task.Delay(checkInterval); } -} -static async Task GetPublicIpAsync(HttpClient client) -{ - try + static async Task GetDnsRecordAsync(IConfiguration config) { - return await client.GetStringAsync(@"https://api.ipify.org/"); - } - catch - { - await Task.Delay(TimeSpan.FromSeconds(10)); - Console.WriteLine("Reattempting to grab public ip"); + 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 GetPublicIpAsync(client); + return await GetDnsRecordAsync(config); + } } -} -static async Task GetDnsRecordAsync(IConfiguration config) -{ - try + 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"]); - 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"); + object content = new + { + comment = "Update as needed", + content = ip, + name = "@", + proxied = true, + ttl = 3600, + type = "AAAA" + }; - return await GetDnsRecordAsync(config); + 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); } - -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