From 1a6bb348cecf66f8eb07fc72c7eb5e9910a2fe6c Mon Sep 17 00:00:00 2001 From: Executor Date: Tue, 28 Nov 2023 14:24:24 +0800 Subject: [PATCH] =?UTF-8?q?v1.0.6=20=E4=BF=AE=E5=A4=8D=E7=94=B1=E4=BA=8E?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E5=93=94=E5=93=A9=E5=93=94=E5=93=A9=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E5=8A=A3=E5=8C=96=E5=8C=BF=E5=90=8D=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=87=A0=E4=B9=8E=E6=97=A0=E6=B3=95=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=BC=80=E5=85=B3=E6=92=AD=E6=B6=88=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Build.props | 8 +-- .../Clients/AnonymousDanmakuClient.cs | 61 +++++++++++++++++++ src/BililiveNotification/MainWindow.xaml.cs | 8 +-- 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/BililiveNotification/Clients/AnonymousDanmakuClient.cs diff --git a/Directory.Build.props b/Directory.Build.props index 5eb1c24..1e95b58 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,9 +7,9 @@ true true - 1.0.5.0 - 1.0.5.0 - 1.0.5.0 - 1.0.5.0 + 1.0.6.0 + 1.0.6.0 + 1.0.6.0 + 1.0.6.0 diff --git a/src/BililiveNotification/Clients/AnonymousDanmakuClient.cs b/src/BililiveNotification/Clients/AnonymousDanmakuClient.cs new file mode 100644 index 0000000..ae91a60 --- /dev/null +++ b/src/BililiveNotification/Clients/AnonymousDanmakuClient.cs @@ -0,0 +1,61 @@ +using System; +using System.Diagnostics; +using System.Net.Sockets; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Executorlibs.Bilibili.Protocol.Invokers; +using Executorlibs.Bilibili.Protocol.Models; +using Executorlibs.Bilibili.Protocol.Options; +using Executorlibs.Bilibili.Protocol.Services; +using Microsoft.Extensions.Options; +#if NET5_0_OR_GREATER +using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV3; +#else +using Executorlibs.Shared.Extensions; +using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV2; +#endif + +namespace BililiveNotification.Clients +{ + public class AnonymousDanmakuClient : TcpDanmakuClient + { + public AnonymousDanmakuClient(IBilibiliMessageHandlerInvoker invoker, IBilibiliMessageSubscriptionResolver resolver, IOptionsSnapshot options, IDanmakuServerProvider credentialProvider) : base(invoker, resolver, options, credentialProvider) + { + + } + + protected override async Task InternalConnectAsync(CancellationToken token) + { + int roomId = _options.RoomId; + DanmakuServerInfo server = await _credentialProvider.GetDanmakuServerInfoAsync(token); + Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp); + int sendTimeout = (socket.ReceiveTimeout = (int)_options.HeartbeatInterval.TotalMilliseconds + 10000); + socket.SendTimeout = sendTimeout; + token.Register(socket.Dispose); + DanmakuServerHostInfo danmakuServerHostInfo = server.Hosts[(int)(Stopwatch.GetTimestamp() % server.Hosts.Length)]; +#if NET5_0_OR_GREATER + await socket.ConnectAsync(danmakuServerHostInfo.Host, danmakuServerHostInfo.Port, token); +#else + await socket.ConnectAsync(danmakuServerHostInfo.Host, danmakuServerHostInfo.Port); +#endif + await socket.SendAsync(CreateNewJoinRoomPayload(roomId, 0, server.Token), SocketFlags.None, token); + _Socket = socket; + } + + private byte[] CreateNewJoinRoomPayload(int roomId, long userId, string token) + { + byte[] body = JsonSerializer.SerializeToUtf8Bytes(new + { + uid = userId, + roomid = roomId, + protover = Version, + buvid = $"{Guid.NewGuid()}{new Random().Next(10000, 100000)}infoc", + platform = "web", + type = 2, + key = token + }); + return CreatePayload(7, body); + } + } +} diff --git a/src/BililiveNotification/MainWindow.xaml.cs b/src/BililiveNotification/MainWindow.xaml.cs index e902c68..97a45c6 100644 --- a/src/BililiveNotification/MainWindow.xaml.cs +++ b/src/BililiveNotification/MainWindow.xaml.cs @@ -1,3 +1,4 @@ +using BililiveNotification.Clients; using BililiveNotification.Configs; using Executorlibs.Bilibili.Protocol.Builders; using Executorlibs.Bilibili.Protocol.Invokers; @@ -15,11 +16,6 @@ using System.Windows.Forms; using Button = System.Windows.Controls.Button; using MessageBox = System.Windows.MessageBox; -#if NET5_0_OR_GREATER -using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV3; -#else -using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV2; -#endif namespace BililiveNotification { @@ -53,7 +49,7 @@ public MainWindow() .AddParser() .AddParser() .AddInvoker() - .AddClient() + .AddClient() .AddHandler(services => services.GetRequiredService()) .Services .AddSingleton()