示例#1
0
        /// <inheritdoc />
        public void Execute(ICommandSender commandSender, string[] args)
        {
            if (args.Length < 2)
            {
                SendUsage(commandSender);
                return;
            }

            // Store whether the action is to authorize or whether to de-authorize
            var authAction = !args[0].Contains("deauth");
            var identifier = args[1];

            if (AuthUtil.IsValidAuthKey(identifier))
            {
                if (authAction)
                {
                    _authorizedList.Add(identifier);
                    commandSender.SendMessage("Auth key has been authorized");
                }
                else
                {
                    _authorizedList.Remove(identifier);
                    commandSender.SendMessage("Auth key has been deauthorized");
                }
            }
            else
            {
                // Since the given argument was not a valid auth key, we try player names instead
                if (!CommandUtil.TryGetPlayerByName(_serverManager.Players, identifier, out var player))
                {
                    commandSender.SendMessage($"Could not find player with name '{identifier}'");
                    return;
                }

                var playerData = (ServerPlayerData)player;

                if (authAction)
                {
                    _authorizedList.Add(playerData.AuthKey);
                    commandSender.SendMessage($"Auth key of player '{player.Username}' has been authorized");
                }
                else
                {
                    _authorizedList.Remove(playerData.AuthKey);
                    commandSender.SendMessage($"Auth key of player '{player.Username}' has been de-authorized");
                }
            }
        }
示例#2
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}'");
        }
示例#3
0
        /// <inheritdoc />
        public void Execute(ICommandSender commandSender, string[] args)
        {
            if (args.Length < 2)
            {
                SendUsage(commandSender);
                return;
            }

            var action = args[1].ToLower();

            if (action == "on")
            {
                if (_whiteList.IsEnabled)
                {
                    commandSender.SendMessage("Whitelist was already enabled");
                    return;
                }

                _whiteList.IsEnabled = true;
                commandSender.SendMessage("Whitelist has been enabled");
            }
            else if (action == "off")
            {
                if (!_whiteList.IsEnabled)
                {
                    commandSender.SendMessage("Whitelist was already disabled");
                    return;
                }

                _whiteList.IsEnabled = false;
                commandSender.SendMessage("Whitelist has been disabled");
            }
            else if (action == "add" || action == "remove")
            {
                if (args.Length < 3)
                {
                    commandSender.SendMessage($"Invalid usage: {Trigger} <add|remove> <auth key|username>");
                    return;
                }

                // Store whether the action is to add or whether to remove
                var addAction = action == "add";

                var identifier = args[2];
                if (AuthUtil.IsValidAuthKey(identifier))
                {
                    if (addAction)
                    {
                        _whiteList.Add(identifier);
                        commandSender.SendMessage("Auth key has been added to whitelist");
                    }
                    else
                    {
                        _whiteList.Remove(identifier);
                        commandSender.SendMessage("Auth key has been removed from whitelist");
                    }
                }
                else
                {
                    // Since the given argument was not a valid auth key, we try player names instead
                    if (!CommandUtil.TryGetPlayerByName(_serverManager.Players, identifier, out var player))
                    {
                        if (addAction)
                        {
                            _whiteList.AddPreList(identifier);
                            commandSender.SendMessage(
                                $"Added player name '{identifier}' to pre-list. The next player that logs in with that name will be whitelisted.");
                        }
                        else
                        {
                            _whiteList.RemovePreList(identifier);
                            commandSender.SendMessage(
                                $"Removed player name '{identifier}' from pre-list. The next player that logs in with that name will no longer be whitelisted.");
                        }

                        return;
                    }

                    var playerData = (ServerPlayerData)player;

                    if (addAction)
                    {
                        _whiteList.Add(playerData.AuthKey);
                        commandSender.SendMessage(
                            $"Auth key of player '{player.Username}' has been added to whitelist");
                    }
                    else
                    {
                        _whiteList.Remove(playerData.AuthKey);
                        commandSender.SendMessage(
                            $"Auth key of player '{player.Username}' has been removed from whitelist");
                    }
                }
            }
            else if (action == "prelist")
            {
                commandSender.SendMessage($"Pre-listed player names: {_whiteList.GetPreListed()}");
            }
            else if (action == "clear")
            {
                if (args.Length < 3)
                {
                    _whiteList.Clear();
                    commandSender.SendMessage("Cleared whitelist");
                    return;
                }

                if (args[2] == "prelist")
                {
                    _whiteList.ClearPreList();
                    commandSender.SendMessage("Clear pre-list of whitelist");
                }
                else
                {
                    commandSender.SendMessage($"Invalid usage: '{Trigger} clear prelist' to clear pre-list");
                }
            }
            else
            {
                SendUsage(commandSender);
            }
        }
示例#4
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);
            }
        }