From d9c323bc25179e569e9ca3e0415c4bfef6a0ebd5 Mon Sep 17 00:00:00 2001 From: ham1255 Date: Sun, 28 Jun 2020 18:40:53 +0400 Subject: [PATCH] not tested But rewritten the Plugin --- .../java/net/limework/Data/Encryption.java | 37 +++++ .../net/limework/core/LimeworkSpigotCore.java | 75 ++++++++++ .../Managers/RedisManager.java} | 87 +++++++++--- .../Skript}/elements/EffSendMessage.java | 0 .../Skript}/elements/EvtRedis.java | 9 +- .../Skript}/elements/ExprChannel.java | 8 +- .../Skript}/elements/ExprMessage.java | 0 .../events/RedisMessageEvent.java} | 8 +- .../net/limework/skLimework/AddonPlugin.java | 131 ------------------ src/main/resources/config.yml | 2 +- 10 files changed, 192 insertions(+), 165 deletions(-) create mode 100644 src/main/java/net/limework/Data/Encryption.java create mode 100644 src/main/java/net/limework/core/LimeworkSpigotCore.java rename src/main/java/net/limework/{skLimework/Events/RedisSub.java => core/Managers/RedisManager.java} (58%) rename src/main/java/net/limework/{skLimework => core/Skript}/elements/EffSendMessage.java (100%) rename src/main/java/net/limework/{skLimework => core/Skript}/elements/EvtRedis.java (67%) rename src/main/java/net/limework/{skLimework => core/Skript}/elements/ExprChannel.java (78%) rename src/main/java/net/limework/{skLimework => core/Skript}/elements/ExprMessage.java (100%) rename src/main/java/net/limework/{skLimework/Events/onRedisMessage.java => core/events/RedisMessageEvent.java} (83%) delete mode 100644 src/main/java/net/limework/skLimework/AddonPlugin.java diff --git a/src/main/java/net/limework/Data/Encryption.java b/src/main/java/net/limework/Data/Encryption.java new file mode 100644 index 0000000..1e4ade8 --- /dev/null +++ b/src/main/java/net/limework/Data/Encryption.java @@ -0,0 +1,37 @@ +package net.limework.Data; + +import org.bukkit.configuration.Configuration; +import org.cryptomator.siv.SivMode; +import org.cryptomator.siv.UnauthenticCiphertextException; + +import javax.crypto.IllegalBlockSizeException; +import java.nio.charset.StandardCharsets; + +public class Encryption { + + private boolean encryptionEnabled; + private String encryptionKey; + private String macKey; + private final SivMode AES_SIV = new SivMode(); + + public Encryption(Configuration config){ + encryptionEnabled = config.getBoolean("Redis.EncryptMessages"); + if (encryptionEnabled) { + // AES-128 encryption + encryptionKey = config.getString("Redis.EncryptionKey"); + macKey = config.getString("Redis.MacKey"); + } + } + + public boolean isEncryptionEnabled() { return encryptionEnabled; } + + public String decrypt(byte[] message) throws UnauthenticCiphertextException, IllegalBlockSizeException { + return new String(AES_SIV.decrypt(encryptionKey.getBytes(), macKey.getBytes(), message), StandardCharsets.UTF_8); + } + + public byte[] encrypt(String message) { + return AES_SIV.encrypt(encryptionKey.getBytes(), macKey.getBytes(), message.getBytes()); + } + + +} diff --git a/src/main/java/net/limework/core/LimeworkSpigotCore.java b/src/main/java/net/limework/core/LimeworkSpigotCore.java new file mode 100644 index 0000000..fa496e2 --- /dev/null +++ b/src/main/java/net/limework/core/LimeworkSpigotCore.java @@ -0,0 +1,75 @@ +package net.limework.core; + +import ch.njol.skript.Skript; +import ch.njol.skript.SkriptAddon; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.registrations.EventValues; +import ch.njol.skript.util.Getter; +import net.limework.core.Managers.RedisManager; +import net.limework.core.Skript.elements.EvtRedis; +import net.limework.core.Skript.elements.ExprChannel; +import net.limework.core.events.RedisMessageEvent; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.IOException; + +public class LimeworkSpigotCore extends JavaPlugin { + + //Redis manager + private RedisManager rm; + + //Skript + private SkriptAddon addon; + + + @Override + public void onEnable() { + saveDefaultConfig(); + rm = new RedisManager(this); + + + loadSkript(); + rm.start(); + } + + @Override + public void onDisable() { + + } + + private void loadSkript() { + addon = Skript.registerAddon(this); + try { + addon.loadClasses("net.limework.core.Skript", "elements"); + Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); + Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); + EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { + @Override + public String get(RedisMessageEvent e) { + return e.getChannelName(); + } + }, 0); + Skript.registerExpression(net.limework.skLimework.elements.ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message"); + EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter() { + @Override + public String get(RedisMessageEvent e) { + return e.getMessage(); + } + }, 0); + + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public RedisManager getRm() { + return rm; + } + + public SkriptAddon getAddon() { + return addon; + } +} diff --git a/src/main/java/net/limework/skLimework/Events/RedisSub.java b/src/main/java/net/limework/core/Managers/RedisManager.java similarity index 58% rename from src/main/java/net/limework/skLimework/Events/RedisSub.java rename to src/main/java/net/limework/core/Managers/RedisManager.java index 92898d0..e856777 100644 --- a/src/main/java/net/limework/skLimework/Events/RedisSub.java +++ b/src/main/java/net/limework/core/Managers/RedisManager.java @@ -1,31 +1,64 @@ -package net.limework.skLimework.Events; +package net.limework.core.Managers; -import net.limework.skLimework.AddonPlugin; +import net.limework.Data.Encryption; +import net.limework.core.LimeworkSpigotCore; +import net.limework.core.events.RedisMessageEvent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.configuration.Configuration; import org.cryptomator.siv.UnauthenticCiphertextException; import org.json.JSONObject; import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedisPubSub; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; import javax.crypto.IllegalBlockSizeException; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; +public class RedisManager extends BinaryJedisPubSub implements Runnable{ -public class RedisSub extends BinaryJedisPubSub implements Runnable { + private LimeworkSpigotCore plugin; - private AddonPlugin plugin; - private BinaryJedis j; + private JedisPool jedisPool; + private ExecutorService RedisService; + + + + //sub + private BinaryJedis subscribeJedis; private List channels; private AtomicBoolean isShuttingDown = new AtomicBoolean(false); private AtomicBoolean isRedisOnline = new AtomicBoolean(); + private Encryption encryption; - public RedisSub(AddonPlugin plugin, BinaryJedis j, List channels) { + public RedisManager(LimeworkSpigotCore plugin) { this.plugin = plugin; - this.j = j; - this.channels = channels; + Configuration config = this.plugin.getConfig(); + JedisPoolConfig JConfig = new JedisPoolConfig(); + JConfig.setMaxTotal(config.getInt("Redis.MaxConnections")); + JConfig.setMaxIdle(config.getInt("Redis.MaxConnections")); + JConfig.setMinIdle(1); + JConfig.setBlockWhenExhausted(true); + this.jedisPool = new JedisPool(JConfig, + config.getString("Redis.Host"), + config.getInt("Redis.Port"), + config.getInt("Redis.TimeOut"), + config.getString("Redis.Password"), + config.getBoolean("Redis.useSSL")); + RedisService = Executors.newFixedThreadPool(config.getInt("Redis.Threads")); + this.subscribeJedis = this.jedisPool.getResource(); + this.channels = config.getStringList("Channels"); + encryption = new Encryption(config); + + } + + public void start(){ + this.RedisService.execute(this); } @Override @@ -33,7 +66,7 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable { while (!isShuttingDown.get()) { try { message("&e[Jedis] &cConnecting to redis..........."); - if (!this.j.isConnected()) this.j = plugin.getJedisPool().getResource(); + if (!this.subscribeJedis.isConnected()) this.subscribeJedis = this.jedisPool.getResource(); isRedisOnline.set(true); message("&e[Jedis] &aRedis Connected"); int byteArr2dSize = 1; @@ -55,11 +88,11 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable { channelsInByte = new byte[channels.size()][byteArr2dSize]; } } while (reInitializeByteArray); - this.j.subscribe(this, channelsInByte); - + this.subscribeJedis.subscribe(this, channelsInByte); + } catch (Exception e) { message("&e[Jedis] &cConnection to redis has failed! &ereconnecting..."); - this.j.close(); + this.subscribeJedis.close(); isRedisOnline.set(false); } try { @@ -76,20 +109,18 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable { @Override public void onMessage(byte[] channel, byte[] message) { - AddonPlugin plugin = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework"); String channelString = new String(channel); try { String decrypted = null; try { - assert plugin != null; - decrypted = plugin.decrypt(message); + decrypted = encryption.decrypt(message); } catch (UnauthenticCiphertextException | IllegalBlockSizeException e) { e.printStackTrace(); } assert decrypted != null; JSONObject j = new JSONObject(decrypted); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); - plugin.getServer().getPluginManager().callEvent(new onRedisMessage(channelString, j.getString("Message"))); + plugin.getServer().getPluginManager().callEvent(new RedisMessageEvent(channelString, j.getString("Message"))); } catch (Exception e) { e.printStackTrace(); Bukkit.getLogger().warning(ChatColor.translateAlternateColorCodes('&', "&2[&aGBot&a] &cI Got a Message that Was empty from channel " + channel + " Please check your code that you used to send the message. ^ ignore the error.")); @@ -100,11 +131,31 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable { public void shutdown() { this.isShuttingDown.set(true); this.unsubscribe(); - j.close(); + this.RedisService.shutdown(); + subscribeJedis.close(); } public boolean IsRedisOnline() { return isRedisOnline.get(); } -} \ No newline at end of file + public JedisPool getJedisPool() { + return jedisPool; + } + + public ExecutorService getRedisService() { + return RedisService; + } + + public AtomicBoolean getIsShuttingDown() { + return isShuttingDown; + } + + public AtomicBoolean getIsRedisOnline() { + return isRedisOnline; + } + + public Encryption getEncryption() { + return encryption; + } +} diff --git a/src/main/java/net/limework/skLimework/elements/EffSendMessage.java b/src/main/java/net/limework/core/Skript/elements/EffSendMessage.java similarity index 100% rename from src/main/java/net/limework/skLimework/elements/EffSendMessage.java rename to src/main/java/net/limework/core/Skript/elements/EffSendMessage.java diff --git a/src/main/java/net/limework/skLimework/elements/EvtRedis.java b/src/main/java/net/limework/core/Skript/elements/EvtRedis.java similarity index 67% rename from src/main/java/net/limework/skLimework/elements/EvtRedis.java rename to src/main/java/net/limework/core/Skript/elements/EvtRedis.java index a08e6a1..4c0138e 100644 --- a/src/main/java/net/limework/skLimework/elements/EvtRedis.java +++ b/src/main/java/net/limework/core/Skript/elements/EvtRedis.java @@ -1,13 +1,8 @@ -package net.limework.skLimework.elements; - -import ch.njol.skript.Skript; -import ch.njol.skript.lang.ExpressionType; +package net.limework.core.Skript.elements; +; import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.SkriptEvent; import ch.njol.skript.lang.SkriptParser; -import ch.njol.skript.registrations.EventValues; -import ch.njol.skript.util.Getter; -import net.limework.skLimework.Events.onRedisMessage; import org.bukkit.event.Event; public class EvtRedis extends SkriptEvent { diff --git a/src/main/java/net/limework/skLimework/elements/ExprChannel.java b/src/main/java/net/limework/core/Skript/elements/ExprChannel.java similarity index 78% rename from src/main/java/net/limework/skLimework/elements/ExprChannel.java rename to src/main/java/net/limework/core/Skript/elements/ExprChannel.java index 6133d1a..36e54ac 100644 --- a/src/main/java/net/limework/skLimework/elements/ExprChannel.java +++ b/src/main/java/net/limework/core/Skript/elements/ExprChannel.java @@ -1,11 +1,11 @@ -package net.limework.skLimework.elements; +package net.limework.core.Skript.elements; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Kleenean; -import net.limework.skLimework.Events.onRedisMessage; +import net.limework.core.events.RedisMessageEvent; import org.bukkit.event.Event; public class ExprChannel extends SimpleExpression { @@ -34,8 +34,8 @@ public class ExprChannel extends SimpleExpression { @Override protected String[] get(Event e) { - if (e instanceof onRedisMessage){ - return new String[]{((onRedisMessage) e).getChannelName()}; + if (e instanceof RedisMessageEvent){ + return new String[]{((RedisMessageEvent) e).getChannelName()}; } return null; } diff --git a/src/main/java/net/limework/skLimework/elements/ExprMessage.java b/src/main/java/net/limework/core/Skript/elements/ExprMessage.java similarity index 100% rename from src/main/java/net/limework/skLimework/elements/ExprMessage.java rename to src/main/java/net/limework/core/Skript/elements/ExprMessage.java diff --git a/src/main/java/net/limework/skLimework/Events/onRedisMessage.java b/src/main/java/net/limework/core/events/RedisMessageEvent.java similarity index 83% rename from src/main/java/net/limework/skLimework/Events/onRedisMessage.java rename to src/main/java/net/limework/core/events/RedisMessageEvent.java index 285a75e..33d2d4e 100644 --- a/src/main/java/net/limework/skLimework/Events/onRedisMessage.java +++ b/src/main/java/net/limework/core/events/RedisMessageEvent.java @@ -1,18 +1,18 @@ -package net.limework.skLimework.Events; +package net.limework.core.events; import net.limework.skLimework.AddonPlugin; import org.bukkit.Bukkit; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; - -public class onRedisMessage extends Event { +@SuppressWarnings("unused") +public class RedisMessageEvent extends Event { private final static HandlerList HANDLERS = new HandlerList(); private String channelName; private String message; - onRedisMessage(String channelName , String message) { + public RedisMessageEvent(String channelName , String message) { super(true); this.channelName = channelName; AddonPlugin instance = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework"); diff --git a/src/main/java/net/limework/skLimework/AddonPlugin.java b/src/main/java/net/limework/skLimework/AddonPlugin.java deleted file mode 100644 index 43659db..0000000 --- a/src/main/java/net/limework/skLimework/AddonPlugin.java +++ /dev/null @@ -1,131 +0,0 @@ -package net.limework.skLimework; - -import ch.njol.skript.Skript; -import ch.njol.skript.SkriptAddon; -import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.registrations.EventValues; -import ch.njol.skript.util.Getter; -import net.limework.skLimework.Events.RedisSub; -import net.limework.skLimework.Events.onRedisMessage; -import net.limework.skLimework.elements.EvtRedis; -import net.limework.skLimework.elements.ExprChannel; -import net.limework.skLimework.elements.ExprMessage; -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.plugin.java.JavaPlugin; -import org.cryptomator.siv.SivMode; -import org.cryptomator.siv.UnauthenticCiphertextException; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; - -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.spec.SecretKeySpec; -import java.beans.Expression; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; -import java.security.InvalidKeyException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; -import java.util.Base64; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -public class AddonPlugin extends JavaPlugin { - private AddonPlugin instance; - private SkriptAddon addon; - private JedisPool jedisPool; - private RedisSub redisSub; - private ExecutorService service; - private boolean encryptionEnabled; - private String encryptionKey; - private String macKey; - private final SivMode AES_SIV = new SivMode(); - - @Override - public void onEnable(){ - instance = this; - this.saveDefaultConfig(); - FileConfiguration config = this.getConfig(); - encryptionEnabled = config.getBoolean("Redis.EncryptMessages"); - if (encryptionEnabled) { - // AES-128 encryption - encryptionKey = config.getString("Redis.EncryptionKey"); - macKey = config.getString("Redis.MacKey"); - } - - addon = Skript.registerAddon(this); - try { addon.loadClasses("net.limework.skLimework", "elements"); - Skript.registerEvent("redis message", EvtRedis.class, onRedisMessage.class, "redis message"); - Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); - EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter() { - @Override - public String get(onRedisMessage e) { - return e.getChannelName(); - } - }, 0); - Skript.registerExpression(ExprMessage.class, String.class, ExpressionType.SIMPLE, "redis message"); - EventValues.registerEventValue(onRedisMessage.class, String.class, new Getter() { - @Override - public String get(onRedisMessage e) { - return e.getMessage(); - } - }, 0); - - - - } catch (IOException e) { e.printStackTrace(); } - JedisPoolConfig jconfig = new JedisPoolConfig(); - jconfig.setMaxTotal(this.getConfig().getInt("Redis.MaxConnections")); - jconfig.setMaxIdle(this.getConfig().getInt("Redis.MaxConnections")); - jconfig.setMinIdle(1); - jedisPool = new JedisPool(jconfig, - this.getConfig().getString("Redis.Host"), - this.getConfig().getInt("Redis.Port") , - this.getConfig().getInt("Redis.TimeOut"), - this.getConfig().getString("Redis.Password"), - this.getConfig().getBoolean("Redis.useSSL")); - redisSub = new RedisSub(this, jedisPool.getResource(), this.getConfig().getStringList("Channels")); - service = Executors.newFixedThreadPool(this.getConfig().getInt("Redis.Threads")); - service.execute(redisSub); - - Bukkit.getLogger().info("[Govindas limework Addon] was enabled!"); - } - - - @Override - public void onDisable(){ - redisSub.shutdown(); - service.shutdown(); - jedisPool.close(); - } - - public AddonPlugin getInstance() { - return instance; - } - - public SkriptAddon getAddonInstance() { - return addon; - } - - public ExecutorService getJedisExecutionService() { - return service; - } - - public JedisPool getJedisPool() { return jedisPool; } - - public boolean isEncryptionEnabled() { return encryptionEnabled; } - - public String decrypt(byte[] message) throws UnauthenticCiphertextException, IllegalBlockSizeException { - return new String(AES_SIV.decrypt(encryptionKey.getBytes(), macKey.getBytes(), message), StandardCharsets.UTF_8); - } - - public byte[] encrypt(String message) { - return AES_SIV.encrypt(encryptionKey.getBytes(), macKey.getBytes(), message.getBytes()); - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ee6d0b5..1ad2ede 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,7 +5,7 @@ Redis: Threads: 10 Port: 6379 TimeOut: 40000 - useSSL: true + useSSL: FALSE #useful if SSL is disabled EncryptMessages: false EncryptionKey: "16CHARACTERS KEY"