mirror of
https://github.com/Megghy/MegghysAPI.git
synced 2025-12-06 14:16:56 +08:00
feat: 添加DNS解析功能及相关配置项
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Unicode;
|
using System.Text.Unicode;
|
||||||
|
|
||||||
@@ -63,5 +63,11 @@ namespace MegghysAPI
|
|||||||
public string MinIOSecretKey { get; set; } = "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq";
|
public string MinIOSecretKey { get; set; } = "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq";
|
||||||
public string MinIORegion { get; set; } = "cn-main";
|
public string MinIORegion { get; set; } = "cn-main";
|
||||||
public string MinIOBucket { get; set; } = "general";
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
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")]
|
[Route("api/public")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class PublicController : MControllerBase
|
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")]
|
[HttpGet("header")]
|
||||||
public IActionResult Header(bool? order = false)
|
public IActionResult Header(bool? order = false)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,5 +7,11 @@
|
|||||||
"MinIOAccessKey": "RBzbElm21lf7sy7wK7wG",
|
"MinIOAccessKey": "RBzbElm21lf7sy7wK7wG",
|
||||||
"MinIOSecretKey": "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq",
|
"MinIOSecretKey": "Nko5azOSUiYgOUeLsj8hLxGz4cKC8XOcH0VS7lWq",
|
||||||
"MinIORegion": "cn-main",
|
"MinIORegion": "cn-main",
|
||||||
"MinIOBucket": "general"
|
"MinIOBucket": "general",
|
||||||
|
"DnsResolver": {
|
||||||
|
"AccountId": "720341",
|
||||||
|
"AkId": "720341_30798028364391424",
|
||||||
|
"AkSecret": "0739b7584ab54c1b9585f10594af0cd0",
|
||||||
|
"Endpoint": "https://223.5.5.5/resolve"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user