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()