/// <summary> /// Verifica a senha de usuário e garante acesso ao sistema. /// </summary> /// <param name="result"></param> /// <param name="username"></param> public static void Login(bool result, string username) { var pData = FindByUsername(username); //se nao achar em outro lugar, verifica no proprio login server if (!result) //(se falso), check login server //Verifica se o usuário já está conectado, caso verdadeiro, envia mensagem de erro { if (Authentication.IsConnected(pData.Username)) { pData.LoginAttempt++; TryingToAccess(pData); return; } } else { pData.LoginAttempt++; TryingToAccess(pData); return; } //Verifica se os campos estão corretos, caso falso, envia mensagem de erro if (!Accounts_DB.ExistPassword(pData.Username, pData.Password)) { LoginPacket.Message(pData.HexID, (int)PacketList.LoginServer_Client_InvalidNamePass); return; } //muda o nome de usuario para o campo oficial de usuario e limpar o campo temporario pData.Account = pData.Username; pData.Username = string.Empty; FileLog.WriteLog($"User Login: {pData.Account} {pData.IP}", System.Drawing.Color.Black); //carrega as informações da conta Accounts_DB.LoadAccountData(pData); Accounts_DB.LoadAccountService(pData); //verifica os serviços pData.Service.VerifyServices(pData.ID); Accounts_DB.UpdateDateLasteLogin(pData.Account); Accounts_DB.UpdateCurrentIP(pData.Account, pData.IP); Accounts_DB.UpdateLoggedIn(pData.Account, 1); //1 = true //envia a lista de servidores e muda a tela no cliente LoginPacket.ServerList(pData.HexID); LoginPacket.GameState(pData.HexID, 2); //tela 2, lista de servidor }
/// <summary> /// Recebe os dados dos clientes. /// </summary> public static void ReceivedData() { while ((msg = socket.ReadMessage()) != null) { pData = Authentication.FindByConnection(msg.SenderConnection); switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryRequest: #region Find Banned Country var ip = msg.SenderEndPoint.Address.ToString(); if (GeoIp.Enabled) { //Verifica se o ip já está bloqueado temporariamente (evitar processamento desnecessario) if (!GeoIp.IsIpBlocked(ip)) { //verifica se o ip do país está na lista de bloqueados. if (GeoIp.IsCountryIpBlocked(ip)) { var country = GeoIp.FindCountryByIp(ip); //adiciona na lista de bloqueado temporareamente GeoIp.AddIpAddress(ip); FileLog.WriteLog($"Banned country trying to connect: {ip} {country.Country}-{country.Code}", Color.Coral); return; } } else { return; } } #endregion #region Find Banned IP if (Accounts_DB.IsBannedIp(msg.SenderEndPoint.Address.ToString()) == true) { FileLog.WriteLog("Warning: Attempted IP Banned " + msg.SenderEndPoint.Address, Color.Coral); return; } #endregion LoginNetwork.socket.SendDiscoveryResponse(null, msg.SenderEndPoint); FileLog.WriteLog($"Discovery Response IPEndPoint: {msg.SenderEndPoint.Address}", Color.Coral); break; case NetIncomingMessageType.ErrorMessage: FileLog.WriteLog($"Error: {msg.ReadString()}", Color.Coral); break; case NetIncomingMessageType.StatusChanged: #region Status Changed Connected NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { FileLog.WriteLog($"Status changed to connected: {msg.SenderEndPoint.Address}", Color.Coral); Authentication.Player.Add(new PlayerData(msg.SenderConnection, NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier), msg.SenderEndPoint.Address.ToString())); } #endregion #region Status Changed Disconnected if (status == NetConnectionStatus.Disconnected) { if (pData == null) { return; } FileLog.WriteLog($"Status changed to disconnected: {pData?.ID} {pData?.Account} {msg?.SenderEndPoint.Address} {pData?.HexID}", Color.Coral); Accounts_DB.UpdateLastIP(pData.Account, pData.IP); Accounts_DB.UpdateLoggedIn(pData.Account, 0); //0 disconnect Accounts_DB.UpdateCurrentIP(pData.Account, string.Empty); //limpa o ip atual Authentication.Player.Remove(pData); } #endregion break; case NetIncomingMessageType.Data: LoginData.HandleData(pData.HexID, msg); break; default: //Registra qualquer mensagem invalida FileLog.WriteLog($"Unhandled type: {msg.MessageType}", Color.DarkRed); break; } LoginNetwork.socket.Recycle(msg); } }
/// <summary> /// Recebe os dados dos clientes. /// </summary> public static void ReceivedData() { NetIncomingMessage msg; while ((msg = Socket.ReadMessage()) != null) { var pData = Authentication.FindByConnection(msg.SenderConnection); switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryRequest: #region Find Banned IP if (Accounts_DB.BannedIP(msg.SenderEndPoint.Address.ToString()) == true) { LogConfig.WriteLog("Warning: Attempted IP Banned " + msg.SenderEndPoint.Address); LogConfig.WriteLog("Warning: Attempted IP Banned " + msg.SenderEndPoint.Address, Color.Coral); return; } #endregion LoginServerNetwork.Socket.SendDiscoveryResponse(null, msg.SenderEndPoint); LogConfig.WriteLog($"Discovery Response IPEndPoint: {msg.SenderEndPoint.Address}", Color.Coral); break; case NetIncomingMessageType.ErrorMessage: LogConfig.WriteLog($"Error: {msg.ReadString()}", Color.Coral); break; case NetIncomingMessageType.StatusChanged: #region Status Changed Connected NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { LogConfig.WriteLog($"Status changed to connected: {msg.SenderEndPoint.Address}", Color.Coral); Authentication.Player.Add(new PlayerData(msg.SenderConnection, NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier), msg.SenderEndPoint.Address.ToString())); } #endregion #region Status Changed Disconnected if (status == NetConnectionStatus.Disconnected) { pData = Authentication.FindByHexID(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier)); //1 - enabled, 0 - disabled if (Settings.LogSystem == 1) { LogConfig.WriteLog($"Status changed to disconnected: {pData?.ID} {pData?.Account} {msg.SenderEndPoint.Address} {pData.HexID}"); } if (!Settings.LogSystemScreen) { LogConfig.WriteLog($"Status changed to disconnected: {pData?.ID} {pData?.Account} {msg.SenderEndPoint.Address} {pData.HexID}", Color.Coral); } Accounts_DB.UpdateLastIP(pData.Account, pData.IP); Accounts_DB.UpdateLoggedIn(pData.Account, 0); //0 disconnect Accounts_DB.UpdateCurrentIP(pData.Account, ""); Authentication.Player.Remove(pData); } #endregion break; case NetIncomingMessageType.Data: LoginServerData.HandleData(pData.HexID, msg); break; default: //Registra qualquer mensagem invalida LogConfig.WriteLog($"Unhandled type: {msg.MessageType}", Color.DarkRed); break; } LoginServerNetwork.Socket.Recycle(msg); } }