/src/net/megapowers/accessed/AccessedThread.java

https://github.com/phr0z3nt04st/Accessed · Java · 207 lines · 173 code · 22 blank · 12 comment · 27 complexity · 0550cdfaa941b917451b523e081218b8 MD5 · raw file

  1. package net.megapowers.accessed;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.net.SocketException;
  9. import java.util.concurrent.Callable;
  10. import java.util.concurrent.ExecutionException;
  11. import java.util.concurrent.Future;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import org.bukkit.Server;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.plugin.Plugin;
  17. import org.bukkit.plugin.PluginManager;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19. import org.bukkit.util.config.Configuration;
  20. public class AccessedThread extends Thread {
  21. ServerSocket serverSocket;
  22. Socket clientSocket;
  23. PluginManager pluginManager;
  24. Server server;
  25. Plugin plugin;
  26. Configuration config;
  27. public boolean running;
  28. BufferedReader reader;
  29. PrintWriter writer;
  30. public AccessedThread(ServerSocket serverSocket, JavaPlugin plugin) {
  31. this.serverSocket = serverSocket;
  32. try {
  33. serverSocket.setSoTimeout(2000);
  34. } catch (SocketException e) {
  35. e.printStackTrace();
  36. }
  37. this.plugin = plugin;
  38. this.server = plugin.getServer();
  39. this.config = plugin.getConfiguration();
  40. this.pluginManager = server.getPluginManager();
  41. running = true;
  42. }
  43. @Override
  44. public void run() {
  45. while (running) {
  46. try {
  47. clientSocket = serverSocket.accept();
  48. //TODO: make ip firewall system properly.
  49. /*String hostAddress = clientSocket.getInetAddress().getHostAddress();
  50. if (!hostAddress.equals("127.0.0.1") && !hostAddress.equals("0:0:0:0:0:0:0:1")) {
  51. Logger.getLogger("Minecraft").log(Level.INFO, "[Accessed] {0} tried accessing, but was denied.", hostAddress);
  52. clientSocket.close();
  53. continue;
  54. }*/
  55. reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  56. writer = new PrintWriter(clientSocket.getOutputStream(), true);
  57. String inputLine;
  58. String[] inputLineSplit;
  59. while ((inputLine = reader.readLine()) != null) {
  60. inputLineSplit = inputLine.split(" ");
  61. Callable c = null;
  62. if (inputLineSplit[0].equalsIgnoreCase("reload")) {
  63. c = new InputCallable<String, String>(inputLineSplit[1]) {
  64. @Override
  65. public String call() throws Exception {
  66. try {
  67. Plugin pluginToReload = pluginManager.getPlugin(input);
  68. pluginManager.disablePlugin(pluginToReload);
  69. pluginManager.enablePlugin(pluginToReload);
  70. return "done";
  71. } catch (Exception ex) {
  72. return "failed";
  73. }
  74. }
  75. };
  76. } else if (inputLineSplit[0].equalsIgnoreCase("reloadall")) {
  77. if (reloadAll()) {
  78. writer.println("done");
  79. } else {
  80. writer.println("failed");
  81. }
  82. } else if (inputLineSplit[0].equalsIgnoreCase("command")) {
  83. String commandToSend = inputLine.substring(inputLineSplit[0].length() + 1);
  84. if (commandToSend.equals("reload")) {
  85. if (reloadAll()) {
  86. writer.println("done");
  87. } else {
  88. writer.println("failed");
  89. }
  90. } else {
  91. Logger.getLogger("Minecraft").log(Level.INFO, "[Accessed] Dispatching command \"{0}\" to console.", commandToSend);
  92. c = new InputCallable<String, String>(commandToSend) {
  93. @Override
  94. public String call() throws Exception {
  95. try {
  96. server.dispatchCommand(server.getConsoleSender(), input);
  97. return "done";
  98. } catch (Exception ex) {
  99. return "failed";
  100. }
  101. }
  102. };
  103. }
  104. } else if (inputLineSplit[0].equalsIgnoreCase("list")) {
  105. c = new Callable<String>() {
  106. @Override
  107. public String call() throws Exception {
  108. try {
  109. Player[] in = server.getOnlinePlayers();
  110. String out = new String();
  111. int max = in.length;
  112. //for each player
  113. for (int i = 0; i < max; i++) {
  114. //append name to output
  115. out += in[i].getName();
  116. //formatting control
  117. if (i < max - 1) {
  118. out += ", ";
  119. }
  120. }
  121. //send output
  122. return out;
  123. } catch (Exception ex) {
  124. }
  125. return "";
  126. }
  127. };
  128. } else {
  129. writer.println("unknown");
  130. }
  131. if (c != null) {
  132. Future<String> returnFuture = server.getScheduler().callSyncMethod(plugin, c);
  133. try {
  134. writer.println(returnFuture.get());
  135. } catch (InterruptedException ex) {
  136. Logger.getLogger(AccessedThread.class.getName()).log(Level.SEVERE, null, ex);
  137. } catch (ExecutionException ex) {
  138. Logger.getLogger(AccessedThread.class.getName()).log(Level.SEVERE, null, ex);
  139. }
  140. }
  141. }
  142. } catch (IOException e) {
  143. }
  144. }
  145. try {
  146. if (serverSocket != null) {
  147. serverSocket.close();
  148. }
  149. } catch (IOException e) {
  150. }
  151. }
  152. boolean reloadAll() {
  153. Future<Boolean> returnFuture = server.getScheduler().callSyncMethod(plugin, new Callable<Boolean>() {
  154. @Override
  155. public Boolean call() throws Exception {
  156. try {
  157. for (Plugin plugin : pluginManager.getPlugins()) {
  158. if (plugin.getDescription().getName().equals("Accessed")) {
  159. continue;
  160. }
  161. pluginManager.disablePlugin(plugin);
  162. }
  163. for (Plugin plugin : pluginManager.getPlugins()) {
  164. if (plugin.getDescription().getName().equals("Accessed")) {
  165. continue;
  166. }
  167. pluginManager.enablePlugin(plugin);
  168. }
  169. } catch (Exception ex) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. });
  175. try {
  176. return returnFuture.get();
  177. } catch (InterruptedException ex) {
  178. Logger.getLogger(AccessedThread.class.getName()).log(Level.SEVERE, null, ex);
  179. } catch (ExecutionException ex) {
  180. Logger.getLogger(AccessedThread.class.getName()).log(Level.SEVERE, null, ex);
  181. }
  182. //everything failed.
  183. return false;
  184. }
  185. }