43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using VirtualDDNSRouter.Client.Helpers;
|
|
|
|
var settings = await Helpers.GetSettings();
|
|
|
|
using var cts = new CancellationTokenSource();
|
|
Console.CancelKeyPress += (s, e) =>
|
|
{
|
|
Console.WriteLine("Shutdown requested…");
|
|
e.Cancel = true;
|
|
cts.Cancel();
|
|
};
|
|
|
|
var timer = new PeriodicTimer(TimeSpan.FromMinutes(settings.refreshIntervalMinutes));
|
|
|
|
var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
|
|
var builder = new UriBuilder
|
|
{
|
|
Scheme = "http",
|
|
Host = settings.host,
|
|
Path = $"setip/{settings.path}/{settings.destinationPort}/{settings.apiKey}"
|
|
};
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, builder.Uri);
|
|
|
|
Console.WriteLine("[INFO] App started. Scheduling IP address update every " + settings.refreshIntervalMinutes +
|
|
" minutes.");
|
|
|
|
try
|
|
{
|
|
while (await timer.WaitForNextTickAsync(cts.Token))
|
|
{
|
|
if (cts.IsCancellationRequested) break;
|
|
var response = await client.SendAsync(request, cts.Token);
|
|
if (response.IsSuccessStatusCode)
|
|
Console.WriteLine("[INFO] IP address updated at " + DateTime.Now.ToString("HH:mm:ss"));
|
|
else
|
|
Console.WriteLine("[ERROR] IP address update failed at " + DateTime.Now.ToString("HH:mm:ss"));
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
Console.WriteLine("[INFO] Shutdown complete.");
|
|
} |