Kajetan Johannes Hammerle 2 years ago
parent
commit
af102c97fe
2 changed files with 98 additions and 0 deletions
  1. 85 0
      src/main/java/me/km/Server.java
  2. 13 0
      src/main/java/me/km/snuviscript/ScriptEvents.java

+ 85 - 0
src/main/java/me/km/Server.java

@@ -24,6 +24,20 @@ import me.km.snuviscript.CommandScript;
 import me.km.snuviscript.MinecraftFunctions;
 import me.km.snuviscript.ScriptEvents;
 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)
 public class Server {
@@ -36,6 +50,66 @@ public class Server {
     public static ScriptEvents scriptEvents;
     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) {
         server = serverIn;
         logger = new SnuviLogger();
@@ -107,12 +181,23 @@ public class Server {
                 server, playerBank, customEventCaller, databank, plotMap, commands);
 
         scheduler.scheduleTask(() -> scripts.startScript(null, "startscript"));
+        startVotifier(conf.getString(null, "pkey"));
     }
 
     public static void onStop() {
         scripts.startScript("endscript");
         scheduler.stop();
         databank.closeDataBankConnection();
+
+        isRunning = false;
+        try {
+            if(votifierSocket != null) {
+                votifierSocket.close();
+                votifierAccept.join(1000);
+            }
+        } catch(Exception ex) {
+            ex.printStackTrace();
+        }
     }
 
     public static ISnuviLogger getLogger() {

+ 13 - 0
src/main/java/me/km/snuviscript/ScriptEvents.java

@@ -3,6 +3,7 @@ package me.km.snuviscript;
 import java.util.*;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
+import jdk.nashorn.api.scripting.ScriptUtils;
 import me.hammerle.snuviscript.code.*;
 import me.hammerle.snuviscript.inputprovider.Variable;
 import me.kcm.BlockHarvest;
@@ -793,4 +794,16 @@ public class ScriptEvents implements BlockHarvest, Craft {
         result.setInventorySlotContents(0, wrapper.get());
         serverplayerentity.connection.sendPacket(new SSetSlotPacket(id, 0, wrapper.get()));
     }
+
+    public void onVote(String[] data) {
+        if(data.length < 5) {
+            return;
+        }
+        handleEvent("vote", sc -> {
+            sc.setVar("from", data[1]);
+            sc.setVar("name", data[2]);
+            sc.setVar("ip", data[3]);
+            sc.setVar("timestamp", SnuviUtils.convert(data[4]));
+        });
+    }
 }