From 9c5863b3e89ff5e15b46c32ae21654cdba7929f9 Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Thu, 16 Nov 2023 19:18:33 +0800 Subject: [PATCH] [All] Reconnect Signature --- Lagrange.Core/Utility/Sign/LinuxSigner.cs | 30 ++++++++++++++++++- Lagrange.Core/Utility/Sign/MacSigner.cs | 6 ++++ Lagrange.Core/Utility/Sign/SignProvider.cs | 2 ++ Lagrange.Core/Utility/Sign/WindowsSigner.cs | 5 ++++ Lagrange.OneBot/Utility/OneBotSigner.cs | 32 +++++++++++++++++++-- 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/Lagrange.Core/Utility/Sign/LinuxSigner.cs b/Lagrange.Core/Utility/Sign/LinuxSigner.cs index 056dea0b8..d055d7fff 100644 --- a/Lagrange.Core/Utility/Sign/LinuxSigner.cs +++ b/Lagrange.Core/Utility/Sign/LinuxSigner.cs @@ -7,7 +7,18 @@ namespace Lagrange.Core.Utility.Sign; internal class LinuxSigner : SignProvider { - private const string Url = ""; + private const string Url = "https://sign.libfekit.so/api/sign"; + + private readonly Timer _timer; + + public LinuxSigner() + { + _timer = new Timer(_ => + { + bool reconnect = Available = Test(); + if (reconnect) _timer?.Change(-1, 5000); + }); + } public override byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token) { @@ -32,8 +43,25 @@ internal class LinuxSigner : SignProvider catch (Exception) { Available = false; + _timer.Change(0, 5000); + Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(LinuxSigner)}] Failed to get signature, using dummy signature"); return new byte[20]; // Dummy signature } } + + public override bool Test() + { + try + { + string response = Http.GetAsync($"{Url}/ping").GetAwaiter().GetResult(); + if (JsonSerializer.Deserialize(response)?["code"]?.GetValue() == 0) return true; + } + catch + { + return false; + } + + return false; + } } \ No newline at end of file diff --git a/Lagrange.Core/Utility/Sign/MacSigner.cs b/Lagrange.Core/Utility/Sign/MacSigner.cs index f35599f3b..f7a2f0c8b 100644 --- a/Lagrange.Core/Utility/Sign/MacSigner.cs +++ b/Lagrange.Core/Utility/Sign/MacSigner.cs @@ -36,7 +36,13 @@ internal class MacSigner : SignProvider { Available = false; Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{nameof(MacSigner)}] Failed to get signature, using dummy signature"); + return new byte[35]; // Dummy signature } } + + public override bool Test() + { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/Lagrange.Core/Utility/Sign/SignProvider.cs b/Lagrange.Core/Utility/Sign/SignProvider.cs index cc4e4c6d4..5ef4fe487 100644 --- a/Lagrange.Core/Utility/Sign/SignProvider.cs +++ b/Lagrange.Core/Utility/Sign/SignProvider.cs @@ -46,4 +46,6 @@ public abstract class SignProvider }; public abstract byte[]? Sign(string cmd, uint seq, byte[] body, out byte[]? ver, out string? token); + + public abstract bool Test(); } \ No newline at end of file diff --git a/Lagrange.Core/Utility/Sign/WindowsSigner.cs b/Lagrange.Core/Utility/Sign/WindowsSigner.cs index 8f8ad151d..30c9be070 100644 --- a/Lagrange.Core/Utility/Sign/WindowsSigner.cs +++ b/Lagrange.Core/Utility/Sign/WindowsSigner.cs @@ -39,4 +39,9 @@ internal class WindowsSigner : SignProvider return new byte[35]; // Dummy signature } } + + public override bool Test() + { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/Lagrange.OneBot/Utility/OneBotSigner.cs b/Lagrange.OneBot/Utility/OneBotSigner.cs index ceeb8174c..d398fb6d4 100644 --- a/Lagrange.OneBot/Utility/OneBotSigner.cs +++ b/Lagrange.OneBot/Utility/OneBotSigner.cs @@ -14,13 +14,12 @@ public class OneBotSigner : SignProvider { private const string Tag = nameof(OneBotSigner); private readonly string _signServer; - private readonly HttpClient _client; private readonly ILogger _logger; - + private readonly Timer _timer; + public OneBotSigner(IConfiguration config, ILogger logger) { _signServer = config["SignServerUrl"] ?? ""; - _client = new HttpClient(); _logger = logger; if (string.IsNullOrEmpty(_signServer)) @@ -32,6 +31,12 @@ public OneBotSigner(IConfiguration config, ILogger logger) { logger.LogInformation($"[{Tag}]: Signature Service is successfully established"); } + + _timer = new Timer(_ => + { + bool reconnect = Available = Test(); + if (reconnect) _timer?.Change(-1, 5000); + }); } public override byte[]? Sign(string cmd, uint seq, byte[] body, [UnscopedRef] out byte[]? ver, [UnscopedRef] out string? token) @@ -60,8 +65,29 @@ public OneBotSigner(IConfiguration config, ILogger logger) catch { Available = false; + _timer.Change(0, 5000); + _logger.LogWarning($"[{Tag}] Failed to get signature, using dummy signature"); return new byte[35]; // Dummy signature } } + + public override bool Test() + { + try + { + string response = Http.GetAsync($"{_signServer}/ping").GetAwaiter().GetResult(); + if (JsonSerializer.Deserialize(response)?["code"]?.GetValue() == 0) + { + _logger.LogInformation($"[{Tag}] Reconnected to Signature Service successfully"); + return true; + } + } + catch + { + return false; + } + + return false; + } } \ No newline at end of file