diff --git a/src/config.yml b/src/config.yml new file mode 100644 index 0000000..06da249 --- /dev/null +++ b/src/config.yml @@ -0,0 +1,16 @@ +# Config for SteamWar TeamServer-Integration. +# Every setting is locally and will not be sent to SteamWar. + +# Set this to 'false' to disable the TeamServer-Integration completely. +plugin-enabled: true + +# Set this to 'true' to force the joining coming from SteamWar. +force-only-steamwar-join: false + +# Set this to 'true' to enable the TeamServer-Integration whitelist. +# This will not affect normal connections, only those coming from SteamWar. +whitelist-enabled: false + +# You can either whitelist a UUID(recommended) or a UserName. +# UUID's need to be in the format of 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' +whitelisted-players: [] diff --git a/src/de/steamwar/ConfigSystem.java b/src/de/steamwar/ConfigSystem.java new file mode 100644 index 0000000..b24df69 --- /dev/null +++ b/src/de/steamwar/ConfigSystem.java @@ -0,0 +1,95 @@ +package de.steamwar; + +import lombok.experimental.UtilityClass; +import net.md_5.bungee.config.Configuration; +import net.md_5.bungee.config.ConfigurationProvider; +import net.md_5.bungee.config.YamlConfiguration; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashSet; +import java.util.Set; + +@UtilityClass +public class ConfigSystem { + + private File configFile; + + private boolean enabled = false; + private long lastModified = 0; + + private Configuration config; + + private Set whitelist = null; + + static { + configFile = new File(SteamWarBungeeTeamserver.plugin.getDataFolder(), "config.yml"); + if (!configFile.exists()) { + try { + configFile.createNewFile(); + FileOutputStream fileOutputStream = new FileOutputStream(configFile); + InputStream inputStream = SteamWarBungeeTeamserver.plugin.getResourceAsStream("config.yml"); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, length); + } + fileOutputStream.close(); + inputStream.close(); + enabled = true; + } catch (Exception e) { + + } + } + } + + private synchronized void load() { + if (!enabled) return; + if (lastModified >= configFile.lastModified()) return; + try { + config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); + whitelist = null; + lastModified = configFile.lastModified(); + } catch (IOException e) { + // Ignored + } + } + + public boolean isEnabled() { + load(); + if (config != null) { + return config.getBoolean("plugin-enabled", false) && enabled; + } + return enabled; + } + + public boolean isOnlySteamWarJoin() { + load(); + if (config != null) { + return config.getBoolean("force-only-steamwar-join", false); + } + return false; + } + + public boolean isWhitelistEnabled() { + load(); + if (config != null) { + return config.getBoolean("whitelist-enabled", false); + } + return false; + } + + public Set getWhitelist() { + load(); + if (whitelist == null) { + if (config != null) { + whitelist = new HashSet<>(config.getStringList("whitelisted-players")); + } else { + whitelist = new HashSet<>(); + } + } + return whitelist; + } +} diff --git a/src/de/steamwar/SteamWarBungeeTeamserver.java b/src/de/steamwar/SteamWarBungeeTeamserver.java index 5582053..07480cd 100644 --- a/src/de/steamwar/SteamWarBungeeTeamserver.java +++ b/src/de/steamwar/SteamWarBungeeTeamserver.java @@ -5,12 +5,16 @@ import net.md_5.bungee.api.plugin.Plugin; public final class SteamWarBungeeTeamserver extends Plugin { + public static SteamWarBungeeTeamserver plugin; + @Override public void onEnable() { + plugin = this; getProxy().getPluginManager().registerListener(this, new SteamwarConnectionListener()); } @Override public void onDisable() { + plugin = null; } } diff --git a/src/de/steamwar/listener/SteamwarConnectionListener.java b/src/de/steamwar/listener/SteamwarConnectionListener.java index 1cb0ecf..ff73666 100644 --- a/src/de/steamwar/listener/SteamwarConnectionListener.java +++ b/src/de/steamwar/listener/SteamwarConnectionListener.java @@ -1,5 +1,6 @@ package de.steamwar.listener; +import de.steamwar.ConfigSystem; import io.netty.buffer.ByteBuf; import net.md_5.bungee.api.event.PreLoginEvent; import net.md_5.bungee.api.plugin.Listener; @@ -15,6 +16,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.util.Arrays; +import java.util.Set; import java.util.UUID; import java.util.function.Supplier; @@ -68,12 +70,32 @@ public class SteamwarConnectionListener implements Listener { @EventHandler public void preLoginEvent(PreLoginEvent preLoginEvent) { + if (!ConfigSystem.isEnabled()) return; + String address = preLoginEvent.getConnection().getSocketAddress().toString(); if (address.startsWith("/78.31.71.136:")) { InitialHandler initialHandler = (InitialHandler) preLoginEvent.getConnection(); String extraData = initialHandler.getExtraDataInHandshake(); String[] split = extraData.split("\0"); + StringBuilder uuidStringBuilder = new StringBuilder(split[2]); + uuidStringBuilder.insert(8, '-'); + uuidStringBuilder.insert(13, '-'); + uuidStringBuilder.insert(18, '-'); + uuidStringBuilder.insert(23, '-'); + + if (ConfigSystem.isWhitelistEnabled()) { + Set whitelist = ConfigSystem.getWhitelist(); + if (!whitelist.contains(uuidStringBuilder.toString())) { + preLoginEvent.setCancelled(true); + return; + } + if (!whitelist.contains(preLoginEvent.getConnection().getName())) { + preLoginEvent.setCancelled(true); + return; + } + } + try { ChannelWrapper channelWrapper = ((ChannelWrapper) chField.get(initialHandler)); channelWrapper.setRemoteAddress(new InetSocketAddress(split[1], ((InetSocketAddress) channelWrapper.getRemoteAddress()).getPort())); @@ -85,12 +107,9 @@ public class SteamwarConnectionListener implements Listener { // 0: host, 1: address, 2: uuid, (3): mojangProfile (properties, so skin data) preLoginEvent.getConnection().setOnlineMode(false); - StringBuilder st = new StringBuilder(split[2]); - st.insert(8, '-'); - st.insert(13, '-'); - st.insert(18, '-'); - st.insert(23, '-'); - initialHandler.setUniqueId(UUID.fromString(st.toString())); + initialHandler.setUniqueId(UUID.fromString(uuidStringBuilder.toString())); + } else if (ConfigSystem.isOnlySteamWarJoin()) { + preLoginEvent.setCancelled(true); } } }