Advertisement
Guest User

Untitled

a guest
Sep 30th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. /*
  2. * This file is part of the OpenNos Emulator Project. See AUTHORS file for Copyright information
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14.  
  15. using OpenNos.Core;
  16. using OpenNos.DAL;
  17. using OpenNos.Data;
  18. using OpenNos.Domain;
  19. using OpenNos.GameObject;
  20. using OpenNos.GameObject.Packets.ClientPackets;
  21. using OpenNos.Master.Library.Client;
  22. using System;
  23. using System.Configuration;
  24. using System.Linq;
  25.  
  26. namespace OpenNos.Handler
  27. {
  28. public class LoginPacketHandler : IPacketHandler
  29. {
  30. #region Members
  31.  
  32. private readonly ClientSession _session;
  33.  
  34. #endregion
  35.  
  36. #region Instantiation
  37.  
  38. public LoginPacketHandler(ClientSession session) => _session = session;
  39.  
  40. #endregion
  41.  
  42. #region Methods
  43.  
  44. private string BuildServersPacket(string username, int sessionId, bool ignoreUserName)
  45. {
  46. string channelpacket =
  47. CommunicationServiceClient.Instance.RetrieveRegisteredWorldServers(username, sessionId, ignoreUserName);
  48.  
  49. if (channelpacket == null || !channelpacket.Contains(':'))
  50. {
  51. Logger.Debug(
  52. "Could not retrieve Worldserver groups. Please make sure they've already been registered.");
  53. _session.SendPacket($"fail {Language.Instance.GetMessageFromKey("NO_WORLDSERVERS")}");
  54. }
  55.  
  56. return channelpacket;
  57. }
  58.  
  59. /// <summary>
  60. /// login packet
  61. /// </summary>
  62. /// <param name="loginPacket"></param>
  63. public void VerifyLogin(LoginPacket loginPacket)
  64. {
  65. if (loginPacket == null)
  66. {
  67. return;
  68. }
  69.  
  70. UserDTO user = new UserDTO
  71. {
  72. Name = loginPacket.Name,
  73. Password = ConfigurationManager.AppSettings["UseOldCrypto"] == "true"
  74. ? CryptographyBase.Sha512(LoginCryptography.GetPassword(loginPacket.Password)).ToUpper()
  75. : loginPacket.Password
  76. };
  77. AccountDTO loadedAccount = DAOFactory.AccountDAO.LoadByName(user.Name);
  78. if (loadedAccount?.Password.ToUpper().Equals(user.Password) == true)
  79. {
  80. string ipAddress = _session.IpAddress;
  81. DAOFactory.AccountDAO.WriteGeneralLog(loadedAccount.AccountId, ipAddress, null,
  82. GeneralLogType.Connection, "LoginServer");
  83.  
  84. //check if the account is connected
  85. if (!CommunicationServiceClient.Instance.IsAccountConnected(loadedAccount.AccountId))
  86. {
  87. AuthorityType type = loadedAccount.Authority;
  88. PenaltyLogDTO penalty = DAOFactory.PenaltyLogDAO.LoadByAccount(loadedAccount.AccountId)
  89. .FirstOrDefault(s => s.DateEnd > DateTime.Now && s.Penalty == PenaltyType.Banned);
  90. if (penalty != null)
  91. {
  92. _session.SendPacket(
  93. $"fail {string.Format(Language.Instance.GetMessageFromKey("BANNED"), penalty.Reason, penalty.DateEnd.ToString("yyyy-MM-dd-HH:mm"))}");
  94. }
  95. else
  96. {
  97. switch (type)
  98. {
  99. case AuthorityType.Unconfirmed:
  100. {
  101. _session.SendPacket($"fail {Language.Instance.GetMessageFromKey("NOTVALIDATE")}");
  102. }
  103. break;
  104.  
  105. case AuthorityType.Banned:
  106. {
  107. _session.SendPacket(
  108. $"fail {string.Format(Language.Instance.GetMessageFromKey("BANNED"), "Unknown", "Unknown")}");
  109. }
  110. break;
  111.  
  112. case AuthorityType.Closed:
  113. {
  114. _session.SendPacket($"fail {Language.Instance.GetMessageFromKey("IDERROR")}");
  115. }
  116. break;
  117.  
  118. default:
  119. {
  120. if (loadedAccount.Authority == AuthorityType.User
  121. || loadedAccount.Authority == AuthorityType.BitchNiggerFaggot)
  122. {
  123. MaintenanceLogDTO maintenanceLog = DAOFactory.MaintenanceLogDAO.LoadFirst();
  124. if (maintenanceLog != null && maintenanceLog.DateStart < DateTime.Now)
  125. {
  126. _session.SendPacket(
  127. $"fail {string.Format(Language.Instance.GetMessageFromKey("MAINTENANCE"), maintenanceLog.DateEnd, maintenanceLog.Reason)}");
  128. return;
  129. }
  130. }
  131.  
  132. int newSessionId = SessionFactory.Instance.GenerateSessionId();
  133. Logger.Debug(string.Format(Language.Instance.GetMessageFromKey("CONNECTION"), user.Name,
  134. newSessionId));
  135. try
  136. {
  137. ipAddress = ipAddress.Substring(6, ipAddress.LastIndexOf(':') - 6);
  138. CommunicationServiceClient.Instance.RegisterAccountLogin(loadedAccount.AccountId,
  139. newSessionId, ipAddress);
  140. }
  141. catch (Exception ex)
  142. {
  143. Logger.Error("General Error SessionId: " + newSessionId, ex);
  144. }
  145.  
  146. string[] clientData = loginPacket.ClientData.Split('.');
  147. bool ignoreUserName = short.TryParse(clientData[0], out short clientVersion)
  148. && (clientVersion < 3091
  149. || ConfigurationManager.AppSettings["UseOldCrypto"] == "true");
  150. _session.SendPacket(BuildServersPacket(user.Name, newSessionId, ignoreUserName));
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. else
  157. {
  158. _session.SendPacket($"fail {Language.Instance.GetMessageFromKey("ALREADY_CONNECTED")}");
  159. }
  160. }
  161. else
  162. {
  163. _session.SendPacket($"fail {Language.Instance.GetMessageFromKey("IDERROR")}");
  164. }
  165. }
  166.  
  167. #endregion
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement