public SapphireRedisSyncModule(RedisStore redisStore, SapphireDatabaseOptions options)
        {
            this.options     = options;
            redisMultiplexer = redisStore.RedisCache.Multiplexer;

            redisMultiplexer.GetSubscriber().Subscribe($"{options.Sync.Prefix}sapphiresync/*", (channel, message) =>
            {
                string channelPath = channel.ToString().Split('/').LastOrDefault();

                switch (channelPath)
                {
                case "changes":
                    SendChangesRequest changesRequest = JsonConvert.DeserializeObject <SendChangesRequest>(message);
                    SyncRequestRequestReceived?.Invoke(changesRequest);
                    break;

                case "publish":
                    SendPublishRequest publishRequest = JsonConvert.DeserializeObject <SendPublishRequest>(message);
                    SyncRequestRequestReceived?.Invoke(publishRequest);
                    break;

                case "message":
                    SendMessageRequest messageRequest = JsonConvert.DeserializeObject <SendMessageRequest>(message);
                    SyncRequestRequestReceived?.Invoke(messageRequest);
                    break;
                }
            });
        }
예제 #2
0
        public async Task Invoke(HttpContext context, SapphireChangeNotifier changeNotifier, SapphireMessageSender sender)
        {
            if (context.Request.Method != "POST")
            {
                await next(context);

                return;
            }

            logger.LogInformation("Started handling nlb message");

            string originId = context.Request.Headers["OriginId"].ToString();

            if (context.Request.Headers["Secret"].ToString().ComputeHash() != options.Nlb.Secret ||
                options.Nlb.Entries.All(e => e.Id != originId))
            {
                logger.LogError("Prevented unauthorized access to nlb sync methods");
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.WriteAsync("You are not allowed to access this endpoint");

                return;
            }

            StreamReader sr          = new StreamReader(context.Request.Body);
            string       requestBody = await sr.ReadToEndAsync();

            requestBody = requestBody.Decrypt(options.Nlb.EncryptionKey);

            string requestPath = context.Request.Path.Value.Split('/').LastOrDefault();

            switch (requestPath)
            {
            case "changes":
                SendChangesRequest changesRequest = JsonConvert.DeserializeObject <SendChangesRequest>(requestBody);

                Type dbType = Assembly.GetEntryAssembly()?.DefinedTypes.FirstOrDefault(t => t.FullName == changesRequest.DbType);

                if (dbType != null)
                {
                    logger.LogInformation("Handling changes from other server");
                    changeNotifier.HandleChanges(changesRequest.Changes, dbType);
                }
                break;

            case "publish":
                SendPublishRequest publishRequest = JsonConvert.DeserializeObject <SendPublishRequest>(requestBody);
                logger.LogInformation("Handling publish from other server");
                sender.Publish(publishRequest.Topic, publishRequest.Message, true);
                break;

            case "message":
                SendMessageRequest messageRequest = JsonConvert.DeserializeObject <SendMessageRequest>(requestBody);
                logger.LogInformation("Handling message from other server");
                sender.Send(messageRequest.Message, true);
                break;
            }
        }
예제 #3
0
        public void SendPublish(string topic, object message)
        {
            SendPublishRequest sendPublishRequest = new SendPublishRequest()
            {
                Topic   = topic,
                Message = message
            };

            SendToNlbs(sendPublishRequest, "publish");
        }
예제 #4
0
        public void SendPublish(string topic, object message, bool retain)
        {
            SendPublishRequest sendPublishRequest = new SendPublishRequest()
            {
                Topic    = topic,
                Message  = message,
                Retain   = retain,
                OriginId = syncContext.SessionId
            };

            Publish(sendPublishRequest);
        }
예제 #5
0
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Method != "POST")
            {
                await next(context);

                return;
            }

            logger.LogInformation("Started handling sync request");

            string originId = context.Request.Headers["OriginId"].ToString();

            if (context.Request.Headers["Secret"].ToString().ComputeHash() != options.Sync.Secret ||
                options.Sync.Entries.All(e => e.Id != originId))
            {
                logger.LogError("Prevented unauthorized access to nlb sync methods");
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.WriteAsync("You are not allowed to access this endpoint");

                return;
            }

            StreamReader sr          = new StreamReader(context.Request.Body);
            string       requestBody = await sr.ReadToEndAsync();

            string requestPath = context.Request.Path.Value.Split('/').LastOrDefault();

            switch (requestPath)
            {
            case "changes":
                SendChangesRequest changesRequest = JsonConvert.DeserializeObject <SendChangesRequest>(requestBody);
                sapphireHttpSyncModule.Received(changesRequest);
                break;

            case "publish":
                SendPublishRequest publishRequest = JsonConvert.DeserializeObject <SendPublishRequest>(requestBody);
                sapphireHttpSyncModule.Received(publishRequest);
                break;

            case "message":
                SendMessageRequest messageRequest = JsonConvert.DeserializeObject <SendMessageRequest>(requestBody);
                sapphireHttpSyncModule.Received(messageRequest);
                break;
            }
        }