From aeab2d12d3870da431606fd18b19842897c49bed Mon Sep 17 00:00:00 2001 From: SALTWOOD <105980161+SALTWOOD@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:24:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BE=9B=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=EF=BC=88=E6=9C=AA=E5=AE=8C=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSharp-OpenBMCLAPI/Modules/Cluster.cs | 16 +++++++++++-- CSharp-OpenBMCLAPI/Modules/Config.cs | 14 ++++++++++- .../Modules/ExtensionMethods.cs | 2 +- CSharp-OpenBMCLAPI/Modules/HttpServer.cs | 24 +++++++++++++++++++ CSharp-OpenBMCLAPI/Modules/Utils.cs | 24 +++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 CSharp-OpenBMCLAPI/Modules/HttpServer.cs create mode 100644 CSharp-OpenBMCLAPI/Modules/Utils.cs diff --git a/CSharp-OpenBMCLAPI/Modules/Cluster.cs b/CSharp-OpenBMCLAPI/Modules/Cluster.cs index c77cb29..93ea05a 100644 --- a/CSharp-OpenBMCLAPI/Modules/Cluster.cs +++ b/CSharp-OpenBMCLAPI/Modules/Cluster.cs @@ -16,6 +16,7 @@ using TeraIO.Extension; using System.Diagnostics; using System.Text.Json; +using System.Net; namespace CSharpOpenBMCLAPI.Modules { @@ -68,10 +69,21 @@ protected async Task AsyncRun() Connect(); + HttpServer hs = new(); + hs.PORT = 4000; + hs.UriPrefixes = new() + { + "http://localhost:4000/" + }; + hs.Load(); + hs.Start(); + await RequestCertification(); await Enable(); + SharedData.Logger.LogInfo($"工作进程 {guid} 在 {SharedData.Config.HOST}:{SharedData.Config.PORT} 提供服务"); + _keepAlive = Task.Run(() => { while (true) @@ -170,7 +182,7 @@ protected async Task CheckFiles() Avro.IO.Decoder decoder = new BinaryDecoder(new MemoryStream(buffer)); - object[] f = new GenericDatumReader(schema, schema).Read(null!, decoder); + object[] files = new GenericDatumReader(schema, schema).Read(null!, decoder); DownloadConfiguration option = new DownloadConfiguration() { @@ -186,7 +198,7 @@ protected async Task CheckFiles() } }; - Parallel.ForEach(f, (obj) => + Parallel.ForEach(files, (obj) => { GenericRecord? record = obj as GenericRecord; if (record != null) diff --git a/CSharp-OpenBMCLAPI/Modules/Config.cs b/CSharp-OpenBMCLAPI/Modules/Config.cs index 0ea849d..5f83294 100644 --- a/CSharp-OpenBMCLAPI/Modules/Config.cs +++ b/CSharp-OpenBMCLAPI/Modules/Config.cs @@ -19,7 +19,19 @@ public class Config public string clusterVersion; // 用户访问时使用的 IP 或域名 [JsonProperty("host")] - public string HOST { get; set; } + public string HOST + { + get + { + if (string.IsNullOrEmpty(this._host)) + { + HttpClient client = new HttpClient(); + this._host = client.GetAsync("https://4.ipw.cn/").Result.Content.ReadAsStringAsync().Result; + } + return this._host; + } + set => this._host = value; + } // 对外服务端口 [JsonProperty("port")] public int PORT { get; set; } diff --git a/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs b/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs index 89092d8..a33db65 100644 --- a/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs +++ b/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs @@ -17,7 +17,7 @@ public static T ThrowIfNull(this T? item) public static string ToStandardTimeString(this DateTime dt) => dt.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); - public static void PrintTypeInfo(T instance) + public static void PrintTypeInfo(this T instance) { Type type = typeof(T); Console.WriteLine($"Type: {type.Name}"); diff --git a/CSharp-OpenBMCLAPI/Modules/HttpServer.cs b/CSharp-OpenBMCLAPI/Modules/HttpServer.cs new file mode 100644 index 0000000..dd02911 --- /dev/null +++ b/CSharp-OpenBMCLAPI/Modules/HttpServer.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TeraIO.Network.Http; + +namespace CSharpOpenBMCLAPI.Modules +{ + public class HttpServer : HttpServerAppBase + { + public HttpServer() : base() { } + + [HttpHandler("/measure")] + public void Measure(HttpClientRequest req) + { + var a = req.Request; + Debugger.Break(); + } + + public void Load() => this.LoadNew(); + } +} diff --git a/CSharp-OpenBMCLAPI/Modules/Utils.cs b/CSharp-OpenBMCLAPI/Modules/Utils.cs new file mode 100644 index 0000000..42564c1 --- /dev/null +++ b/CSharp-OpenBMCLAPI/Modules/Utils.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace CSharpOpenBMCLAPI.Modules +{ + public static class Utils + { + public static bool CheckSign(string hash, string secret, string s, string e) + { + if (string.IsNullOrEmpty(e) || string.IsNullOrEmpty(s)) + { + return false; + } + var sha1 = SHA1.Create(); + var sign = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes($"{secret}{hash}{e}"))); + return sign == s && DateTime.Now.Ticks < (Convert.ToInt32(e, 36) / 100); + } + + } +}