Esempio n. 1
0
        /// <inheritdoc />
        public void Execute(ICommandSender commandSender, string[] args)
        {
            if (args.Length < 2)
            {
                commandSender.SendMessage($"Invalid usage: {Trigger} <auth key|username|ip address>");
                return;
            }

            var identifier = args[1];

            // Cast each element in the collection of players to ServerPlayerData
            var players = _serverManager.Players.Select(p => (ServerPlayerData)p).ToList();

            // Check if the identifier argument is an authentication key, which by definition means that it can't
            // be a player name or IP address
            if (AuthUtil.IsValidAuthKey(identifier))
            {
                if (!CommandUtil.TryGetPlayerByAuthKey(players, identifier, out var playerWithAuthKey))
                {
                    commandSender.SendMessage("Could not find player with given auth key");
                    return;
                }

                commandSender.SendMessage("Player with auth key has been kicked");
                KickPlayer(playerWithAuthKey);
                return;
            }

            // Check if a player is connected that has the same IP as the given argument
            if (CommandUtil.TryGetPlayerByIpAddress(players, identifier, out var playerWithIp))
            {
                commandSender.SendMessage($"Player with IP '{identifier}' has been kicked");
                KickPlayer(playerWithIp);
                return;
            }

            // Check if a player is connected with the same name as the given argument
            if (CommandUtil.TryGetPlayerByName(_serverManager.Players, identifier, out var playerWithName))
            {
                commandSender.SendMessage($"Player '{playerWithName.Username}' has been kicked");
                KickPlayer(playerWithName);
                return;
            }

            commandSender.SendMessage($"Could not find player with name, auth key or IP address '{identifier}'");
        }
Esempio n. 2
0
        /// <inheritdoc />
        public void Execute(ICommandSender commandSender, string[] args)
        {
            var ipBan = args[0].Contains("ip");
            var unban = args[0].Contains("unban");

            if (args.Length < 2)
            {
                SendUsage(commandSender, ipBan, unban);
                return;
            }

            var       identifier = args[1];
            IPAddress address;

            if (unban)
            {
                if (ipBan)
                {
                    if (!IPAddress.TryParse(identifier, out address))
                    {
                        commandSender.SendMessage("Given argument is not a valid IP address");
                        return;
                    }

                    _banList.RemoveIp(address.ToString());
                    commandSender.SendMessage($"IP address '{identifier}' has been unbanned");
                }
                else
                {
                    if (!AuthUtil.IsValidAuthKey(identifier))
                    {
                        commandSender.SendMessage("Given argument is not a valid authentication key");
                        return;
                    }

                    _banList.Remove(identifier);
                    commandSender.SendMessage("Auth key has been unbanned");
                }

                return;
            }

            // If the command is not an unban, we check whether the second argument is a special argument
            if (identifier == "clear")
            {
                if (ipBan)
                {
                    _banList.ClearIps();
                    commandSender.SendMessage("Cleared IP addresses from ban list");
                }
                else
                {
                    _banList.Clear();
                    commandSender.SendMessage("Cleared auth keys from ban list");
                }

                return;
            }

            // To make sure we can still (ip) ban a player that has the name "clear",
            // we add an intermediate argument
            if (identifier == "add")
            {
                if (args.Length < 3)
                {
                    SendUsage(commandSender, ipBan, false, true);
                    return;
                }

                identifier = args[2];
            }

            // Cast each element in the collection of players to ServerPlayerData
            var players = _serverManager.Players.Select(p => (ServerPlayerData)p).ToList();

            // Check if the identifier argument is an authentication key, which by definition means that it can't
            // be a player name or IP address
            if (AuthUtil.IsValidAuthKey(identifier))
            {
                var foundPlayerWithAuthKey = CommandUtil.TryGetPlayerByAuthKey(
                    players,
                    identifier,
                    out var playerWithAuthKey
                    );

                // First check if this is not an IP ban, because then we simply add the auth key to the ban list
                if (!ipBan)
                {
                    _banList.Add(identifier);
                    commandSender.SendMessage("Auth key has been banned");

                    if (foundPlayerWithAuthKey)
                    {
                        DisconnectPlayer(playerWithAuthKey);
                    }

                    return;
                }

                // If it is an IP ban, we can only issue it if a player with that auth key is online
                if (!foundPlayerWithAuthKey)
                {
                    commandSender.SendMessage($"Could not find player with given auth key");
                    return;
                }

                _banList.AddIp(playerWithAuthKey.IpAddressString);
                commandSender.SendMessage($"IP address '{playerWithAuthKey.IpAddressString}' has been banned");

                DisconnectPlayer(playerWithAuthKey);

                return;
            }

            // Now we check whether the argument supplied is the username of a player
            if (CommandUtil.TryGetPlayerByName(_serverManager.Players, identifier, out var player))
            {
                var playerData = (ServerPlayerData)player;

                // Based on whether it is an IP ban or not, add it to the correct ban list and inform the
                // command sender of the behaviour
                if (ipBan)
                {
                    _banList.AddIp(playerData.IpAddressString);
                    commandSender.SendMessage($"IP address of player '{playerData.Username}' has been banned");
                }
                else
                {
                    _banList.Add(playerData.AuthKey);
                    commandSender.SendMessage($"Player '{playerData.Username}' has been banned");
                }

                DisconnectPlayer(playerData);

                return;
            }

            // If it is not an IP ban, we have not found the user that was targeted by the identifier
            if (!ipBan)
            {
                commandSender.SendMessage($"Could not find player with name or auth key '{identifier}'");
                return;
            }

            // Now we can check whether the argument was an IP address
            if (!IPAddress.TryParse(identifier, out address))
            {
                commandSender.SendMessage($"Could not find player with name, auth key or IP address '{identifier}'");
                return;
            }

            _banList.AddIp(address.ToString());
            commandSender.SendMessage($"IP address '{identifier}' has been banned");

            // If a player with the given IP is connected, disconnect them
            if (CommandUtil.TryGetPlayerByIpAddress(players, address.ToString(), out var playerWithIp))
            {
                DisconnectPlayer(playerWithIp);
            }
        }