Added "Logged in from another location" protection.

This commit is contained in:
samo_lego 2020-04-15 19:21:15 +02:00
parent bd595b60de
commit 52f2f3c3f8
5 changed files with 80 additions and 30 deletions

View File

@ -2,12 +2,12 @@
org.gradle.jvmargs=-Xmx1G org.gradle.jvmargs=-Xmx1G
# Fabric properties # Fabric properties
minecraft_version=20w15a minecraft_version=20w16a
yarn_mappings=20w15a+build.1 yarn_mappings=20w16a+build.1
loader_version=0.8.2+build.194 loader_version=0.8.2+build.194
#Fabric api #Fabric api
fabric_version=0.5.9+build.319-1.16 fabric_version=0.5.10+build.320-1.16
# Mod Properties # Mod Properties
mod_version = 1.3.3 mod_version = 1.3.3

View File

@ -110,7 +110,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Player no longer needs to be invisible and invulnerable // Player no longer needs to be invisible and invulnerable
player.setInvulnerable(false); player.setInvulnerable(false);
player.setInvisible(false); player.setInvisible(false);
player.sendMessage(msg); player.sendMessage(msg, false);
} }
// De-authenticates player // De-authenticates player
@ -120,10 +120,10 @@ public class SimpleAuth implements DedicatedServerModInitializer {
SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, player.getIp())); SimpleAuth.deauthenticatedUsers.put(uuid, new PlayerCache(uuid, player.getIp()));
// Player is now not authenticated // Player is now not authenticated
player.sendMessage(notAuthenticated()); player.sendMessage(notAuthenticated(), false);
// Setting the player to be invisible to mobs and also invulnerable // Setting the player to be invisible to mobs and also invulnerable
player.setInvulnerable(SimpleAuth.config.main.playerInvulnerable); player.setInvulnerable(SimpleAuth.config.experimental.playerInvulnerable);
player.setInvisible(SimpleAuth.config.main.playerInvisible); player.setInvisible(SimpleAuth.config.experimental.playerInvisible);
Timer timer = new Timer(); Timer timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override

View File

@ -0,0 +1,43 @@
package org.samo_lego.simpleauth.mixin;
import com.mojang.authlib.GameProfile;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerLoginNetworkHandler;
import net.minecraft.text.LiteralText;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static org.samo_lego.simpleauth.SimpleAuth.config;
@Mixin(ServerLoginNetworkHandler.class)
public abstract class MixinServerLoginNetworkHandler {
@Shadow @Final
private MinecraftServer server;
@Shadow
private GameProfile profile;
@Inject(method = "acceptPlayer()V", at = @At("HEAD"), cancellable = true)
private void acceptPlayer(CallbackInfo ci) {
// Player pre-join event, we don't do standard callback, since
// there are lots of variables that would need to be passed over
PlayerEntity onlinePlayer = this.server.getPlayerManager().getPlayer(this.profile.getName());
// Getting network handler
ServerLoginNetworkHandler handler = (ServerLoginNetworkHandler) (Object) this;
if (config.experimental.disableAnotherLocationKick && onlinePlayer != null) {
// Player needs to be kicked, since there's already a player with that name
// playing on the server
handler.disconnect(new LiteralText(String.format(config.lang.playerAlreadyOnline, onlinePlayer.getName().asString())));
ci.cancel();
}
}
}

View File

@ -30,28 +30,8 @@ import java.io.IOException;
public class AuthConfig { public class AuthConfig {
// If player is not authenticated, following conditions apply // If player is not authenticated, following conditions apply
public static class MainConfig { public static class MainConfig {
// Allows chat (but not commands, except for /login and /register) // Allows "right-clicking" on an entity (e.g. clicking on villagers)
public boolean allowChat = false;
// Allows player movement
public boolean allowMovement = false;
// Allows block "use" - right clicking (e.g. opening a chest)
public boolean allowBlockUse = false;
// Allows mining || punching blocks
public boolean allowBlockPunch = false;
// Allows dropping items from inventory
public boolean allowItemDrop = false;
// Allows moving item through inventory
public boolean allowItemMoving = false;
// Allows item "use" - right click function (e.g. using a bow)
public boolean allowItemUse = false;
// Allows attacking mobs
public boolean allowEntityPunch = false;
// Allows "right-clicking" on an entity (e.g. trading with villagers)
public boolean allowEntityInteract = false; public boolean allowEntityInteract = false;
// If player should be invulnerable before authentication
public boolean playerInvulnerable = true;
// If player should be invisible to mobs before authentication
public boolean playerInvisible = true;
// Maximum login tries before kicking the player from server // Maximum login tries before kicking the player from server
// Set to -1 to allow unlimited, not recommended however // Set to -1 to allow unlimited, not recommended however
public int maxLoginTries = 1; public int maxLoginTries = 1;
@ -102,6 +82,31 @@ public class AuthConfig {
public String maxPasswordChars = "§6Password can be at most %d characters long!"; public String maxPasswordChars = "§6Password can be at most %d characters long!";
public String minPasswordChars = "§6Password needs to be at least %d characters long!"; public String minPasswordChars = "§6Password needs to be at least %d characters long!";
public String disallowedUsername = "§6Invalid username characters! Allowed character regex: %s"; public String disallowedUsername = "§6Invalid username characters! Allowed character regex: %s";
public String playerAlreadyOnline = "§cPlayer %s is already online!";
}
public static class ExperimentalConfig {
// Prevents player being kicked because another player with the same name has joined the server
public boolean disableAnotherLocationKick = true;
// If player should be invulnerable before authentication
public boolean playerInvulnerable = true;
// If player should be invisible to mobs before authentication
public boolean playerInvisible = true;
// Allows chat (but not commands, except for /login and /register)
public boolean allowChat = false;
// Allows player movement
public boolean allowMovement = false;
// Allows block "use" - right clicking (e.g. opening a chest)
public boolean allowBlockUse = false;
// Allows mining || punching blocks
public boolean allowBlockPunch = false;
// Allows dropping items from inventory
public boolean allowItemDrop = false;
// Allows moving item through inventory
public boolean allowItemMoving = false;
// Allows item "use" - right click function (e.g. using a bow)
public boolean allowItemUse = false;
// Allows attacking mobs
public boolean allowEntityPunch = false;
} }
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder() private static final Gson gson = new GsonBuilder()
@ -110,6 +115,7 @@ public class AuthConfig {
public MainConfig main = new MainConfig(); public MainConfig main = new MainConfig();
public LangConfig lang = new LangConfig(); public LangConfig lang = new LangConfig();
public ExperimentalConfig experimental = new ExperimentalConfig();
public static AuthConfig load(File file) { public static AuthConfig load(File file) {
AuthConfig config; AuthConfig config;

View File

@ -5,9 +5,10 @@
"mixins": [ "mixins": [
], ],
"server": [ "server": [
"MixinServerPlayNetworkHandler",
"MixinPlayerManager",
"MixinPlayerEntity", "MixinPlayerEntity",
"MixinPlayerManager",
"MixinServerLoginNetworkHandler",
"MixinServerPlayNetworkHandler",
"MixinSlot" "MixinSlot"
], ],
"injectors": { "injectors": {