From 55c0cdfa0d395cd60e15a4cd41d313e715432e5c Mon Sep 17 00:00:00 2001 From: microwin7 Date: Sun, 16 Jul 2023 09:11:58 +0300 Subject: [PATCH] [FEATURE] Replaced with use TimeUnit (#666) --- .../gravit/launchserver/auth/MySQLSourceConfig.java | 5 +++-- .../launchserver/auth/PostgreSQLSourceConfig.java | 7 +++++-- .../auth/core/AbstractSQLCoreProvider.java | 11 +++++++---- .../auth/protect/AdvancedProtectHandler.java | 6 ++++-- .../launchserver/components/AbstractLimiter.java | 2 +- .../launchserver/config/LaunchServerConfig.java | 13 ++++++++----- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/MySQLSourceConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/MySQLSourceConfig.java index 7000c845..3b40a5f5 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/MySQLSourceConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/MySQLSourceConfig.java @@ -11,6 +11,8 @@ import java.sql.Connection; import java.sql.SQLException; +import static java.util.concurrent.TimeUnit.MINUTES; + public final class MySQLSourceConfig implements AutoCloseable, SQLSourceConfig { public static final int TIMEOUT = VerifyHelper.verifyInt( @@ -33,7 +35,7 @@ public final class MySQLSourceConfig implements AutoCloseable, SQLSourceConfig { private String password; private String database; private String timezone; - private long hikariMaxLifetime = 30*60*1000; // 30 minutes + private long hikariMaxLifetime = MINUTES.toMillis(30); private boolean useHikari; // Cache @@ -109,7 +111,6 @@ public synchronized Connection getConnection() throws SQLException { hikariConfig.setMaximumPoolSize(MAX_POOL_SIZE); hikariConfig.setConnectionTestQuery("SELECT 1"); hikariConfig.setConnectionTimeout(1000); - hikariConfig.setAutoCommit(true); hikariConfig.setLeakDetectionThreshold(2000); hikariConfig.setMaxLifetime(hikariMaxLifetime); // Set HikariCP pool diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/PostgreSQLSourceConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/PostgreSQLSourceConfig.java index 6e5ad777..38744764 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/PostgreSQLSourceConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/PostgreSQLSourceConfig.java @@ -10,6 +10,9 @@ import java.sql.Connection; import java.sql.SQLException; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.SECONDS; + public final class PostgreSQLSourceConfig implements AutoCloseable, SQLSourceConfig { public static final int TIMEOUT = VerifyHelper.verifyInt( Integer.parseUnsignedInt(System.getProperty("launcher.postgresql.idleTimeout", Integer.toString(5000))), @@ -27,7 +30,7 @@ public final class PostgreSQLSourceConfig implements AutoCloseable, SQLSourceCon private String password; private String database; - private long hikariMaxLifetime = 30*60*1000; // 30 minutes + private long hikariMaxLifetime = MINUTES.toMillis(30); // 30 minutes // Cache private transient DataSource source; @@ -67,7 +70,7 @@ public synchronized Connection getConnection() throws SQLException { hikariSource.setPoolName(poolName); hikariSource.setMinimumIdle(0); hikariSource.setMaximumPoolSize(MAX_POOL_SIZE); - hikariSource.setIdleTimeout(TIMEOUT * 1000L); + hikariSource.setIdleTimeout(SECONDS.toMillis(TIMEOUT)); hikariSource.setMaxLifetime(hikariMaxLifetime); // Replace source with hds diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AbstractSQLCoreProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AbstractSQLCoreProvider.java index 643fe34e..6950b88f 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AbstractSQLCoreProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/core/AbstractSQLCoreProvider.java @@ -28,9 +28,12 @@ import java.util.List; import java.util.UUID; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.SECONDS; + public abstract class AbstractSQLCoreProvider extends AuthCoreProvider { public final transient Logger logger = LogManager.getLogger(); - public int expireSeconds = 3600; + public long expireSeconds = HOURS.toSeconds(1); public String uuidColumn; public String usernameColumn; public String accessTokenColumn; @@ -129,7 +132,7 @@ public AuthManager.AuthReport refreshAccessToken(String refreshToken, AuthRespon return null; } var accessToken = LegacySessionHelper.makeAccessJwtTokenFromString(user, LocalDateTime.now(Clock.systemUTC()).plusSeconds(expireSeconds), server.keyAgreementManager.ecdsaPrivateKey); - return new AuthManager.AuthReport(null, accessToken, refreshToken, expireSeconds * 1000L, new SQLUserSession(user)); + return new AuthManager.AuthReport(null, accessToken, refreshToken, SECONDS.toMillis(expireSeconds), new SQLUserSession(user)); } @Override @@ -153,9 +156,9 @@ public AuthManager.AuthReport authorize(String login, AuthResponse.AuthContext c if (minecraftAccess) { String minecraftAccessToken = SecurityHelper.randomStringToken(); updateAuth(SQLUser, minecraftAccessToken); - return AuthManager.AuthReport.ofOAuthWithMinecraft(minecraftAccessToken, accessToken, refreshToken, expireSeconds * 1000L, session); + return AuthManager.AuthReport.ofOAuthWithMinecraft(minecraftAccessToken, accessToken, refreshToken, SECONDS.toMillis(expireSeconds), session); } else { - return AuthManager.AuthReport.ofOAuth(accessToken, refreshToken, expireSeconds * 1000L, session); + return AuthManager.AuthReport.ofOAuth(accessToken, refreshToken, SECONDS.toMillis(expireSeconds), session); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java index d740b7a3..37b610f2 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/protect/AdvancedProtectHandler.java @@ -21,6 +21,8 @@ import java.util.Base64; import java.util.Date; +import static java.util.concurrent.TimeUnit.SECONDS; + public class AdvancedProtectHandler extends StdProtectHandler implements SecureProtectHandler, HardwareProtectHandler, JoinServerProtectHandler { private transient final Logger logger = LogManager.getLogger(); public boolean enableHardwareFeature; @@ -104,7 +106,7 @@ public String createHardwareToken(String username, UserHardware hardware) { return Jwts.builder() .setIssuer("LaunchServer") .setSubject(username) - .setExpiration(new Date(System.currentTimeMillis() + 1000 * server.config.netty.security.hardwareTokenExpire)) + .setExpiration(new Date(System.currentTimeMillis() + SECONDS.toMillis(server.config.netty.security.hardwareTokenExpire))) .claim("hardware", hardware.getId()) .signWith(server.keyAgreementManager.ecdsaPrivateKey) .compact(); @@ -114,7 +116,7 @@ public String createPublicKeyToken(String username, byte[] publicKey) { return Jwts.builder() .setIssuer("LaunchServer") .setSubject(username) - .setExpiration(new Date(System.currentTimeMillis() + 1000 * server.config.netty.security.publicKeyTokenExpire)) + .setExpiration(new Date(System.currentTimeMillis() + SECONDS.toMillis(server.config.netty.security.publicKeyTokenExpire))) .claim("publicKey", Base64.getEncoder().encodeToString(publicKey)) .signWith(server.keyAgreementManager.ecdsaPrivateKey) .compact(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java index 121dd00a..3fd4f2eb 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AbstractLimiter.java @@ -16,7 +16,7 @@ public abstract class AbstractLimiter extends Component implements Reconfigur protected final transient Map map = new HashMap<>(); private transient final Logger logger = LogManager.getLogger(); public int rateLimit; - public int rateLimitMillis; + public long rateLimitMillis; @Override public Map getCommands() { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java index f71705be..49966b8e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java @@ -23,6 +23,9 @@ import java.io.File; import java.util.*; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.SECONDS; + public final class LaunchServerConfig { private final static List oldMirrorList = List.of("https://mirror.gravit.pro/5.2.x/", "https://mirror.gravit.pro/5.3.x/", "https://mirror.gravitlauncher.com/5.2.x/", "https://mirror.gravitlauncher.com/5.3.x/"); private transient final Logger logger = LogManager.getLogger(); @@ -94,12 +97,12 @@ public static LaunchServerConfig getDefault(LaunchServer.LaunchServerEnv env) { newConfig.components = new HashMap<>(); AuthLimiterComponent authLimiterComponent = new AuthLimiterComponent(); authLimiterComponent.rateLimit = 3; - authLimiterComponent.rateLimitMillis = 8000; + authLimiterComponent.rateLimitMillis = SECONDS.toMillis(8); authLimiterComponent.message = "Превышен лимит авторизаций"; newConfig.components.put("authLimiter", authLimiterComponent); RegLimiterComponent regLimiterComponent = new RegLimiterComponent(); regLimiterComponent.rateLimit = 3; - regLimiterComponent.rateLimitMillis = 1000 * 60 * 60 * 10; //Блок на 10 часов + regLimiterComponent.rateLimitMillis = HOURS.toMillis(10); regLimiterComponent.message = "Превышен лимит регистраций"; newConfig.components.put("regLimiter", regLimiterComponent); ProGuardComponent proGuardComponent = new ProGuardComponent(); @@ -310,9 +313,9 @@ public NettyBindAddress(String address, int port) { } public static class NettySecurityConfig { - public long hardwareTokenExpire = 60 * 60 * 8; - public long publicKeyTokenExpire = 60 * 60 * 8; + public long hardwareTokenExpire = HOURS.toSeconds(8); + public long publicKeyTokenExpire = HOURS.toSeconds(8); - public long launcherTokenExpire = 60 * 60 * 8; + public long launcherTokenExpire = HOURS.toSeconds(8); } }