|
@@ -24,6 +24,20 @@ import me.km.snuviscript.CommandScript;
|
|
import me.km.snuviscript.MinecraftFunctions;
|
|
import me.km.snuviscript.MinecraftFunctions;
|
|
import me.km.snuviscript.ScriptEvents;
|
|
import me.km.snuviscript.ScriptEvents;
|
|
import me.km.snuviscript.Scripts;
|
|
import me.km.snuviscript.Scripts;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.net.ServerSocket;
|
|
|
|
+import java.net.Socket;
|
|
|
|
+import java.security.InvalidKeyException;
|
|
|
|
+import java.security.KeyFactory;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.security.PrivateKey;
|
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
|
+import java.util.Base64;
|
|
|
|
+import javax.crypto.BadPaddingException;
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
|
+import javax.crypto.IllegalBlockSizeException;
|
|
|
|
+import javax.crypto.NoSuchPaddingException;
|
|
|
|
+import org.lwjgl.system.CallbackI.S;
|
|
|
|
|
|
@OnlyIn(Dist.DEDICATED_SERVER)
|
|
@OnlyIn(Dist.DEDICATED_SERVER)
|
|
public class Server {
|
|
public class Server {
|
|
@@ -36,6 +50,66 @@ public class Server {
|
|
public static ScriptEvents scriptEvents;
|
|
public static ScriptEvents scriptEvents;
|
|
private static WorldPlotMap plotMap;
|
|
private static WorldPlotMap plotMap;
|
|
|
|
|
|
|
|
+ private static volatile boolean isRunning = true;
|
|
|
|
+ private static ServerSocket votifierSocket = null;
|
|
|
|
+ private static Thread votifierAccept = null;
|
|
|
|
+ private static PrivateKey privateKey = null;
|
|
|
|
+
|
|
|
|
+ public static PrivateKey getPrivateKey(String base64) {
|
|
|
|
+ try {
|
|
|
|
+ PKCS8EncodedKeySpec keySpec =
|
|
|
|
+ new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64.getBytes()));;
|
|
|
|
+ return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
|
|
|
|
+ } catch(Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String decrypt(byte[] data, PrivateKey privateKey) throws Exception {
|
|
|
|
+ Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
|
|
|
+ return new String(cipher.doFinal(data));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void startVotifier(String pKey) {
|
|
|
|
+ privateKey = getPrivateKey(pKey);
|
|
|
|
+ if(privateKey == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ votifierSocket = new ServerSocket(12345);
|
|
|
|
+ votifierAccept = new Thread(() -> {
|
|
|
|
+ while(isRunning) {
|
|
|
|
+ try {
|
|
|
|
+ Socket client = votifierSocket.accept();
|
|
|
|
+ InputStream in = client.getInputStream();
|
|
|
|
+ byte[] data = new byte[256];
|
|
|
|
+ for(int i = 0; i < 256; i++) {
|
|
|
|
+ data[i] = (byte) in.read();
|
|
|
|
+ }
|
|
|
|
+ String[] s = decrypt(data, privateKey).split("\n");
|
|
|
|
+ scheduler.scheduleTask(() -> {
|
|
|
|
+ scriptEvents.onVote(s);
|
|
|
|
+ });
|
|
|
|
+ } catch(Exception ex) {
|
|
|
|
+ if(!votifierSocket.isClosed()) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ votifierAccept.start();
|
|
|
|
+ logger.print("votifier started");
|
|
|
|
+ } catch(Exception ex) {
|
|
|
|
+ logger.print("------------------------------------------------");
|
|
|
|
+ logger.print("cannot start votifier socket");
|
|
|
|
+ logger.print("------------------------------------------------");
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void onStart(DedicatedServer serverIn) {
|
|
public static void onStart(DedicatedServer serverIn) {
|
|
server = serverIn;
|
|
server = serverIn;
|
|
logger = new SnuviLogger();
|
|
logger = new SnuviLogger();
|
|
@@ -107,12 +181,23 @@ public class Server {
|
|
server, playerBank, customEventCaller, databank, plotMap, commands);
|
|
server, playerBank, customEventCaller, databank, plotMap, commands);
|
|
|
|
|
|
scheduler.scheduleTask(() -> scripts.startScript(null, "startscript"));
|
|
scheduler.scheduleTask(() -> scripts.startScript(null, "startscript"));
|
|
|
|
+ startVotifier(conf.getString(null, "pkey"));
|
|
}
|
|
}
|
|
|
|
|
|
public static void onStop() {
|
|
public static void onStop() {
|
|
scripts.startScript("endscript");
|
|
scripts.startScript("endscript");
|
|
scheduler.stop();
|
|
scheduler.stop();
|
|
databank.closeDataBankConnection();
|
|
databank.closeDataBankConnection();
|
|
|
|
+
|
|
|
|
+ isRunning = false;
|
|
|
|
+ try {
|
|
|
|
+ if(votifierSocket != null) {
|
|
|
|
+ votifierSocket.close();
|
|
|
|
+ votifierAccept.join(1000);
|
|
|
|
+ }
|
|
|
|
+ } catch(Exception ex) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public static ISnuviLogger getLogger() {
|
|
public static ISnuviLogger getLogger() {
|