From c763ca4fcb719037295d98eaf34a18d589b8768e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= Date: Wed, 24 Jan 2024 19:13:14 +0800 Subject: [PATCH] ? --- src/Program.cs | 128 ++++++++++++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 55 deletions(-) diff --git a/src/Program.cs b/src/Program.cs index fa7ae65..e3f3fa6 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Hosting; using System.Text.Encodings.Web; using System.Text.Json; +using Tea; HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); builder.Configuration.Sources.Clear(); @@ -29,77 +30,94 @@ }); while (true) { - using HttpClient httpClient = new(); - string? ipv4 = default; - string? ipv6 = default; try { - string response = await httpClient.GetStringAsync("https://ipv4.test-ipv6.com/ip/"); - string dataString = Regex.DataRegex().Match(response).Groups[1].Value; - Data? data = JsonSerializer.Deserialize(dataString); - ipv4 = data!.Ip; - } - catch (Exception ex) when (ex is NullReferenceException or HttpRequestException) - { - } + using HttpClient httpClient = new(); + string? ipv4 = default; + string? ipv6 = default; + try + { + string response = await httpClient.GetStringAsync("https://ipv4.test-ipv6.com/ip/"); + string dataString = Regex.DataRegex().Match(response).Groups[1].Value; + Data? data = JsonSerializer.Deserialize(dataString); + ipv4 = data!.Ip; + } + catch (Exception ex) when (ex is NullReferenceException or HttpRequestException) + { + } - try - { - string response = await httpClient.GetStringAsync("https://ipv6.test-ipv6.com/ip/"); - string dataString = Regex.DataRegex().Match(response).Groups[1].Value; - Data? data = JsonSerializer.Deserialize(dataString); - ipv6 = data!.Ip; - } - catch (Exception ex) when (ex is NullReferenceException or HttpRequestException) - { - } + try + { + string response = await httpClient.GetStringAsync("https://ipv6.test-ipv6.com/ip/"); + string dataString = Regex.DataRegex().Match(response).Groups[1].Value; + Data? data = JsonSerializer.Deserialize(dataString); + ipv6 = data!.Ip; + } + catch (Exception ex) when (ex is NullReferenceException or HttpRequestException) + { + } - foreach (Domain domain in config.Domains) - { - foreach (string subDomain in domain.SubDomains) + foreach (Domain domain in config.Domains) { - DescribeDomainRecordsRequest request = new() - { - DomainName = domain.Name, - RRKeyWord = subDomain - }; - DescribeDomainRecordsResponse response = await client.DescribeDomainRecordsAsync(request); - foreach (DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecords. - DescribeDomainRecordsResponseBodyDomainRecordsRecord record in response.Body.DomainRecords - .Record) + foreach (string subDomain in domain.SubDomains) { - UpdateDomainRecordRequest updateRequest = new() + DescribeDomainRecordsRequest request = new() { - RecordId = record.RecordId, - RR = record.RR, - Type = record.Type + DomainName = domain.Name, + RRKeyWord = subDomain }; - switch (record.Type) + DescribeDomainRecordsResponse response = await client.DescribeDomainRecordsAsync(request); + foreach (DescribeDomainRecordsResponseBody.DescribeDomainRecordsResponseBodyDomainRecords. + DescribeDomainRecordsResponseBodyDomainRecordsRecord record in response.Body.DomainRecords + .Record) { - case "A": - if (!string.IsNullOrWhiteSpace(ipv4) || record.Value == ipv4) - { - continue; - } + UpdateDomainRecordRequest updateRequest = new() + { + Line = record.Line, + Priority = record.Priority, + RR = record.RR, + RecordId = record.RecordId, + TTL = record.TTL, + Type = record.Type + }; + switch (record.Type) + { + case "A": + if (!string.IsNullOrWhiteSpace(ipv4) || record.Value == ipv4) + { + continue; + } + + updateRequest.Value = ipv4; + break; + case "AAAA": + if (!string.IsNullOrWhiteSpace(ipv6) || record.Value == ipv6) + { + continue; + } - updateRequest.Value = ipv4; - break; - case "AAAA": - if (!string.IsNullOrWhiteSpace(ipv6) || record.Value == ipv6) - { + updateRequest.Value = ipv6; + break; + default: continue; - } + } - updateRequest.Value = ipv6; - break; - default: - continue; + try + { + await client.UpdateDomainRecordAsync(updateRequest); + } + catch (TeaException) + { + } } - - await client.UpdateDomainRecordAsync(updateRequest); } } } + catch (Exception ex) + { + Directory.CreateDirectory("logs"); + File.AppendAllText($"logs/{DateTime.Now:yy-MM-ddTHH-mm-ss}", ex.ToString()); + } await Task.Delay(config.Interval); } \ No newline at end of file