Esempio n. 1
0
 public override void OnServerStart()
 {
     DarkLog.Normal("[ModdedVesselPositions] Starting...");
     Config.SetupConfig();
     string[] OutDatedFileList = Directory.GetFiles(VesselPositions.VesselPosFolder);
     foreach (string vesselFile in OutDatedFileList)
     {
         if (File.Exists(vesselFile))
         {
             File.Delete(vesselFile);
         }
     }
     DarkLog.Debug("[ModdedVesselPositions] Reset 'PluginData/DMPServerMap-FrostBird347/VesselPos'");
     if (inited)
     {
         return;
     }
     PlanetInfo.Init();
     ServerTime.Init();
     foreach (string file in Directory.GetFiles(Path.Combine(Server.universeDirectory, "Vessels")))
     {
         string croppedName = Path.GetFileNameWithoutExtension(file);
         if (Guid.TryParse(croppedName, out Guid vesselID))
         {
             byte[] vesselData = File.ReadAllBytes(file);
             UpdateVessel(null, vesselID, vesselData);
         }
     }
     inited = true;
     DarkLog.Normal("[ModdedVesselPositions] Started! - Version " + PluginV + ".");
     CommandHandler.RegisterCommand("reloadpos", ReloadConfig, "Reload the ModdedVesselPositions plugin config.");
 }
Esempio n. 2
0
 public override void OnServerStop()
 {
     if (backupCommon.restoreID != 0)
     {
         DateTime restoreDateTime = new DateTime(backupCommon.restoreID, DateTimeKind.Utc);
         DarkLog.Normal("Restoring backup from: " + restoreDateTime.ToLocalTime().ToLongDateString() + " " + restoreDateTime.ToLocalTime().ToLongTimeString());
         backupCommon.RestoreUniverse(backupCommon.restoreID);
     }
     backupCommon.restoreID = 0;
 }
Esempio n. 3
0
        public static void CheckHeartBeat(ClientObject client)
        {
            long currentTime = Server.serverClock.ElapsedMilliseconds;

            if ((currentTime - client.lastReceiveTime) > Common.CONNECTION_TIMEOUT)
            {
                //Heartbeat timeout
                DarkLog.Normal("Disconnecting client " + client.playerName + ", endpoint " + client.endpoint + ", Connection timed out");
                ClientHandler.DisconnectClient(client);
            }
            else
            {
                if (client.sendMessageQueueHigh.Count == 0 && client.sendMessageQueueSplit.Count == 0 && client.sendMessageQueueLow.Count == 0)
                {
                    if ((currentTime - client.lastSendTime) > Common.HEART_BEAT_INTERVAL)
                    {
                        Messages.Heartbeat.Send(client);
                    }
                }
            }
        }
Esempio n. 4
0
        public static void HandleModpackMessage(ClientObject client, byte[] messageData)
        {
            if (messageData == null || messageData.Length == 0)
            {
                ConnectionEnd.SendConnectionEnd(client, "Invalid mod control message from client");
                return;
            }

            if (!client.authenticated)
            {
                ConnectionEnd.SendConnectionEnd(client, "Unauthenticated client tried to send modpack message");
            }

            using (MessageReader mr = new MessageReader(messageData))
            {
                ModpackDataMessageType type = (ModpackDataMessageType)mr.Read <int>();
                switch (type)
                {
                case ModpackDataMessageType.CKAN:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    if (Settings.settingsStore.modpackMode != ModpackMode.CKAN)
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Please set server modpackMode to CKAN");
                        return;
                    }
                    byte[] fileBytes = mr.Read <byte[]>();
                    ModpackSystem.fetch.SaveCKANData(fileBytes);
                }
                break;

                case ModpackDataMessageType.MOD_LIST:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    if (Settings.settingsStore.modpackMode != ModpackMode.GAMEDATA)
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Please set server modpackMode to GAMEDATA");
                        return;
                    }
                    DarkLog.Normal("Modpack uploaded from " + client.playerName);
                    string[] files = mr.Read <string[]>();
                    string[] sha   = mr.Read <string[]>();
                    ModpackSystem.fetch.HandleNewGameData(files, sha, client);
                }
                break;

                case ModpackDataMessageType.REQUEST_OBJECT:
                {
                    string[] sha256sums = mr.Read <string[]>();
                    ModpackSystem.fetch.HandleSendList(client, sha256sums);
                }
                break;

                case ModpackDataMessageType.RESPONSE_OBJECT:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    string sha256sum = mr.Read <string>();
                    if (mr.Read <bool>())
                    {
                        byte[] fileBytes = mr.Read <byte[]>();
                        DarkLog.Debug("Received object: " + sha256sum);
                        ModpackSystem.fetch.SaveModObject(fileBytes, sha256sum);
                    }
                    else
                    {
                        DarkLog.Normal("Failed to recieve: " + sha256sum);
                    }
                }
                break;

                case ModpackDataMessageType.MOD_DONE:
                {
                    if (!DarkMultiPlayerServer.AdminSystem.fetch.IsAdmin(client.playerName))
                    {
                        ConnectionEnd.SendConnectionEnd(client, "Kicked from the server, non admin " + client.playerName + " tried to upload modpack");
                        return;
                    }
                    //Has gamedata upload
                    if (mr.Read <bool>())
                    {
                        DarkLog.Debug("Mod control file updated");
                        byte[] newModControl = mr.Read <byte[]>();
                        File.WriteAllBytes(Server.modFile, newModControl);
                    }
                    ModpackSystem.fetch.HandleModDone();
                }
                break;
                }
            }
        }
Esempio n. 5
0
        public static void HandleHandshakeResponse(ClientObject client, byte[] messageData)
        {
            int    protocolVersion;
            string playerName = "";
            string playerPublicKey;

            byte[] playerChallangeSignature;
            string clientVersion = "";
            string reason        = "";
            Regex  regex         = new Regex(@"[\""<>|$]"); // Regex to detect quotation marks, and other illegal characters
            //0 - Success
            HandshakeReply handshakeReponse = HandshakeReply.HANDSHOOK_SUCCESSFULLY;

            try
            {
                using (MessageReader mr = new MessageReader(messageData))
                {
                    protocolVersion          = mr.Read <int>();
                    playerName               = mr.Read <string>();
                    playerPublicKey          = mr.Read <string>();
                    playerChallangeSignature = mr.Read <byte[]>();
                    clientVersion            = mr.Read <string>();
                    try
                    {
                        client.compressionEnabled = mr.Read <bool>();
                    }
                    catch
                    {
                        //This is safe to ignore. We want to tell people about version mismatches still.
                        client.compressionEnabled = false;
                    }
                }
            }
            catch (Exception e)
            {
                DarkLog.Debug("Error in HANDSHAKE_REQUEST from " + client.playerName + ": " + e);
                SendHandshakeReply(client, HandshakeReply.MALFORMED_HANDSHAKE, "Malformed handshake");
                return;
            }
            if (regex.IsMatch(playerName))
            {
                // Invalid username
                handshakeReponse = HandshakeReply.INVALID_PLAYERNAME;
                reason           = "Invalid username";
            }
            if (playerName.Contains("/") || playerName.Contains(@"\") || playerName.Contains("\n") || playerName.Contains("\r"))
            {
                handshakeReponse = HandshakeReply.INVALID_PLAYERNAME;
                reason           = "Invalid username";
            }
            if (protocolVersion != Common.PROTOCOL_VERSION)
            {
                //Protocol mismatch
                handshakeReponse = HandshakeReply.PROTOCOL_MISMATCH;
                reason           = "Protocol mismatch";
            }
            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                //Check client isn't already connected
                ClientObject testClient = ClientHandler.GetClientByName(playerName);
                if (testClient != null)
                {
                    Messages.Heartbeat.Send(testClient);
                    Thread.Sleep(1000);
                }
                if (ClientHandler.ClientConnected(testClient))
                {
                    handshakeReponse = HandshakeReply.ALREADY_CONNECTED;
                    reason           = "Client already connected";
                }
            }
            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                bool reserveKick = false;
                //Check the client isn't using a reserved name
                if (playerName == "Initial")
                {
                    reserveKick = true;
                }
                if (playerName == Settings.settingsStore.consoleIdentifier)
                {
                    reserveKick = true;
                }
                if (reserveKick)
                {
                    handshakeReponse = HandshakeReply.RESERVED_NAME;
                    reason           = "Kicked for using a reserved name";
                }
            }
            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                //Check the client matches any database entry
                string storedPlayerFile      = Path.Combine(Server.universeDirectory, "Players", playerName + ".txt");
                string storedPlayerPublicKey = "";
                if (File.Exists(storedPlayerFile))
                {
                    storedPlayerPublicKey = File.ReadAllText(storedPlayerFile);
                    if (playerPublicKey != storedPlayerPublicKey)
                    {
                        handshakeReponse = HandshakeReply.INVALID_KEY;
                        reason           = "Invalid key for user";
                    }
                    else
                    {
                        using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024))
                        {
                            rsa.PersistKeyInCsp = false;
                            rsa.FromXmlString(playerPublicKey);
                            bool result = rsa.VerifyData(client.challange, CryptoConfig.CreateFromName("SHA256"), playerChallangeSignature);
                            if (!result)
                            {
                                handshakeReponse = HandshakeReply.INVALID_KEY;
                                reason           = "Public/private key mismatch";
                            }
                        }
                    }
                }
                else
                {
                    try
                    {
                        File.WriteAllText(storedPlayerFile, playerPublicKey);
                        DarkLog.Debug("Client " + playerName + " registered!");
                    }
                    catch
                    {
                        handshakeReponse = HandshakeReply.INVALID_PLAYERNAME;
                        reason           = "Invalid username";
                    }
                }
            }

            client.playerName    = playerName;
            client.publicKey     = playerPublicKey;
            client.clientVersion = clientVersion;

            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                if (BanSystem.fetch.IsPlayerNameBanned(client.playerName) || BanSystem.fetch.IsIPBanned(client.ipAddress) || BanSystem.fetch.IsPublicKeyBanned(client.publicKey))
                {
                    handshakeReponse = HandshakeReply.PLAYER_BANNED;
                    reason           = "You were banned from the server!";
                }
            }

            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                if (ClientHandler.GetActiveClientCount() >= Settings.settingsStore.maxPlayers)
                {
                    handshakeReponse = HandshakeReply.SERVER_FULL;
                    reason           = "Server is full";
                }
            }

            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                if (Settings.settingsStore.whitelisted && !WhitelistSystem.fetch.IsWhitelisted(client.playerName))
                {
                    handshakeReponse = HandshakeReply.NOT_WHITELISTED;
                    reason           = "You are not on the whitelist";
                }
            }

            if (handshakeReponse == HandshakeReply.HANDSHOOK_SUCCESSFULLY)
            {
                client.authenticated = true;
                string devClientVersion = "";
                DMPPluginHandler.FireOnClientAuthenticated(client);

                if (client.clientVersion.Length == 40)
                {
                    devClientVersion = client.clientVersion.Substring(0, 7);
                }
                else
                {
                    devClientVersion = client.clientVersion;
                }
                DarkLog.Normal("Client " + playerName + " handshook successfully, version: " + devClientVersion);

                if (!Directory.Exists(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName)))
                {
                    Directory.CreateDirectory(Path.Combine(Server.universeDirectory, "Scenarios", client.playerName));
                    foreach (string file in Directory.GetFiles(Path.Combine(Server.universeDirectory, "Scenarios", "Initial")))
                    {
                        File.Copy(file, Path.Combine(Server.universeDirectory, "Scenarios", playerName, Path.GetFileName(file)));
                    }
                }
                SendHandshakeReply(client, handshakeReponse, "success");
                Server.playerCount = ClientHandler.GetActiveClientCount();
                Server.players     = ClientHandler.GetActivePlayerNames();
                DarkLog.Debug("Online players is now: " + Server.playerCount + ", connected: " + ClientHandler.GetClients().Length);
            }
            else
            {
                DarkLog.Normal("Client " + playerName + " failed to handshake: " + reason);
                SendHandshakeReply(client, handshakeReponse, reason);
            }
        }
Esempio n. 6
0
 public void ReloadConfig(string input)
 {
     DarkLog.Normal("[ModdedVesselPositions] Reloading config...");
     Config.SetupConfig();
     DarkLog.Normal("[ModdedVesselPositions] Finished.");
 }