not tested But rewritten the Plugin

This commit is contained in:
ham1255 2020-06-28 18:40:53 +04:00
parent 7a51aef643
commit d9c323bc25
10 changed files with 192 additions and 165 deletions

View File

@ -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());
}
}

View File

@ -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<String, RedisMessageEvent>() {
@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<String, RedisMessageEvent>() {
@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;
}
}

View File

@ -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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
import org.cryptomator.siv.UnauthenticCiphertextException; import org.cryptomator.siv.UnauthenticCiphertextException;
import org.json.JSONObject; import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis; import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.BinaryJedisPubSub; import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean; 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 JedisPool jedisPool;
private BinaryJedis j; private ExecutorService RedisService;
//sub
private BinaryJedis subscribeJedis;
private List<String> channels; private List<String> channels;
private AtomicBoolean isShuttingDown = new AtomicBoolean(false); private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
private AtomicBoolean isRedisOnline = new AtomicBoolean(); private AtomicBoolean isRedisOnline = new AtomicBoolean();
private Encryption encryption;
public RedisSub(AddonPlugin plugin, BinaryJedis j, List<String> channels) { public RedisManager(LimeworkSpigotCore plugin) {
this.plugin = plugin; this.plugin = plugin;
this.j = j; Configuration config = this.plugin.getConfig();
this.channels = channels; 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 @Override
@ -33,7 +66,7 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable {
while (!isShuttingDown.get()) { while (!isShuttingDown.get()) {
try { try {
message("&e[Jedis] &cConnecting to redis..........."); 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); isRedisOnline.set(true);
message("&e[Jedis] &aRedis Connected"); message("&e[Jedis] &aRedis Connected");
int byteArr2dSize = 1; int byteArr2dSize = 1;
@ -55,11 +88,11 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable {
channelsInByte = new byte[channels.size()][byteArr2dSize]; channelsInByte = new byte[channels.size()][byteArr2dSize];
} }
} while (reInitializeByteArray); } while (reInitializeByteArray);
this.j.subscribe(this, channelsInByte); this.subscribeJedis.subscribe(this, channelsInByte);
} catch (Exception e) { } catch (Exception e) {
message("&e[Jedis] &cConnection to redis has failed! &ereconnecting..."); message("&e[Jedis] &cConnection to redis has failed! &ereconnecting...");
this.j.close(); this.subscribeJedis.close();
isRedisOnline.set(false); isRedisOnline.set(false);
} }
try { try {
@ -76,20 +109,18 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable {
@Override @Override
public void onMessage(byte[] channel, byte[] message) { public void onMessage(byte[] channel, byte[] message) {
AddonPlugin plugin = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");
String channelString = new String(channel); String channelString = new String(channel);
try { try {
String decrypted = null; String decrypted = null;
try { try {
assert plugin != null; decrypted = encryption.decrypt(message);
decrypted = plugin.decrypt(message);
} catch (UnauthenticCiphertextException | IllegalBlockSizeException e) { } catch (UnauthenticCiphertextException | IllegalBlockSizeException e) {
e.printStackTrace(); e.printStackTrace();
} }
assert decrypted != null; assert decrypted != null;
JSONObject j = new JSONObject(decrypted); JSONObject j = new JSONObject(decrypted);
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); //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) { } catch (Exception e) {
e.printStackTrace(); 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.")); 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() { public void shutdown() {
this.isShuttingDown.set(true); this.isShuttingDown.set(true);
this.unsubscribe(); this.unsubscribe();
j.close(); this.RedisService.shutdown();
subscribeJedis.close();
} }
public boolean IsRedisOnline() { public boolean IsRedisOnline() {
return isRedisOnline.get(); return isRedisOnline.get();
} }
} 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;
}
}

View File

@ -1,13 +1,8 @@
package net.limework.skLimework.elements; package net.limework.core.Skript.elements;
;
import ch.njol.skript.Skript;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal; import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent; import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser; 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; import org.bukkit.event.Event;
public class EvtRedis extends SkriptEvent { public class EvtRedis extends SkriptEvent {

View File

@ -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.Expression;
import ch.njol.skript.lang.SkriptParser; import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean; import ch.njol.util.Kleenean;
import net.limework.skLimework.Events.onRedisMessage; import net.limework.core.events.RedisMessageEvent;
import org.bukkit.event.Event; import org.bukkit.event.Event;
public class ExprChannel extends SimpleExpression<String> { public class ExprChannel extends SimpleExpression<String> {
@ -34,8 +34,8 @@ public class ExprChannel extends SimpleExpression<String> {
@Override @Override
protected String[] get(Event e) { protected String[] get(Event e) {
if (e instanceof onRedisMessage){ if (e instanceof RedisMessageEvent){
return new String[]{((onRedisMessage) e).getChannelName()}; return new String[]{((RedisMessageEvent) e).getChannelName()};
} }
return null; return null;
} }

View File

@ -1,18 +1,18 @@
package net.limework.skLimework.Events; package net.limework.core.events;
import net.limework.skLimework.AddonPlugin; import net.limework.skLimework.AddonPlugin;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@SuppressWarnings("unused")
public class onRedisMessage extends Event { public class RedisMessageEvent extends Event {
private final static HandlerList HANDLERS = new HandlerList(); private final static HandlerList HANDLERS = new HandlerList();
private String channelName; private String channelName;
private String message; private String message;
onRedisMessage(String channelName , String message) { public RedisMessageEvent(String channelName , String message) {
super(true); super(true);
this.channelName = channelName; this.channelName = channelName;
AddonPlugin instance = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework"); AddonPlugin instance = (AddonPlugin) Bukkit.getPluginManager().getPlugin("SKLimework");

View File

@ -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<String, onRedisMessage>() {
@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<String, onRedisMessage>() {
@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());
}
}

View File

@ -5,7 +5,7 @@ Redis:
Threads: 10 Threads: 10
Port: 6379 Port: 6379
TimeOut: 40000 TimeOut: 40000
useSSL: true useSSL: FALSE
#useful if SSL is disabled #useful if SSL is disabled
EncryptMessages: false EncryptMessages: false
EncryptionKey: "16CHARACTERS KEY" EncryptionKey: "16CHARACTERS KEY"