Exemplo n.º 1
0
        private void SendCommand(RconConnection connection, string commandLine)
        {
            var success = _commandHandler.HandleCommand(connection, commandLine, "");

            if (!success)
            {
                connection.WriteLine("\"" + commandLine + "\": command not found.", Color.Red);
            }
        }
Exemplo n.º 2
0
        private void HandleConnection(object state)
        {
            RconConnection connection = (RconConnection)state;

            connection.WriteLine("RocketRcon v" + _runtime.Version, Color.Cyan);
            connection.WriteLine("Please log in with \"login <username> <password>\".");

            try
            {
                int nonAuthCommandCount = 0;
                if (_config.MaxConcurrentConnections > 0 && _connections.Count > _config.MaxConcurrentConnections)
                {
                    connection.Close("Too many clients connected to RCON!");
                    _logger.LogWarning(connection.ConnectionName + ": Maximum RCON connections has been reached.");
                }
                else
                {
                    while (connection.IsOnline)
                    {
                        connection.Write("> ", Color.Cyan);

                        Thread.Sleep(100);
                        var commandLine = connection.Read();
                        if (commandLine == "")
                        {
                            continue;
                        }

                        if (!connection.Authenticated)
                        {
                            nonAuthCommandCount++;
                            if (nonAuthCommandCount > 4)
                            {
                                connection.Close("Too many commands sent before authentication!");
                                _logger.LogWarning(connection.ConnectionName + ": Client has sent too many commands before authentication!");
                                break;
                            }
                        }

                        commandLine = commandLine.Trim('\n', '\r', ' ', '\0');
                        if (commandLine == "quit")
                        {
                            connection.Close("Quit.");
                            break;
                        }


                        if (string.IsNullOrEmpty(commandLine))
                        {
                            continue;
                        }

                        if (commandLine == "login")
                        {
                            connection.WriteLine(connection.Authenticated
                                ? "Notice: You are already logged in!"
                                : "Syntax: login <user> <password>", Color.Red);
                            continue;
                        }

                        var args = commandLine.Split(' ');
                        if (args.Length > 2 && args[0] == "login")
                        {
                            if (connection.Authenticated)
                            {
                                connection.WriteLine("Notice: You are already logged in!", Color.Red);
                                continue;
                            }

                            foreach (var user in _config.RconUsers)
                            {
                                if (args[1].Equals(user.Name, StringComparison.Ordinal) && args[2].Equals(user.Password, StringComparison.Ordinal))
                                {
                                    connection.Authenticated = true;
                                    _logger.LogInformation(connection.ConnectionName + " has logged in.");
                                    break;
                                }
                            }

                            if (connection.Authenticated)
                            {
                                continue;
                            }

                            connection.Close("Invalid password!");
                            _logger.LogWarning("Client has failed to log in.");
                            break;
                        }


                        if (!connection.Authenticated)
                        {
                            connection.WriteLine("Error: You have not logged in yet! Login with syntax: login <username> <password>", Color.Red);
                            continue;
                        }

                        if (_host.Name == "Rocket.Console" || _scheduler == null)
                        {
                            SendCommand(connection, commandLine);
                        }
                        else
                        {
                            //execute command on main thread
                            _scheduler.ScheduleNextFrame(RconPlugin,
                                                         () => SendCommand(connection, commandLine), "RconCommandExecutionTask");
                        }
                    }
                }
                _connections.Remove(connection);
                _logger.LogInformation(connection.ConnectionName + " has disconnected.");
                connection.Client.Close();
            }
            catch (Exception ex)
            {
                _logger.LogError(connection.ConnectionName + " caused error:", ex);
            }
        }