Skip to content

Commit 4bc328c

Browse files
author
games647
committed
Fail safely if BungeeCord implementation specific classes are not found
1 parent 357430b commit 4bc328c

File tree

8 files changed

+69
-37
lines changed

8 files changed

+69
-37
lines changed

.github/workflows/maven.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Automatically build, run unit and integration tests to detect errors early (CI provided by GitHub)
2+
# including making pull requests review easier
23

34
# Human readable name in the actions tab
45
name: Java CI
@@ -45,4 +46,4 @@ jobs:
4546
# Run non-interactive, package (with compile+test),
4647
# ignore snapshot updates, because they are likely to have breaking changes, enforce checksums to validate
4748
# possible errors in dependencies
48-
run: mvn test --batch-mode --no-snapshot-updates --strict-checksums --file pom.xml
49+
run: mvn package test --batch-mode --no-snapshot-updates --strict-checksums --file pom.xml

bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/protocollib/EncryptionUtil.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.security.PrivateKey;
1111
import java.security.PublicKey;
1212
import java.util.Random;
13-
1413
import javax.crypto.Cipher;
1514
import javax.crypto.SecretKey;
1615
import javax.crypto.spec.SecretKeySpec;
@@ -27,7 +26,7 @@ public class EncryptionUtil {
2726
public static final String KEY_PAIR_ALGORITHM = "RSA";
2827

2928
private EncryptionUtil() {
30-
//utility
29+
// utility
3130
}
3231

3332
/**
@@ -43,7 +42,7 @@ public static KeyPair generateKeyPair() {
4342
keyPairGenerator.initialize(1_024);
4443
return keyPairGenerator.generateKeyPair();
4544
} catch (NoSuchAlgorithmException nosuchalgorithmexception) {
46-
//Should be existing in every vm
45+
// Should be existing in every vm
4746
throw new ExceptionInInitializerError(nosuchalgorithmexception);
4847
}
4948
}
@@ -65,9 +64,9 @@ public static byte[] generateVerifyToken(Random random) {
6564
/**
6665
* Generate the server id based on client and server data.
6766
*
68-
* @param sessionId session for the current login attempt
67+
* @param sessionId session for the current login attempt
6968
* @param sharedSecret shared secret between the client and the server
70-
* @param publicKey public key of the server
69+
* @param publicKey public key of the server
7170
* @return the server id formatted as a hexadecimal string.
7271
*/
7372
public static String getServerIdHashString(String sessionId, SecretKey sharedSecret, PublicKey publicKey) {
@@ -85,8 +84,8 @@ public static String getServerIdHashString(String sessionId, SecretKey sharedSec
8584
/**
8685
* Decrypts the content and extracts the key spec.
8786
*
88-
* @param cipher decryption cipher initialized with the private key
89-
* @param sharedKey the encrypted shared key
87+
* @param privateKey private server key
88+
* @param sharedKey the encrypted shared key
9089
* @return shared secret key
9190
* @throws GeneralSecurityException if it fails to decrypt the data
9291
*/
@@ -106,16 +105,18 @@ public static byte[] decrypt(PrivateKey key, byte[] data) throws GeneralSecurity
106105
* Decrypted the given data using the cipher.
107106
*
108107
* @param cipher decryption cypher initialized with the private key
109-
* @param data the encrypted data
108+
* @param data the encrypted data
110109
* @return clear text data
111110
* @throws GeneralSecurityException if it fails to decrypt the data
112111
*/
113112
private static byte[] decrypt(Cipher cipher, byte[] data) throws GeneralSecurityException {
114-
// inlined: byte[] a(int var0, Key var1, byte[] var2), Cipher a(int var0, String var1, Key var2)
113+
// inlined: byte[] a(int var0, Key var1, byte[] var2), Cipher a(int var0, String var1, Key
114+
// var2)
115115
return cipher.doFinal(data);
116116
}
117117

118-
private static byte[] getServerIdHash(String sessionId, PublicKey publicKey, SecretKey sharedSecret)
118+
private static byte[] getServerIdHash(
119+
String sessionId, PublicKey publicKey, SecretKey sharedSecret)
119120
throws NoSuchAlgorithmException {
120121
// byte[] a(String var0, PublicKey var1, SecretKey var2)
121122
MessageDigest digest = MessageDigest.getInstance("SHA-1");

bungee/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<plugin>
2222
<groupId>org.apache.maven.plugins</groupId>
2323
<artifactId>maven-shade-plugin</artifactId>
24-
<version>3.2.3</version>
24+
<version>3.2.4</version>
2525
<configuration>
2626
<createDependencyReducedPom>false</createDependencyReducedPom>
2727
<shadedArtifactAttached>false</shadedArtifactAttached>
@@ -84,7 +84,7 @@
8484
<dependency>
8585
<groupId>net.md-5</groupId>
8686
<artifactId>bungeecord-proxy</artifactId>
87-
<version>1.15-SNAPSHOT</version>
87+
<version>1.16-R0.5-SNAPSHOT</version>
8888
<scope>provided</scope>
8989
</dependency>
9090

bungee/src/main/java/com/github/games647/fastlogin/bungee/listener/ConnectListener.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,50 @@
3131
import net.md_5.bungee.event.EventPriority;
3232

3333
import org.geysermc.floodgate.FloodgateAPI;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
/**
3638
* Enables online mode logins for specified users and sends plugin message to the Bukkit version of this plugin in
3739
* order to clear that the connection is online mode.
3840
*/
3941
public class ConnectListener implements Listener {
4042

41-
private final FastLoginBungee plugin;
42-
private final RateLimiter rateLimiter;
43-
44-
private final Property[] emptyProperties = {};
45-
private final boolean floodGateAvailable;
46-
47-
4843
private static final String UUID_FIELD_NAME = "uniqueId";
44+
private static final boolean initialHandlerClazzFound;
4945
private static final MethodHandle uniqueIdSetter;
5046

5147
static {
5248
MethodHandle setHandle = null;
49+
boolean handlerFound = false;
5350
try {
5451
Lookup lookup = MethodHandles.lookup();
5552

53+
Class.forName("net.md_5.bungee.connection.InitialHandler");
54+
handlerFound = true;
55+
5656
Field uuidField = InitialHandler.class.getDeclaredField(UUID_FIELD_NAME);
5757
uuidField.setAccessible(true);
5858
setHandle = lookup.unreflectSetter(uuidField);
59+
} catch (ClassNotFoundException classNotFoundException) {
60+
Logger logger = LoggerFactory.getLogger(ConnectListener.class);
61+
logger.error(
62+
"Cannot find Bungee initial handler; Disabling premium UUID and skin won't work.",
63+
classNotFoundException
64+
);
5965
} catch (ReflectiveOperationException reflectiveOperationException) {
6066
reflectiveOperationException.printStackTrace();
6167
}
6268

69+
initialHandlerClazzFound = handlerFound;
6370
uniqueIdSetter = setHandle;
6471
}
6572

73+
private final FastLoginBungee plugin;
74+
private final RateLimiter rateLimiter;
75+
private final Property[] emptyProperties = {};
76+
private final boolean floodGateAvailable;
77+
6678
public ConnectListener(FastLoginBungee plugin, RateLimiter rateLimiter, boolean floodgateAvailable) {
6779
this.plugin = plugin;
6880
this.rateLimiter = rateLimiter;
@@ -97,29 +109,31 @@ public void onLogin(LoginEvent loginEvent) {
97109

98110
//use the login event instead of the post login event in order to send the login success packet to the client
99111
//with the offline uuid this makes it possible to set the skin then
100-
final PendingConnection connection = loginEvent.getConnection();
101-
final InitialHandler initialHandler = (InitialHandler) connection;
102-
112+
PendingConnection connection = loginEvent.getConnection();
103113
if (connection.isOnlineMode()) {
104114
LoginSession session = plugin.getSession().get(connection);
105-
LoginResult loginProfile = initialHandler.getLoginProfile();
106115

107116
UUID verifiedUUID = connection.getUniqueId();
117+
String verifiedUsername = connection.getName();
108118
session.setUuid(verifiedUUID);
109-
session.setVerifiedUsername(loginProfile.getName());
119+
session.setVerifiedUsername(verifiedUsername);
110120

111121
StoredProfile playerProfile = session.getProfile();
112122
playerProfile.setId(verifiedUUID);
113123

114-
//bungeecord will do this automatically so override it on disabled option
115-
if (!plugin.getCore().getConfig().get("premiumUuid", true)) {
116-
String username = loginProfile.getName();
117-
setOfflineId(initialHandler, username);
118-
}
124+
// bungeecord will do this automatically so override it on disabled option
125+
if (uniqueIdSetter != null) {
126+
InitialHandler initialHandler = (InitialHandler) connection;
127+
128+
if (!plugin.getCore().getConfig().get("premiumUuid", true)) {
129+
setOfflineId(initialHandler, verifiedUsername);
130+
}
119131

120-
if (!plugin.getCore().getConfig().get("forwardSkin", true)) {
121-
// this is null on offline mode
122-
loginProfile.setProperties(emptyProperties);
132+
if (!plugin.getCore().getConfig().get("forwardSkin", true)) {
133+
// this is null on offline mode
134+
LoginResult loginProfile = initialHandler.getLoginProfile();
135+
loginProfile.setProperties(emptyProperties);
136+
}
123137
}
124138
}
125139
}

core/src/main/java/com/github/games647/fastlogin/core/AuthStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void save(StoredProfile playerProfile) {
206206

207207
saveStmt.execute();
208208
try (ResultSet generatedKeys = saveStmt.getGeneratedKeys()) {
209-
if (generatedKeys != null && generatedKeys.next()) {
209+
if (generatedKeys.next()) {
210210
playerProfile.setRowId(generatedKeys.getInt(1));
211211
}
212212
}

core/src/main/java/com/github/games647/fastlogin/core/StoredProfile.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.games647.craftapi.model.Profile;
44

55
import java.time.Instant;
6+
import java.util.Objects;
67
import java.util.Optional;
78
import java.util.UUID;
89
import java.util.concurrent.locks.ReentrantLock;
@@ -86,6 +87,21 @@ public synchronized void setLastLogin(Instant lastLogin) {
8687
this.lastLogin = lastLogin;
8788
}
8889

90+
@Override
91+
public boolean equals(Object o) {
92+
if (this == o) return true;
93+
if (!(o instanceof StoredProfile)) return false;
94+
if (!super.equals(o)) return false;
95+
StoredProfile that = (StoredProfile) o;
96+
return rowId == that.rowId && premium == that.premium
97+
&& Objects.equals(lastIp, that.lastIp) && lastLogin.equals(that.lastLogin);
98+
}
99+
100+
@Override
101+
public int hashCode() {
102+
return Objects.hash(super.hashCode(), rowId, premium, lastIp, lastLogin);
103+
}
104+
89105
@Override
90106
public synchronized String toString() {
91107
return this.getClass().getSimpleName() + '{' +

core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void load() {
9090
}
9191

9292
int maxCon = config.getInt("anti-bot.connections", 200);
93-
long expireTime = config.getInt("anti-bot.expire", 5) * 60 * 1_000L;
93+
long expireTime = config.getLong("anti-bot.expire", 5) * 60 * 1_000L;
9494
if (expireTime > MAX_EXPIRE_RATE) {
9595
expireTime = MAX_EXPIRE_RATE;
9696
}
@@ -132,7 +132,7 @@ private Configuration loadFile(String fileName) throws IOException {
132132
config = configProvider.load(reader, defaults);
133133
}
134134

135-
//explicitly add keys here, because Configuration.getKeys doesn't return the keys from the default configuration
135+
// explicitly add keys here, because Configuration.getKeys doesn't return the keys from the default configuration
136136
for (String key : defaults.getKeys()) {
137137
config.set(key, config.get(key));
138138
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<plugin>
5353
<groupId>pl.project13.maven</groupId>
5454
<artifactId>git-commit-id-plugin</artifactId>
55-
<version>4.0.0</version>
55+
<version>4.0.3</version>
5656
<configuration>
5757
<failOnNoGitDirectory>false</failOnNoGitDirectory>
5858
</configuration>

0 commit comments

Comments
 (0)