Skip to content

Commit

Permalink
[OneBot] Custom SignServer url
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 24, 2023
1 parent e13f23a commit 5b7f4be
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 7 deletions.
2 changes: 0 additions & 2 deletions Lagrange.OneBot/Core/Network/MsgRecvEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.Extensions.Hosting;

namespace Lagrange.OneBot.Core.Network;

public class MsgRecvEventArgs(string data, string? identifier = null) : EventArgs
Expand Down
12 changes: 7 additions & 5 deletions Lagrange.OneBot/LagrangeApp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.Json;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Utility.Sign;
using Lagrange.OneBot.Core.Message;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Core.Operation;
Expand Down Expand Up @@ -44,7 +45,9 @@ internal LagrangeApp(IHost host)
{
await _hostApp.StartAsync(cancellationToken);
Logger.LogInformation("Lagrange.OneBot Implementation has started");
Logger.LogInformation($"Protocol: {Configuration.GetValue<string>("Protocol")} | {Instance.ContextCollection.AppInfo.CurrentVersion}");
Logger.LogInformation($"Protocol: {Configuration["Protocol"]} | {Instance.ContextCollection.AppInfo.CurrentVersion}");

Instance.ContextCollection.Packet.SignProvider = Services.GetRequiredService<SignProvider>();

Check failure on line 50 in Lagrange.OneBot/LagrangeApp.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm)

'PacketContext' does not contain a definition for 'SignProvider' and no accessible extension method 'SignProvider' accepting a first argument of type 'PacketContext' could be found (are you missing a using directive or an assembly reference?)

Instance.Invoker.OnBotLogEvent += (_, args) => Logger.Log(args.Level switch
{
Expand All @@ -71,11 +74,10 @@ internal LagrangeApp(IHost host)
Instance.ContextCollection.Keystore.Session.TempPassword == null)
{
Logger.LogInformation("Session expired or Password not filled in, try to login by QrCode");

var qrCode = await Instance.FetchQrCode();
if (qrCode != null)

if (await Instance.FetchQrCode() is { } qrCode)
{
QrCodeHelper.Output(qrCode.Value.Url ?? "");
QrCodeHelper.Output(qrCode.Url ?? "");
await Instance.LoginByQrCode();
}
}
Expand Down
3 changes: 3 additions & 0 deletions Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Text.Json;
using Lagrange.Core.Common;
using Lagrange.Core.Common.Interface;
using Lagrange.Core.Utility.Sign;
using Lagrange.OneBot.Core.Message;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Core.Operation;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Utility;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -77,6 +79,7 @@ public LagrangeAppBuilder ConfigureOneBot()
{
Services.AddSingleton<ContextBase, LiteDbContext>();
Services.AddSingleton<ILagrangeWebService, ReverseWSService>();
Services.AddSingleton<SignProvider, OneBotSigner>();

Services.AddSingleton<MessageService>();
Services.AddSingleton<OperationService>();
Expand Down
67 changes: 67 additions & 0 deletions Lagrange.OneBot/Utility/OneBotSigner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core.Utility.Extension;
using Lagrange.Core.Utility.Network;
using Lagrange.Core.Utility.Sign;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Lagrange.OneBot.Utility;

public class OneBotSigner : SignProvider
{
private const string Tag = nameof(OneBotSigner);
private readonly string _signServer;
private readonly HttpClient _client;
private readonly ILogger _logger;

public OneBotSigner(IConfiguration config, ILogger<LagrangeApp> logger)
{
_signServer = config["SignServerUrl"] ?? "";
_client = new HttpClient();
_logger = logger;

if (string.IsNullOrEmpty(_signServer))
{
Available = false;
logger.LogWarning($"[{Tag}]: Signature Service is not available, login may be failed");
}
else
{
logger.LogInformation($"[{Tag}]: Signature Service is successfully established");
}
}

public override byte[]? Sign(string cmd, uint seq, byte[] body, [UnscopedRef] out byte[]? ver, [UnscopedRef] out string? token)
{
ver = null;
token = null;
if (!WhiteListCommand.Contains(cmd)) return null;
if (!Available || string.IsNullOrEmpty(_signServer)) return new byte[35]; // Dummy signature

var payload = new Dictionary<string, string>
{
{ "cmd", cmd },
{ "seq", seq.ToString() },
{ "src", body.Hex() },
};

try
{
string response = Http.GetAsync(_signServer, payload).GetAwaiter().GetResult();
var json = JsonSerializer.Deserialize<JsonObject>(response);

ver = json?["value"]?["extra"]?.ToString().UnHex() ?? Array.Empty<byte>();
token = Encoding.ASCII.GetString(json?["value"]?["token"]?.ToString().UnHex() ?? Array.Empty<byte>());
return json?["value"]?["sign"]?.ToString().UnHex() ?? new byte[35];
}
catch
{
Available = false;
_logger.LogWarning($"[{Tag}] Failed to get signature, using dummy signature");
return new byte[35]; // Dummy signature
}
}
}

0 comments on commit 5b7f4be

Please sign in to comment.