/// <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"); } } }
/// <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}'"); }
/// <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); } }
public ClientManager( NetClient netClient, ServerManager serverManager, PacketManager packetManager, UiManager uiManager, Settings.GameSettings gameSettings, ModSettings modSettings ) { _netClient = netClient; _serverManager = serverManager; _uiManager = uiManager; _gameSettings = gameSettings; _modSettings = modSettings; _playerData = new Dictionary <ushort, ClientPlayerData>(); _playerManager = new PlayerManager(packetManager, gameSettings, _playerData); _animationManager = new AnimationManager(netClient, _playerManager, packetManager, gameSettings); _mapManager = new MapManager(netClient, gameSettings); _entityManager = new EntityManager(netClient); new PauseManager(netClient).RegisterHooks(); _commandManager = new ClientCommandManager(); var eventAggregator = new EventAggregator(); RegisterCommands(); var clientApi = new ClientApi(this, _commandManager, uiManager, netClient, eventAggregator); _addonManager = new ClientAddonManager(clientApi); ModHooks.FinishedLoadingModsHook += _addonManager.LoadAddons; // Check if there is a valid authentication key and if not, generate a new one if (!AuthUtil.IsValidAuthKey(modSettings.AuthKey)) { modSettings.AuthKey = AuthUtil.GenerateAuthKey(); } // Then authorize the key on the locally hosted server serverManager.AuthorizeKey(modSettings.AuthKey); // Register packet handlers packetManager.RegisterClientPacketHandler <HelloClient>(ClientPacketId.HelloClient, OnHelloClient); packetManager.RegisterClientPacketHandler <ServerClientDisconnect>(ClientPacketId.ServerClientDisconnect, OnDisconnect); packetManager.RegisterClientPacketHandler <PlayerConnect>(ClientPacketId.PlayerConnect, OnPlayerConnect); packetManager.RegisterClientPacketHandler <ClientPlayerDisconnect>(ClientPacketId.PlayerDisconnect, OnPlayerDisconnect); packetManager.RegisterClientPacketHandler <ClientPlayerEnterScene>(ClientPacketId.PlayerEnterScene, OnPlayerEnterScene); packetManager.RegisterClientPacketHandler <ClientPlayerAlreadyInScene>(ClientPacketId.PlayerAlreadyInScene, OnPlayerAlreadyInScene); packetManager.RegisterClientPacketHandler <GenericClientData>(ClientPacketId.PlayerLeaveScene, OnPlayerLeaveScene); packetManager.RegisterClientPacketHandler <PlayerUpdate>(ClientPacketId.PlayerUpdate, OnPlayerUpdate); packetManager.RegisterClientPacketHandler <EntityUpdate>(ClientPacketId.EntityUpdate, OnEntityUpdate); packetManager.RegisterClientPacketHandler <GameSettingsUpdate>(ClientPacketId.GameSettingsUpdated, OnGameSettingsUpdated); packetManager.RegisterClientPacketHandler <ChatMessage>(ClientPacketId.ChatMessage, OnChatMessage); // Register handlers for events from UI uiManager.ConnectInterface.ConnectButtonPressed += Connect; uiManager.ConnectInterface.DisconnectButtonPressed += () => Disconnect(); uiManager.SettingsInterface.OnTeamRadioButtonChange += InternalChangeTeam; uiManager.SettingsInterface.OnSkinIdChange += InternalChangeSkin; UiManager.InternalChatBox.ChatInputEvent += OnChatInput; netClient.ConnectEvent += response => uiManager.OnSuccessfulConnect(); netClient.ConnectFailedEvent += uiManager.OnFailedConnect; // Register the Hero Controller Start, which is when the local player spawns On.HeroController.Start += (orig, self) => { // Execute the original method orig(self); // If we are connect to a server, add a username to the player object if (netClient.IsConnected) { _playerManager.AddNameToPlayer( HeroController.instance.gameObject, _username, _playerManager.LocalPlayerTeam ); } }; // Register handlers for scene change and player update UnityEngine.SceneManagement.SceneManager.activeSceneChanged += OnSceneChange; On.HeroController.Update += OnPlayerUpdate; // Register client connect and timeout handler netClient.ConnectEvent += OnClientConnect; netClient.TimeoutEvent += OnTimeout; // Register application quit handler ModHooks.ApplicationQuitHook += OnApplicationQuit; }
/// <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); } }