goauth

Fabric plugin for enhanced whitelists
git clone git://git.bain.cz/goauth.git
Log | Files | Refs | README | LICENSE

commit 71e10f78010c9b6c0bcf46770464c78a16eefab4
parent d6a5ccfaf0a34585993885ab1e847a3060fadb11
Author: bain <bain@bain.cz>
Date:   Thu,  3 Feb 2022 22:18:08 +0100

refactor

Diffstat:
Mgradle.properties | 2+-
Asrc/main/java/cz/bain/plugins/goauth/AuthUpdate.java | 23+++++++++++++++++++++++
Msrc/main/java/cz/bain/plugins/goauth/AuthUpdateRunner.java | 22++++++++++++----------
Msrc/main/java/cz/bain/plugins/goauth/Goauth.java | 31+++++++++++++++----------------
Msrc/main/java/cz/bain/plugins/goauth/GoauthConfig.java | 15++++-----------
Dsrc/main/java/cz/bain/plugins/goauth/OAuthUpdate.java | 23-----------------------
Msrc/main/java/cz/bain/plugins/goauth/StringRepository.java | 7+++----
Msrc/main/java/cz/bain/plugins/goauth/events/listeners/OnPlayerConnectListener.java | 6+++---
Msrc/main/java/cz/bain/plugins/goauth/events/listeners/OnServerTickListener.java | 4++--
9 files changed, 63 insertions(+), 70 deletions(-)

diff --git a/gradle.properties b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.18 yarn_mappings=1.18+build.1 loader_version=0.12.6 # Mod Properties -mod_version=1.0 +mod_version=1.0.1 maven_group=cz.bain.plugins archives_base_name=goauth # Dependencies diff --git a/src/main/java/cz/bain/plugins/goauth/AuthUpdate.java b/src/main/java/cz/bain/plugins/goauth/AuthUpdate.java @@ -0,0 +1,23 @@ +package cz.bain.plugins.goauth; + +import com.google.gson.annotations.SerializedName; + +public class AuthUpdate { + public enum UpdateType { + @SerializedName("update") + UPDATE, + @SerializedName("remove") + REMOVE + } + + UpdateType type; + String username; + + public UpdateType getType() { + return type; + } + + public String getUsername() { + return username; + } +} diff --git a/src/main/java/cz/bain/plugins/goauth/AuthUpdateRunner.java b/src/main/java/cz/bain/plugins/goauth/AuthUpdateRunner.java @@ -19,10 +19,12 @@ public class AuthUpdateRunner implements Runnable { private final SecretKeySpec key; private final Goauth plugin; + private final int port; - public AuthUpdateRunner(byte[] key, Goauth plugin) { + public AuthUpdateRunner(byte[] key, Goauth plugin, int port) { this.key = new SecretKeySpec(key, "ChaCha20"); this.plugin = plugin; + this.port = port; } @Override @@ -30,11 +32,11 @@ public class AuthUpdateRunner implements Runnable { ServerSocket serverSocket; try { serverSocket = new ServerSocket(); - serverSocket.bind(new InetSocketAddress(Goauth.port), 2); + serverSocket.bind(new InetSocketAddress(port), 2); serverSocket.setSoTimeout(5); } catch (IOException e) { - Goauth.LOGGER.error("Failed to create socket for updates!"); + plugin.logger.error("Failed to create socket for updates!"); return; } while (this.plugin.running) { @@ -50,7 +52,7 @@ public class AuthUpdateRunner implements Runnable { if (size > 2048 || size <= 0) { outputStream.write(0x01); outputStream.flush(); - Goauth.LOGGER.warn("Whitelist update rejected: payload size too big ({})", size); + plugin.logger.warn("Whitelist update rejected: payload size too big ({})", size); continue; } else { outputStream.write(0x00); @@ -64,21 +66,21 @@ public class AuthUpdateRunner implements Runnable { try { plainText = decryptMessage(cipherText, iv); } catch (GeneralSecurityException e) { - Goauth.LOGGER.warn("Whitelist update rejected: failed to decrypt payload"); + plugin.logger.warn("Whitelist update rejected: failed to decrypt payload"); outputStream.write(0x01); outputStream.flush(); continue; } JsonArray el = JsonParser.parseString(new String(plainText, StandardCharsets.UTF_8)).getAsJsonArray(); - Goauth.LOGGER.info("Got " + el.size() + " new whitelist updates"); + plugin.logger.info("Got " + el.size() + " new whitelist updates"); Gson gson = new Gson(); synchronized (this.plugin.completed) { for (JsonElement user : el) { - OAuthUpdate update; + AuthUpdate update; try { - update = gson.fromJson(user, OAuthUpdate.class); + update = gson.fromJson(user, AuthUpdate.class); } catch (JsonSyntaxException ignored) { - Goauth.LOGGER.warn("Whitelist update rejected: bad update data"); + plugin.logger.warn("Whitelist update rejected: bad update data"); outputStream.write(0x01); outputStream.flush(); break; @@ -89,7 +91,7 @@ public class AuthUpdateRunner implements Runnable { } } catch (SocketTimeoutException ignored) { } catch (IOException | IllegalStateException e) { - Goauth.LOGGER.warn("Error while processing OAuth updates, dropping connection"); + plugin.logger.warn("Error while processing OAuth updates, dropping connection"); e.printStackTrace(); } } diff --git a/src/main/java/cz/bain/plugins/goauth/Goauth.java b/src/main/java/cz/bain/plugins/goauth/Goauth.java @@ -15,18 +15,17 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Objects; public class Goauth implements ModInitializer { - public static final Logger LOGGER = LogManager.getLogger("goauth"); - public final List<OAuthUpdate> completed = new ArrayList<>(); + public final Logger logger = LogManager.getLogger("goauth"); + public final List<AuthUpdate> completed = new ArrayList<>(); public final HashMap<String, ServerPlayerEntity> playersLimbo = new HashMap<>(); - public static int port = 8000; public String authLink; public boolean running = true; public AuthStore authStore; + public String authLinkText; @Override public void onInitialize() { @@ -38,33 +37,33 @@ public class Goauth implements ModInitializer { try { config = new GoauthConfig(new File("goauth.properties")); } catch (IOException e) { - LOGGER.error(e.getMessage()); + this.logger.error(e.getMessage()); return; } - port = Integer.parseInt(config.getRequired("port")); + int port = Integer.parseInt(config.getRequired("port")); byte[] key; try { key = hexStringToByteArray(config.getRequired("key")); } catch (IllegalStateException e) { - LOGGER.error("Failed to parse communication key! Needs to be in hex."); + this.logger.error("Failed to parse communication key! Needs to be in hex."); return; } if (key.length != 32) { - LOGGER.error("Communication key is not 64 bytes long!"); + this.logger.error("Communication key is not 32 bytes long!"); return; } - StringRepository.authLinkText = config.getRequired("auth-link-text"); - authLink = Objects.requireNonNullElse(config.get("auth-link"), ""); + this.authLinkText = config.getRequired("auth-link-text"); + this.authLink = config.get("auth-link", ""); - authStore = new AuthStore( - LOGGER, - new File(Objects.requireNonNullElse(config.get("db-location"), "authdb.json")) + this.authStore = new AuthStore( + this.logger, + new File(config.get("db-location", "authdb.json")) ); - authStore.load(); + this.authStore.load(); - new Thread(new AuthUpdateRunner(key, this)).start(); - LOGGER.info("Initialized!"); + new Thread(new AuthUpdateRunner(key, this, port)).start(); + this.logger.info("Initialized!"); } public static byte[] hexStringToByteArray(String s) throws IllegalStateException { diff --git a/src/main/java/cz/bain/plugins/goauth/GoauthConfig.java b/src/main/java/cz/bain/plugins/goauth/GoauthConfig.java @@ -1,10 +1,6 @@ package cz.bain.plugins.goauth; -import org.jetbrains.annotations.Nullable; -import org.lwjgl.system.CallbackI; - import java.io.*; -import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -23,12 +19,9 @@ public class GoauthConfig { } BufferedReader reader = new BufferedReader(new FileReader(file)); + List<String> required = Arrays.asList("port", "key", "auth-link-text"); String line; int line_no = 0; - List<String> required = new ArrayList<>(); - required.add("port"); - required.add("key"); - required.add("auth-link-text"); while ((line = reader.readLine()) != null) { line_no++; if (line.startsWith("#") || line.length() == 0) continue; @@ -41,14 +34,14 @@ public class GoauthConfig { options.put(parsed[0], parsed[1]); } reader.close(); + if (!required.isEmpty()) { throw new IOException("Missing configuration options: " + Arrays.toString(required.toArray())); } } - @Nullable - public String get(String key) { - return options.getOrDefault(key, null); + public String get(String key, String default_) { + return options.getOrDefault(key, default_); } public String getRequired(String key) { diff --git a/src/main/java/cz/bain/plugins/goauth/OAuthUpdate.java b/src/main/java/cz/bain/plugins/goauth/OAuthUpdate.java @@ -1,23 +0,0 @@ -package cz.bain.plugins.goauth; - -import com.google.gson.annotations.SerializedName; - -public class OAuthUpdate { - public enum UpdateType { - @SerializedName("update") - UPDATE, - @SerializedName("remove") - REMOVE - } - - UpdateType type; - String username; - - public UpdateType getType() { - return type; - } - - public String getUsername() { - return username; - } -} diff --git a/src/main/java/cz/bain/plugins/goauth/StringRepository.java b/src/main/java/cz/bain/plugins/goauth/StringRepository.java @@ -1,8 +1,7 @@ package cz.bain.plugins.goauth; public class StringRepository { - public static String authLinkText; - public static String limboRemoveDisconnect = "Successfully authenticated, please reconnect"; - public static String limboExpiredDisconnect = "Authentication period expired"; - public static String playerUnregisterKick = "You have been unregistered"; + public static final String limboRemoveDisconnect = "Successfully authenticated, please reconnect"; + public static final String limboExpiredDisconnect = "Authentication period expired"; + public static final String playerUnregisterKick = "You have been unregistered"; } diff --git a/src/main/java/cz/bain/plugins/goauth/events/listeners/OnPlayerConnectListener.java b/src/main/java/cz/bain/plugins/goauth/events/listeners/OnPlayerConnectListener.java @@ -45,10 +45,10 @@ public class OnPlayerConnectListener implements OnPlayerConnectCallback { // the player is in the whitelist, let the server do its things if (this.plugin.authStore.allowed(player, connection)) return ActionResult.PASS; - Goauth.LOGGER.info(player.getGameProfile().getName() + " is connecting from a new ip - putting him into limbo"); + this.plugin.logger.info(player.getGameProfile().getName() + " is connecting from a new ip - putting him into limbo"); // fake initial connection info, use DummyServerPlayNetworkHandler to override all events and ignore them - ServerPlayNetworkHandler serverPlayNetworkHandler = new DummyServerPlayNetworkHandler(server, connection, player, plugin); + ServerPlayNetworkHandler serverPlayNetworkHandler = new DummyServerPlayNetworkHandler(server, connection, player, this.plugin); serverPlayNetworkHandler.sendPacket(new GameJoinS2CPacket( player.getId(), false, GameMode.SPECTATOR, null, server.getWorldRegistryKeys(), ((PlayerManagerAccessor) server.getPlayerManager()).getRegistryManager(), @@ -66,7 +66,7 @@ public class OnPlayerConnectListener implements OnPlayerConnectCallback { LiteralText welcomeMsg = new LiteralText("Welcome! Please authenticate: "); welcomeMsg.setStyle(Style.EMPTY.withBold(true).withColor(TextColor.parse("yellow"))); - LiteralText text = new LiteralText(StringRepository.authLinkText); + LiteralText text = new LiteralText(this.plugin.authLinkText); Style urlStyle = Style.EMPTY .withBold(false) .withColor(TextColor.parse("white")) diff --git a/src/main/java/cz/bain/plugins/goauth/events/listeners/OnServerTickListener.java b/src/main/java/cz/bain/plugins/goauth/events/listeners/OnServerTickListener.java @@ -1,7 +1,7 @@ package cz.bain.plugins.goauth.events.listeners; import cz.bain.plugins.goauth.Goauth; -import cz.bain.plugins.goauth.OAuthUpdate; +import cz.bain.plugins.goauth.AuthUpdate; import cz.bain.plugins.goauth.StringRepository; import cz.bain.plugins.goauth.events.callbacks.OnServerTickCallback; import net.minecraft.server.PlayerManager; @@ -22,7 +22,7 @@ public class OnServerTickListener implements OnServerTickCallback { public ActionResult interact(PlayerManager playerManager, long ticks) { if (ticks % 20 == 0) { synchronized (this.plugin.completed) { - for (OAuthUpdate update : this.plugin.completed) { + for (AuthUpdate update : this.plugin.completed) { switch (update.getType()) { case UPDATE -> { ServerPlayerEntity player = this.plugin.playersLimbo.get(update.getUsername());