public override void HandleMessage(ClientStructure client, IMessageData messageData)
        {
            var message = messageData as HandshakeBaseMsgData;

            switch (message?.HandshakeMessageType)
            {
            case HandshakeMessageType.Request:
                SetAndSendHandshakeChallangeMessage(client);
                break;

            case HandshakeMessageType.Response:
                var data = (HandshakeResponseMsgData)message;
                try
                {
                    HandshakeHandler.HandleHandshakeResponse(client, data);
                }
                catch (Exception e)
                {
                    LunaLog.Debug($"Error in HANDSHAKE_REQUEST from {data.PlayerName}: {e}");
                    HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.MalformedHandshake, "Malformed handshake");
                }
                break;

            default:
                throw new NotImplementedException("Warp Type not implemented");
            }
        }
        public override void HandleMessage(ClientStructure client, IClientMessageBase message)
        {
            var data = message.Data as HandshakeBaseMsgData;

            switch (data?.HandshakeMessageType)
            {
            case HandshakeMessageType.Request:
                SetAndSendHandshakeChallangeMessage(client);
                break;

            case HandshakeMessageType.Response:
                try
                {
                    HandshakeHandler.HandleHandshakeResponse(client, (HandshakeResponseMsgData)data);
                }
                catch (Exception e)
                {
                    LunaLog.Debug($"Error in HANDSHAKE_REQUEST from {((HandshakeResponseMsgData)data).PlayerName}: {e}");
                    HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.MalformedHandshake, "Malformed handshake");
                }
                break;

            default:
                throw new NotImplementedException("Handshake type not implemented");
            }

            //We don't use this message so we can recycle it
            message.Recycle();
        }
Exemple #3
0
 private bool CheckUsernameIsReserved(ClientStructure client, string playerName)
 {
     if (playerName == "Initial" || playerName == GeneralSettings.SettingsStore.ConsoleIdentifier)
     {
         Reason = "Using reserved name";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.ReservedName, Reason);
         return(false);
     }
     return(true);
 }
Exemple #4
0
 private bool CheckWhitelist(ClientStructure client, string playerName)
 {
     if (GeneralSettings.SettingsStore.Whitelisted && !WhitelistCommands.Retrieve().Contains(playerName))
     {
         Reason = "Not on whitelist";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.NotWhitelisted, Reason);
         return(false);
     }
     return(true);
 }
Exemple #5
0
 private bool CheckServerFull(ClientStructure client)
 {
     if (ClientRetriever.GetActiveClientCount() >= GeneralSettings.SettingsStore.MaxPlayers)
     {
         Reason = "Server full";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.ServerFull, Reason);
         return(false);
     }
     return(true);
 }
Exemple #6
0
 private bool CheckUsernameLength(ClientStructure client, string username)
 {
     if (username.Length > GeneralSettings.SettingsStore.MaxUsernameLength)
     {
         Reason = $"Username too long. Max chars: {GeneralSettings.SettingsStore.MaxUsernameLength}";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.ServerFull, Reason);
         return(false);
     }
     return(true);
 }
 private bool CheckUsernameLength(ClientStructure client, string username)
 {
     if (username.Length > 10)
     {
         Reason = "User too long. Max: 10";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.SERVER_FULL, Reason);
         return(false);
     }
     return(true);
 }
Exemple #8
0
        private bool CheckUsernameCharacters(ClientStructure client, string playerName)
        {
            var regex = new Regex(@"^[-_a-zA-Z0-9]+$"); // Regex to only allow alphanumeric, dashes and underscore

            if (!regex.IsMatch(playerName))
            {
                Reason = "Invalid username characters";
                HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.InvalidPlayername, Reason);
                return(false);
            }
            return(true);
        }
Exemple #9
0
        private bool CheckPlayerIsAlreadyConnected(ClientStructure client, string playerName)
        {
            var existingClient = ClientRetriever.GetClientByName(playerName);

            if (existingClient != null)
            {
                Reason = "Username already connected";
                HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.AlreadyConnected, Reason);
                return(false);
            }
            return(true);
        }
Exemple #10
0
 private bool CheckPlayerIsBanned(ClientStructure client, string playerName, string ipAddress, string publicKey)
 {
     if (BanCommands.RetrieveBannedUsernames().Contains(playerName) ||
         BanCommands.RetrieveBannedIps().Contains(ipAddress) ||
         BanCommands.RetrieveBannedKeys().Contains(publicKey))
     {
         Reason = "Banned";
         HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.PlayerBanned, Reason);
         return(false);
     }
     return(true);
 }
        private bool CheckKey(ClientStructure client, string playerName, string playerPublicKey,
                              byte[] playerChallangeSignature)
        {
            //Check the client matches any database entry
            var storedPlayerFile = Path.Combine(ServerContext.UniverseDirectory, "Players", playerName + ".txt");

            if (FileHandler.FileExists(storedPlayerFile))
            {
                var storedPlayerPublicKey = FileHandler.ReadFileText(storedPlayerFile);
                if (playerPublicKey != storedPlayerPublicKey)
                {
                    Reason = "Invalid key";
                    HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.INVALID_KEY, Reason);
                    return(false);
                }
                using (var rsa = new RSACryptoServiceProvider(1024))
                {
                    rsa.PersistKeyInCsp = false;
                    rsa.FromXmlString(playerPublicKey);
                    var result = rsa.VerifyData(client.Challange, CryptoConfig.CreateFromName("SHA256"),
                                                playerChallangeSignature);
                    if (!result)
                    {
                        Reason = "Public/priv key mismatch";
                        HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.INVALID_KEY, Reason);
                        return(false);
                    }
                }
            }
            else
            {
                try
                {
                    FileHandler.WriteToFile(storedPlayerFile, playerPublicKey);
                    LunaLog.Debug($"Client {playerName} registered!");
                }
                catch
                {
                    Reason = "Invalid username";
                    HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.INVALID_PLAYERNAME, Reason);
                    return(false);
                }
            }
            return(true);
        }
Exemple #12
0
        public void HandleHandshakeResponse(ClientStructure client, HandshakeResponseMsgData data)
        {
            var valid = CheckServerFull(client);

            valid &= valid && CheckUsernameLength(client, data.PlayerName);
            valid &= valid && CheckUsernameCharacters(client, data.PlayerName);
            valid &= valid && CheckWhitelist(client, data.PlayerName);
            valid &= valid && CheckPlayerIsAlreadyConnected(client, data.PlayerName);
            valid &= valid && CheckUsernameIsReserved(client, data.PlayerName);
            valid &= valid && CheckPlayerIsBanned(client, data.PlayerName, client.Endpoint.Address.ToString(), data.PublicKey);
            valid &= valid && CheckKey(client, data.PlayerName, data.PublicKey, data.ChallengeSignature);

            if (!valid)
            {
                LunaLog.Normal($"Client {data.PlayerName} failed to handshake: {Reason}. Disconnecting");
                client.DisconnectClient = true;
                ClientConnectionHandler.DisconnectClient(client, Reason);
            }
            else
            {
                client.PlayerName    = data.PlayerName;
                client.PublicKey     = data.PublicKey;
                client.Id            = Guid.NewGuid();
                client.Authenticated = true;

                LmpPluginHandler.FireOnClientAuthenticated(client);

                LunaLog.Normal($"Client {data.PlayerName} handshook successfully, Version: {data.MajorVersion}.{data.MinorVersion}.{data.BuildVersion}");

                CreatePlayerScenarioFiles(client, data.PlayerName);

                HandshakeSystemSender.SendHandshakeReply(client, HandshakeReply.HandshookSuccessfully, "success");

                var msgData = ServerContext.ServerMessageFactory.CreateNewMessageData <PlayerConnectionJoinMsgData>();
                msgData.PlayerName = client.PlayerName;

                MessageQueuer.RelayMessage <PlayerConnectionSrvMsg>(client, msgData);

                LunaLog.Debug($"Online Players: {ServerContext.PlayerCount}, connected: {ClientRetriever.GetClients().Length}");
            }
        }