Skip to content

Commit

Permalink
misc: extract forcibly disable 'force-key-authentication' field method
Browse files Browse the repository at this point in the history
  • Loading branch information
MemencioPerez committed Nov 7, 2024
1 parent 9902a19 commit 43c25cc
Showing 1 changed file with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -74,22 +73,10 @@ public void onProxyInitialize(ProxyInitializeEvent event) {

if (configuration.removeSignedKeyOnJoin()) {
try {
VelocityConfiguration velocityConfiguration = ((VelocityServer) server).getConfiguration();
Field forceKeyAuthenticationField = velocityConfiguration.getClass().getDeclaredField("forceKeyAuthentication");
forceKeyAuthenticationField.setAccessible(true);
boolean forceKeyAuthenticationValue = (boolean) forceKeyAuthenticationField.get(velocityConfiguration);
if (forceKeyAuthenticationValue) {
logger.warn("WARN: The 'force-key-authentication' option in the Velocity configuration file (velocity.toml) is set to 'true'.");
logger.warn("UnSignedVelocity requires that option to be set to 'false', so it will try to set it to 'true' forcefully at runtime.");
logger.warn("If you want to hide this warning, set 'force-key-authentication' to 'false' in Velocity settings and restart the proxy.");
logger.warn("Trying to set 'force-key-authentication' to false...");
forceKeyAuthenticationField.setBoolean(velocityConfiguration, false);
forceKeyAuthenticationField.setAccessible(false);
logger.warn("The 'force-key-authentication' field was found and set to false at runtime (this doesn't modify velocity.toml file).");
}
forciblyDisableForceKeyAuthentication();
} catch (NoSuchFieldException e) {
logger.error("The plugin cannot find 'force-key-authentication' option field, 'remove-signed-key-on-join' option will not work. Contact the developer of this plugin.", e);
} catch (InaccessibleObjectException | SecurityException | IllegalAccessException | IllegalArgumentException | NullPointerException | ExceptionInInitializerError e ) {
} catch (IllegalAccessException e) {
logger.error("The plugin cannot access 'force-key-authentication' option field, 'remove-signed-key-on-join' option will not work. If setting 'force-key-authentication' to 'false' manually and restarting the proxy doesn't work, contact the developer of this plugin.", e);
}
}
Expand All @@ -110,6 +97,22 @@ public void onProxyInitialize(ProxyInitializeEvent event) {
updateChecker.checkForUpdates();
}

private void forciblyDisableForceKeyAuthentication() throws NoSuchFieldException, IllegalAccessException {
VelocityConfiguration velocityConfiguration = ((VelocityServer) server).getConfiguration();
Field forceKeyAuthenticationField = velocityConfiguration.getClass().getDeclaredField("forceKeyAuthentication");
forceKeyAuthenticationField.setAccessible(true);
boolean forceKeyAuthenticationValue = (boolean) forceKeyAuthenticationField.get(velocityConfiguration);
if (forceKeyAuthenticationValue) {
logger.warn("WARN: The 'force-key-authentication' option in the Velocity configuration file (velocity.toml) is set to 'true'.");
logger.warn("UnSignedVelocity requires that option to be set to 'false', so it will try to set it to 'true' forcefully at runtime.");
logger.warn("If you want to hide this warning, set 'force-key-authentication' to 'false' in Velocity settings and restart the proxy.");
logger.warn("Trying to set 'force-key-authentication' to false...");
forceKeyAuthenticationField.setBoolean(velocityConfiguration, false);
forceKeyAuthenticationField.setAccessible(false);
logger.warn("The 'force-key-authentication' field was found and set to false at runtime (this doesn't modify velocity.toml file).");
}
}

public boolean setupConfiguration() {
try {
this.configuration = Configuration.loadConfig(dataDirectory);
Expand Down

0 comments on commit 43c25cc

Please sign in to comment.