using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Mime; using System.Security.Authentication; using System.Threading.Tasks; using DDNSUpdater.APIs.Ionos; using DDNSUpdater.APIs.Ionos.ApiClient; using DDNSUpdater.APIs.Ionos.ApiClient.Models; using DDNSUpdater.Interfaces; using DDNSUpdater.Logging; using DDNSUpdater.Models; using DDNSUpdater.Models.Requests; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Kiota.Abstractions; using Microsoft.Kiota.Abstractions.Authentication; using Microsoft.Kiota.Http.HttpClientLibrary; using Newtonsoft.Json; using RestSharp; using Spectre.Console; using Console = Spectre.Console.AnsiConsole; using ContentType = RestSharp.ContentType; using DynamicDns = DDNSUpdater.Models.Requests.DynamicDns; using Method = RestSharp.Method; namespace DDNSUpdater.Services; public class DDNSService : IDDNSService { private List? UpdateURLs { get; set; } public List Domains { get; set; } private readonly ILogger _logger; private IonosAPIClient _client; public DDNSService(ILogger logger,IConfiguration configuration) { _logger = logger; Domains = new List(); foreach (DictionaryEntry de in Environment.GetEnvironmentVariables()) { if (de.Key.ToString().ToLower().Contains("domain-")) { // domain;key var env = de.Value.ToString().Split(";").ToList(); Domains.Add(new Domain(env[0], env[1])); } } var authProvider = new AnonymousAuthenticationProvider(); var requestAdapter = new HttpClientRequestAdapter(authProvider); _client = new IonosAPIClient(requestAdapter); } public async void Start() { _logger.LogInformation("Fetching UpdateURLs"); UpdateURLs = await GetUpdateURLs(); while (UpdateURLs == null || UpdateURLs.Count == 0 ) { UpdateURLs = await GetUpdateURLs(); } _logger.LogInformation($"Fetched {UpdateURLs.Count} UpdateURLs"); } public async void Update() { if(UpdateURLs != null && UpdateURLs.Count != 0) foreach (var UpdateURL in UpdateURLs) { try { 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); var response = await client.ExecuteAsync(request); _logger.LogInformation("Requesting Update on Ionos."); } catch (ApiException e) { _logger.LogError(e.Message); throw; } } } public async void SetUpdateURL() { UpdateURLs = await GetUpdateURLs(); } private async Task> GetUpdateURLs() { List updateURLs = new List(); Dictionary> domainDict = new Dictionary>(); Dictionary tables = new Dictionary(); foreach (var domain in Domains) { if (!domainDict.ContainsKey(domain.Key)) { domainDict.Add(domain.Key, new List()); var t = new Table(); t.AddColumn("Key"); t.AddColumn("Domain"); tables.Add(domain.Key,t); } domainDict[domain.Key].Add(domain.DomainString); tables[domain.Key].AddRow(domain.Key, domain.DomainString); } foreach (var keyValuePair in tables) { Console.Write(tables[keyValuePair.Key]); } foreach (var (key, value) in domainDict) { try { var request = new DynDnsRequest(); request.Domains = value; request.Description = "My DynamicDns"; var reply = await _client.V1.Dyndns.PostAsync(request, configuration => { configuration.Headers = new RequestHeaders() { { "X-API-Key", key } }; }); updateURLs.Add(reply.UpdateUrl); } catch (ApiException error) { string message = error.Message; _logger.LogError(message); return null; } } return updateURLs; } }