public static void HandleCommand(string commandArgs) { ClientObject pmPlayer = null; int matchedLength = 0; foreach (ClientObject testPlayer in ClientHandler.GetClients()) { //Only search authenticated players if (testPlayer.authenticated) { //Try to match the longest player name if (commandArgs.StartsWith(testPlayer.playerName) && testPlayer.playerName.Length > matchedLength) { //Double check there is a space after the player name if ((commandArgs.Length > (testPlayer.playerName.Length + 1)) ? commandArgs[testPlayer.playerName.Length] == ' ' : false) { pmPlayer = testPlayer; matchedLength = testPlayer.playerName.Length; } } } } if (pmPlayer != null) { string messageText = commandArgs.Substring(pmPlayer.playerName.Length + 1); Messages.Chat.SendChatMessageToClient(pmPlayer, messageText); } else { DarkLog.Normal("Player not found!"); } }
internal void SetVesselProtectionCommand(string commandText) { if (editVessel == Guid.Empty) { DarkLog.Normal("Set /editvessel first"); } if (!vesselPermissions.ContainsKey(editVessel)) { DarkLog.Normal("Set /vesselowner first"); return; } if (commandText == "public") { DarkLog.Normal("Set " + editVessel + " to public"); SetVesselProtection(editVessel, VesselProtectionType.PUBLIC); } if (commandText == "private") { DarkLog.Normal("Set " + editVessel + " to private"); SetVesselProtection(editVessel, VesselProtectionType.PRIVATE); } if (commandText == "group") { if (vesselPermissions[editVessel].group == null || vesselPermissions[editVessel].group == "") { DarkLog.Normal("Set /vesselgroup first"); return; } SetVesselProtection(editVessel, VesselProtectionType.GROUP); DarkLog.Normal("Set " + editVessel + " to group"); } }
public void BanPublicKey(string commandArgs) { string publicKey = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { publicKey = commandArgs.Substring(0, commandArgs.IndexOf(" ", StringComparison.Ordinal)); reason = commandArgs.Substring(commandArgs.IndexOf(" ", StringComparison.Ordinal) + 1); } ClientObject player = ClientHandler.GetClientByPublicKey(publicKey); if (reason == "") { reason = "no reason specified"; } if (player != null) { Messages.ConnectionEnd.SendConnectionEnd(player, "You were banned from the server!"); } bannedPublicKeys.Add(publicKey); SaveBans(); DarkLog.Normal("Public key '" + publicKey + "' was banned from the server: " + reason); }
public void AddPlayerAdmin(string playerName, string groupName) { if (groupName == null || groupName == "") { DarkLog.Normal("Cannot create groups with empty name"); return; } if (groupName.StartsWith(".", StringComparison.Ordinal)) { DarkLog.Normal("Cannot create groups with a leading dot."); return; } AddPlayerToGroup(playerName, groupName); lock (playerGroups) { if (!groupAdmins.ContainsKey(groupName)) { groupAdmins.Add(groupName, new List <string>()); } if (!groupAdmins[groupName].Contains(playerName)) { groupAdmins[groupName].Add(playerName); } } SavePlayer(playerName); }
private static void AcceptTcpClient(IAsyncResult ar) { if (!_running) { return; } TcpClient tcpClient = _tcpListener.EndAcceptTcpClient(ar); // check if IP is on banlist if (BanSystem.fetch.IsIPBanned(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address)) { DarkLog.Normal("Refused RCON connection from " + ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString() + " (IP Banned)"); tcpClient.Close(); _tcpListener.BeginAcceptTcpClient(AcceptTcpClient, null); return; } RCONClient client = new RCONClient(tcpClient); DarkLog.Normal("RCON connection from " + client.RemoteIP.ToString()); Clients.Add(client); client.Start(); // accept another client _tcpListener.BeginAcceptTcpClient(AcceptTcpClient, null); }
internal void HandleNewGameData(string[] files, string[] sha, ClientObject client) { if (files.Length != sha.Length) { DarkLog.Normal("Files and SHA list does not match, not using modpack"); return; } clientReceived = 0; clientData.Clear(); List <string> tempRequestObjects = new List <string>(); for (int i = 0; i < files.Length; i++) { clientData.Add(files[i], sha[i]); //Skip files we have if (modpackData.ContainsKey(files[i]) && modpackData[files[i]] == sha[i]) { continue; } if (!tempRequestObjects.Contains(sha[i])) { tempRequestObjects.Add(sha[i]); } } using (MessageWriter mw = new MessageWriter()) { mw.Write <int>((int)ModpackDataMessageType.REQUEST_OBJECT); mw.Write <string[]>(tempRequestObjects.ToArray()); Messages.Modpack.SendModData(client, mw.GetMessageBytes()); } }
public void KickPlayer(string commandArgs) { string playerName = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { playerName = commandArgs.Substring(0, commandArgs.IndexOf(" ")); reason = commandArgs.Substring(commandArgs.IndexOf(" ") + 1); } ClientObject player = null; if (playerName != "") { player = GetClientByName(playerName); if (player != null) { DarkLog.Normal("Kicking " + playerName + " from the server"); if (reason == "") { reason = "no reason specified"; } m_server.Kick(player.Id, "You are banned from the server : " + reason); } } else { DarkLog.Error("Syntax error. Usage: /kick playername [reason]"); } }
public void BanPlayer(string commandArgs) { string playerName = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { playerName = commandArgs.Substring(0, commandArgs.IndexOf(" ")); reason = commandArgs.Substring(commandArgs.IndexOf(" ") + 1); } if (playerName != "") { ClientObject player = GetClientByName(playerName); if (reason == "") { reason = "no reason specified"; } if (player != null) { m_server.Kick(player.Id, "You are banned from the server : " + reason); } DarkLog.Normal("Player '" + playerName + "' was banned from the server: " + reason); m_banList.AddName(playerName); } }
public void BanIP(string commandArgs) { string ip = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { ip = commandArgs.Substring(0, commandArgs.IndexOf(" ", StringComparison.Ordinal)); reason = commandArgs.Substring(commandArgs.IndexOf(" ", StringComparison.Ordinal) + 1); } IPAddress ipAddress; if (IPAddress.TryParse(ip, out ipAddress)) { ClientObject player = ClientHandler.GetClientByIP(ipAddress); if (player != null) { Messages.ConnectionEnd.SendConnectionEnd(player, "You were banned from the server!"); } bannedIPs.Add(ipAddress); SaveBans(); DarkLog.Normal("IP Address '" + ip + "' was banned from the server: " + reason); } else { DarkLog.Normal(ip + " is not a valid IP address"); } }
public static void HandleServerInput(string input) { string commandPart = input; string argumentPart = ""; if (commandPart.Contains(" ")) { if (commandPart.Length > commandPart.IndexOf(' ') + 1) { argumentPart = commandPart.Substring(commandPart.IndexOf(' ') + 1); } commandPart = commandPart.Substring(0, commandPart.IndexOf(' ')); } if (commandPart.Length > 0) { if (commands.ContainsKey(commandPart)) { try { commands[commandPart].func(argumentPart); } catch (Exception e) { DarkLog.Error("Error handling server command " + commandPart + ", Exception " + e); } } else { DarkLog.Normal("Unknown server command: " + commandPart); } } }
public static void RunNukeEverything(string commandText) { lock (Server.universeSizeLock) { string[] vesselList = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); int numberOfRemovals = 0; foreach (string vesselFile in vesselList) { string vesselID = Path.GetFileNameWithoutExtension(vesselFile); DarkLog.Normal("Removing vessel " + vesselID + " from universe"); //Delete it from the universe if (File.Exists(vesselFile)) { File.Delete(vesselFile); } //Send a vessel remove message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_REMOVE; using (MessageWriter mw = new MessageWriter()) { //Send it with a delete time of 0 so it shows up for all players. mw.Write <double>(0); mw.Write <string>(vesselID); mw.Write <bool>(false); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(null, newMessage, false); numberOfRemovals++; } DarkLog.Normal("Nuked " + numberOfRemovals + " from the universe"); } }
public void BanPublicKey(string commandArgs) { string publicKey = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { publicKey = commandArgs.Substring(0, commandArgs.IndexOf(" ")); reason = commandArgs.Substring(commandArgs.IndexOf(" ") + 1); } ClientObject player = GetClientByPublicKey(publicKey); if (reason == "") { reason = "no reason specified"; } if (player != null) { DisconnectClient(player, "You were banned from the server!"); } //m_banList.BanKey(publicKey); DarkLog.Normal("Public key '" + publicKey + "' was banned from the server: " + reason); }
private static void SetupTCPServer() { try { IPAddress bindAddress = IPAddress.Parse(Settings.settingsStore.address); TCPServer = new TcpListener(new IPEndPoint(bindAddress, Settings.settingsStore.port)); try { if (System.Net.Sockets.Socket.OSSupportsIPv6) { //Windows defaults to v6 only, but this option does not exist in mono so it has to be in a try/catch block along with the casted int. if (Environment.OSVersion.Platform != PlatformID.MacOSX && Environment.OSVersion.Platform != PlatformID.Unix) { TCPServer.Server.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0); } } } catch { //Don't care - On linux and mac this throws because it's already set, and on windows it just works. } TCPServer.Start(4); TCPServer.BeginAcceptTcpClient(new AsyncCallback(NewClientCallback), null); } catch (Exception e) { DarkLog.Normal("Error setting up server, Exception: " + e); Server.serverRunning = false; } Server.serverStarting = false; }
public void BanPlayer(string commandArgs) { string playerName = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { playerName = commandArgs.Substring(0, commandArgs.IndexOf(" ", StringComparison.Ordinal)); reason = commandArgs.Substring(commandArgs.IndexOf(" ", StringComparison.Ordinal) + 1); } if (playerName != "") { ClientObject player = ClientHandler.GetClientByName(playerName); if (reason == "") { reason = "no reason specified"; } if (player != null) { Messages.ConnectionEnd.SendConnectionEnd(player, "You were banned from the server!"); } DarkLog.Normal("Player '" + playerName + "' was banned from the server"); bannedNames.Add(playerName); SaveBans(); } }
private void Load() { vesselPermissions.Clear(); lock (vesselPermissions) { string[] files = Directory.GetFiles(vesselsFolder); foreach (string file in files) { Guid vesselGuid = Guid.Parse(Path.GetFileNameWithoutExtension(file)); try { using (StreamReader sr = new StreamReader(file)) { string playerString = sr.ReadLine(); string protectionString = sr.ReadLine(); string groupName = sr.ReadLine(); VesselPermission vp = new VesselPermission(vesselGuid, playerString); vp.protection = (VesselProtectionType)Enum.Parse(typeof(VesselProtectionType), protectionString); vp.group = groupName; vesselPermissions.Add(vesselGuid, vp); } } catch { DarkLog.Normal("Deleting vessel permissions file " + file + " as it is broken."); File.Delete(file); } } } }
private static void StartHTTPServer() { if (Settings.settingsStore.httpPort > 0) { DarkLog.Normal("Starting HTTP server..."); httpListener = new HttpListener(); try { if (Settings.settingsStore.address != "0.0.0.0") { httpListener.Prefixes.Add("http://" + Settings.settingsStore.address + ":" + Settings.settingsStore.httpPort + '/'); } else { httpListener.Prefixes.Add("http://*:" + Settings.settingsStore.httpPort + '/'); } httpListener.Start(); httpListener.BeginGetContext(asyncHTTPCallback, httpListener); } catch (Exception e) { DarkLog.Error("Error while starting HTTP server: " + e + "\nPlease try running the server as an administrator."); } } }
public static void KickPlayer(string commandArgs) { string playerName = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { playerName = commandArgs.Substring(0, commandArgs.IndexOf(" ", StringComparison.Ordinal)); reason = commandArgs.Substring(commandArgs.IndexOf(" ", StringComparison.Ordinal) + 1); } ClientObject player = null; if (playerName != "") { player = ClientHandler.GetClientByName(playerName); if (player != null) { DarkLog.Normal("Kicking " + playerName + " from the server"); if (reason != "") { Messages.ConnectionEnd.SendConnectionEnd(player, "Kicked from the server, " + reason); } else { Messages.ConnectionEnd.SendConnectionEnd(player, "Kicked from the server"); } } } else { DarkLog.Error("Syntax error. Usage: /kick playername [reason]"); } }
public void BanIP(string commandArgs) { string ip = commandArgs; string reason = ""; if (commandArgs.Contains(" ")) { ip = commandArgs.Substring(0, commandArgs.IndexOf(" ")); reason = commandArgs.Substring(commandArgs.IndexOf(" ") + 1); } IPAddress ipAddress; if (IPAddress.TryParse(ip, out ipAddress)) { ClientObject player = GetClientByIP(ipAddress); if (reason == "") { reason = "no reason specified"; } if (player != null) { m_server.Kick(player.Id, "You are banned from the server : " + reason); } m_banList.AddIp(ipAddress); DarkLog.Normal("IP Address '" + ip + "' was banned from the server: " + reason); } else { DarkLog.Normal(ip + " is not a valid IP address"); } }
public void Run() { string input = AsyncConsoleReader.ReadLine(); while (input != "") { try { DarkLog.Normal("Command input: " + input); if (input.StartsWith("/")) { HandleServerInput(input.Substring(1)); } else { if (input != "") { m_commands["say"].func(input); } } input = AsyncConsoleReader.ReadLine(); } catch (Exception e) { if (Server.serverRunning) { DarkLog.Fatal("Error in command handler thread, Exception: " + e); throw; } } } }
public static void RunNukeKSC(string commandText) { lock (Server.universeSizeLock) { string[] vesselList = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); int numberOfRemovals = 0; foreach (string vesselFile in vesselList) { string vesselID = Path.GetFileNameWithoutExtension(vesselFile); bool landedAtKSC = false; bool landedAtRunway = false; using (StreamReader sr = new StreamReader(vesselFile)) { string currentLine = sr.ReadLine(); while (currentLine != null && !landedAtKSC && !landedAtRunway) { string trimmedLine = currentLine.Trim(); if (trimmedLine.StartsWith("landedAt = ")) { string landedAt = trimmedLine.Substring(trimmedLine.IndexOf("=") + 2); if (landedAt == "KSC") { landedAtKSC = true; } if (landedAt == "Runway") { landedAtRunway = true; } } currentLine = sr.ReadLine(); } } if (landedAtKSC | landedAtRunway) { DarkLog.Normal("Removing vessel " + vesselID + " from KSC"); //Delete it from the universe if (File.Exists(vesselFile)) { File.Delete(vesselFile); } //Send a vessel remove message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_REMOVE; using (MessageWriter mw = new MessageWriter()) { //Send it with a delete time of 0 so it shows up for all players. mw.Write <int>(0); mw.Write <double>(0); mw.Write <string>(vesselID); mw.Write <bool>(false); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(null, newMessage, false); numberOfRemovals++; } } DarkLog.Normal("Nuked " + numberOfRemovals + " vessels around the KSC"); } }
private static void StopHTTPServer() { if (Settings.settingsStore.httpPort > 0) { DarkLog.Normal("Stopping HTTP server..."); httpListener.Stop(); } }
public static void ThreadMain() { try { //Register commands CommandHandler.RegisterCommand("help", CommandHandler.DisplayHelp, "Displays this help"); CommandHandler.RegisterCommand("say", CommandHandler.Say, "Broadcasts a message to clients"); CommandHandler.RegisterCommand("dekessler", Dekessler.RunDekessler, "Clears out debris from the server"); CommandHandler.RegisterCommand("nukeksc", NukeKSC.RunNukeKSC, "Clears ALL vessels from KSC and the Runway"); CommandHandler.RegisterCommand("listclients", ListClients, "Lists connected clients"); CommandHandler.RegisterCommand("countclients", CountClients, "Counts connected clients"); CommandHandler.RegisterCommand("connectionstats", ConnectionStats, "Displays network traffic usage"); CommandHandler.RegisterCommand("nukeall", NukeKSC.RunNukeEverything, "Clears ALL vessels from the ENTIRE game universe"); //Main loop while (Server.serverRunning) { string input = ""; try { input = Console.ReadLine(); if (input == null) { DarkLog.Debug("Terminal may be not attached or broken, Exiting out of command handler"); return; } } catch { if (Server.serverRunning) { DarkLog.Debug("Ignored mono Console.ReadLine() bug"); } Thread.Sleep(500); } DarkLog.Normal("Command input: " + input); if (input.StartsWith("/")) { HandleServerInput(input.Substring(1)); } else { if (input != "") { commands["say"].func(input); } } } } catch (Exception e) { if (Server.serverRunning) { DarkLog.Fatal("Error in command handler thread, Exception: " + e); throw; } } }
protected override void ProcessNetwork() { NetIncomingMessage inc; while ((inc = m_server.ReadMessage()) != null) { switch (inc.MessageType) { case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)inc.ReadByte(); switch (status) { case NetConnectionStatus.Connected: Trigger(new GameServerConnectionEvent { Id = inc.SenderConnection.RemoteUniqueIdentifier }); break; case NetConnectionStatus.Disconnected: m_connections.Remove(inc.SenderConnection.RemoteUniqueIdentifier); Trigger(new GameServerDisconnectionEvent { Id = inc.SenderConnection.RemoteUniqueIdentifier }); break; case NetConnectionStatus.Disconnecting: case NetConnectionStatus.InitiatedConnect: case NetConnectionStatus.RespondedConnect: break; } break; //Check for client attempting to connect case NetIncomingMessageType.ConnectionApproval: HandleConnectionChallenge(inc); break; case NetIncomingMessageType.Data: HandleData(inc); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: DarkLog.Debug(inc.ReadString()); break; case NetIncomingMessageType.WarningMessage: DarkLog.Normal(inc.ReadString()); break; case NetIncomingMessageType.ErrorMessage: DarkLog.Error(inc.ReadString()); break; } m_server.Recycle(inc); } }
internal void SetVesselOwnerCommand(string commandText) { if (editVessel == Guid.Empty) { DarkLog.Normal("Set /editvessel first"); } DarkLog.Normal(editVessel + " now belongs to player: " + commandText); SetVesselOwner(editVessel, commandText); }
private static void StartHTTPServer() { string OS = Environment.OSVersion.Platform.ToString(); if (Settings.settingsStore.httpPort > 0) { DarkLog.Normal("Starting HTTP server..."); httpListener = new HttpListener(); try { if (Settings.settingsStore.address != "0.0.0.0" && Settings.settingsStore.address != "::") { string listenAddress = Settings.settingsStore.address; if (listenAddress.Contains(":")) { //Sorry DarkLog.Error("Error: The server status port does not support specific IPv6 addresses. Sorry."); //listenAddress = "[" + listenAddress + "]"; return; } httpListener.Prefixes.Add("http://" + listenAddress + ":" + Settings.settingsStore.httpPort + '/'); } else { httpListener.Prefixes.Add("http://*:" + Settings.settingsStore.httpPort + '/'); } httpListener.Start(); httpListener.BeginGetContext(asyncHTTPCallback, httpListener); } catch (HttpListenerException e) { if (OS == "Win32NT" || OS == "Win32S" || OS == "Win32Windows" || OS == "WinCE") // if OS is Windows { if (e.ErrorCode == 5) // Access Denied { DarkLog.Debug("HTTP Server: access denied."); DarkLog.Debug("Prompting user to switch to administrator mode."); ProcessStartInfo startInfo = new ProcessStartInfo("DMPServer.exe") { Verb = "runas" }; Process.Start(startInfo); Environment.Exit(0); } } else { DarkLog.Fatal("Error while starting HTTP server.\n" + e); } throw; } } }
public static void RunNukeKSC(string commandText) { lock (Server.universeSizeLock) { string[] vesselList = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); int numberOfRemovals = 0; foreach (string vesselFile in vesselList) { string vesselID = Path.GetFileNameWithoutExtension(vesselFile); bool landedAtKSC = false; bool landedAtRunway = false; using (StreamReader sr = new StreamReader(vesselFile)) { string currentLine = sr.ReadLine(); while (currentLine != null && !landedAtKSC && !landedAtRunway) { string trimmedLine = currentLine.Trim(); if (trimmedLine.StartsWith("landedAt = ")) { string landedAt = trimmedLine.Substring(trimmedLine.IndexOf("=") + 2); if (landedAt == "KSC") { landedAtKSC = true; } if (landedAt == "Runway") { landedAtRunway = true; } } currentLine = sr.ReadLine(); } } if (landedAtKSC | landedAtRunway) { DarkLog.Normal("Removing vessel " + vesselID + " from KSC"); //Delete it from the universe if (File.Exists(vesselFile)) { File.Delete(vesselFile); } Messages.ServerClient_VesselRemoveSend msg = new Messages.ServerClient_VesselRemoveSend { subspace = 0, clock = 0.0, name = vesselID, unk = false }; WorldManager.Instance.Broadcast(msg); numberOfRemovals++; } } DarkLog.Normal("Nuked " + numberOfRemovals + " vessels around the KSC"); } }
internal void AddPlayerAdminCommand(string commandText) { if (editGroup == null) { DarkLog.Normal("Set /editgroup first"); return; } AddPlayerAdmin(commandText, editGroup); DarkLog.Normal("Added admin " + commandText + " to " + editGroup); }
internal void RemovePlayerAdminCommand(string commandText) { if (editGroup == null) { DarkLog.Normal("Set /editgroup first"); return; } RemovePlayerAdmin(commandText, editGroup); DarkLog.Normal("Removed admin " + commandText + " from " + editGroup); }
public static void RunDekessler(string commandText) { lock (Server.universeSizeLock) { string[] vesselList = Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")); int numberOfRemovals = 0; foreach (string vesselFile in vesselList) { string vesselID = Path.GetFileNameWithoutExtension(vesselFile); bool vesselIsDebris = false; using (StreamReader sr = new StreamReader(vesselFile)) { string currentLine = sr.ReadLine(); while (currentLine != null && !vesselIsDebris) { string trimmedLine = currentLine.Trim(); if (trimmedLine.StartsWith("type = ", StringComparison.Ordinal)) { string vesselType = trimmedLine.Substring(trimmedLine.IndexOf("=", StringComparison.Ordinal) + 2); if (vesselType == "Debris") { vesselIsDebris = true; } } currentLine = sr.ReadLine(); } } if (vesselIsDebris) { DarkLog.Normal("Removing vessel: " + vesselID); //Delete it from the universe if (File.Exists(vesselFile)) { File.Delete(vesselFile); } //Send a vessel remove message ServerMessage newMessage = new ServerMessage(); newMessage.type = ServerMessageType.VESSEL_REMOVE; using (MessageWriter mw = new MessageWriter()) { //Send it with a delete time of 0 so it shows up for all players. mw.Write <double>(0); mw.Write <string>(vesselID); mw.Write <bool>(false); newMessage.data = mw.GetMessageBytes(); } ClientHandler.SendToAll(null, newMessage, false); numberOfRemovals++; DarkLog.Debug("Removed debris vessel " + vesselID); } } DarkLog.Normal("Removed " + numberOfRemovals + " debris"); } }
private static void ListClients(string commandArgs) { if (Server.players != "") { DarkLog.Normal("Online players: " + Server.players); } else { DarkLog.Normal("No clients connected"); } }