private async Task <bool> RunCommand(Network.Rcon rcon, string command, bool output = true) { try { if (this.Verbose) { Console.WriteLine("Running command: ".DarkGray(), command.Gray()); } var response = await rcon.Command(command, TimeSpan.FromSeconds(Math.Max(this.Timeout, 1))); if (response.Equals($"Invalid password.{Environment.NewLine}", StringComparison.InvariantCulture) || response.Equals($"The server must set rcon_password to be able to use this command.{Environment.NewLine}", StringComparison.InvariantCulture) || response.StartsWith("No such command ", StringComparison.InvariantCulture)) { Console.Write(response.DarkRed()); return(!response.StartsWith("No such command ", StringComparison.InvariantCulture)); } if (output) { Console.Write(response); } return(false); } catch (TimeoutException ex) { Console.WriteLine("Unable to communicate with ".DarkRed(), $"{this.Host}:{this.Port}".Red(), $": {ex.Message}".DarkRed()); return(true); } }
public override async Task <int> Main() { if (string.IsNullOrEmpty(this.Password)) { this.Password = Input.Password("Password"); System.Console.CursorTop = 0; System.Console.CursorLeft = 0; System.Console.Write(new string(' ', System.Console.WindowWidth - 1)); System.Console.CursorLeft = 0; } if (!this.Quiet) { Console.WriteLine("Connecting to ", $"{this.Host}:{this.Port}".White(), "..."); } var result = 1; var ip = Dns.GetHostEntry(this.Host).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork); if (this.Verbose) { Console.WriteLine("Connecting to ".DarkGray(), $"{ip}:{this.Port}".Gray(), "...".DarkGray()); } using (var rcon = new Network.Rcon(ip, this.Port, this.Password)) { if (this.Command == null) { if (this.Verbose) { Console.WriteLine("Testing connection...".DarkGray()); } var connected = !await RunCommand(rcon, "version", false); if (this.Verbose) { if (connected) { Console.WriteLine("Connection successful".DarkGray()); Console.WriteLine("Running interactively".DarkGray()); } else { Console.WriteLine("Connection failed".DarkGray()); } } while (connected && !await RunCommand(rcon, Input.String("#"))) { } } else { result = await RunCommand(rcon, this.Command) ? 1 : 0; } } if (!this.Quiet) { Console.WriteLine("Closing connection to server"); } return(result); }