Guest User

Untitled

a guest
Aug 11th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. public class LoginPacketRequest extends InboundPacket {
  2.     User user;
  3.     long packetId;
  4.     int clientVersion;
  5.     ChatManager chatManager;
  6.     String login;
  7.     String password;
  8.     final String salt = "7CHSEIoTv7";
  9.  
  10.     public LoginPacketRequest(User user, ChatManager chatManager) {
  11.         this.user = user;
  12.         this.chatManager = chatManager;
  13.     }
  14.  
  15.     @Override
  16.     public void run() {
  17.         Connection con = HikariCP.getInstance().getConnection();
  18.  
  19.         try {
  20.             boolean userExists = DatabaseRequests.getUserInfo(con, user, login);
  21.             if (!userExists) {
  22.                 thisSession.send(new LoginPacketResponse(packetId, PacketErrorCodes.PACKET_WRONG_USERNAME_OR_PASSWORD));
  23.                 return;
  24.             }
  25.  
  26.             String enteredPasswdUppercase = password.toUpperCase();
  27.             String dbPasswd = user.getPasswd().toUpperCase();
  28.  
  29.             //password verification
  30.             if (!dbPasswd.equals(enteredPasswdUppercase)) {
  31.                 thisSession.send(new LoginPacketResponse(packetId, PacketErrorCodes.PACKET_WRONG_USERNAME_OR_PASSWORD));
  32.                 return;
  33.             }
  34.  
  35.             boolean userValid = DatabaseRequests.checkUserValid(con, user);
  36.             if (!userValid) {
  37.                 thisSession.send(new LoginPacketResponse(packetId, PacketErrorCodes.PACKET_USER_IS_NOT_VALID));
  38.                 return;
  39.             }
  40.  
  41.             user.setClientVersion(clientVersion);
  42.             UserManager.getInstance().add(user);
  43.             chatManager.onConnectToChat(user.getLogin(), thisSession);
  44.  
  45.             FileLog.getInstance().d("UserManager size = " + UserManager.getInstance().count() +
  46.                     ", chatManager size = " + chatManager.size());
  47.  
  48.             thisSession.send(new LoginPacketResponse(packetId, PacketErrorCodes.PACKET_OK,
  49.                     KartofanServer.SERVER_VERSION, user.getRole(), user.getTicket()));
  50.         } catch (Exception e) {
  51.             FileLog.getInstance().e(e);
  52.             thisSession.send(new LoginPacketResponse(packetId, PacketErrorCodes.PACKET_COULDNT_VERIFY_LOGIN_PASS));
  53.         } finally {
  54.             try {
  55.                 con.close();
  56.             } catch (SQLException e) {
  57.                 FileLog.getInstance().e(e);
  58.             }
  59.         }
  60.     }
  61.  
  62.     @Override
  63.     public boolean read() {
  64.         try {
  65.             packetId = readQword();
  66.             login = readString();
  67.             password = readString();
  68.             clientVersion = readDword();
  69.             //user.setEmail(readString());
  70.  
  71.         } catch (Exception e) {
  72.             e.printStackTrace();
  73.             return false;
  74.         }
  75.  
  76.         return true;
  77.     }
  78. }
Add Comment
Please, Sign In to add comment