diff --git a/.gitignore b/.gitignore index 9491a2f..eaa9902 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,4 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd diff --git a/Config.cs b/Config.cs index 9fdfed1..d6895eb 100644 --- a/Config.cs +++ b/Config.cs @@ -1,4 +1,4 @@ -using System.Text.Encodings.Web; +using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; @@ -63,5 +63,11 @@ namespace MegghysAPI public string MinIOSecretKey { get; set; } = "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq"; public string MinIORegion { get; set; } = "cn-main"; public string MinIOBucket { get; set; } = "general"; + + // DNS解析配置 + public string DnsResolverAccountId { get; set; } = "720341"; + public string DnsResolverAkId { get; set; } = "720341_30798028364391424"; + public string DnsResolverAkSecret { get; set; } = "0739b7584ab54c1b9585f10594af0cd0"; + public string DnsResolverEndpoint { get; set; } = "https://223.5.5.5/resolve"; } } diff --git a/Controllers/PublicController.cs b/Controllers/PublicController.cs index 5a79cbd..9a82fc8 100644 --- a/Controllers/PublicController.cs +++ b/Controllers/PublicController.cs @@ -1,11 +1,109 @@ -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Serialization; namespace MegghysAPI.Controllers { + public class DnsRecord + { + [JsonPropertyName("name")] + public string Name { get; set; } = string.Empty; + + [JsonPropertyName("type")] + public int Type { get; set; } + + [JsonPropertyName("TTL")] + public int Ttl { get; set; } + + [JsonPropertyName("data")] + public string Data { get; set; } = string.Empty; + } + + public class DnsResponse + { + [JsonPropertyName("Status")] + public int Status { get; set; } + + [JsonPropertyName("TC")] + public bool Tc { get; set; } + + [JsonPropertyName("RD")] + public bool Rd { get; set; } + + [JsonPropertyName("RA")] + public bool Ra { get; set; } + + [JsonPropertyName("AD")] + public bool Ad { get; set; } + + [JsonPropertyName("CD")] + public bool Cd { get; set; } + + [JsonPropertyName("Question")] + public List> Question { get; set; } = new(); + + [JsonPropertyName("Answer")] + public List? Answer { get; set; } + } + + public class DnsRequest + { + public string Domain { get; set; } = string.Empty; + public string RecordType { get; set; } = "A"; + } [Route("api/public")] [ApiController] public class PublicController : MControllerBase { + private static readonly HttpClient _httpClient = new(); + + private static string CalculateKey(string accountId, string akSecret, long timestamp, string qname, string akId) + { + var input = $"{accountId}{akSecret}{timestamp}{qname}{akId}"; + using var sha256 = SHA256.Create(); + var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input)); + return BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); + } + + [HttpPost("resolve-dns")] + public static async Task ResolveDns([FromBody] DnsRequest request) + { + try + { + var config = Config.Instance; + var accountId = config.DnsResolverAccountId; + var akId = config.DnsResolverAkId; + var akSecret = config.DnsResolverAkSecret; + var endpoint = config.DnsResolverEndpoint; + + if (string.IsNullOrEmpty(accountId) || string.IsNullOrEmpty(akId) || string.IsNullOrEmpty(akSecret) || string.IsNullOrEmpty(endpoint)) + { + return new BadRequestObjectResult("DNS解析配置不完整。"); + } + + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + var key = CalculateKey(accountId, akSecret, timestamp, request.Domain, akId); + + var url = $"{endpoint}?name={Uri.EscapeDataString(request.Domain)}&type={request.RecordType}&uid={accountId}&ak={akId}&key={key}&ts={timestamp}"; + + Logs.Info($"正在向 {url} 发起DNS请求"); + + var response = await _httpClient.GetAsync(url); + response.EnsureSuccessStatusCode(); + + var content = await response.Content.ReadAsStringAsync(); + var dnsResponse = JsonSerializer.Deserialize(content); + + return new OkObjectResult(dnsResponse); + } + catch (Exception ex) + { + Logs.Error($"解析DNS记录时出错: {ex.Message}"); + return new StatusCodeResult(500); + } + } [HttpGet("header")] public IActionResult Header(bool? order = false) { diff --git a/Data/Config.json b/Data/Config.json index ca98819..ebf1cad 100644 --- a/Data/Config.json +++ b/Data/Config.json @@ -7,5 +7,11 @@ "MinIOAccessKey": "RBzbElm21lf7sy7wK7wG", "MinIOSecretKey": "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq", "MinIORegion": "cn-main", - "MinIOBucket": "general" + "MinIOBucket": "general", + "DnsResolver": { + "AccountId": "720341", + "AkId": "720341_30798028364391424", + "AkSecret": "0739b7584ab54c1b9585f10594af0cd0", + "Endpoint": "https://223.5.5.5/resolve" + } } \ No newline at end of file