1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-17 02:01:53 +01:00

support message pack protocol for signalr

This commit is contained in:
Kyle Spearrin 2018-08-23 21:56:48 -04:00
parent d458d77511
commit 68c349f72f
6 changed files with 62 additions and 37 deletions

View File

@ -68,9 +68,11 @@ namespace Bit.Notifications
{ {
foreach(var message in messages) foreach(var message in messages)
{ {
var notificationJson = message.AsString;
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>( var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(
message.AsString); notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification, _hubContext, cancellationToken); await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson,
_hubContext, cancellationToken);
await _queue.DeleteMessageAsync(message); await _queue.DeleteMessageAsync(message);
} }
} }

View File

@ -1,9 +1,12 @@
using System.Threading.Tasks; using System.IO;
using System.Text;
using System.Threading.Tasks;
using Bit.Core.Models; using Bit.Core.Models;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
namespace Bit.Notifications namespace Bit.Notifications
{ {
@ -19,9 +22,17 @@ namespace Bit.Notifications
} }
[HttpPost("~/notifications")] [HttpPost("~/notifications")]
public async Task PostNotification([FromBody]PushNotificationData<object> model) public async Task PostNotification()
{ {
await HubHelpers.SendNotificationToHubAsync(model, _hubContext); using(var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var notificationJson = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(notificationJson))
{
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, _hubContext);
}
}
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Bit.Core.Enums;
using Bit.Core.Models; using Bit.Core.Models;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -8,45 +9,48 @@ namespace Bit.Notifications
{ {
public static class HubHelpers public static class HubHelpers
{ {
public static async Task SendNotificationToHubAsync(PushNotificationData<object> notification, public static async Task SendNotificationToHubAsync(PushType type, string notificationJson,
IHubContext<NotificationsHub> hubContext, CancellationToken cancellationToken = default(CancellationToken)) IHubContext<NotificationsHub> hubContext, CancellationToken cancellationToken = default(CancellationToken))
{ {
switch(notification.Type) switch(type)
{ {
case Core.Enums.PushType.SyncCipherUpdate: case PushType.SyncCipherUpdate:
case Core.Enums.PushType.SyncCipherCreate: case PushType.SyncCipherCreate:
case Core.Enums.PushType.SyncCipherDelete: case PushType.SyncCipherDelete:
case Core.Enums.PushType.SyncLoginDelete: case PushType.SyncLoginDelete:
var cipherPayload = JsonConvert.DeserializeObject<SyncCipherPushNotification>( var cipherNotification =
JsonConvert.SerializeObject(notification.Payload)); JsonConvert.DeserializeObject<PushNotificationData<SyncCipherPushNotification>>(
if(cipherPayload.UserId.HasValue) notificationJson);
if(cipherNotification.Payload.UserId.HasValue)
{ {
await hubContext.Clients.User(cipherPayload.UserId.ToString()) await hubContext.Clients.User(cipherNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", notification, cancellationToken); .SendAsync("ReceiveMessage", cipherNotification, cancellationToken);
} }
else if(cipherPayload.OrganizationId.HasValue) else if(cipherNotification.Payload.OrganizationId.HasValue)
{ {
await hubContext.Clients.Group( await hubContext.Clients.Group(
$"Organization_{cipherPayload.OrganizationId}") $"Organization_{cipherNotification.Payload.OrganizationId}")
.SendAsync("ReceiveMessage", notification, cancellationToken); .SendAsync("ReceiveMessage", cipherNotification, cancellationToken);
} }
break; break;
case Core.Enums.PushType.SyncFolderUpdate: case PushType.SyncFolderUpdate:
case Core.Enums.PushType.SyncFolderCreate: case PushType.SyncFolderCreate:
case Core.Enums.PushType.SyncFolderDelete: case PushType.SyncFolderDelete:
var folderPayload = JsonConvert.DeserializeObject<SyncFolderPushNotification>( var folderNotification =
JsonConvert.SerializeObject(notification.Payload)); JsonConvert.DeserializeObject<PushNotificationData<SyncFolderPushNotification>>(
await hubContext.Clients.User(folderPayload.UserId.ToString()) notificationJson);
.SendAsync("ReceiveMessage", notification, cancellationToken); await hubContext.Clients.User(folderNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", folderNotification, cancellationToken);
break; break;
case Core.Enums.PushType.SyncCiphers: case PushType.SyncCiphers:
case Core.Enums.PushType.SyncVault: case PushType.SyncVault:
case Core.Enums.PushType.SyncOrgKeys: case PushType.SyncOrgKeys:
case Core.Enums.PushType.SyncSettings: case PushType.SyncSettings:
var userPayload = JsonConvert.DeserializeObject<SyncUserPushNotification>( var userNotification =
JsonConvert.SerializeObject(notification.Payload)); JsonConvert.DeserializeObject<PushNotificationData<SyncUserPushNotification>>(
await hubContext.Clients.User(userPayload.UserId.ToString()) notificationJson);
.SendAsync("ReceiveMessage", notification, cancellationToken); await hubContext.Clients.User(userNotification.Payload.UserId.ToString())
.SendAsync("ReceiveMessage", userNotification, cancellationToken);
break; break;
default: default:
break; break;

View File

@ -21,7 +21,7 @@ namespace Bit.Notifications.Jobs
{ {
var everyFiveMinutesTrigger = TriggerBuilder.Create() var everyFiveMinutesTrigger = TriggerBuilder.Create()
.StartNow() .StartNow()
.WithCronSchedule("0 */5 * * * ?") .WithCronSchedule("0 */30 * * * ?")
.Build(); .Build();
Jobs = new List<Tuple<Type, ITrigger>> Jobs = new List<Tuple<Type, ITrigger>>

View File

@ -10,6 +10,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" /> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.2" /> <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.0.3" />
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.0.0-preview1-10015" /> <PackageReference Include="Microsoft.Azure.SignalR" Version="1.0.0-preview1-10015" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
using Bit.Core; using System.Collections.Generic;
using Bit.Core;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using IdentityModel; using IdentityModel;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -54,7 +55,13 @@ namespace Bit.Notifications
} }
else else
{ {
services.AddSignalR(); services.AddSignalR().AddMessagePackProtocol(options =>
{
options.FormatterResolvers = new List<MessagePack.IFormatterResolver>()
{
MessagePack.Resolvers.ContractlessStandardResolver.Instance
};
});
} }
services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>(); services.AddSingleton<IUserIdProvider, SubjectUserIdProvider>();
services.AddSingleton<ConnectionCounter>(); services.AddSingleton<ConnectionCounter>();