public void Test1()
        {
            const string original = "test string for encryption and decryption test";

            var encrypted = EncryptionProvider.AESEncrypt(original, _key, _iv);

            Assert.NotNull(encrypted);
            Assert.IsNotEmpty(encrypted);

            var decrypted = EncryptionProvider.AESDecrypt(encrypted, _key, _iv);

            Assert.NotNull(decrypted);
            Assert.IsNotEmpty(decrypted);

            Assert.AreEqual(original, decrypted);
        }
        public async Task TriggerRconNotification(PickupQueue queue)
        {
            if (!queue.Rcon)
            {
                return;
            }

            var rconPassword = "";
            var rconPort     = 0;
            var rconHost     = "";

            var server = await _serverRepository.Find(Convert.ToUInt64(queue.GuildId), queue.Host);

            if (server != null)
            {
                rconPort     = server.Port;
                rconHost     = server.Host;
                rconPassword = !string.IsNullOrWhiteSpace(server.RconPassword) ?
                               EncryptionProvider.AESDecrypt(server.RconPassword, _encryptionSettings.Key, _encryptionSettings.IV) :
                               string.Empty;
            }

            if (string.IsNullOrWhiteSpace(rconPassword) || string.IsNullOrWhiteSpace(rconHost) || rconPort == 0)
            {
                return;
            }

            try
            {
                var redTeam  = queue.Teams.FirstOrDefault();
                var blueTeam = queue.Teams.LastOrDefault();

                var command = $"say \"^2Pickup '^3{queue.Name}^2' has started! " +
                              $"^1RED TEAM: ^5{string.Join(", ", redTeam.Subscribers.Select(w => w.Name))} ^7- " +
                              $"^4BLUE TEAM: ^5{string.Join(", ", blueTeam.Subscribers.Select(w => w.Name))}\"";

                await RCON.UDPSendCommand(command, rconHost, rconPassword, rconPort, true);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }
Пример #3
0
        public async Task ServerStatus(string host = "")
        {
            if (string.IsNullOrWhiteSpace(_rconPassword) || string.IsNullOrWhiteSpace(_rconHost) || _rconPort == 0)
            {
                return;
            }

            var rconPassword = _rconPassword;
            var rconPort     = _rconPort;
            var rconHost     = _rconHost;

            if (!string.IsNullOrEmpty(host))
            {
                var server = await _serverRepository.Find(Context.Guild.Id, host);

                if (server != null)
                {
                    rconPort     = server.Port;
                    rconHost     = server.Host;
                    rconPassword = !string.IsNullOrWhiteSpace(server.RconPassword) ?
                                   EncryptionProvider.AESDecrypt(server.RconPassword, _encryptionSettings.Key, _encryptionSettings.IV) :
                                   string.Empty;

                    if (string.IsNullOrWhiteSpace(rconPassword) || string.IsNullOrWhiteSpace(rconHost) || rconPort == 0)
                    {
                        await Context.Message.ReplyAsync(
                            $"Can't show server status since no rcon password has been set for the server {host}")
                        .AutoRemoveMessage(15);

                        return;
                    }
                }
            }

            try
            {
                var response = await RCON.UDPSendCommand("status", rconHost, rconPassword, rconPort).ConfigureAwait(false);

                _logger.LogInformation($"serverstatus response: {response}");
                var serverStatus = new ServerStatus(response);

                var embed = new EmbedBuilder
                {
                    Title       = $"Server status for {rconHost}",
                    Description = $"**Map:** _{serverStatus.Map} _" +
                                  $"{Environment.NewLine}" +
                                  "**Players**" +
                                  $"{Environment.NewLine}"
                };

                embed.Description += $"```{Environment.NewLine}" +
                                     (serverStatus.Players.Any() ?
                                      $"{serverStatus.PlayersToTable()}" :
                                      "No players are currently online") +
                                     $"{Environment.NewLine}```";

                await Context.Message.ReplyAsync(embed : embed.Build());
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }