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 deleted file mode 100644 index 0cf91d4d..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package pro.gravit.launchserver.command.dao; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; -import pro.gravit.launchserver.dao.User; -import pro.gravit.utils.helper.LogHelper; - -public class GetAllUsersCommand extends Command { - public GetAllUsersCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return ""; - } - - @Override - public String getUsageDescription() { - return "get all users information"; - } - - @Override - public void invoke(String... args) { - int count = 0; - 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 deleted file mode 100644 index 3fe4fb1a..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java +++ /dev/null @@ -1,37 +0,0 @@ -package pro.gravit.launchserver.command.dao; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; -import pro.gravit.launchserver.dao.User; -import pro.gravit.utils.helper.LogHelper; - -public class GetUserCommand extends Command { - public GetUserCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return "[username]"; - } - - @Override - public String getUsageDescription() { - return "get user information"; - } - - @Override - public void invoke(String... args) throws Exception { - verifyArgs(args, 1); - 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.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 deleted file mode 100644 index 7fabacc6..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java +++ /dev/null @@ -1,35 +0,0 @@ -package pro.gravit.launchserver.command.dao; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; -import pro.gravit.launchserver.dao.impl.UserHibernateImpl; -import pro.gravit.utils.helper.LogHelper; - -import java.util.UUID; - -public class RegisterCommand extends Command { - public RegisterCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return "[login] [password]"; - } - - @Override - public String getUsageDescription() { - return "Register new user"; - } - - @Override - public void invoke(String... args) throws Exception { - verifyArgs(args, 2); - UserHibernateImpl user = new UserHibernateImpl(); - user.username = args[0]; - user.setPassword(args[1]); - user.uuid = UUID.randomUUID(); - 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 deleted file mode 100644 index d6a0a02e..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java +++ /dev/null @@ -1,36 +0,0 @@ -package pro.gravit.launchserver.command.dao; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; -import pro.gravit.launchserver.dao.User; -import pro.gravit.utils.helper.LogHelper; - -public class SetUserPasswordCommand extends Command { - - public SetUserPasswordCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return "[username] [new password]"; - } - - @Override - public String getUsageDescription() { - return "Set user password"; - } - - @Override - public void invoke(String... args) throws Exception { - verifyArgs(args, 2); - 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.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/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index 7eb27214..45f01c24 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -5,10 +5,6 @@ import pro.gravit.launchserver.command.auth.UUIDToUsernameCommand; import pro.gravit.launchserver.command.auth.UsernameToUUIDCommand; import pro.gravit.launchserver.command.basic.*; -import pro.gravit.launchserver.command.dao.GetAllUsersCommand; -import pro.gravit.launchserver.command.dao.GetUserCommand; -import pro.gravit.launchserver.command.dao.RegisterCommand; -import pro.gravit.launchserver.command.dao.SetUserPasswordCommand; import pro.gravit.launchserver.command.dump.DumpSessionsCommand; import pro.gravit.launchserver.command.hash.*; import pro.gravit.launchserver.command.install.CheckInstallCommand; @@ -56,15 +52,6 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand Category updatesCategory = new Category(updates, "updates", "Update and Sync Management"); handler.registerCategory(updatesCategory); - //Register dao commands - BaseCommandCategory dao = new BaseCommandCategory(); - dao.registerCommand("register", new RegisterCommand(server)); - dao.registerCommand("setUserPassword", new SetUserPasswordCommand(server)); - dao.registerCommand("getUser", new GetUserCommand(server)); - dao.registerCommand("getAllUsers", new GetAllUsersCommand(server)); - Category daoCategory = new Category(dao, "DAO", "Data Management"); - handler.registerCategory(daoCategory); - // Register auth commands BaseCommandCategory auth = new BaseCommandCategory(); auth.registerCommand("auth", new AuthCommand(server)); 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 deleted file mode 100644 index e11fe09d..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java +++ /dev/null @@ -1,83 +0,0 @@ -package pro.gravit.launchserver.dao.impl; - -import org.hibernate.Session; -import org.hibernate.SessionFactory; -import org.hibernate.Transaction; -import pro.gravit.launchserver.dao.User; -import pro.gravit.launchserver.dao.UserDAO; - -import javax.persistence.EntityManager; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Root; -import java.util.List; -import java.util.UUID; - -public class HibernateUserDAOImpl implements UserDAO { - private final SessionFactory factory; - - public HibernateUserDAOImpl(SessionFactory factory) { - this.factory = factory; - } - - public UserHibernateImpl findById(int id) { - try (Session s = factory.openSession()) { - return s.get(UserHibernateImpl.class, id); - } - } - - public UserHibernateImpl findByUsername(String username) { - EntityManager em = factory.createEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - 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(); - em.close(); - return ret.size() == 0 ? null : ret.get(0); - } - - public UserHibernateImpl findByUUID(UUID uuid) { - EntityManager em = factory.createEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - 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(); - em.close(); - return ret.size() == 0 ? null : ret.get(0); - } - - public void save(User user) { - try (Session session = factory.openSession()) { - Transaction tx1 = session.beginTransaction(); - session.save(user); - tx1.commit(); - } - } - - public void update(User user) { - try (Session session = factory.openSession()) { - Transaction tx1 = session.beginTransaction(); - session.update(user); - tx1.commit(); - } - } - - public void delete(User 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").list(); - } - } -} \ No newline at end of file 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 deleted file mode 100644 index caf45f6d..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java +++ /dev/null @@ -1,101 +0,0 @@ -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(name = "User") -@Table(name = "users") -public class UserHibernateImpl implements User { - @Column(unique = true) - public String username; - public String email; - @Column(unique = true) - public UUID uuid; - public String serverID; - public long permissions; - public long flags; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - @Column(name = "password") - private byte[] password; - private String accessToken; - private String password_salt; - - 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, flags); - } - - public void setPermissions(ClientPermissions permissions) { - this.permissions = permissions.permissions; - this.flags = permissions.flags; - } - - 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 9921ac47..a7fad686 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 @@ -9,7 +9,7 @@ public abstract class DaoProvider { public UserDAO userDAO; public static void registerProviders() { - providers.register("hibernate", HibernateDaoProvider.class); + // None } public abstract void init(LaunchServer server); 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 c08cc0e2..bae109bd 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 @@ -6,8 +6,7 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.Reconfigurable; import pro.gravit.launchserver.dao.User; -import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; -import pro.gravit.launchserver.dao.impl.UserHibernateImpl; +import pro.gravit.launchserver.dao.UserDAO; import pro.gravit.utils.command.Command; import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.helper.CommonHelper; @@ -17,7 +16,7 @@ import java.util.HashMap; import java.util.Map; -public class HibernateDaoProvider extends DaoProvider implements Reconfigurable, AutoCloseable { +public abstract class HibernateDaoProvider extends DaoProvider implements Reconfigurable, AutoCloseable { public String driver; public String url; public String username; @@ -26,13 +25,13 @@ public class HibernateDaoProvider extends DaoProvider implements Reconfigurable, public String pool_size; public String hibernateConfig; public boolean parallelHibernateInit; - private transient SessionFactory sessionFactory; + protected transient SessionFactory sessionFactory; @Override public void init(LaunchServer server) { Runnable init = () -> { Configuration cfg = new Configuration() - .addAnnotatedClass(UserHibernateImpl.class) + //.addAnnotatedClass(UserHibernateImpl.class) .setProperty("hibernate.connection.driver_class", driver) .setProperty("hibernate.connection.url", url) .setProperty("hibernate.connection.username", username) @@ -42,8 +41,9 @@ public void init(LaunchServer server) { cfg.setProperty("hibernate.dialect", dialect); if (hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); + onConfigure(cfg); sessionFactory = cfg.buildSessionFactory(); - userDAO = new HibernateUserDAOImpl(sessionFactory); + userDAO = newUserDAO(); }; if (parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); @@ -51,6 +51,10 @@ public void init(LaunchServer server) { init.run(); } + protected abstract void onConfigure(Configuration configuration); + + protected abstract UserDAO newUserDAO(); + @Override public Map getCommands() { Map commands = new HashMap<>(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java index a7436aad..461e9fc7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/Client.java @@ -5,6 +5,7 @@ import pro.gravit.launcher.request.secure.HardwareReportRequest; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.auth.AuthProviderPair; +import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.socket.response.auth.AuthResponse; import pro.gravit.utils.helper.LogHelper; @@ -23,6 +24,8 @@ public class Client { public transient AuthProviderPair auth; + public transient User daoObject; + public Client(long session) { this.session = session; timestamp = System.currentTimeMillis(); 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 c17428b9..79503396 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 @@ -3,7 +3,6 @@ import io.netty.channel.ChannelHandlerContext; import pro.gravit.launcher.ClientPermissions; 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; @@ -11,7 +10,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; -import java.util.UUID; public class RegisterResponse extends SimpleResponse { public String login; @@ -37,12 +35,6 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception { sendError("User already register"); return; } - UserHibernateImpl user = new UserHibernateImpl(); - user.username = login; - user.email = email; - user.setPassword(password); - user.uuid = UUID.randomUUID(); - server.config.dao.userDAO.save(user); } @Override