From cf0f72e98c4157040b32d81433063228ce8888f9 Mon Sep 17 00:00:00 2001 From: genteure Date: Fri, 25 Aug 2023 01:07:52 +0800 Subject: [PATCH] fix(core): changes around danmaku connection --- .../Api/Danmaku/DanmakuTransportWebSocket.cs | 19 +++++++++++++------ BililiveRecorder.Core/Room.cs | 14 +++++++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/BililiveRecorder.Core/Api/Danmaku/DanmakuTransportWebSocket.cs b/BililiveRecorder.Core/Api/Danmaku/DanmakuTransportWebSocket.cs index 1fd9546..9f5d48d 100644 --- a/BililiveRecorder.Core/Api/Danmaku/DanmakuTransportWebSocket.cs +++ b/BililiveRecorder.Core/Api/Danmaku/DanmakuTransportWebSocket.cs @@ -30,13 +30,16 @@ namespace BililiveRecorder.Core.Api.Danmaku if (headerHashTable.GetValue(null) is not Hashtable table) return; - var info = table["User-Agent"]; - if (info is null) return; + foreach (var key in new[] { "User-Agent", "Referer", "Accept" }) + { + var info = table[key]; + if (info is null) continue; - var isRequestRestrictedProperty = info.GetType().GetField("IsRequestRestricted", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); - if (isRequestRestrictedProperty is null) return; + var isRequestRestrictedProperty = info.GetType().GetField("IsRequestRestricted", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); + if (isRequestRestrictedProperty is null) continue; - isRequestRestrictedProperty.SetValue(info, false); + isRequestRestrictedProperty.SetValue(info, false); + } } public DanmakuTransportWebSocket() @@ -48,8 +51,12 @@ namespace BililiveRecorder.Core.Api.Danmaku options.Proxy = null; options.Cookies = null; options.SetRequestHeader("Origin", HttpApiClient.HttpHeaderOrigin); - options.SetRequestHeader("Accept-Language", HttpApiClient.HttpHeaderAcceptLanguage); + options.SetRequestHeader("Referer", HttpApiClient.HttpHeaderReferer); options.SetRequestHeader("User-Agent", HttpApiClient.HttpHeaderUserAgent); + options.SetRequestHeader("Accept-Language", HttpApiClient.HttpHeaderAcceptLanguage); + options.SetRequestHeader("Accept", "*/*"); + options.SetRequestHeader("Pragma", "no-cache"); + options.SetRequestHeader("Cache-Control", "no-cache"); } public async Task ConnectAsync(string host, int port, CancellationToken cancellationToken) diff --git a/BililiveRecorder.Core/Room.cs b/BililiveRecorder.Core/Room.cs index a5fb401..374d4fb 100644 --- a/BililiveRecorder.Core/Room.cs +++ b/BililiveRecorder.Core/Room.cs @@ -558,16 +558,24 @@ retry: private string? DanmakuClient_BeforeHandshake(string json) { - if (this.RoomConfig.DanmakuAuthenticateWithStreamerUid) + var danmakuAuthenticateWithStreamerUid = this.RoomConfig.DanmakuAuthenticateWithStreamerUid; + if (danmakuAuthenticateWithStreamerUid) { var obj = JObject.Parse(json); obj["uid"] = this.Uid; - // delete key and buvid obj.Remove("key"); obj.Remove("buvid"); json = obj.ToString(Formatting.None); } - return this.userScriptRunner.CallOnDanmakuHandshake(this.logger, this, json); + + var scriptUpdatedJson = this.userScriptRunner.CallOnDanmakuHandshake(this.logger, this, json); + + if (scriptUpdatedJson is not null) + return scriptUpdatedJson; + else if (danmakuAuthenticateWithStreamerUid) + return json; + else + return null; } private void DanmakuClient_DanmakuReceived(object? sender, Api.Danmaku.DanmakuReceivedEventArgs e)