Implement AuthMeBungee autologin (#1402)

* Implement AuthMeBungee autologin

There is a failing test due to cyclic dependency injection, @ljacqu could you take a quick look at this?

* Try to fix recursive dependency injection

* Fix codestyle

* Fix the subchannel name, again...

* Split BungeeService into BungeeSender and BungeeReceiver
This commit is contained in:
Gabriele C 2017-11-09 11:19:24 +01:00 committed by GitHub
parent 7e079d0b9c
commit b7c3d4b42e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 241 additions and 179 deletions

View File

@ -27,6 +27,7 @@ import fr.xephi.authme.security.crypts.Sha256;
import fr.xephi.authme.service.BackupService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.MigrationService;
import fr.xephi.authme.service.bungeecord.BungeeReceiver;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SettingsWarner;
import fr.xephi.authme.settings.properties.SecuritySettings;
@ -247,6 +248,9 @@ public class AuthMe extends JavaPlugin {
commandHandler = injector.getSingleton(CommandHandler.class);
backupService = injector.getSingleton(BackupService.class);
// Trigger instantiation (class not used elsewhere)
injector.getSingleton(BungeeReceiver.class);
// Trigger construction of API classes; they will keep track of the singleton
injector.getSingleton(fr.xephi.authme.api.v3.AuthMeApi.class);
injector.getSingleton(NewAPI.class);

View File

@ -4,8 +4,8 @@ import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import org.bukkit.command.CommandSender;
@ -24,7 +24,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand {
private CommonService commonService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments) {
@ -34,7 +34,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand {
for (PlayerAuth auth : dataSource.getAllAuths()) {
resetLastPosition(auth);
dataSource.updateQuitLoc(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName);
}
sender.sendMessage("All players last position locations are now reset");
} else {
@ -47,7 +47,7 @@ public class PurgeLastPositionCommand implements ExecutableCommand {
resetLastPosition(auth);
dataSource.updateQuitLoc(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, playerName);
sender.sendMessage(playerName + "'s last position location is now reset");
}
}

View File

@ -5,8 +5,8 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
@ -37,7 +37,7 @@ public class OnShutdownPlayerSaver {
@Inject
private LimboService limboService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
OnShutdownPlayerSaver() {
}
@ -72,7 +72,7 @@ public class OnShutdownPlayerSaver {
.realName(player.getName())
.location(loc).build();
dataSource.updateQuitLoc(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, player.getName());
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, player.getName());
}
}
}

View File

@ -8,11 +8,11 @@ import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.JoinMessageService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.HooksSettings;
@ -86,7 +86,7 @@ public class PlayerListener implements Listener {
@Inject
private PermissionsManager permissionsManager;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
private boolean isAsyncPlayerPreLoginEventCalled = false;
@ -430,7 +430,7 @@ public class PlayerListener implements Listener {
.location(spawn)
.build();
dataSource.updateQuitLoc(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
}
if (spawn != null && spawn.getWorld() != null) {
event.setRespawnLocation(spawn);

View File

@ -6,10 +6,10 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -31,7 +31,7 @@ public class AsyncChangePassword implements AsynchronousProcess {
private PlayerCache playerCache;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsyncChangePassword() {
}
@ -54,7 +54,7 @@ public class AsyncChangePassword implements AsynchronousProcess {
commonService.send(player, MessageKey.ERROR);
return;
}
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, name);
playerCache.updatePlayer(auth);
commonService.send(player, MessageKey.PASSWORD_CHANGED_SUCCESS);
@ -84,7 +84,7 @@ public class AsyncChangePassword implements AsynchronousProcess {
HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, lowerCaseName);
if (dataSource.updatePassword(lowerCaseName, hashedPassword)) {
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName);
if (sender != null) {
commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS);
ConsoleLogger.info(sender.getName() + " changed password of " + lowerCaseName);

View File

@ -6,9 +6,9 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
@ -33,7 +33,7 @@ public class AsyncAddEmail implements AsynchronousProcess {
private ValidationService validationService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsyncAddEmail() { }
@ -60,7 +60,7 @@ public class AsyncAddEmail implements AsynchronousProcess {
auth.setEmail(email);
if (dataSource.updateEmail(auth)) {
playerCache.updatePlayer(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, playerName);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, playerName);
service.send(player, MessageKey.EMAIL_ADDED_SUCCESS);
} else {
ConsoleLogger.warning("Could not save email for player '" + player + "'");

View File

@ -5,9 +5,9 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import org.bukkit.entity.Player;
@ -31,7 +31,7 @@ public class AsyncChangeEmail implements AsynchronousProcess {
private ValidationService validationService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsyncChangeEmail() { }
@ -68,7 +68,7 @@ public class AsyncChangeEmail implements AsynchronousProcess {
auth.setEmail(newEmail);
if (dataSource.updateEmail(auth)) {
playerCache.updatePlayer(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, player.getName());
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_EMAIL, player.getName());
service.send(player, MessageKey.EMAIL_CHANGED_SUCCESS);
} else {
service.send(player, MessageKey.ERROR);

View File

@ -19,9 +19,9 @@ import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.SessionService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.EmailSettings;
@ -76,7 +76,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
private SessionService sessionService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsynchronousLogin() {
}
@ -226,7 +226,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
auth.setLastLogin(System.currentTimeMillis());
auth.setLastIp(ip);
dataSource.updateSession(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, player.getName());
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, player.getName());
// Successful login, so reset the captcha & temp ban count
final String name = player.getName();
@ -252,7 +252,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
playerCache.updatePlayer(auth);
dataSource.setLogged(name);
sessionService.grantSession(name);
bungeeService.sendAuthMeBungeecordMessage(MessageType.LOGIN, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.LOGIN, name);
// As the scheduling executes the Task most likely after the current
// task, we schedule it in the end

View File

@ -8,10 +8,10 @@ import fr.xephi.authme.events.LoginEvent;
import fr.xephi.authme.events.RestoreInventoryEvent;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.JoinMessageService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.WelcomeMessageConfiguration;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
@ -25,7 +25,7 @@ import static fr.xephi.authme.settings.properties.RestrictionSettings.PROTECT_IN
public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Inject
private LimboService limboService;
@ -96,6 +96,6 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
commandManager.runCommandsOnLogin(player);
// Send Bungee stuff. The service will check if it is enabled or not.
bungeeService.connectPlayerOnLogin(player);
bungeeSender.connectPlayerOnLogin(player);
}
}

View File

@ -7,9 +7,9 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.SessionService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;
@ -40,7 +40,7 @@ public class AsynchronousLogout implements AsynchronousProcess {
private SessionService sessionService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsynchronousLogout() {
}
@ -59,18 +59,18 @@ public class AsynchronousLogout implements AsynchronousProcess {
PlayerAuth auth = playerCache.getAuth(name);
database.updateSession(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_SESSION, name);
if (service.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
auth.setQuitLocation(player.getLocation());
database.updateQuitLoc(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
}
playerCache.removePlayer(name);
codeManager.unverify(name);
database.setUnlogged(name);
sessionService.revokeSession(name);
bungeeService.sendAuthMeBungeecordMessage(MessageType.LOGOUT, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.LOGOUT, name);
syncProcessManager.processSyncPlayerLogout(player);
}
}

View File

@ -10,7 +10,7 @@ import fr.xephi.authme.process.SyncProcessManager;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.SessionService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.SpawnLoader;
import fr.xephi.authme.settings.properties.PluginSettings;
@ -54,7 +54,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
private SessionService sessionService;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsynchronousQuit() {
}
@ -88,7 +88,7 @@ public class AsynchronousQuit implements AsynchronousProcess {
.lastLogin(System.currentTimeMillis())
.build();
database.updateSession(auth);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_QUITLOC, name);
}
//always unauthenticate the player - use session only for auto logins on the same ip

View File

@ -8,10 +8,10 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.process.register.executors.RegistrationExecutor;
import fr.xephi.authme.process.register.executors.RegistrationParameters;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.process.register.executors.RegistrationParameters;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
@ -39,7 +39,7 @@ public class AsyncRegister implements AsynchronousProcess {
@Inject
private SingletonStore<RegistrationExecutor> registrationExecutorFactory;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsyncRegister() {
}
@ -88,7 +88,7 @@ public class AsyncRegister implements AsynchronousProcess {
PlayerAuth auth = executor.buildPlayerAuth(parameters);
if (database.saveAuth(auth)) {
executor.executePostPersistAction(parameters);
bungeeService.sendAuthMeBungeecordMessage(MessageType.REGISTER, parameters.getPlayerName());
bungeeSender.sendAuthMeBungeecordMessage(MessageType.REGISTER, parameters.getPlayerName());
} else {
service.send(parameters.getPlayer(), MessageKey.ERROR);
}

View File

@ -4,8 +4,8 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
@ -19,7 +19,7 @@ import javax.inject.Inject;
public class ProcessSyncPasswordRegister implements SynchronousProcess {
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Inject
private CommonService service;
@ -72,6 +72,6 @@ public class ProcessSyncPasswordRegister implements SynchronousProcess {
}
// Send Bungee stuff. The service will check if it is enabled or not.
bungeeService.connectPlayerOnLogin(player);
bungeeSender.connectPlayerOnLogin(player);
}
}

View File

@ -11,9 +11,9 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.process.AsynchronousProcess;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
@ -54,7 +54,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
private CommandManager commandManager;
@Inject
private BungeeService bungeeService;
private BungeeSender bungeeSender;
AsynchronousUnregister() {
}
@ -115,7 +115,7 @@ public class AsynchronousUnregister implements AsynchronousProcess {
*/
private void performPostUnregisterActions(String name, Player player) {
playerCache.removePlayer(name);
bungeeService.sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name);
bungeeSender.sendAuthMeBungeecordMessage(MessageType.UNREGISTER, name);
if (player == null || !player.isOnline()) {
return;

View File

@ -0,0 +1,92 @@
package fr.xephi.authme.service.bungeecord;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.PluginMessageListener;
import javax.inject.Inject;
public class BungeeReceiver implements PluginMessageListener, SettingsDependent {
private final AuthMe plugin;
private final BukkitService bukkitService;
private final Management management;
private final DataSource dataSource;
private boolean isEnabled;
@Inject
BungeeReceiver(AuthMe plugin, BukkitService bukkitService, Management management, DataSource dataSource,
Settings settings) {
this.plugin = plugin;
this.bukkitService = bukkitService;
this.management = management;
this.dataSource = dataSource;
reload(settings);
}
@Override
public void reload(Settings settings) {
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
if (this.isEnabled) {
Messenger messenger = plugin.getServer().getMessenger();
if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) {
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this);
}
}
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] data) {
if (!isEnabled) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(data);
String subchannel = in.readUTF();
if (!"AuthMe".equals(subchannel)) {
return;
}
String type = in.readUTF();
String name = in.readUTF();
switch (type) {
case MessageType.UNREGISTER:
dataSource.invalidateCache(name);
break;
case MessageType.REFRESH_PASSWORD:
case MessageType.REFRESH_QUITLOC:
case MessageType.REFRESH_EMAIL:
case MessageType.REFRESH:
dataSource.refreshCache(name);
break;
case MessageType.BUNGEE_LOGIN:
handleBungeeLogin(name);
break;
default:
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
}
}
private void handleBungeeLogin(String name) {
Player player = bukkitService.getPlayerExact(name);
if (player != null && player.isOnline()) {
management.forceLogin(player);
ConsoleLogger.info("The user " + player.getName() + " has been automatically logged in, "
+ "as requested by the AuthMeBungee integration.");
}
}
}

View File

@ -0,0 +1,82 @@
package fr.xephi.authme.service.bungeecord;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.Messenger;
import javax.inject.Inject;
public class BungeeSender implements SettingsDependent {
private final AuthMe plugin;
private final BukkitService bukkitService;
private boolean isEnabled;
private String destinationServerOnLogin;
/*
* Constructor.
*/
@Inject
BungeeSender(AuthMe plugin, BukkitService bukkitService, Settings settings) {
this.plugin = plugin;
this.bukkitService = bukkitService;
reload(settings);
}
@Override
public void reload(Settings settings) {
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER);
if (this.isEnabled) {
Messenger messenger = plugin.getServer().getMessenger();
if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) {
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
}
}
}
public boolean isEnabled() {
return isEnabled;
}
private void sendBungeecordMessage(String... data) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
for (String element : data) {
out.writeUTF(element);
}
bukkitService.sendPluginMessage("BungeeCord", out.toByteArray());
}
/**
* Send a player to a specified server. If no server is configured, this will
* do nothing.
*
* @param player The player to send.
*/
public void connectPlayerOnLogin(Player player) {
if (isEnabled && !destinationServerOnLogin.isEmpty()) {
bukkitService.scheduleSyncDelayedTask(() ->
sendBungeecordMessage("Connect", player.getName(), destinationServerOnLogin), 20L);
}
}
/**
* Sends a message to the AuthMe plugin messaging channel, if enabled.
*
* @param type The message type, See {@link MessageType}
* @param playerName the player related to the message
*/
public void sendAuthMeBungeecordMessage(String type, String playerName) {
if (isEnabled) {
sendBungeecordMessage("AuthMe", type, playerName.toLowerCase());
}
}
}

View File

@ -1,121 +0,0 @@
package fr.xephi.authme.service.bungeecord;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.initialization.SettingsDependent;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.plugin.messaging.PluginMessageListener;
import javax.inject.Inject;
/**
* Class to manage all BungeeCord related processes.
*/
public class BungeeService implements SettingsDependent, PluginMessageListener {
private final AuthMe plugin;
private final BukkitService service;
private final DataSource dataSource;
private boolean isEnabled;
private String destinationServerOnLogin;
/*
* Constructor.
*/
@Inject
BungeeService(AuthMe plugin, BukkitService service, Settings settings, DataSource dataSource) {
this.plugin = plugin;
this.service = service;
this.dataSource = dataSource;
reload(settings);
}
@Override
public void reload(Settings settings) {
this.isEnabled = settings.getProperty(HooksSettings.BUNGEECORD);
this.destinationServerOnLogin = settings.getProperty(HooksSettings.BUNGEECORD_SERVER);
Messenger messenger = plugin.getServer().getMessenger();
if (!this.isEnabled) {
return;
}
if (!messenger.isOutgoingChannelRegistered(plugin, "BungeeCord")) {
messenger.registerOutgoingPluginChannel(plugin, "BungeeCord");
}
if (!messenger.isIncomingChannelRegistered(plugin, "BungeeCord")) {
messenger.registerIncomingPluginChannel(plugin, "BungeeCord", this);
}
}
private void sendBungeecordMessage(String... data) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
for (String element : data) {
out.writeUTF(element);
}
service.sendPluginMessage("BungeeCord", out.toByteArray());
}
/**
* Send a player to a specified server. If no server is configured, this will
* do nothing.
*
* @param player The player to send.
*/
public void connectPlayerOnLogin(Player player) {
if (isEnabled && !destinationServerOnLogin.isEmpty()) {
service.scheduleSyncDelayedTask(() ->
sendBungeecordMessage("Connect", player.getName(), destinationServerOnLogin), 20L);
}
}
/**
* Sends a message to the AuthMe plugin messaging channel, if enabled.
*
* @param type The message type, See {@link MessageType}
* @param playerName the player related to the message
*/
public void sendAuthMeBungeecordMessage(String type, String playerName) {
if (isEnabled) {
sendBungeecordMessage("AuthMe", type, playerName.toLowerCase());
}
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] data) {
if (!isEnabled) {
return;
}
ByteArrayDataInput in = ByteStreams.newDataInput(data);
String subchannel = in.readUTF();
if (!"AuthMe".equals(subchannel)) {
return;
}
String type = in.readUTF();
String name = in.readUTF();
switch (type) {
case MessageType.UNREGISTER:
dataSource.invalidateCache(name);
break;
case MessageType.REFRESH_PASSWORD:
case MessageType.REFRESH_QUITLOC:
case MessageType.REFRESH_EMAIL:
case MessageType.REFRESH:
dataSource.refreshCache(name);
break;
default:
ConsoleLogger.debug("Received unsupported bungeecord message type! ({0})", type);
}
}
}

View File

@ -11,6 +11,7 @@ public final class MessageType {
public static final String REFRESH_QUITLOC = "refresh.quitloc";
public static final String REFRESH_EMAIL = "refresh.email";
public static final String REFRESH = "refresh";
public static final String BUNGEE_LOGIN = "bungeelogin";
private MessageType() {
}

View File

@ -16,6 +16,8 @@ import fr.xephi.authme.process.Management;
import fr.xephi.authme.process.login.ProcessSyncPlayerLogin;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeReceiver;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.task.purge.PurgeService;
import org.bukkit.Bukkit;
@ -112,10 +114,12 @@ public class AuthMeInitializationTest {
// then
// Take a few samples and ensure that they are not null
assertThat(injector.getIfAvailable(AuthMeApi.class), not(nullValue()));
assertThat(injector.getIfAvailable(BlockListener.class), not(nullValue()));
assertThat(injector.getIfAvailable(BungeeReceiver.class), not(nullValue()));
assertThat(injector.getIfAvailable(BungeeSender.class), not(nullValue()));
assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue()));
assertThat(injector.getIfAvailable(Management.class), not(nullValue()));
assertThat(injector.getIfAvailable(AuthMeApi.class), not(nullValue()));
assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue()));
assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue()));
assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));

View File

@ -3,8 +3,8 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -37,7 +37,7 @@ public class PurgeLastPositionCommandTest {
private CommonService service;
@Mock
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Test
public void shouldPurgeLastPosOfUser() {

View File

@ -6,8 +6,8 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.command.CommandSender;
import org.junit.Before;
import org.junit.Test;
@ -39,7 +39,7 @@ public class AsyncChangePasswordTest {
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Before
public void setUpLogger() {

View File

@ -5,9 +5,9 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.entity.Player;
import org.junit.BeforeClass;
import org.junit.Test;
@ -47,7 +47,7 @@ public class AsyncAddEmailTest {
private ValidationService validationService;
@Mock
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@BeforeClass
public static void setUp() {

View File

@ -4,9 +4,9 @@ import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -46,7 +46,7 @@ public class AsyncChangeEmailTest {
private ValidationService validationService;
@Mock
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@Test
public void shouldChangeEmail() {

View File

@ -10,9 +10,9 @@ import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.bungeecord.BungeeService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import org.bukkit.command.CommandSender;
@ -63,7 +63,7 @@ public class AsynchronousUnregisterTest {
@Mock
private CommandManager commandManager;
@Mock
private BungeeService bungeeService;
private BungeeSender bungeeSender;
@BeforeClass
public static void initLogger() {