2020-05-10 06:52:06 +00:00
|
|
|
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;
|
2020-05-27 11:43:16 +00:00
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
2020-05-10 06:52:06 +00:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
2020-05-27 11:43:16 +00:00
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
2020-05-10 06:52:06 +00:00
|
|
|
import java.beans.Expression;
|
|
|
|
import java.io.IOException;
|
2020-05-27 11:43:16 +00:00
|
|
|
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;
|
2020-05-10 06:52:06 +00:00
|
|
|
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;
|
2020-05-27 13:22:05 +00:00
|
|
|
private Cipher encryptionCipher;
|
|
|
|
private Cipher decryptionCipher;
|
2020-05-27 11:43:16 +00:00
|
|
|
private boolean encryptionEnabled;
|
2020-05-10 06:52:06 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEnable(){
|
|
|
|
instance = this;
|
|
|
|
this.saveDefaultConfig();
|
2020-05-27 11:43:16 +00:00
|
|
|
FileConfiguration config = this.getConfig();
|
|
|
|
encryptionEnabled = config.getBoolean("Redis.EncryptMessages");
|
|
|
|
if (encryptionEnabled) {
|
|
|
|
// AES-128 encryption
|
|
|
|
String configKey = config.getString("Redis.EncryptionKey");
|
|
|
|
byte[] key = null;
|
|
|
|
assert configKey != null;
|
|
|
|
key = configKey.getBytes(StandardCharsets.UTF_8);
|
|
|
|
MessageDigest sha = null;
|
|
|
|
try {
|
|
|
|
sha = MessageDigest.getInstance("SHA-1");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-05-27 12:10:26 +00:00
|
|
|
assert sha != null;
|
|
|
|
key = sha.digest(key);
|
2020-05-27 11:43:16 +00:00
|
|
|
key = Arrays.copyOf(key, 16);
|
|
|
|
SecretKeySpec encryptionKey = new SecretKeySpec(key, "AES");
|
|
|
|
|
2020-05-27 13:22:05 +00:00
|
|
|
encryptionCipher = null;
|
2020-05-27 11:43:16 +00:00
|
|
|
try {
|
2020-05-27 13:22:05 +00:00
|
|
|
encryptionCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchPaddingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
encryptionCipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
decryptionCipher = null;
|
|
|
|
try {
|
|
|
|
decryptionCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchPaddingException e) {
|
2020-05-27 11:43:16 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
try {
|
2020-05-27 13:22:05 +00:00
|
|
|
decryptionCipher.init(Cipher.DECRYPT_MODE, encryptionKey);
|
2020-05-27 11:43:16 +00:00
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 06:52:06 +00:00
|
|
|
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, "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);
|
|
|
|
|
2020-05-27 11:43:16 +00:00
|
|
|
|
2020-05-10 06:52:06 +00:00
|
|
|
|
|
|
|
} 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"));
|
2020-05-10 19:10:45 +00:00
|
|
|
service.execute(redisSub);
|
2020-05-10 06:52:06 +00:00
|
|
|
|
|
|
|
Bukkit.getLogger().info("[Govindas limework Addon] was enabled!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable(){
|
2020-05-10 19:10:45 +00:00
|
|
|
redisSub.shutdown();
|
2020-05-10 06:52:06 +00:00
|
|
|
service.shutdown();
|
2020-05-17 21:57:36 +00:00
|
|
|
jedisPool.close();
|
2020-05-10 06:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public AddonPlugin getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SkriptAddon getAddonInstance() {
|
|
|
|
return addon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ExecutorService getJedisExecutionService() {
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JedisPool getJedisPool() { return jedisPool; }
|
2020-05-27 11:43:16 +00:00
|
|
|
|
|
|
|
public boolean isEncryptionEnabled() { return encryptionEnabled; }
|
|
|
|
|
|
|
|
public String encrypt(String message) {
|
|
|
|
String encrypted = null;
|
|
|
|
try {
|
2020-05-27 13:22:05 +00:00
|
|
|
encrypted = Base64.getEncoder().encodeToString(encryptionCipher.doFinal(message.getBytes(StandardCharsets.UTF_8)));
|
2020-05-27 11:43:16 +00:00
|
|
|
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return encrypted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String decrypt(String message) {
|
|
|
|
String decrypted = null;
|
|
|
|
try {
|
2020-05-27 13:22:05 +00:00
|
|
|
decrypted = new String(decryptionCipher.doFinal(Base64.getDecoder().decode(message)), StandardCharsets.UTF_8);
|
2020-05-27 11:43:16 +00:00
|
|
|
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return decrypted;
|
|
|
|
}
|
2020-05-10 06:52:06 +00:00
|
|
|
}
|