Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. package rawrxd;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.InetSocketAddress;
  8. import java.net.Socket;
  9. import java.security.KeyFactory;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.PublicKey;
  12. import java.security.spec.InvalidKeySpecException;
  13. import java.security.spec.X509EncodedKeySpec;
  14. import java.util.ArrayList;
  15. import java.util.Date;
  16. import java.util.Iterator;
  17. import java.util.List;
  18.  
  19. import javax.xml.bind.DatatypeConverter;
  20.  
  21. import org.bukkit.Bukkit;
  22. import org.bukkit.command.Command;
  23. import org.bukkit.command.CommandExecutor;
  24. import org.bukkit.command.CommandSender;
  25. import org.bukkit.configuration.ConfigurationSection;
  26. import org.bukkit.entity.Player;
  27. import org.bukkit.event.EventHandler;
  28. import org.bukkit.event.EventPriority;
  29. import org.bukkit.event.Listener;
  30. import org.bukkit.plugin.Plugin;
  31. import org.bukkit.plugin.java.JavaPlugin;
  32.  
  33. import com.vexsoftware.votifier.crypto.RSA;
  34. import com.vexsoftware.votifier.model.Vote;
  35. import com.vexsoftware.votifier.model.VotifierEvent;
  36.  
  37. public class VoteClient {
  38.  
  39. public class VoteSend extends JavaPlugin implements Listener, CommandExecutor {
  40. private List<VoteServer> voteServers = new ArrayList<VoteServer>();
  41.  
  42. public void onEnable() {
  43. InputStream in;
  44. File file;
  45. this.getServer().getPluginManager().registerEvents((Listener) this, (Plugin) this);
  46. this.getCommand("vsreload").setExecutor((CommandExecutor) this);
  47. this.getCommand("sendvote").setExecutor((CommandExecutor) this);
  48. if (!this.getDataFolder().exists()) {
  49. this.getDataFolder().mkdir();
  50. }
  51. if (!(file = new File(this.getDataFolder(), "config.yml")).exists()
  52. && (in = VoteSend.class.getResourceAsStream("/res/config.yml")) != null) {
  53. try {
  54. FileOutputStream out = new FileOutputStream(file);
  55. byte[] buffer = new byte[8192];
  56. int length = 0;
  57. while ((length = in.read(buffer)) > 0) {
  58. out.write(buffer, 0, length);
  59. }
  60. if (in != null) {
  61. in.close();
  62. }
  63. if (out != null) {
  64. out.close();
  65. }
  66. } catch (Exception out) {
  67. // empty catch block
  68. }
  69. }
  70. this.reload();
  71. this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Enabled");
  72. }
  73.  
  74. public void onDisable() {
  75. this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Disabled");
  76. }
  77.  
  78. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  79. if (command.getName().equalsIgnoreCase("vsreload") && sender.isOp()) {
  80. this.reload();
  81. if (sender instanceof Player) {
  82. ((Player) sender)
  83. .sendMessage(String.valueOf(this.getDescription().getFullName()) + " Config Reloaded");
  84. }
  85. this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Config Reloaded");
  86. return true;
  87. }
  88. if (command.getName().equalsIgnoreCase("sendvote") && sender.isOp()) {
  89. String arg2;
  90. String arg1 = args.length > 0 ? args[0] : null;
  91. String string = arg2 = args.length > 1 ? args[1] : null;
  92. if (arg1 == null) {
  93. if (sender instanceof Player) {
  94. ((Player) sender).sendMessage("- /sendvote <player> <servicename>");
  95. }
  96. return true;
  97. }
  98. Vote fakeVote = new Vote();
  99. fakeVote.setUsername(arg1);
  100. fakeVote.setServiceName(arg2 == null ? "testVote" : arg2);
  101. fakeVote.setAddress("1.2.3.4");
  102. Date date = new Date();
  103. fakeVote.setTimeStamp(String.valueOf(date.getTime()));
  104. this.processVote(fakeVote);
  105. if (sender instanceof Player) {
  106. ((Player) sender).sendMessage("sent test vote!");
  107. }
  108. this.getLogger().info("Sent test vote: " + fakeVote.toString());
  109. return true;
  110. }
  111. return false;
  112. }
  113.  
  114. @EventHandler(priority = EventPriority.NORMAL)
  115. public void onVotifierEvent(VotifierEvent event) {
  116. this.processVote(event.getVote());
  117. }
  118.  
  119. public void processVote(final Vote vote) {
  120. this.getServer().getScheduler().runTaskAsynchronously((Plugin) this, new Runnable() {
  121.  
  122. @Override
  123. public void run() {
  124. try {
  125. for (VoteServer server : VoteSend.this.voteServers) {
  126. if (server.serverIP.length() == 0)
  127. continue;
  128. try {
  129. if (server.custom.length() > 0) {
  130. vote.setServiceName(server.custom);
  131. }
  132. String VoteString = "VOTE\n" + vote.getServiceName() + "\n" + vote.getUsername() + "\n"
  133. + vote.getAddress() + "\n" + vote.getTimeStamp() + "\n";
  134. InetSocketAddress sockAddr = new InetSocketAddress(server.serverIP, server.serverPort);
  135. Socket socket = new Socket();
  136. socket.connect(sockAddr, 1000);
  137. OutputStream socketOutputStream = socket.getOutputStream();
  138. socketOutputStream.write(
  139. RSA.encrypt((byte[]) VoteString.getBytes(), (PublicKey) server.publicKey));
  140. socketOutputStream.close();
  141. socket.close();
  142. Bukkit.getLogger().info("Vote -> " + server + ": " + vote.toString());
  143. continue;
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. } catch (Exception server) {
  149. // empty catch block
  150. }
  151. }
  152. });
  153. }
  154.  
  155. public void reload() {
  156. this.reloadConfig();
  157. this.getConfig().options().pathSeparator('/');
  158. this.voteServers.clear();
  159. ConfigurationSection cs = this.getConfig().getConfigurationSection("servers");
  160. if (cs != null) {
  161. Iterator i = cs.getKeys(false).iterator();
  162. while (i.hasNext()) {
  163. String server = (String) i.next();
  164. ConfigurationSection serverConfig = cs.getConfigurationSection(server);
  165. if (serverConfig == null)
  166. continue;
  167. server = server.toLowerCase();
  168. try {
  169. this.voteServers
  170. .add(new VoteServer(server, serverConfig.getString("Key"), serverConfig.getString("IP"),
  171. serverConfig.getInt("Port"), serverConfig.getString("Custom")));
  172. continue;
  173. } catch (InvalidKeySpecException e) {
  174. e.printStackTrace();
  175. continue;
  176. } catch (NoSuchAlgorithmException e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. }
  181. }
  182.  
  183. public class VoteServer {
  184. public String name;
  185. public PublicKey publicKey;
  186. public String serverIP;
  187. public int serverPort;
  188. public String custom;
  189.  
  190. public VoteServer(String n, String pKey, String sIP, int sPort, String c)
  191. throws InvalidKeySpecException, NoSuchAlgorithmException {
  192. byte[] encodedPublicKey = DatatypeConverter.parseBase64Binary(pKey);
  193. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  194. X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
  195. this.publicKey = keyFactory.generatePublic(publicKeySpec);
  196. this.serverIP = sIP;
  197. this.serverPort = sPort;
  198. this.name = n;
  199. this.custom = c;
  200. }
  201. }
  202.  
  203. }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement