Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.alexpshkov.admintoolplugin.remote;
- import org.json.simple.JSONObject;
- import org.json.simple.JSONValue;
- import org.json.simple.parser.ParseException;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class RemoteAccess {
- private final String ip;
- private final Integer port;
- private boolean isServerOnline;
- private final String availableKey = "DJKFLdkjsfjldsfhdskfjhdss4654sFdsf";
- public RemoteAccess(String ip, int port) {
- this.ip = ip;
- this.port = port;
- }
- public boolean isServerOnline() {
- return isServerOnline;
- }
- public String handleRequest(String request) {
- try {
- JSONObject jsonRequest = (JSONObject) JSONValue.parseWithException(request);
- if(!jsonRequest.containsKey("key")) return "Error: Invalid format";
- String key = (String) jsonRequest.get("key");
- if(!availableKey.equals(key)) return "Error: Invalid key, please contact admin if this is an error";
- if(jsonRequest.containsKey("action")) {
- String action = (String) jsonRequest.get("action");
- switch (action) {
- case "pingServer":
- return Utils.makeJsonServerInfo().toJSONString();
- case "getProfile":
- if(!jsonRequest.containsKey("playerName")) return "Error: Invalid request. Please choose [playerName]";
- return Utils.makeJsonPlayerProfile((String)jsonRequest.get("playerName")).toJSONString();
- case "getTop":
- if(!jsonRequest.containsKey("topName")) return "Error: Invalid request. Please choose [topName]";
- return Utils.makeJsonTop((String)jsonRequest.get("topName")).toJSONString();
- case "getDailyBonus":
- if(!jsonRequest.containsKey("playerName")) return "Error: Invalid request. Please choose [playerName]";
- return Utils.makeJsonDaily((String)jsonRequest.get("playerName")).toJSONString();
- default:
- return "Error: Invalid action, please contact admin if this is an error";
- }
- }
- return "Error: Make something";
- } catch (ParseException e) {
- e.printStackTrace();
- return "Error: Invalid format";
- }
- }
- public JSONObject handleResult(String result) {
- try {
- isServerOnline = true;
- return (JSONObject) JSONValue.parseWithException(result);
- } catch (ParseException e) {
- System.out.println(result);
- return null;
- }
- }
- public void startAsyncServer() {
- new Thread(() -> {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(port);
- serverSocket.setReuseAddress(true);
- while (!serverSocket.isClosed()) {
- Socket socket = serverSocket.accept();
- InputStream input = socket.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- OutputStream output = socket.getOutputStream();
- PrintWriter writer = new PrintWriter(output, true);
- String result = handleRequest(reader.readLine());
- writer.println(result);
- socket.close();
- }
- } catch (IOException ignored) {
- } finally {
- if (serverSocket != null)
- try {
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- public JSONObject sendRequest(String request) {
- try (Socket socket = new Socket(ip, port)) {
- OutputStream output = socket.getOutputStream();
- PrintWriter writer = new PrintWriter(output, true);
- writer.println(request);
- InputStream input = socket.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- return handleResult(reader.readLine());
- } catch (IOException e) {
- isServerOnline = false;
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment