mirror of
https://github.com/eliasstepanik/IonosDDNSUpdater.git
synced 2026-01-10 11:08:27 +00:00
Added Structure to Code.
Added automatic request of the UpdateURL.
This commit is contained in:
commit
e5e1dff331
16
DDNSUpdater.sln
Normal file
16
DDNSUpdater.sln
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DDNSUpdater", "DDNSUpdater\DDNSUpdater.csproj", "{EDEDF642-4CFC-4EEB-A8F2-3872DBEC63E3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{EDEDF642-4CFC-4EEB-A8F2-3872DBEC63E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EDEDF642-4CFC-4EEB-A8F2-3872DBEC63E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EDEDF642-4CFC-4EEB-A8F2-3872DBEC63E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EDEDF642-4CFC-4EEB-A8F2-3872DBEC63E3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
33
DDNSUpdater/DDNSUpdater.csproj
Normal file
33
DDNSUpdater/DDNSUpdater.csproj
Normal file
@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\.dockerignore">
|
||||
<Link>.dockerignore</Link>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||
<PackageReference Include="RestSharp" Version="108.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
DDNSUpdater/Interfaces/IDDNSService.cs
Normal file
8
DDNSUpdater/Interfaces/IDDNSService.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace DDNSUpdater.Interfaces;
|
||||
|
||||
public interface IDDNSService
|
||||
{
|
||||
public void Start();
|
||||
public void Update();
|
||||
public void SetUpdateURL();
|
||||
}
|
||||
6
DDNSUpdater/Interfaces/ITimerService.cs
Normal file
6
DDNSUpdater/Interfaces/ITimerService.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace DDNSUpdater.Interfaces;
|
||||
|
||||
public interface ITimerService
|
||||
{
|
||||
void Start();
|
||||
}
|
||||
12
DDNSUpdater/Models/Requests/DynamicDns.cs
Normal file
12
DDNSUpdater/Models/Requests/DynamicDns.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DDNSUpdater.Models.Requests;
|
||||
|
||||
public class DynamicDns
|
||||
{
|
||||
[JsonProperty("domains")]
|
||||
public List<string> Domains { get; set; }
|
||||
|
||||
[JsonProperty("description")]
|
||||
public string Description { get; set; }
|
||||
}
|
||||
9
DDNSUpdater/Models/Requests/DynamicDnsResponse.cs
Normal file
9
DDNSUpdater/Models/Requests/DynamicDnsResponse.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace DDNSUpdater.Models.Requests;
|
||||
|
||||
public class DynamicDnsResponse
|
||||
{
|
||||
public string BulkId { get; set; }
|
||||
public string UpdateUrl { get; set; }
|
||||
public List<string> Domains { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
27
DDNSUpdater/Program.cs
Normal file
27
DDNSUpdater/Program.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using DDNSUpdater.Interfaces;
|
||||
using DDNSUpdater.Services;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RestSharp;
|
||||
|
||||
var builder = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);
|
||||
|
||||
var configuration = builder.Build();
|
||||
|
||||
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddSingleton<IConfiguration>(configuration)
|
||||
.AddLogging(logging => logging.AddConsole())
|
||||
.AddSingleton<ITimerService, TimerService>()
|
||||
.AddSingleton<DDNSService>()
|
||||
.BuildServiceProvider();
|
||||
|
||||
var dataAccess = serviceProvider.GetService<DDNSService>();
|
||||
dataAccess.Start();
|
||||
|
||||
var timerService = serviceProvider.GetService<ITimerService>();
|
||||
timerService.Start();
|
||||
|
||||
Console.ReadKey();
|
||||
87
DDNSUpdater/Services/DDNSService.cs
Normal file
87
DDNSUpdater/Services/DDNSService.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System.Net;
|
||||
using DDNSUpdater.Interfaces;
|
||||
using DDNSUpdater.Models.Requests;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using RestSharp;
|
||||
using RestSharp.Serializers;
|
||||
|
||||
namespace DDNSUpdater.Services;
|
||||
|
||||
public class DDNSService : IDDNSService
|
||||
{
|
||||
private string UpdateURL { get; set; }
|
||||
public string APIKey { get; set; }
|
||||
public List<string> Domains { get; set; }
|
||||
|
||||
private readonly ILogger<DDNSService> _logger;
|
||||
|
||||
public DDNSService(ILogger<DDNSService> logger,IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
APIKey = configuration.GetValue<string>("APIKey");
|
||||
Domains = configuration.GetSection("Domains").Get<List<string>>();
|
||||
}
|
||||
|
||||
public async void Start()
|
||||
{
|
||||
UpdateURL = await GetUpdateURL();
|
||||
_logger.LogInformation("Got new Update URL: " + UpdateURL);
|
||||
}
|
||||
|
||||
public async void Update()
|
||||
{
|
||||
var client = new RestClient(UpdateURL);
|
||||
var request = new RestRequest("",Method.Get);
|
||||
request.AddHeader("Cookie", "0b04270753322c986927738ac2b6c0d8=ea099cbd8a6109c688f9831d6bbfa7a1; 5b66c83e4535f5f6bef8295496cfe559=e85228fccae97f107478bf9ef664e4eb; DPX=v1:ghOJrOzFTj:htgOaKFW:63d3bf8f:de");
|
||||
var body = @"";
|
||||
request.AddParameter("text/plain", body, ParameterType.RequestBody);
|
||||
|
||||
try
|
||||
{
|
||||
var response = await client.ExecuteAsync(request);
|
||||
_logger.LogInformation("Send Update to Ionos");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async void SetUpdateURL()
|
||||
{
|
||||
UpdateURL = await GetUpdateURL();
|
||||
}
|
||||
|
||||
private async Task<string> GetUpdateURL()
|
||||
{
|
||||
var dyndns = new DynamicDns()
|
||||
{
|
||||
Domains = Domains,
|
||||
Description = "My DynamicDns"
|
||||
};
|
||||
var content = JsonConvert.SerializeObject(dyndns);
|
||||
var client = new RestClient("https://api.hosting.ionos.com/dns/v1");
|
||||
var request = new RestRequest("/dyndns", Method.Post);
|
||||
|
||||
|
||||
request.AddHeader("X-API-Key", APIKey);
|
||||
|
||||
request.AddStringBody(content, ContentType.Json);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var response = client.ExecutePost<DynamicDnsResponse>(request);
|
||||
return response.Data.UpdateUrl;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
_logger.LogError(error.Message);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
36
DDNSUpdater/Services/TimerService.cs
Normal file
36
DDNSUpdater/Services/TimerService.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using DDNSUpdater.Interfaces;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DDNSUpdater.Services;
|
||||
|
||||
public class TimerService : ITimerService
|
||||
{
|
||||
private Timer timer;
|
||||
private readonly ILogger<TimerService> _logger;
|
||||
private readonly IServiceScopeFactory _factory;
|
||||
private readonly int intervalMinutes;
|
||||
|
||||
public TimerService(ILogger<TimerService> logger,IServiceScopeFactory factory, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
_factory = factory;
|
||||
intervalMinutes = configuration.GetValue<int>("TimerIntervalMinutes");
|
||||
timer = new Timer(TimerCallback, null, TimeSpan.Zero, TimeSpan.FromMinutes(intervalMinutes));
|
||||
}
|
||||
|
||||
private async void TimerCallback(Object o)
|
||||
{
|
||||
_logger.LogDebug("Timer callback executed at " + DateTime.Now);
|
||||
await using var asyncScope = _factory.CreateAsyncScope();
|
||||
var ddnsService = asyncScope.ServiceProvider.GetRequiredService<DDNSService>();
|
||||
|
||||
ddnsService.Update();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_logger.LogInformation("Timer service started.");
|
||||
}
|
||||
}
|
||||
14
DDNSUpdater/appsettings.json
Normal file
14
DDNSUpdater/appsettings.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug"
|
||||
}
|
||||
},
|
||||
"TimerIntervalMinutes": 1,
|
||||
"APIKey": "4dc281058e9648919a988315c84058fa.z0eKvfJSuUpeU-2W-quUCsM_6aSshAX8tdPrJ1NQUBtcaImOtoQCk82nT4kDWzBjj2l2PMo1vGXCc6vGW9bKHA",
|
||||
"Domains": [
|
||||
"*.sailehd.de",
|
||||
"*.sailehd.dev",
|
||||
"*.sailehd.com"
|
||||
]
|
||||
}
|
||||
18
Dockerfile
Normal file
18
Dockerfile
Normal file
@ -0,0 +1,18 @@
|
||||
FROM mcr.microsoft.com/dotnet/runtime:7.0 AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
|
||||
WORKDIR /src
|
||||
COPY ["DDNSUpdater/DDNSUpdater.csproj", "DDNSUpdater/"]
|
||||
RUN dotnet restore "DDNSUpdater/DDNSUpdater.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/DDNSUpdater"
|
||||
RUN dotnet build "DDNSUpdater.csproj" -c Release -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "DDNSUpdater.csproj" -c Release -o /app/publish
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "DDNSUpdater.dll"]
|
||||
Loading…
x
Reference in New Issue
Block a user