From 953cc165e2879233c87036f732db46f79d188d05 Mon Sep 17 00:00:00 2001 From: endymx <345793738@qq.com> Date: Mon, 13 Nov 2023 15:49:40 +0800 Subject: [PATCH 1/2] [OneBot] feat: Forward WS AccessToken --- .../Core/Network/Service/ForwardWSService.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs b/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs index 241404f15..4840fec9f 100644 --- a/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs +++ b/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs @@ -18,6 +18,8 @@ public sealed class ForwardWSService : LagrangeWSService private readonly Timer _timer; + private readonly string _accessToken; + public ForwardWSService(IConfiguration config, ILogger logger, uint uin) : base(config, logger, uin) { string url = $"ws://{config["Host"]}:{config["Port"]}"; @@ -28,6 +30,7 @@ public ForwardWSService(IConfiguration config, ILogger logger, uint }; _timer = new Timer(OnHeartbeat, null, 1, config.GetValue("HeartBeatInterval")); + _accessToken = string.IsNullOrEmpty(config["AccessToken"]) ? "" : config["AccessToken"]!; } public override Task StartAsync(CancellationToken cancellationToken) @@ -53,6 +56,15 @@ public override Task StartAsync(CancellationToken cancellationToken) conn.OnOpen = () => { + if (!string.IsNullOrEmpty(_accessToken)) + { + if (!conn.ConnectionInfo.Headers.ContainsKey("Authorization") || conn.ConnectionInfo.Headers["Authorization"] != $"Bearer {_accessToken}") + { + conn.Close(1002); + return; + } + } + Logger.LogInformation($"[{Tag}]: Connected"); var lifecycle = new OneBotLifecycle(Uin, "connect"); From fcf60e5e5be2e660d3ce57ab90781cfd9692edd8 Mon Sep 17 00:00:00 2001 From: endymx <345793738@qq.com> Date: Mon, 13 Nov 2023 15:59:05 +0800 Subject: [PATCH 2/2] [OneBot] fix: ForwardWS closed error --- Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs b/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs index 4840fec9f..5e56d2670 100644 --- a/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs +++ b/Lagrange.OneBot/Core/Network/Service/ForwardWSService.cs @@ -76,6 +76,7 @@ public override Task StartAsync(CancellationToken cancellationToken) conn.OnClose = () => { Logger.LogWarning($"[{Tag}: Disconnected]"); + _connection = null; }; }); }, cancellationToken);