Skip to content

Commit

Permalink
[All] Reconnect Signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Nov 16, 2023
1 parent 9949170 commit 9c5863b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
30 changes: 29 additions & 1 deletion Lagrange.Core/Utility/Sign/LinuxSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<JsonObject>(response)?["code"]?.GetValue<int>() == 0) return true;
}
catch
{
return false;
}

return false;
}
}
6 changes: 6 additions & 0 deletions Lagrange.Core/Utility/Sign/MacSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Utility/Sign/SignProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
5 changes: 5 additions & 0 deletions Lagrange.Core/Utility/Sign/WindowsSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ internal class WindowsSigner : SignProvider
return new byte[35]; // Dummy signature
}
}

public override bool Test()
{
throw new NotImplementedException();
}
}
32 changes: 29 additions & 3 deletions Lagrange.OneBot/Utility/OneBotSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LagrangeApp> logger)
{
_signServer = config["SignServerUrl"] ?? "";
_client = new HttpClient();
_logger = logger;

if (string.IsNullOrEmpty(_signServer))
Expand All @@ -32,6 +31,12 @@ public OneBotSigner(IConfiguration config, ILogger<LagrangeApp> 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)
Expand Down Expand Up @@ -60,8 +65,29 @@ public OneBotSigner(IConfiguration config, ILogger<LagrangeApp> 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<JsonObject>(response)?["code"]?.GetValue<int>() == 0)
{
_logger.LogInformation($"[{Tag}] Reconnected to Signature Service successfully");
return true;
}
}
catch
{
return false;
}

return false;
}
}

0 comments on commit 9c5863b

Please sign in to comment.