mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 14:16:56 +08:00
将项目从 Fluent UI 迁移到 Ant Design Blazor: - 替换 UI 组件,更新样式和脚本。 - 移除 Fluent UI 相关依赖,添加 Ant Design 依赖。 优化 DNS 解析接口: - 将解析接口从 POST 改为 GET,简化参数结构。 - 更新 `DnsResponse` 和配置文件结构。 数据库模型优化: - 为 JSON 数据存储添加 `JArray` 映射支持。 初始化逻辑重构: - 简化 `AutoInit` 初始化逻辑,提升代码可读性。 其他改动: - 升级依赖包版本。 - 移除无用命名空间和属性。
119 lines
3.9 KiB
C#
119 lines
3.9 KiB
C#
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
|
|
{
|
|
public class DnsQuestion
|
|
{
|
|
public string Name { get; set; }
|
|
public int type { get; set; }
|
|
}
|
|
[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 DnsQuestion Question { get; set; }
|
|
|
|
[JsonPropertyName("Answer")]
|
|
public List<DnsRecord>? Answer { get; set; }
|
|
}
|
|
|
|
[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();
|
|
}
|
|
|
|
[HttpGet("resolve-dns")]
|
|
public async Task<IActionResult> ResolveDns([FromQuery] string domain, [FromQuery] string recordType = "A")
|
|
{
|
|
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, domain, akId);
|
|
|
|
var url = $"{endpoint}?name={Uri.EscapeDataString(domain)}&type={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<DnsResponse>(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)
|
|
{
|
|
var headers = Request.Headers.Select(h => $"{h.Key}: {h.Value}");
|
|
if (order == true)
|
|
{
|
|
headers = headers.OrderBy(h => h);
|
|
}
|
|
return Ok(string.Join(Environment.NewLine, headers));
|
|
}
|
|
}
|
|
}
|