mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 22:26:56 +08:00
119 lines
4.0 KiB
C#
119 lines
4.0 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
|
|
{
|
|
[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<Dictionary<string, object>> Question { get; set; } = new();
|
|
|
|
[JsonPropertyName("Answer")]
|
|
public List<DnsRecord>? 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<IActionResult> 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<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));
|
|
}
|
|
}
|
|
}
|