public void Awake() { listener = new TcpListener(IPAddress.Any, R.Settings.Instance.RCON.Port); listener.Start(); // Logger.Log("Waiting for new connection..."); waitingThread = new Thread(() => { while (!exiting) { RCONConnection newclient = new RCONConnection(listener.AcceptTcpClient()); clients.Add(newclient); newclient.Send("RocketRcon v" + Assembly.GetExecutingAssembly().GetName().Version + "\r\n"); ThreadPool.QueueUserWorkItem(handleConnection, newclient); } }); waitingThread.Start(); }
private static void handleConnection(object obj) { try { RCONConnection newclient = (RCONConnection)obj; string command = ""; while (newclient.Client.Client.Connected) { Thread.Sleep(100); command = newclient.Read(); if (command == "") { break; } command = command.TrimEnd('\n', '\r', ' '); if (command == "quit") { break; } if (command == "ia") { //newclient.Send("Toggled interactive mode"); newclient.Interactive = !newclient.Interactive; } if (command == "") { continue; } if (command == "login") { if (newclient.Authenticated) { newclient.Send("Notice: You are already logged in!\r\n"); } else { newclient.Send("Syntax: login <password>"); } continue; } if (command.Split(' ').Length > 1 && command.Split(' ')[0] == "login") { if (newclient.Authenticated) { newclient.Send("Notice: You are already logged in!\r\n"); continue; } else { if (command.Split(' ')[1] == R.Settings.Instance.RCON.Password) { newclient.Authenticated = true; //newclient.Send("Success: You have logged in!\r\n"); //Logger.Log("Client has logged in!"); continue; } else { newclient.Send("Error: Invalid password!\r\n"); Logger.Log("Client has failed to log in."); break; } } } if (command == "set") { newclient.Send("Syntax: set [option] [value]"); continue; } if (!newclient.Authenticated) { newclient.Send("Error: You have not logged in yet!\r\n"); continue; } if (command != "ia") { Logger.Log("Client has executed command \"" + command + "\""); } R.Commands.Execute(new ConsolePlayer(), command); command = ""; } clients.Remove(newclient); newclient.Send("Good bye!"); Thread.Sleep(1500); Logger.Log("Client has disconnected! (IP: " + newclient.Client.Client.RemoteEndPoint + ")"); newclient.Close(); } catch (Exception ex) { Logger.LogException(ex); } }
private static void handleConnection(object obj) { try { RCONConnection newclient = (RCONConnection)obj; string command = ""; int nonAuthCommandCount = 0; bool maxClientsReached = false; if (R.Settings.Instance.RCON.EnableMaxGlobalConnections) { if (clients.Count > R.Settings.Instance.RCON.MaxGlobalConnections) { maxClientsReached = true; newclient.Send("Error: Too many clients connected to RCON, not accepting connection!\r\n"); Logger.LogWarning("Maximum global RCON connections has been reached."); } } if (R.Settings.Instance.RCON.EnableMaxLocalConnections && !maxClientsReached) { int currentLocalCount = 0; for (int i = 0; i < clients.Count; i++) { if (newclient.Client.Client.Connected && clients[i].Client.Client.Connected) { if (((IPEndPoint)newclient.Client.Client.RemoteEndPoint).Address.Equals(((IPEndPoint)clients[i].Client.Client.RemoteEndPoint).Address)) { currentLocalCount++; if (currentLocalCount > R.Settings.Instance.RCON.MaxLocalConnections) { maxClientsReached = true; newclient.Send("Error: Too many clients connected from your address, not accepting connection!\r\n"); Logger.LogWarning("Maximum Local RCON connections has been reached for address: " + newclient.Address + "."); break; } } } } } while (newclient.Client.Client.Connected && !maxClientsReached) { Thread.Sleep(100); command = newclient.Read(); if (command == "") { break; } if (!newclient.Authenticated) { nonAuthCommandCount++; if (nonAuthCommandCount > 4) { newclient.Send("Error: Too many commands sent before Authentication!\r\n"); Logger.LogWarning("Client has sent too many commands before Authentication!"); break; } } command = command.Trim('\n', '\r', ' ', '\0'); if (command == "quit") { break; } if (command == "ia") { //newclient.Send("Toggled interactive mode"); newclient.Interactive = !newclient.Interactive; } if (command == "") { continue; } if (command == "login") { if (newclient.Authenticated) { newclient.Send("Notice: You are already logged in!\r\n"); } else { newclient.Send("Syntax: login <password>\r\n"); } continue; } if (command.Split(' ').Length > 1 && command.Split(' ')[0] == "login") { if (newclient.Authenticated) { newclient.Send("Notice: You are already logged in!\r\n"); continue; } else { if (command.Split(' ')[1] == R.Settings.Instance.RCON.Password) { newclient.Authenticated = true; //newclient.Send("Success: You have logged in!\r\n"); //Logger.Log("Client has logged in!"); continue; } else { newclient.Send("Error: Invalid password!\r\n"); Logger.LogWarning("Client has failed to log in."); break; } } } if (command == "set") { newclient.Send("Syntax: set [option] [value]"); continue; } if (!newclient.Authenticated) { newclient.Send("Error: You have not logged in yet! Login with syntax: login <password>\r\n"); continue; } if (command != "ia") { Logger.Log("Client ID: " + newclient.InstanceID + " has executed command \"" + command + "\""); } lock (commands) { commands.Enqueue(command); } command = ""; } clients.Remove(newclient); newclient.Send("Good bye!"); Thread.Sleep(1500); Logger.Log("Client ID: " + newclient.InstanceID + " has disconnected! (IP: " + newclient.Address + ")"); newclient.Close(); } catch (Exception ex) { Logging.Logger.LogException(ex); } }