mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE][EXPERIMENTAL] New Hibernate DAO
This commit is contained in:
parent
f0ca39cde9
commit
43e1533c87
22 changed files with 314 additions and 301 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<? extends UserHWID> findHWIDs(HWID hwid);
|
||||
|
||||
void save(UserHWID user);
|
||||
|
||||
void update(UserHWID user);
|
||||
|
||||
void delete(UserHWID user);
|
||||
|
||||
List<UserHWID> findAll();
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<UserHWID> findHWID(OshiHWID hwid);
|
||||
|
||||
void save(User user);
|
||||
|
||||
void update(User user);
|
||||
|
|
|
@ -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<OshiHWID> 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();
|
||||
}
|
||||
|
|
|
@ -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<User> findAllUsers() {
|
||||
return usersDao.findAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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<UserHWID> 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<User> findAll() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
|
@ -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<UserHWIDImpl> 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<UserHWIDImpl> personCriteria = cb.createQuery(UserHWIDImpl.class);
|
||||
Root<UserHWIDImpl> 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<UserHWIDImpl> 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<UserHWID> findAll() {
|
||||
try (Session s = factory.openSession()) {
|
||||
return (List<UserHWID>) s.createQuery("From user_hwids").list();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<User> personCriteria = cb.createQuery(User.class);
|
||||
Root<User> rootUser = personCriteria.from(User.class);
|
||||
CriteriaQuery<UserHibernateImpl> personCriteria = cb.createQuery(UserHibernateImpl.class);
|
||||
Root<UserHibernateImpl> rootUser = personCriteria.from(UserHibernateImpl.class);
|
||||
personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username));
|
||||
List<User> ret = em.createQuery(personCriteria).getResultList();
|
||||
List<UserHibernateImpl> 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<User> personCriteria = cb.createQuery(User.class);
|
||||
Root<User> rootUser = personCriteria.from(User.class);
|
||||
CriteriaQuery<UserHibernateImpl> personCriteria = cb.createQuery(UserHibernateImpl.class);
|
||||
Root<UserHibernateImpl> rootUser = personCriteria.from(UserHibernateImpl.class);
|
||||
personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid));
|
||||
List<User> ret = em.createQuery(personCriteria).getResultList();
|
||||
List<UserHibernateImpl> ret = em.createQuery(personCriteria).getResultList();
|
||||
em.close();
|
||||
return ret.size() == 0 ? null : ret.get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserHWID> findHWID(OshiHWID hwid) {
|
||||
EntityManager em = factory.createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<UserHWID> personCriteria = cb.createQuery(UserHWID.class);
|
||||
Root<UserHWID> 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<UserHWID> ret = em.createQuery(personCriteria).getResultList();
|
||||
em.close();
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void save(User user) {
|
||||
try (Session session = factory.openSession()) {
|
||||
Transaction tx1 = session.beginTransaction();
|
||||
|
|
|
@ -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<OshiHWID> 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<DaoProvider> providers = new ProviderMap<>("DaoProvider");
|
||||
public UserDAO userDAO;
|
||||
public UserService userService;
|
||||
public HwidDAO hwidDao;
|
||||
|
||||
public static void registerProviders() {
|
||||
providers.register("hibernate", HibernateDaoProvider.class);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
|
2
modules
2
modules
|
@ -1 +1 @@
|
|||
Subproject commit 9cb09c549abea86348597137e05a0ec29bc36b62
|
||||
Subproject commit 12cb835489667b39453e874f43bd314a67f0719e
|
Loading…
Reference in a new issue