Add debug
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace JoshHeaps.Net.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class DebugController : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpGet("IpCheck")]
|
||||||
|
public ActionResult<string> GetIpCheckingStatus()
|
||||||
|
{
|
||||||
|
return Ok(Program.CheckingForIpUpdates.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
@@ -11,7 +11,9 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="wwwroot\css\debug\" />
|
||||||
<Folder Include="wwwroot\images\Chess Images\" />
|
<Folder Include="wwwroot\images\Chess Images\" />
|
||||||
|
<Folder Include="wwwroot\images\Game Images\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
@page
|
||||||
|
@model JoshHeaps.Net.Pages.GameModel
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Game";
|
||||||
|
Layout = "_Layout";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div id="IpChecking">
|
||||||
|
<label id="IpCheckingLabel">Is checking for ip updates:</label>
|
||||||
|
<div id="IsIpChecking">...</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@section Scripts {
|
||||||
|
<script src="~/js/Debug/debug.js"></script>
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
|
|
||||||
|
namespace JoshHeaps.Net.Pages
|
||||||
|
{
|
||||||
|
public class DebugModel : PageModel
|
||||||
|
{
|
||||||
|
public void OnGet()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+126
-114
@@ -2,147 +2,159 @@ using JoshHeaps.Net.Hubs;
|
|||||||
using JoshHeaps.Net.Services.Implementations;
|
using JoshHeaps.Net.Services.Implementations;
|
||||||
using JoshHeaps.Net.Services.Interfaces;
|
using JoshHeaps.Net.Services.Interfaces;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
namespace JoshHeaps.Net;
|
||||||
|
|
||||||
// Add services to the container.
|
// need to declare the class so I can add the public static bool
|
||||||
builder.Services.AddRazorPages();
|
public class Program
|
||||||
var configuration = builder.Configuration;
|
|
||||||
Task updateIpTask;
|
|
||||||
|
|
||||||
if (!builder.Environment.IsDevelopment())
|
|
||||||
updateIpTask = Run(configuration);
|
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
|
||||||
|
|
||||||
builder.Services.AddSignalR();
|
|
||||||
|
|
||||||
builder.Services.AddSingleton<IChessService, ChessService>();
|
|
||||||
|
|
||||||
var app = builder.Build();
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
|
||||||
if (!app.Environment.IsDevelopment())
|
|
||||||
{
|
{
|
||||||
app.UseExceptionHandler("/Error");
|
public static bool CheckingForIpUpdates { get; private set; } = false;
|
||||||
// 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();
|
public static void Main(string[] args)
|
||||||
|
|
||||||
app.UseStaticFiles(new StaticFileOptions
|
|
||||||
{
|
|
||||||
OnPrepareResponse = ctx =>
|
|
||||||
{
|
{
|
||||||
ctx.Context.Response.Headers.Append("Cache-Control", "no-cache, no-store, must-revalidate");
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
ctx.Context.Response.Headers.Append("Pragma", "no-cache");
|
|
||||||
ctx.Context.Response.Headers.Append("Expires", "0");
|
// 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<IChessService, ChessService>();
|
||||||
|
|
||||||
|
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>("/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>("/chessHub");
|
await Task.Delay(checkInterval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
app.Run();
|
static async Task<string> GetPublicIpAsync(HttpClient client)
|
||||||
|
|
||||||
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
|
try
|
||||||
{
|
{
|
||||||
string currentIp = await GetPublicIpAsync(httpClient) ?? "";
|
return await client.GetStringAsync(@"https://api.ipify.org/");
|
||||||
|
|
||||||
if (lastKnownIp != currentIp)
|
|
||||||
{
|
|
||||||
await UpdateDnsIpAsync(config, dnsRecord, currentIp);
|
|
||||||
|
|
||||||
lastKnownIp = currentIp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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<string> GetPublicIpAsync(HttpClient client)
|
static async Task<AAAARecord> GetDnsRecordAsync(IConfiguration config)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
return await client.GetStringAsync(@"https://api.ipify.org/");
|
try
|
||||||
}
|
{
|
||||||
catch
|
HttpClient cfClient = new();
|
||||||
{
|
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
|
||||||
await Task.Delay(TimeSpan.FromSeconds(10));
|
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
|
||||||
Console.WriteLine("Reattempting to grab public ip");
|
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 GetPublicIpAsync(client);
|
return await GetDnsRecordAsync(config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static async Task<AAAARecord> GetDnsRecordAsync(IConfiguration config)
|
static async Task UpdateDnsIpAsync(IConfiguration config, AAAARecord record, string ip)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
HttpClient cfClient = new();
|
HttpClient cfClient = new();
|
||||||
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
|
cfClient.DefaultRequestHeaders.Add("X-Auth-Email", config["cfEmail"]);
|
||||||
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
|
cfClient.DefaultRequestHeaders.Add("X-Auth-Key", config["cfKey"]);
|
||||||
var result = await cfClient.GetAsync(@$"https://api.cloudflare.com/client/v4/zones/{config["zoneId"]}/dns_records");
|
object content = new
|
||||||
Console.WriteLine(await result.Content.ReadAsStringAsync());
|
{
|
||||||
var records = System.Text.Json.JsonSerializer.Deserialize<RecordList>(await result.Content.ReadAsStringAsync());
|
comment = "Update as needed",
|
||||||
return records!.result[0];
|
content = ip,
|
||||||
}
|
name = "@",
|
||||||
catch
|
proxied = true,
|
||||||
{
|
ttl = 3600,
|
||||||
await Task.Delay(TimeSpan.FromSeconds(10));
|
type = "AAAA"
|
||||||
Console.WriteLine("Reattempting to grab current ip");
|
};
|
||||||
|
|
||||||
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<AAAARecord> 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<AAAARecord> result);
|
|
||||||
Reference in New Issue
Block a user