AlexPshkov

Untitled

May 9th, 2021 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. package ru.alexpshkov.admintoolplugin.remote;
  2.  
  3. import org.json.simple.JSONObject;
  4. import org.json.simple.JSONValue;
  5. import org.json.simple.parser.ParseException;
  6.  
  7. import java.io.*;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10.  
  11. public class RemoteAccess {
  12.  
  13.     private final String ip;
  14.     private final Integer port;
  15.     private boolean isServerOnline;
  16.     private final String availableKey = "DJKFLdkjsfjldsfhdskfjhdss4654sFdsf";
  17.  
  18.     public RemoteAccess(String ip, int port) {
  19.         this.ip = ip;
  20.         this.port = port;
  21.     }
  22.  
  23.     public boolean isServerOnline() {
  24.         return isServerOnline;
  25.     }
  26.  
  27.     public String handleRequest(String request) {
  28.         try {
  29.             JSONObject jsonRequest = (JSONObject) JSONValue.parseWithException(request);
  30.             if(!jsonRequest.containsKey("key")) return "Error: Invalid format";
  31.             String key = (String) jsonRequest.get("key");
  32.             if(!availableKey.equals(key)) return "Error: Invalid key, please contact admin if this is an error";
  33.  
  34.             if(jsonRequest.containsKey("action")) {
  35.                 String action = (String) jsonRequest.get("action");
  36.                 switch (action) {
  37.                     case "pingServer":
  38.                         return Utils.makeJsonServerInfo().toJSONString();
  39.                     case "getProfile":
  40.                         if(!jsonRequest.containsKey("playerName")) return "Error: Invalid request. Please choose [playerName]";
  41.                         return Utils.makeJsonPlayerProfile((String)jsonRequest.get("playerName")).toJSONString();
  42.                     case "getTop":
  43.                         if(!jsonRequest.containsKey("topName")) return "Error: Invalid request. Please choose [topName]";
  44.                         return Utils.makeJsonTop((String)jsonRequest.get("topName")).toJSONString();
  45.                     case "getDailyBonus":
  46.                         if(!jsonRequest.containsKey("playerName")) return "Error: Invalid request. Please choose [playerName]";
  47.                         return Utils.makeJsonDaily((String)jsonRequest.get("playerName")).toJSONString();
  48.                     default:
  49.                         return "Error: Invalid action, please contact admin if this is an error";
  50.                 }
  51.             }
  52.             return "Error: Make something";
  53.         } catch (ParseException e) {
  54.             e.printStackTrace();
  55.             return "Error: Invalid format";
  56.         }
  57.     }
  58.  
  59.     public JSONObject handleResult(String result) {
  60.         try {
  61.             isServerOnline = true;
  62.             return (JSONObject) JSONValue.parseWithException(result);
  63.         } catch (ParseException e) {
  64.             System.out.println(result);
  65.             return null;
  66.         }
  67.     }
  68.  
  69.     public void startAsyncServer() {
  70.         new Thread(() -> {
  71.             ServerSocket serverSocket = null;
  72.             try {
  73.                 serverSocket = new ServerSocket(port);
  74.                 serverSocket.setReuseAddress(true);
  75.                 while (!serverSocket.isClosed()) {
  76.                     Socket socket = serverSocket.accept();
  77.                     InputStream input = socket.getInputStream();
  78.                     BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  79.                     OutputStream output = socket.getOutputStream();
  80.                     PrintWriter writer = new PrintWriter(output, true);
  81.                     String result = handleRequest(reader.readLine());
  82.                     writer.println(result);
  83.                     socket.close();
  84.                 }
  85.             } catch (IOException ignored) {
  86.             } finally {
  87.                 if (serverSocket != null)
  88.                     try {
  89.                         serverSocket.close();
  90.                     } catch (IOException e) {
  91.                         e.printStackTrace();
  92.                     }
  93.             }
  94.         }).start();
  95.     }
  96.  
  97.     public JSONObject sendRequest(String request) {
  98.         try (Socket socket = new Socket(ip, port)) {
  99.             OutputStream output = socket.getOutputStream();
  100.             PrintWriter writer = new PrintWriter(output, true);
  101.             writer.println(request);
  102.  
  103.             InputStream input = socket.getInputStream();
  104.             BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  105.             return handleResult(reader.readLine());
  106.         } catch (IOException e) {
  107.             isServerOnline = false;
  108.             return null;
  109.         }
  110.     }
  111.  
  112. }
  113.  
Add Comment
Please, Sign In to add comment