diff --git a/Docker/run/config/standalone/server-standalone.json b/Docker/run/config/standalone/server-standalone.json index 42812fd..84b76da 100644 --- a/Docker/run/config/standalone/server-standalone.json +++ b/Docker/run/config/standalone/server-standalone.json @@ -49,10 +49,6 @@ "Endpoints": { "Http": { "Url": "http://+:6000" - }, - "Grpc": { - "Protocols": "Http2", - "Url": "http://+:6005" } } }, diff --git a/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs b/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs new file mode 100644 index 0000000..92fc9b2 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosServer/Controllers/ClientMessageController.cs @@ -0,0 +1,43 @@ +using MareSynchronos.API.Data.Enum; +using MareSynchronos.API.SignalR; +using MareSynchronosServer.Hubs; +using MareSynchronosShared.Utils; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; + +namespace MareSynchronosServer.Controllers; + +[Route("/msgc")] +[Authorize(Policy = "Internal")] +public class ClientMessageController : Controller +{ + private ILogger _logger; + private IHubContext _hubContext; + + public ClientMessageController(ILogger logger, IHubContext hubContext) + { + _logger = logger; + _hubContext = hubContext; + } + + [Route("sendMessage")] + [HttpPost] + public async Task SendMessage(ClientMessage msg) + { + bool hasUid = !string.IsNullOrEmpty(msg.UID); + + if (!hasUid) + { + _logger.LogInformation("Sending Message of severity {severity} to all online users: {message}", msg.Severity, msg.Message); + await _hubContext.Clients.All.Client_ReceiveServerMessage(msg.Severity, msg.Message).ConfigureAwait(false); + } + else + { + _logger.LogInformation("Sending Message of severity {severity} to user {uid}: {message}", msg.Severity, msg.UID, msg.Message); + await _hubContext.Clients.User(msg.UID).Client_ReceiveServerMessage(msg.Severity, msg.Message).ConfigureAwait(false); + } + + return Empty; + } +} diff --git a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj index 9d29e39..b662f74 100644 --- a/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj +++ b/MareSynchronosServer/MareSynchronosServer/MareSynchronosServer.csproj @@ -21,8 +21,6 @@ - - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs b/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs deleted file mode 100644 index ca40ec4..0000000 --- a/MareSynchronosServer/MareSynchronosServer/Services/ClientMessageService.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Grpc.Core; -using MareSynchronos.API.Data.Enum; -using MareSynchronos.API.SignalR; -using MareSynchronosServer.Hubs; -using MareSynchronosShared.Protos; -using Microsoft.AspNetCore.SignalR; -using static MareSynchronosShared.Protos.ClientMessageService; - -namespace MareSynchronosServer.Services; - -public class GrpcClientMessageService : ClientMessageServiceBase -{ - private readonly ILogger _logger; - private readonly IHubContext _hubContext; - - public GrpcClientMessageService(ILogger logger, IHubContext hubContext) - { - _logger = logger; - _hubContext = hubContext; - } - - public override async Task SendClientMessage(ClientMessage request, ServerCallContext context) - { - bool hasUid = !string.IsNullOrEmpty(request.Uid); - - var severity = request.Type switch - { - MessageType.Info => MessageSeverity.Information, - MessageType.Warning => MessageSeverity.Warning, - MessageType.Error => MessageSeverity.Error, - _ => MessageSeverity.Information, - }; - - if (!hasUid) - { - _logger.LogInformation("Sending Message of severity {severity} to all online users: {message}", severity, request.Message); - await _hubContext.Clients.All.Client_ReceiveServerMessage(severity, request.Message).ConfigureAwait(false); - } - else - { - _logger.LogInformation("Sending Message of severity {severity} to user {uid}: {message}", severity, request.Uid, request.Message); - await _hubContext.Clients.User(request.Uid).Client_ReceiveServerMessage(severity, request.Message).ConfigureAwait(false); - } - - return new Empty(); - } -} diff --git a/MareSynchronosServer/MareSynchronosServer/Startup.cs b/MareSynchronosServer/MareSynchronosServer/Startup.cs index b45da78..8f3d499 100644 --- a/MareSynchronosServer/MareSynchronosServer/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServer/Startup.cs @@ -5,8 +5,6 @@ using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.Authorization; using AspNetCoreRateLimit; using MareSynchronosShared.Data; -using MareSynchronosShared.Protos; -using Grpc.Net.Client.Configuration; using MareSynchronosShared.Metrics; using MareSynchronosServer.Services; using MareSynchronosShared.Utils; @@ -73,7 +71,7 @@ public class Startup a.FeatureProviders.Remove(a.FeatureProviders.OfType().First()); if (mareConfig.GetValue(nameof(ServerConfiguration.MainServerAddress), defaultValue: null) == null) { - a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(JwtController))); + a.FeatureProviders.Add(new AllowedControllersFeatureProvider(typeof(MareServerConfigurationController), typeof(MareAuthBaseConfigurationController), typeof(JwtController), typeof(ClientMessageController))); } else { @@ -288,8 +286,6 @@ public class Startup { services.AddSingleton, MareConfigurationServiceServer>(); services.AddSingleton, MareConfigurationServiceServer>(); - - services.AddGrpc(); } } @@ -321,11 +317,6 @@ public class Startup options.Transports = HttpTransportType.WebSockets | HttpTransportType.ServerSentEvents | HttpTransportType.LongPolling; }); - if (config.IsMain) - { - endpoints.MapGrpcService().AllowAnonymous(); - } - endpoints.MapHealthChecks("/health").AllowAnonymous(); endpoints.MapControllers(); diff --git a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs index 536ccfc..af6df71 100644 --- a/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs +++ b/MareSynchronosServer/MareSynchronosServices/Discord/MareModule.cs @@ -8,9 +8,8 @@ using Prometheus; using MareSynchronosShared.Models; using MareSynchronosShared.Utils; using MareSynchronosShared.Services; -using Grpc.Net.ClientFactory; -using MareSynchronosShared.Protos; using StackExchange.Redis; +using MareSynchronos.API.Data.Enum; namespace MareSynchronosServices.Discord; @@ -30,21 +29,19 @@ public class MareModule : InteractionModuleBase private readonly DiscordBotServices _botServices; private readonly IConfigurationService _mareClientConfigurationService; private readonly IConfigurationService _mareServicesConfiguration; - private readonly GrpcClientFactory _grpcClientFactory; private readonly IConnectionMultiplexer _connectionMultiplexer; private Random random = new(); public MareModule(ILogger logger, IServiceProvider services, DiscordBotServices botServices, IConfigurationService mareClientConfigurationService, IConfigurationService mareServicesConfiguration, - GrpcClientFactory grpcClientFactory, IConnectionMultiplexer connectionMultiplexer) + IConnectionMultiplexer connectionMultiplexer) { _logger = logger; _services = services; _botServices = botServices; _mareClientConfigurationService = mareClientConfigurationService; _mareServicesConfiguration = mareServicesConfiguration; - _grpcClientFactory = grpcClientFactory; _connectionMultiplexer = connectionMultiplexer; } @@ -352,7 +349,7 @@ public class MareModule : InteractionModuleBase [SlashCommand("message", "ADMIN ONLY: sends a message to clients")] public async Task SendMessageToClients([Summary("message", "Message to send")] string message, - [Summary("severity", "Severity of the message")] MareSynchronosShared.Protos.MessageType messageType = MareSynchronosShared.Protos.MessageType.Info, + [Summary("severity", "Severity of the message")] MessageSeverity messageType = MessageSeverity.Information, [Summary("uid", "User ID to the person to send the message to")] string? uid = null) { _logger.LogInformation("SlashCommand:{userId}:{Method}:{message}:{type}:{uid}", Context.Interaction.User.Id, nameof(SendMessageToClients), message, messageType, uid); @@ -374,13 +371,10 @@ public class MareModule : InteractionModuleBase try { - var client = _grpcClientFactory.CreateClient("MessageClient"); - await client.SendClientMessageAsync(new ClientMessage() - { - Message = message, - Type = messageType, - Uid = uid ?? string.Empty - }); + using HttpClient c = new HttpClient(); + await c.PostAsJsonAsync(new Uri(_mareServicesConfiguration.GetValue + (nameof(ServicesConfiguration.MainServerAddress)), "/msgc/sendMessage"), new ClientMessage(messageType, message, uid ?? string.Empty)) + .ConfigureAwait(false); var discordChannelForMessages = _mareServicesConfiguration.GetValueOrDefault(nameof(ServicesConfiguration.DiscordChannelForMessages), null); if (uid == null && discordChannelForMessages != null) @@ -390,9 +384,9 @@ public class MareModule : InteractionModuleBase { var embedColor = messageType switch { - MareSynchronosShared.Protos.MessageType.Info => Color.Blue, - MareSynchronosShared.Protos.MessageType.Warning => new Color(255, 255, 0), - MareSynchronosShared.Protos.MessageType.Error => Color.Red, + MessageSeverity.Information => Color.Blue, + MessageSeverity.Warning => new Color(255, 255, 0), + MessageSeverity.Error => Color.Red, _ => Color.Blue }; diff --git a/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj b/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj index 1de88d1..db6d3b3 100644 --- a/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj +++ b/MareSynchronosServer/MareSynchronosServices/MareSynchronosServices.csproj @@ -22,7 +22,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/MareSynchronosServer/MareSynchronosServices/Startup.cs b/MareSynchronosServer/MareSynchronosServices/Startup.cs index a0a9b20..cc6cf68 100644 --- a/MareSynchronosServer/MareSynchronosServices/Startup.cs +++ b/MareSynchronosServer/MareSynchronosServices/Startup.cs @@ -4,8 +4,6 @@ using MareSynchronosShared.Metrics; using Microsoft.EntityFrameworkCore; using Prometheus; using MareSynchronosShared.Utils; -using Grpc.Net.Client.Configuration; -using MareSynchronosShared.Protos; using MareSynchronosShared.Services; using StackExchange.Redis; using MessagePack.Resolvers; @@ -53,12 +51,6 @@ public class Startup services.AddSingleton(m => new MareMetrics(m.GetService>(), new List { }, new List { })); - var noRetryConfig = new MethodConfig - { - Names = { MethodName.Default }, - RetryPolicy = null - }; - var redis = mareConfig.GetValue(nameof(ServerConfiguration.RedisConnectionString), string.Empty); var options = ConfigurationOptions.Parse(redis); options.ClientName = "Mare"; @@ -66,18 +58,6 @@ public class Startup ConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect(options); services.AddSingleton(connectionMultiplexer); - services.AddGrpcClient("MessageClient", c => - { - c.Address = new Uri(mareConfig.GetValue(nameof(ServicesConfiguration.MainServerGrpcAddress))); - }).ConfigureChannel(c => - { - c.ServiceConfig = new ServiceConfig { MethodConfigs = { noRetryConfig } }; - c.HttpHandler = new SocketsHttpHandler() - { - EnableMultipleHttp2Connections = true - }; - }); - var signalRServiceBuilder = services.AddSignalR(hubOptions => { hubOptions.MaximumReceiveMessageSize = long.MaxValue; diff --git a/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj b/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj index fa5a231..f1e1092 100644 --- a/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj +++ b/MareSynchronosServer/MareSynchronosShared/MareSynchronosShared.csproj @@ -5,10 +5,6 @@ enable - - - - @@ -20,7 +16,6 @@ - all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -58,9 +53,7 @@ - - Both - + diff --git a/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto b/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto deleted file mode 100644 index 8426269..0000000 --- a/MareSynchronosServer/MareSynchronosShared/Protos/mareservices.proto +++ /dev/null @@ -1,23 +0,0 @@ -syntax = "proto3"; - -option csharp_namespace = "MareSynchronosShared.Protos"; - -package mareservices; - -service ClientMessageService { - rpc SendClientMessage (ClientMessage) returns (Empty); -} - -message ClientMessage { - MessageType type = 1; - string message = 2; - string uid = 3; -} - -enum MessageType { - INFO = 0; - WARNING = 1; - ERROR = 2; -} - -message Empty { } \ No newline at end of file diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs new file mode 100644 index 0000000..cc7e294 --- /dev/null +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ClientMessage.cs @@ -0,0 +1,4 @@ +using MareSynchronos.API.Data.Enum; + +namespace MareSynchronosShared.Utils; +public record ClientMessage(MessageSeverity Severity, string Message, string UID); diff --git a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs index 8481c46..6e1768f 100644 --- a/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs +++ b/MareSynchronosServer/MareSynchronosShared/Utils/ServicesConfiguration.cs @@ -7,14 +7,13 @@ public class ServicesConfiguration : MareConfigurationBase public string DiscordBotToken { get; set; } = string.Empty; public ulong? DiscordChannelForMessages { get; set; } = null; public ulong? DiscordChannelForReports { get; set; } = null; - public Uri MainServerGrpcAddress { get; set; } = null; public override string ToString() { StringBuilder sb = new(); sb.AppendLine(base.ToString()); sb.AppendLine($"{nameof(DiscordBotToken)} => {DiscordBotToken}"); - sb.AppendLine($"{nameof(MainServerGrpcAddress)} => {MainServerGrpcAddress}"); + sb.AppendLine($"{nameof(MainServerAddress)} => {MainServerAddress}"); sb.AppendLine($"{nameof(DiscordChannelForMessages)} => {DiscordChannelForMessages}"); sb.AppendLine($"{nameof(DiscordChannelForReports)} => {DiscordChannelForReports}"); return sb.ToString();