From ba4049e4526083a9552ab14d961c757b3fea0fe2 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Fri, 22 Nov 2024 19:18:05 +0800 Subject: [PATCH 1/2] add dist cdn --- .../Controller/DistributionController.cs | 31 +++++++++++++ .../Snap.Hutao.Server/Option/AppOptions.cs | 2 + .../Snap.Hutao.Server/Program.cs | 2 + .../Distribution/DistributionService.cs | 44 +++++++++++++++++++ .../Distribution/HutaoPackageMirror.cs | 16 +++++++ 5 files changed, 95 insertions(+) create mode 100644 src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs create mode 100644 src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs create mode 100644 src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/HutaoPackageMirror.cs diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs new file mode 100644 index 0000000..dfc3077 --- /dev/null +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs @@ -0,0 +1,31 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Server.Controller.Filter; +using Snap.Hutao.Server.Model.Response; +using Snap.Hutao.Server.Service.Distribution; + +namespace Snap.Hutao.Server.Controller; + +[ApiController] +[Route("[controller]")] +[ServiceFilter(typeof(CountRequests))] +[ApiExplorerSettings(GroupName = "Distribution")] +public class DistributionController : ControllerBase +{ + private readonly DistributionService distributionService; + + public DistributionController(DistributionService distributionService) + { + this.distributionService = distributionService; + } + + [Authorize] + [ServiceFilter(typeof(ValidateGachaLogPermission))] + [HttpGet("GetAccMirror")] + public async Task GetAcceleratedMirror([FromQuery(Name = "Filename")] string filename) + { + HutaoPackageMirror? mirror = await distributionService.GetAcceleratedMirrorAsync(filename); + return Response.Success("获取加速镜像成功", mirror); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs index 3c3fc13..a9c973d 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs @@ -15,6 +15,8 @@ public sealed class AppOptions public string RSAPrivateKey { get; set; } = default!; + public string CDNToken { get; set; } = default!; + public AfdianOptions Afdian { get; set; } = default!; public DiscordOptions Discord { get; set; } = default!; diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Program.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Program.cs index f6cfeab..0166df8 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Program.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Program.cs @@ -18,6 +18,7 @@ using Snap.Hutao.Server.Service.Announcement; using Snap.Hutao.Server.Service.Authorization; using Snap.Hutao.Server.Service.Discord; +using Snap.Hutao.Server.Service.Distribution; using Snap.Hutao.Server.Service.GachaLog; using Snap.Hutao.Server.Service.GachaLog.Statistics; using Snap.Hutao.Server.Service.Github; @@ -91,6 +92,7 @@ public static void Main(string[] args) .AddResponseCompression() .AddScoped() .AddScoped() + .AddScoped() .AddScoped() .AddScoped() .AddScoped() diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs new file mode 100644 index 0000000..dcad0b2 --- /dev/null +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs @@ -0,0 +1,44 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Snap.Hutao.Server.Model.Response; +using Snap.Hutao.Server.Option; + +namespace Snap.Hutao.Server.Service.Distribution; + +// Scoped +public sealed class DistributionService +{ + private readonly ILogger logger; + private readonly HttpClient httpClient; + private readonly string cdnToken; + + public DistributionService(IServiceProvider serviceProvider) + { + logger = serviceProvider.GetRequiredService>(); + httpClient = serviceProvider.GetRequiredService(); + cdnToken = serviceProvider.GetRequiredService().CDNToken; + } + + public async ValueTask GetAcceleratedMirrorAsync(string filename) + { + using (HttpRequestMessage req = new(HttpMethod.Get, $"https://api.qhy04.com/hutaocdn/get?filename={filename}")) + { + req.Headers.UserAgent.ParseAdd("Snap Hutao Server/1.0"); + req.Headers.Authorization = new("Bearer", cdnToken); + + using (HttpResponseMessage resp = await httpClient.SendAsync(req)) + { + Response? mirrorResp = await resp.Content.ReadFromJsonAsync>(); + ArgumentNullException.ThrowIfNull(mirrorResp); + if (mirrorResp is not { Code: ReturnCode.Success }) + { + logger.LogWarning("Failed to get accelerated mirror for {Filename}, raw message: {Message}", filename, mirrorResp.Message); + return default; + } + + return mirrorResp.Data; + } + } + } +} \ No newline at end of file diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/HutaoPackageMirror.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/HutaoPackageMirror.cs new file mode 100644 index 0000000..2ba4722 --- /dev/null +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/HutaoPackageMirror.cs @@ -0,0 +1,16 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +namespace Snap.Hutao.Server.Service.Distribution; + +public sealed class HutaoPackageMirror +{ + [JsonPropertyName("url")] + public string Url { get; set; } = default!; + + [JsonPropertyName("mirror_name")] + public string MirrorName { get; set; } = default!; + + [JsonPropertyName("mirror_type")] + public string MirrorType { get; set; } = default!; +} From c6670f4d35d4d00d6643d3550640b78d52de5e3d Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Fri, 22 Nov 2024 21:42:40 +0800 Subject: [PATCH 2/2] apply review --- .../Snap.Hutao.Server/Controller/DistributionController.cs | 2 +- .../Snap.Hutao.Server/Option/AppOptions.cs | 4 +++- .../Service/Distribution/DistributionService.cs | 6 ++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs index dfc3077..8878ea0 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Controller/DistributionController.cs @@ -22,7 +22,7 @@ public DistributionController(DistributionService distributionService) [Authorize] [ServiceFilter(typeof(ValidateGachaLogPermission))] - [HttpGet("GetAccMirror")] + [HttpGet("GetAcceleratedMirror")] public async Task GetAcceleratedMirror([FromQuery(Name = "Filename")] string filename) { HutaoPackageMirror? mirror = await distributionService.GetAcceleratedMirrorAsync(filename); diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs index a9c973d..d2cd2ba 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Option/AppOptions.cs @@ -15,7 +15,9 @@ public sealed class AppOptions public string RSAPrivateKey { get; set; } = default!; - public string CDNToken { get; set; } = default!; + public string CdnEndpoint { get; set; } = default!; + + public string CdnToken { get; set; } = default!; public AfdianOptions Afdian { get; set; } = default!; diff --git a/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs index dcad0b2..9c7627f 100644 --- a/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs +++ b/src/Snap.Hutao.Server/Snap.Hutao.Server/Service/Distribution/DistributionService.cs @@ -11,18 +11,20 @@ public sealed class DistributionService { private readonly ILogger logger; private readonly HttpClient httpClient; + private readonly string cdnEndpoint; private readonly string cdnToken; public DistributionService(IServiceProvider serviceProvider) { logger = serviceProvider.GetRequiredService>(); httpClient = serviceProvider.GetRequiredService(); - cdnToken = serviceProvider.GetRequiredService().CDNToken; + cdnEndpoint = serviceProvider.GetRequiredService().CdnEndpoint; + cdnToken = serviceProvider.GetRequiredService().CdnToken; } public async ValueTask GetAcceleratedMirrorAsync(string filename) { - using (HttpRequestMessage req = new(HttpMethod.Get, $"https://api.qhy04.com/hutaocdn/get?filename={filename}")) + using (HttpRequestMessage req = new(HttpMethod.Get, string.Format(cdnEndpoint, filename))) { req.Headers.UserAgent.ParseAdd("Snap Hutao Server/1.0"); req.Headers.Authorization = new("Bearer", cdnToken);