Skip to content

Commit

Permalink
Merge branch 'LagrangeDev:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 authored Aug 23, 2024
2 parents 95f9141 + c62e95f commit c5e51bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Lagrange.Core/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class BotContext : IDisposable

internal readonly ContextCollection ContextCollection;

private readonly BotAppInfo _appInfo;
public BotAppInfo AppInfo { get; }

private readonly BotConfig _config;
public BotConfig Config { get; }

private readonly BotDeviceInfo _deviceInfo;

Expand All @@ -29,12 +29,12 @@ internal BotContext(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keys
Invoker = new EventInvoker(this);
Scheduler = new Utility.TaskScheduler();

_config = config;
_appInfo = BotAppInfo.ProtocolToAppInfo[config.Protocol];
Config = config;
AppInfo = BotAppInfo.ProtocolToAppInfo[config.Protocol];
_deviceInfo = deviceInfo;
_keystore = keystore;

ContextCollection = new ContextCollection(_keystore, _appInfo, _deviceInfo, _config, Invoker, Scheduler);
ContextCollection = new ContextCollection(_keystore, AppInfo, _deviceInfo, Config, Invoker, Scheduler);
}

public void Dispose()
Expand Down
39 changes: 37 additions & 2 deletions Lagrange.OneBot/Utility/OneBotSigner.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Utility.Sign;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
Expand All @@ -13,12 +13,20 @@ namespace Lagrange.OneBot.Utility;

public class OneBotSigner : SignProvider
{
private ILogger<OneBotSigner> _logger;

private readonly string? _signServer;

private readonly HttpClient _client;

public OneBotSigner(IConfiguration config, ILogger<OneBotSigner> logger)
private readonly string platform;

private readonly string version;

public OneBotSigner(IConfiguration config, ILogger<OneBotSigner> logger, BotContext bot)
{
_logger = logger;

_signServer = config["SignServerUrl"] ?? "";
string? signProxyUrl = config["SignProxyUrl"]; // Only support HTTP proxy

Expand All @@ -33,6 +41,15 @@ public OneBotSigner(IConfiguration config, ILogger<OneBotSigner> logger)
}, disposeHandler: true);

if (string.IsNullOrEmpty(_signServer)) logger.LogWarning("Signature Service is not available, login may be failed");

platform = bot.Config.Protocol switch
{
Lagrange.Core.Common.Protocols.Windows => "Windows",
Lagrange.Core.Common.Protocols.MacOs => "MacOs",
Lagrange.Core.Common.Protocols.Linux => "Linux",
_ => "Unknown"
};
version = bot.AppInfo.CurrentVersion;
}

public override byte[]? Sign(string cmd, uint seq, byte[] body, [UnscopedRef] out byte[]? e, [UnscopedRef] out string? t)
Expand All @@ -59,6 +76,24 @@ public OneBotSigner(IConfiguration config, ILogger<OneBotSigner> logger)
if (message.StatusCode != HttpStatusCode.OK) throw new Exception($"Signer server returned a {message.StatusCode}");
var json = JsonDocument.Parse(message.Content.ReadAsStream()).RootElement;

if (json.TryGetProperty("platform", out JsonElement platformJson))
{
if (platformJson.GetString() != platform) throw new Exception("Signer platform mismatch");
}
else
{
_logger.LogWarning("Signer platform miss");
}

if (json.TryGetProperty("version", out JsonElement versionJson))
{
if (versionJson.GetString() != version) throw new Exception("Signer version mismatch");
}
else
{
_logger.LogWarning("Signer version miss");
}

var valueJson = json.GetProperty("value");
var extraJson = valueJson.GetProperty("extra");
var tokenJson = valueJson.GetProperty("token");
Expand Down

0 comments on commit c5e51bc

Please sign in to comment.