diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java index 6f688c0b..021eb3c3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -7,31 +7,31 @@ public class HibernateAuthHandler extends CachedAuthHandler { @Override protected Entry fetchEntry(String username) { - User user = srv.config.dao.userService.findUserByUsername(username); + User user = srv.config.dao.userDAO.findByUsername(username); if (user == null) return null; - return new Entry(user.uuid, username, user.getAccessToken(), user.serverID); + return new Entry(user.getUuid(), username, user.getAccessToken(), user.getServerID()); } @Override protected Entry fetchEntry(UUID uuid) { - User user = srv.config.dao.userService.findUserByUUID(uuid); + User user = srv.config.dao.userDAO.findByUUID(uuid); if (user == null) return null; - return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID); + return new Entry(user.getUuid(), user.getUsername(), user.getAccessToken(), user.getServerID()); } @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) { - User user = srv.config.dao.userService.findUserByUUID(uuid); + User user = srv.config.dao.userDAO.findByUUID(uuid); user.setAccessToken(accessToken); - srv.config.dao.userService.updateUser(user); + srv.config.dao.userDAO.update(user); return true; } @Override protected boolean updateServerID(UUID uuid, String serverID) { - User user = srv.config.dao.userService.findUserByUUID(uuid); - user.serverID = serverID; - srv.config.dao.userService.updateUser(user); + User user = srv.config.dao.userDAO.findByUUID(uuid); + user.setServerID(serverID); + srv.config.dao.userDAO.update(user); return true; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java index 2cb030a2..15702db5 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java @@ -7,17 +7,17 @@ public class HibernatePermissionsHandler extends PermissionsHandler { @Override public ClientPermissions getPermissions(String username) { - User user = srv.config.dao.userService.findUserByUsername(username); + User user = srv.config.dao.userDAO.findByUsername(username); if (user == null) return ClientPermissions.DEFAULT; return user.getPermissions(); } @Override public void setPermissions(String username, ClientPermissions permissions) { - User user = srv.config.dao.userService.findUserByUsername(username); + User user = srv.config.dao.userDAO.findByUsername(username); if (user == null) return; user.setPermissions(permissions); - srv.config.dao.userService.updateUser(user); + srv.config.dao.userDAO.update(user); } @Override diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java index 89587633..4f041d98 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -13,7 +13,7 @@ public class HibernateAuthProvider extends AuthProvider { @Override public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws Exception { if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported"); - User user = srv.config.dao.userService.findUserByUsername(login); + User user = srv.config.dao.userDAO.findByUsername(login); if (user == null && autoReg) { AuthHookManager.RegContext context = new AuthHookManager.RegContext(login, ((AuthPlainPassword) password).password, ip, false); if (srv.authHookManager.registraion.hook(context)) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java index bfb6cf59..0cf91d4d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java @@ -23,8 +23,8 @@ public String getUsageDescription() { @Override public void invoke(String... args) { int count = 0; - for (User user : server.config.dao.userService.findAllUsers()) { - LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString()); + for (User user : server.config.dao.userDAO.findAll()) { + LogHelper.subInfo("[%s] UUID: %s", user.getUsername(), user.getUuid().toString()); count++; } LogHelper.info("Print %d users", count); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java index 7b0ae432..3fe4fb1a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java @@ -23,12 +23,12 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 1); - User user = server.config.dao.userService.findUserByUsername(args[0]); + User user = server.config.dao.userDAO.findByUsername(args[0]); if (user == null) { LogHelper.error("User %s not found", args[0]); return; } - LogHelper.info("[%s] UUID: %s", user.username, user.uuid.toString()); + LogHelper.info("[%s] UUID: %s", user.getUsername(), user.getUuid().toString()); //for(UserHWID hwid : user.hwids) //{ // LogHelper.info("[%s] HWID: memory: %d | serial %s | hwdiskserial: %s | processorID %s | macAddr %s", user.username, hwid.totalMemory, hwid.serialNumber, hwid.HWDiskSerial, hwid.processorID, hwid.macAddr); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java index c970551a..7fabacc6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java @@ -2,7 +2,7 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; -import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.impl.UserHibernateImpl; import pro.gravit.utils.helper.LogHelper; import java.util.UUID; @@ -25,11 +25,11 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 2); - User user = new User(); + UserHibernateImpl user = new UserHibernateImpl(); user.username = args[0]; user.setPassword(args[1]); user.uuid = UUID.randomUUID(); - server.config.dao.userService.saveUser(user); + server.config.dao.userDAO.save(user); LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString()); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java index 8d0a6c47..d6a0a02e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java @@ -24,13 +24,13 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 2); - User user = server.config.dao.userService.findUserByUsername(args[0]); + User user = server.config.dao.userDAO.findByUsername(args[0]); if (user == null) { LogHelper.error("User %s not found", args[1]); return; } user.setPassword(args[1]); - server.config.dao.userService.updateUser(user); - LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]); + server.config.dao.userDAO.update(user); + LogHelper.info("[%s] UUID: %s | New Password: %s", user.getUsername(), user.getUuid().toString(), args[1]); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/HwidDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/HwidDAO.java new file mode 100644 index 00000000..9333d220 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/HwidDAO.java @@ -0,0 +1,19 @@ +package pro.gravit.launchserver.dao; + +import pro.gravit.launcher.hwid.HWID; + +import java.util.List; + +public interface HwidDAO { + UserHWID findById(long id); + + List findHWIDs(HWID hwid); + + void save(UserHWID user); + + void update(UserHWID user); + + void delete(UserHWID user); + + List findAll(); +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java index 28da0d4c..23b1e900 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java @@ -1,71 +1,19 @@ package pro.gravit.launchserver.dao; import pro.gravit.launcher.ClientPermissions; -import pro.gravit.utils.helper.LogHelper; -import pro.gravit.utils.helper.SecurityHelper; -import javax.persistence.*; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; import java.util.UUID; -@Entity -@Table(name = "users") -public class User { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - @Column(unique = true) - public String username; - public String email; - @Column(unique = true) - public UUID uuid; - @Column(name = "password") - private byte[] password; - private String accessToken; - public String serverID; - private String password_salt; - public long permissions; - - public void setPassword(String password) { - password_salt = SecurityHelper.randomStringAESKey(); - MessageDigest digest; - try { - digest = MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - LogHelper.error(e); - return; - } - this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); - } - - public boolean verifyPassword(String password) { - MessageDigest digest; - try { - digest = MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - LogHelper.error(e); - return false; - } - byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); - return Arrays.equals(enpassword, this.password); - } - - public ClientPermissions getPermissions() { - return new ClientPermissions(permissions); - } - - public void setPermissions(ClientPermissions permissions) { - this.permissions = permissions.toLong(); - } - - public String getAccessToken() { - return accessToken; - } - - public void setAccessToken(String accessToken) { - this.accessToken = accessToken; - } +public interface User { + String getUsername(); + ClientPermissions getPermissions(); + void setPermissions(ClientPermissions permissions); + boolean verifyPassword(String password); + void setPassword(String password); + String getAccessToken(); + void setAccessToken(String accessToken); + String getServerID(); + void setServerID(String serverID); + UUID getUuid(); + void setUuid(UUID uuid); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java index 7a666c8f..85987e85 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserDAO.java @@ -1,6 +1,6 @@ package pro.gravit.launchserver.dao; -import pro.gravit.launcher.hwid.OshiHWID; +import pro.gravit.launcher.hwid.HWID; import java.util.List; import java.util.UUID; @@ -12,8 +12,6 @@ public interface UserDAO { User findByUUID(UUID uuid); - List findHWID(OshiHWID hwid); - void save(User user); void update(User user); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserHWID.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserHWID.java index 2c6fe8e3..da8f438c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserHWID.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserHWID.java @@ -1,63 +1,8 @@ package pro.gravit.launchserver.dao; import pro.gravit.launcher.hwid.HWID; -import pro.gravit.launcher.hwid.OshiHWID; -import javax.persistence.*; -import java.util.function.Supplier; - -@Entity -@Table(name = "users_hwids") -public class UserHWID implements HWID { - private final transient Supplier oshiSupp = () -> { - OshiHWID hwid = new OshiHWID(); - hwid.HWDiskSerial = this.HWDiskSerial; - hwid.macAddr = this.macAddr; - hwid.processorID = this.processorID; - hwid.serialNumber = this.serialNumber; - hwid.totalMemory = this.totalMemory; - return hwid; - }; - private transient OshiHWID oshi = null; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - - public final long totalMemory = 0; - public String serialNumber; - public String HWDiskSerial; - public String processorID; - public String macAddr; - - public boolean banned; - - public OshiHWID toHWID() { - if (oshi == null) oshi = oshiSupp.get(); - return oshi; - } - - @Override - public int getLevel() { - return toHWID().getLevel(); - } - - @Override - public int getAntiLevel() { - return 0; - } - - @Override - public int compare(HWID hwid) { - return toHWID().compare(hwid); - } - - @Override - public boolean isNull() { - return toHWID().isNull(); - } - - @Override - public void normalize() { - - } +public interface UserHWID { + boolean isBanned(); + HWID toHWID(); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java deleted file mode 100644 index cdc7fb23..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java +++ /dev/null @@ -1,51 +0,0 @@ -package pro.gravit.launchserver.dao; - -import java.util.List; -import java.util.UUID; - -public class UserService { - - private final UserDAO usersDao; - - public UserService(UserDAO usersDAO) { - this.usersDao = usersDAO; - } - - public User findUser(int id) { - return usersDao.findById(id); - } - - public User findUserByUsername(String username) { - return usersDao.findByUsername(username); - } - - public User findUserByUUID(UUID uuid) { - return usersDao.findByUUID(uuid); - } - - public User registerNewUser(String username, String password) { - User user = new User(); - user.username = username; - user.setPassword(password); - user.uuid = UUID.randomUUID(); - return user; - } - - public void saveUser(User user) { - usersDao.save(user); - } - - public void deleteUser(User user) { - usersDao.delete(user); - } - - public void updateUser(User user) { - usersDao.update(user); - } - - public List findAllUsers() { - return usersDao.findAll(); - } - - -} \ No newline at end of file diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java deleted file mode 100644 index 0c2509e0..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/DefaultUserDAOImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -package pro.gravit.launchserver.dao.impl; - -import pro.gravit.launcher.hwid.OshiHWID; -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.dao.User; -import pro.gravit.launchserver.dao.UserDAO; -import pro.gravit.launchserver.dao.UserHWID; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class DefaultUserDAOImpl implements UserDAO { - public DefaultUserDAOImpl(LaunchServer srv) { - } - - @Override - public User findById(int id) { - return null; - } - - @Override - public User findByUsername(String username) { - return null; - } - - @Override - public User findByUUID(UUID uuid) { - return null; - } - - @Override - public List findHWID(OshiHWID hwid) { - return new ArrayList<>(); - } - - @Override - public void save(User user) { - throw new UnsupportedOperationException(); - } - - @Override - public void update(User user) { - throw new UnsupportedOperationException(); - } - - @Override - public void delete(User user) { - throw new UnsupportedOperationException(); - } - - @Override - public List findAll() { - return new ArrayList<>(); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateHwidDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateHwidDAOImpl.java new file mode 100644 index 00000000..037e25cc --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateHwidDAOImpl.java @@ -0,0 +1,83 @@ +package pro.gravit.launchserver.dao.impl; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import pro.gravit.launcher.hwid.HWID; +import pro.gravit.launcher.hwid.OshiHWID; +import pro.gravit.launchserver.dao.HwidDAO; +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.UserHWID; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import java.util.List; + +public class HibernateHwidDAOImpl implements HwidDAO { + private final SessionFactory factory; + + public HibernateHwidDAOImpl(SessionFactory factory) { + this.factory = factory; + } + + @Override + public List findHWIDs(HWID hwid) { + if(!(hwid instanceof OshiHWID)) throw new UnsupportedOperationException(); + OshiHWID oshiHWID = (OshiHWID) hwid; + EntityManager em = factory.createEntityManager(); + em.getTransaction().begin(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery personCriteria = cb.createQuery(UserHWIDImpl.class); + Root rootUser = personCriteria.from(UserHWIDImpl.class); + personCriteria.select(rootUser).where( + cb.or( + cb.equal(rootUser.get("totalMemory"), oshiHWID.totalMemory), + cb.equal(rootUser.get("HWDiskSerial"), oshiHWID.HWDiskSerial), + cb.equal(rootUser.get("serialNumber"), oshiHWID.serialNumber), + cb.equal(rootUser.get("processorID"), oshiHWID.processorID), + cb.equal(rootUser.get("macAddr"), oshiHWID.macAddr) + ) + ); + List list = em.createQuery(personCriteria).getResultList(); + em.close(); + return list; + } + + public UserHWIDImpl findById(long id) { + try (Session s = factory.openSession()) { + return s.get(UserHWIDImpl.class, id); + } + } + public void save(UserHWID user) { + try (Session session = factory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.save(user); + tx1.commit(); + } + } + + public void update(UserHWID user) { + try (Session session = factory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.update(user); + tx1.commit(); + } + } + + public void delete(UserHWID user) { + try (Session session = factory.openSession()) { + Transaction tx1 = session.beginTransaction(); + session.delete(user); + tx1.commit(); + } + } + + @SuppressWarnings("unchecked") + public List findAll() { + try (Session s = factory.openSession()) { + return (List) s.createQuery("From user_hwids").list(); + } + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java index a31e106e..03c8bd5a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java @@ -3,6 +3,7 @@ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; +import pro.gravit.launcher.hwid.HWID; import pro.gravit.launcher.hwid.OshiHWID; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserDAO; @@ -22,57 +23,36 @@ public HibernateUserDAOImpl(SessionFactory factory) { this.factory = factory; } - public User findById(int id) { + public UserHibernateImpl findById(int id) { try (Session s = factory.openSession()) { - return s.get(User.class, id); + return s.get(UserHibernateImpl.class, id); } } - public User findByUsername(String username) { + public UserHibernateImpl findByUsername(String username) { EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery personCriteria = cb.createQuery(User.class); - Root rootUser = personCriteria.from(User.class); + CriteriaQuery personCriteria = cb.createQuery(UserHibernateImpl.class); + Root rootUser = personCriteria.from(UserHibernateImpl.class); personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username)); - List ret = em.createQuery(personCriteria).getResultList(); + List ret = em.createQuery(personCriteria).getResultList(); em.close(); return ret.size() == 0 ? null : ret.get(0); } - public User findByUUID(UUID uuid) { + public UserHibernateImpl findByUUID(UUID uuid) { EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery personCriteria = cb.createQuery(User.class); - Root rootUser = personCriteria.from(User.class); + CriteriaQuery personCriteria = cb.createQuery(UserHibernateImpl.class); + Root rootUser = personCriteria.from(UserHibernateImpl.class); personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid)); - List ret = em.createQuery(personCriteria).getResultList(); + List ret = em.createQuery(personCriteria).getResultList(); em.close(); return ret.size() == 0 ? null : ret.get(0); } - @Override - public List findHWID(OshiHWID hwid) { - EntityManager em = factory.createEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery personCriteria = cb.createQuery(UserHWID.class); - Root rootUser = personCriteria.from(UserHWID.class); - personCriteria.select(rootUser).where( - cb.or( - cb.equal(rootUser.get("totalMemory"), hwid.totalMemory), - cb.equal(rootUser.get("HWDiskSerial"), hwid.HWDiskSerial), - cb.equal(rootUser.get("serialNumber"), hwid.serialNumber), - cb.equal(rootUser.get("processorID"), hwid.processorID), - cb.equal(rootUser.get("macAddr"), hwid.macAddr) - ) - ); - List ret = em.createQuery(personCriteria).getResultList(); - em.close(); - return ret; - } - public void save(User user) { try (Session session = factory.openSession()) { Transaction tx1 = session.beginTransaction(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHWIDImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHWIDImpl.java new file mode 100644 index 00000000..7cc39c0e --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHWIDImpl.java @@ -0,0 +1,44 @@ +package pro.gravit.launchserver.dao.impl; + +import pro.gravit.launcher.hwid.HWID; +import pro.gravit.launcher.hwid.OshiHWID; +import pro.gravit.launchserver.dao.UserHWID; + +import javax.persistence.*; +import java.util.function.Supplier; + +@Entity +@Table(name = "users_hwids") +public class UserHWIDImpl implements UserHWID { + private final transient Supplier oshiSupp = () -> { + OshiHWID hwid = new OshiHWID(); + hwid.HWDiskSerial = this.HWDiskSerial; + hwid.macAddr = this.macAddr; + hwid.processorID = this.processorID; + hwid.serialNumber = this.serialNumber; + hwid.totalMemory = this.totalMemory; + return hwid; + }; + private transient OshiHWID oshi = null; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + public final long totalMemory = 0; + public String serialNumber; + public String HWDiskSerial; + public String processorID; + public String macAddr; + + public boolean banned; + + @Override + public boolean isBanned() { + return banned; + } + + public OshiHWID toHWID() { + if (oshi == null) oshi = oshiSupp.get(); + return oshi; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java new file mode 100644 index 00000000..6094d391 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java @@ -0,0 +1,99 @@ +package pro.gravit.launchserver.dao.impl; + +import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launchserver.dao.User; +import pro.gravit.utils.helper.LogHelper; +import pro.gravit.utils.helper.SecurityHelper; + +import javax.persistence.*; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.UUID; + +@Entity +@Table(name = "users") +public class UserHibernateImpl implements User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + @Column(unique = true) + public String username; + public String email; + @Column(unique = true) + public UUID uuid; + @Column(name = "password") + private byte[] password; + private String accessToken; + public String serverID; + private String password_salt; + public long permissions; + + public void setPassword(String password) { + password_salt = SecurityHelper.randomStringAESKey(); + MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + LogHelper.error(e); + return; + } + this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); + } + + public boolean verifyPassword(String password) { + MessageDigest digest; + try { + digest = MessageDigest.getInstance("SHA-256"); + } catch (NoSuchAlgorithmException e) { + LogHelper.error(e); + return false; + } + byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8)); + return Arrays.equals(enpassword, this.password); + } + + public ClientPermissions getPermissions() { + return new ClientPermissions(permissions); + } + + public void setPermissions(ClientPermissions permissions) { + this.permissions = permissions.toLong(); + } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } + + @Override + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + @Override + public String getServerID() { + return serverID; + } + + @Override + public void setServerID(String serverID) { + this.serverID = serverID; + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java index f794f720..927e9a6d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java @@ -1,14 +1,14 @@ package pro.gravit.launchserver.dao.provider; import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.dao.HwidDAO; import pro.gravit.launchserver.dao.UserDAO; -import pro.gravit.launchserver.dao.UserService; import pro.gravit.utils.ProviderMap; public abstract class DaoProvider { public static final ProviderMap providers = new ProviderMap<>("DaoProvider"); public UserDAO userDAO; - public UserService userService; + public HwidDAO hwidDao; public static void registerProviders() { providers.register("hibernate", HibernateDaoProvider.class); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java index 82d9197f..7ca931bb 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java @@ -1,10 +1,11 @@ package pro.gravit.launchserver.dao.provider; +import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.dao.User; -import pro.gravit.launchserver.dao.UserHWID; -import pro.gravit.launchserver.dao.UserService; +import pro.gravit.launchserver.dao.impl.HibernateHwidDAOImpl; +import pro.gravit.launchserver.dao.impl.UserHibernateImpl; +import pro.gravit.launchserver.dao.impl.UserHWIDImpl; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; import pro.gravit.utils.helper.CommonHelper; @@ -19,13 +20,14 @@ public class HibernateDaoProvider extends DaoProvider { public String pool_size; public String hibernateConfig; public boolean parallelHibernateInit; + private transient SessionFactory sessionFactory; @Override public void init(LaunchServer server) { Runnable init = () -> { Configuration cfg = new Configuration() - .addAnnotatedClass(User.class) - .addAnnotatedClass(UserHWID.class) + .addAnnotatedClass(UserHibernateImpl.class) + .addAnnotatedClass(UserHWIDImpl.class) .setProperty("hibernate.connection.driver_class", driver) .setProperty("hibernate.connection.url", url) .setProperty("hibernate.connection.username", username) @@ -35,8 +37,9 @@ public void init(LaunchServer server) { cfg.setProperty("hibernate.dialect", dialect); if (hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - userDAO = new HibernateUserDAOImpl(cfg.buildSessionFactory()); - userService = new UserService(userDAO); + sessionFactory = cfg.buildSessionFactory(); + userDAO = new HibernateUserDAOImpl(sessionFactory); + hwidDao = new HibernateHwidDAOImpl(sessionFactory); }; if (parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java index 2a008f48..e0567e48 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java @@ -2,6 +2,7 @@ import io.netty.channel.ChannelHandlerContext; import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.dao.impl.UserHibernateImpl; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.response.SimpleResponse; @@ -24,17 +25,17 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception { sendError("Hash invalid"); return; } - User checkUser = server.config.dao.userService.findUserByUsername(login); + User checkUser = server.config.dao.userDAO.findByUsername(login); if (checkUser != null) { sendError("User already register"); return; } - User user = new User(); + UserHibernateImpl user = new UserHibernateImpl(); user.username = login; user.email = email; user.setPassword(password); user.uuid = UUID.randomUUID(); - server.config.dao.userService.saveUser(user); + server.config.dao.userDAO.save(user); } @Override diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java index e79d5d1f..f1d9838e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java @@ -31,11 +31,11 @@ public void execute(ChannelHandlerContext ctx, Client client) { return; } if (username != null) { - User user = server.config.dao.userService.findUserByUsername(username); + User user = server.config.dao.userDAO.findByUsername(username); user.setPassword(newPassword); sendResult(new SetPasswordRequestEvent()); } else { - User user = server.config.dao.userService.findUserByUsername(client.username); + User user = server.config.dao.userDAO.findByUsername(client.username); if (user.verifyPassword(oldPassword)) { user.setPassword(newPassword); sendResult(new SetPasswordRequestEvent()); diff --git a/modules b/modules index 9cb09c54..12cb8354 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 9cb09c549abea86348597137e05a0ec29bc36b62 +Subproject commit 12cb835489667b39453e874f43bd314a67f0719e