Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rawrxd;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.security.KeyFactory;
- import java.security.NoSuchAlgorithmException;
- import java.security.PublicKey;
- import java.security.spec.InvalidKeySpecException;
- import java.security.spec.X509EncodedKeySpec;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import javax.xml.bind.DatatypeConverter;
- import org.bukkit.Bukkit;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.vexsoftware.votifier.crypto.RSA;
- import com.vexsoftware.votifier.model.Vote;
- import com.vexsoftware.votifier.model.VotifierEvent;
- public class VoteClient {
- public class VoteSend extends JavaPlugin implements Listener, CommandExecutor {
- private List<VoteServer> voteServers = new ArrayList<VoteServer>();
- public void onEnable() {
- InputStream in;
- File file;
- this.getServer().getPluginManager().registerEvents((Listener) this, (Plugin) this);
- this.getCommand("vsreload").setExecutor((CommandExecutor) this);
- this.getCommand("sendvote").setExecutor((CommandExecutor) this);
- if (!this.getDataFolder().exists()) {
- this.getDataFolder().mkdir();
- }
- if (!(file = new File(this.getDataFolder(), "config.yml")).exists()
- && (in = VoteSend.class.getResourceAsStream("/res/config.yml")) != null) {
- try {
- FileOutputStream out = new FileOutputStream(file);
- byte[] buffer = new byte[8192];
- int length = 0;
- while ((length = in.read(buffer)) > 0) {
- out.write(buffer, 0, length);
- }
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (Exception out) {
- // empty catch block
- }
- }
- this.reload();
- this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Enabled");
- }
- public void onDisable() {
- this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Disabled");
- }
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("vsreload") && sender.isOp()) {
- this.reload();
- if (sender instanceof Player) {
- ((Player) sender)
- .sendMessage(String.valueOf(this.getDescription().getFullName()) + " Config Reloaded");
- }
- this.getLogger().info(String.valueOf(this.getDescription().getFullName()) + " Config Reloaded");
- return true;
- }
- if (command.getName().equalsIgnoreCase("sendvote") && sender.isOp()) {
- String arg2;
- String arg1 = args.length > 0 ? args[0] : null;
- String string = arg2 = args.length > 1 ? args[1] : null;
- if (arg1 == null) {
- if (sender instanceof Player) {
- ((Player) sender).sendMessage("- /sendvote <player> <servicename>");
- }
- return true;
- }
- Vote fakeVote = new Vote();
- fakeVote.setUsername(arg1);
- fakeVote.setServiceName(arg2 == null ? "testVote" : arg2);
- fakeVote.setAddress("1.2.3.4");
- Date date = new Date();
- fakeVote.setTimeStamp(String.valueOf(date.getTime()));
- this.processVote(fakeVote);
- if (sender instanceof Player) {
- ((Player) sender).sendMessage("sent test vote!");
- }
- this.getLogger().info("Sent test vote: " + fakeVote.toString());
- return true;
- }
- return false;
- }
- @EventHandler(priority = EventPriority.NORMAL)
- public void onVotifierEvent(VotifierEvent event) {
- this.processVote(event.getVote());
- }
- public void processVote(final Vote vote) {
- this.getServer().getScheduler().runTaskAsynchronously((Plugin) this, new Runnable() {
- @Override
- public void run() {
- try {
- for (VoteServer server : VoteSend.this.voteServers) {
- if (server.serverIP.length() == 0)
- continue;
- try {
- if (server.custom.length() > 0) {
- vote.setServiceName(server.custom);
- }
- String VoteString = "VOTE\n" + vote.getServiceName() + "\n" + vote.getUsername() + "\n"
- + vote.getAddress() + "\n" + vote.getTimeStamp() + "\n";
- InetSocketAddress sockAddr = new InetSocketAddress(server.serverIP, server.serverPort);
- Socket socket = new Socket();
- socket.connect(sockAddr, 1000);
- OutputStream socketOutputStream = socket.getOutputStream();
- socketOutputStream.write(
- RSA.encrypt((byte[]) VoteString.getBytes(), (PublicKey) server.publicKey));
- socketOutputStream.close();
- socket.close();
- Bukkit.getLogger().info("Vote -> " + server + ": " + vote.toString());
- continue;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- } catch (Exception server) {
- // empty catch block
- }
- }
- });
- }
- public void reload() {
- this.reloadConfig();
- this.getConfig().options().pathSeparator('/');
- this.voteServers.clear();
- ConfigurationSection cs = this.getConfig().getConfigurationSection("servers");
- if (cs != null) {
- Iterator i = cs.getKeys(false).iterator();
- while (i.hasNext()) {
- String server = (String) i.next();
- ConfigurationSection serverConfig = cs.getConfigurationSection(server);
- if (serverConfig == null)
- continue;
- server = server.toLowerCase();
- try {
- this.voteServers
- .add(new VoteServer(server, serverConfig.getString("Key"), serverConfig.getString("IP"),
- serverConfig.getInt("Port"), serverConfig.getString("Custom")));
- continue;
- } catch (InvalidKeySpecException e) {
- e.printStackTrace();
- continue;
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public class VoteServer {
- public String name;
- public PublicKey publicKey;
- public String serverIP;
- public int serverPort;
- public String custom;
- public VoteServer(String n, String pKey, String sIP, int sPort, String c)
- throws InvalidKeySpecException, NoSuchAlgorithmException {
- byte[] encodedPublicKey = DatatypeConverter.parseBase64Binary(pKey);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
- this.publicKey = keyFactory.generatePublic(publicKeySpec);
- this.serverIP = sIP;
- this.serverPort = sPort;
- this.name = n;
- this.custom = c;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement