From 5c6b26788adcde44491a873b59341f7225ead01b Mon Sep 17 00:00:00 2001 From: SALTWOOD <105980161+SALTWOOD@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:47:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=91=E4=B8=BB=E6=8E=A7=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E8=AF=81=E4=B9=A6=20fix:=20Config.byoc=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=BA=94=E4=B8=BA=20bool=20=E8=80=8C=E4=B8=8D?= =?UTF-8?q?=E6=98=AF=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSharp-OpenBMCLAPI/DefaultConfig.json5 | 16 +++++-- CSharp-OpenBMCLAPI/Modules/Cluster.cs | 44 +++++++++++++++++-- CSharp-OpenBMCLAPI/Modules/Config.cs | 24 ++++++---- .../Modules/ExtensionMethods.cs | 2 + 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/CSharp-OpenBMCLAPI/DefaultConfig.json5 b/CSharp-OpenBMCLAPI/DefaultConfig.json5 index aebeb51..d6e8408 100644 --- a/CSharp-OpenBMCLAPI/DefaultConfig.json5 +++ b/CSharp-OpenBMCLAPI/DefaultConfig.json5 @@ -1,6 +1,14 @@ { - // 指示 token 应当在距离其失效前的多少毫秒进行刷新 - "refreshTokenTime": 1800000, - // 指示应该将要服务的文件放在哪里(服务路径) - "clusterFileDirectory": "./" + // 指示 token 应当在距离其失效前的多少毫秒进行刷新 + "refreshTokenTime": 1800000, + // 指示应该将要服务的文件放在哪里(服务路径) + "clusterFileDirectory": "./", + // 用户访问时使用的 IP 或域名 + "host": "", + // 对外服务端口 + "port": 4000, + // 是否使用自定义域名 + "byoc": false, + // 指示是否执行快速上线,若为 true 则每次都不执行 + "noFastEnable": false } \ No newline at end of file diff --git a/CSharp-OpenBMCLAPI/Modules/Cluster.cs b/CSharp-OpenBMCLAPI/Modules/Cluster.cs index 6382e76..c77cb29 100644 --- a/CSharp-OpenBMCLAPI/Modules/Cluster.cs +++ b/CSharp-OpenBMCLAPI/Modules/Cluster.cs @@ -14,6 +14,8 @@ using SocketIOClient; using SocketIO.Core; using TeraIO.Extension; +using System.Diagnostics; +using System.Text.Json; namespace CSharpOpenBMCLAPI.Modules { @@ -66,6 +68,8 @@ protected async Task AsyncRun() Connect(); + await RequestCertification(); + await Enable(); _keepAlive = Task.Run(() => @@ -74,6 +78,7 @@ protected async Task AsyncRun() { Thread.Sleep(25 * 1000); KeepAlive().Wait(); + this.token.FetchToken().Wait(); } }); @@ -114,8 +119,8 @@ await socket.EmitAsync("enable", }, new { - host = SharedData.Config.host, - port = SharedData.Config.port, + host = SharedData.Config.HOST, + port = SharedData.Config.PORT, version = SharedData.Config.clusterVersion, byoc = SharedData.Config.byoc, noFastEnable = SharedData.Config.noFastEnable @@ -133,8 +138,8 @@ await socket.EmitAsync("disable", public async Task KeepAlive() { - string time = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); - socket.Connected.Dump(); + string time = DateTime.Now.ToStandardTimeString(); + // socket.Connected.Dump(); await socket.EmitAsync("keep-alive", (SocketIOResponse resp) => { @@ -148,6 +153,7 @@ await socket.EmitAsync("keep-alive", protected async Task CheckFiles() { + SharedData.Logger.LogInfo("开始检查文件"); var resp = await client.GetAsync("openbmclapi/files"); byte[] buffer = await resp.Content.ReadAsByteArrayAsync(); var decomporess = new Decompressor(); @@ -227,5 +233,35 @@ private void CheckFileAfterDownload(string path, string hash, DownloadService se service.DownloadFileTaskAsync($"{client.BaseAddress}openbmclapi/download/{hash}").Wait(); } } + + public async Task RequestCertification() + { + await socket.EmitAsync("request-cert", (SocketIOResponse resp) => + { + var data = resp; + //Debugger.Break(); + var json = data.GetValue(0)[1]; + JsonElement cert; json.TryGetProperty("cert", out cert); + JsonElement key; json.TryGetProperty("key", out key); + + string? certString = cert.GetString(); + string? keyString = key.GetString(); + + string certPath = $"{SharedData.Config.clusterFileDirectory}certifications/cert.pem"; + string keyPath = $"{SharedData.Config.clusterFileDirectory}certifications/key.pem"; + + Directory.CreateDirectory($"{SharedData.Config.clusterFileDirectory}certifications"); + + using (var file = File.Create(certPath)) + { + if (certString != null) file.Write(Encoding.UTF8.GetBytes(certString)); + } + + using (var file = File.Create(keyPath)) + { + if (keyString != null) file.Write(Encoding.UTF8.GetBytes(keyString)); + } + }); + } } } diff --git a/CSharp-OpenBMCLAPI/Modules/Config.cs b/CSharp-OpenBMCLAPI/Modules/Config.cs index 8550471..0ea849d 100644 --- a/CSharp-OpenBMCLAPI/Modules/Config.cs +++ b/CSharp-OpenBMCLAPI/Modules/Config.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -16,21 +17,28 @@ public class Config // 指示节点端的版本,不应由用户更改 [Browsable(false)] public string clusterVersion; - // Host - public string host; - public int port; - public string byoc; + // 用户访问时使用的 IP 或域名 + [JsonProperty("host")] + public string HOST { get; set; } + // 对外服务端口 + [JsonProperty("port")] + public int PORT { get; set; } + // 是否使用自定义域名 + public bool byoc; + // 指示是否执行快速上线,若为 true 则每次都不执行 public bool noFastEnable; + private string _host = ""; + public Config() { this.refreshTokenTime = 1800000; this.clusterFileDirectory = "./"; this.clusterVersion = "1.9.7"; - this.host = ""; - this.port = 4000; - this.byoc = ""; + this.HOST = ""; + this.PORT = 4000; + this.byoc = false; this.noFastEnable = false; } } diff --git a/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs b/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs index 0155150..89092d8 100644 --- a/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs +++ b/CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs @@ -15,6 +15,8 @@ public static T ThrowIfNull(this T? item) return item; } + public static string ToStandardTimeString(this DateTime dt) => dt.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); + public static void PrintTypeInfo(T instance) { Type type = typeof(T);