public void StartListening() { if (IsListening) { return; } IsListening = true; _listener = new TcpListener(IPAddress.Parse(_config.BindIp), _config.ListenPort); _listener.Start(); // Logger.Log("Waiting for new connection..."); _waitingThread = new Thread(() => { while (IsListening) { RconConnection connection = new RconConnection(_container, _listener.AcceptTcpClient(), this, _logger, ++_connectionId, _config.UseAnsiFormatting); _connections.Add(connection); ThreadPool.QueueUserWorkItem(HandleConnection, connection); } }); _waitingThread.Start(); }
static void Main(string[] args) { /* * new Thread(() => * { * Thread.CurrentThread.IsBackground = true; * ArkChatTest(); * }).Start(); * Console.ReadLine();s * return;*/ RconConnection rc = RconConnection.ConnectToRcon("10.0.1.13", 27020, ""); //127.0.0.1 Console.WriteLine("connected"); while (true) { string msg = Console.ReadLine(); if (msg == "die") { break; } RconResponse rr = rc.SendCommand(msg); Console.WriteLine("sent"); Console.WriteLine("response status: " + rr.status.ToString()); Console.WriteLine("response: " + rr.body); } rc.DisposeNetworking(); Console.WriteLine("disconnected"); Console.ReadLine(); }
private void SendCommand(RconConnection connection, string commandLine) { var success = _commandHandler.HandleCommand(connection, commandLine, ""); if (!success) { connection.WriteLine("\"" + commandLine + "\": command not found.", Color.Red); } }
static void ArkChatTest() { //Connect, then spam. RconConnection rc = RconConnection.ConnectToRcon("10.0.1.13", 27020, ""); Console.WriteLine("connected"); Random rand = new Random(); int good = 0; int bad = 0; while (true) { Thread.Sleep(300); string uuid = RandomString(24, rand); string msg = "ServerChat This is a test. Please ignore. APP_ENDORANCE_TEST_ID_" + uuid; //Send chat rc.SendCommand(msg); RconResponse rr = rc.SendCommand("GetChat"); bool ok = rr.status == RconResponseStatus.Ok; if (ok) { //Check to see if it contains it ok = rr.body.Contains(uuid); } if (!ok) { Console.Write("\r" + rr.body + "\n"); } Console.Write("\rGot message " + uuid + " - Status: " + ok + " - RawStatus: " + rr.status.ToString() + " - OK: " + good.ToString() + " - Bad: " + bad.ToString()); if (ok) { good++; } else { bad++; } } }
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); } }