toko214

AutoRegister

Nov 20th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. package handling.login.handler;
  2.  
  3. import client.LoginCrypto;
  4. import database.DatabaseConnection;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class AutoRegister {
  11.  
  12. private static final int ACCOUNTS_PER_IP = 3;
  13. public static boolean autoRegister = true;
  14. public static boolean success = false;
  15.  
  16. public static boolean getAccountExists(String login) {
  17. boolean accountExists = false;
  18. Connection con = DatabaseConnection.getConnection();
  19. try {
  20. PreparedStatement ps = con.prepareStatement("SELECT name FROM accounts WHERE name = ?");
  21. ps.setString(1, login);
  22. ResultSet rs = ps.executeQuery();
  23. if (rs.first()) {
  24. accountExists = true;
  25. }
  26. } catch (SQLException ex) {
  27. System.out.println(ex);
  28. }
  29. return accountExists;
  30. }
  31.  
  32. public static void createAccount(String login, String pwd, String eip) {
  33. String sockAddr = eip;
  34. Connection con;
  35.  
  36. try {
  37. con = DatabaseConnection.getConnection();
  38. } catch (Exception ex) {
  39. System.out.println(ex);
  40. return;
  41. }
  42.  
  43. try {
  44. ResultSet rs;
  45. try (PreparedStatement ipc = con.prepareStatement("SELECT SessionIP FROM accounts WHERE SessionIP = ?")) {
  46. ipc.setString(1, sockAddr.substring(1, sockAddr.lastIndexOf(':')));
  47. rs = ipc.executeQuery();
  48. if (rs.first() == false || rs.last() == true && rs.getRow() < ACCOUNTS_PER_IP) {
  49. try {
  50. try (PreparedStatement ps = con.prepareStatement("INSERT INTO accounts (name, password, email, birthday, macs, SessionIP) VALUES (?, ?, ?, ?, ?, ?)")) {
  51. ps.setString(1, login);
  52. ps.setString(2, LoginCrypto.hexSha1(pwd));
  53. ps.setString(3, "[email protected]");
  54. ps.setString(4, "2008-04-07");
  55. ps.setString(5, "00-00-00-00-00-00");
  56. ps.setString(6, sockAddr.substring(1, sockAddr.lastIndexOf(':')));
  57. ps.executeUpdate();
  58. }
  59.  
  60. success = true;
  61. } catch (SQLException ex) {
  62. System.out.println(ex);
  63. return;
  64. }
  65. }
  66. }
  67. rs.close();
  68. } catch (SQLException ex) {
  69. System.out.println(ex);
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment