Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. package me.taco.plex.msg.GUI;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.io.FileOutputStream;
  11. import java.io.InputStream;
  12. import java.util.Enumeration;
  13. import java.util.zip.ZipEntry;
  14. import java.util.zip.ZipFile;
  15. import java.util.zip.ZipOutputStream;
  16.  
  17. import javax.swing.JButton;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JTextField;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.UIManager;
  26.  
  27. import org.objectweb.asm.ClassReader;
  28. import org.objectweb.asm.ClassWriter;
  29. import org.objectweb.asm.tree.ClassNode;
  30. import org.objectweb.asm.tree.InsnList;
  31. import org.objectweb.asm.tree.LdcInsnNode;
  32. import org.objectweb.asm.tree.MethodInsnNode;
  33. import org.objectweb.asm.tree.MethodNode;
  34.  
  35. @SuppressWarnings("serial")
  36. public class onDisableGUI extends JFrame {
  37. private JTextField field;
  38. private JTextField messagetoinject;
  39. private static boolean messagegotinjected;
  40.  
  41. public static void main(String[] args) {
  42. createGUI();
  43. }
  44.  
  45. public static void createGUI() {
  46. SwingUtilities.invokeLater(new Runnable() {
  47. @Override
  48. public void run() {
  49. try {
  50. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  51. }
  52. catch (Exception ex) {}
  53. onDisableGUI onDisablegui = new onDisableGUI();
  54. onDisablegui.setTitle("Bukkit Plugin Message onDisablegui");
  55. onDisablegui.setResizable(false);
  56. onDisablegui.setSize(440, 135);
  57. onDisablegui.setLocationRelativeTo(null);
  58. onDisablegui.setDefaultCloseOperation(3);
  59. onDisablegui.getContentPane().setLayout(new FlowLayout());
  60. JLabel label = new JLabel("Select File:");
  61. JLabel label2 = new JLabel("Inject Msg: ");
  62. onDisablegui.field = new JTextField();
  63. onDisablegui.field.setEditable(true);
  64. onDisablegui.field.setColumns(18);
  65. onDisablegui.messagetoinject = new JTextField();
  66. onDisablegui.messagetoinject.setEditable(true);
  67. onDisablegui.messagetoinject.setColumns(25);
  68. JButton selectButton = new JButton("Select");
  69. selectButton.setToolTipText("Select jar file");
  70. selectButton.addActionListener(new ActionListener() {
  71. @Override
  72. public void actionPerformed(ActionEvent e) {
  73. JFileChooser chooser = new JFileChooser();
  74. if (onDisablegui.field.getText() != null && !onDisablegui.field.getText().isEmpty()) {
  75. chooser.setSelectedFile(new File(onDisablegui.field.getText()));
  76. }
  77. chooser.setMultiSelectionDisabled(false);
  78. chooser.setFileSelectionMode(0);
  79. int result = chooser.showOpenDialog(onDisablegui);
  80. if (result == 0) {
  81. SwingUtilities.invokeLater(new Runnable() {
  82. @Override
  83. public void run() {
  84. onDisablegui.field.setText(chooser.getSelectedFile().getAbsolutePath());
  85. }
  86. });
  87. }
  88. }
  89. });
  90. JButton startButton = new JButton("Start");
  91. startButton.addActionListener(new ActionListener() {
  92. @Override
  93. public void actionPerformed(ActionEvent e) {
  94. if (onDisablegui.field.getText() == null || onDisablegui.field.getText().isEmpty() || !onDisablegui.field.getText().endsWith(".jar")) {
  95. JOptionPane.showMessageDialog(null, "You must select a valid jar file!", "Error", 0);
  96. return;
  97. }
  98. if (onDisablegui.messagetoinject.getText() == null || onDisablegui.messagetoinject.getText().isEmpty()) {
  99. JOptionPane.showMessageDialog(null, "You must enter a message to be injected!", "Error", 0);
  100. return;
  101. }
  102. File output = null;
  103. try {
  104. File input = new File(onDisablegui.field.getText());
  105. if (!input.getName().endsWith(".jar")) {
  106. throw new IllegalArgumentException("File must be a jar.");
  107. }
  108. if (!input.exists()) {
  109. throw new FileNotFoundException("The file " + input.getName() + " doesn't exist.");
  110. }
  111. output = new File(String.format("%s-Output.jar", input.getAbsolutePath().substring(0, input.getAbsolutePath().lastIndexOf("."))));
  112. if (output.exists()) {
  113. output.delete();
  114. }
  115. process(input, output, onDisablegui.messagetoinject.getText());
  116. checkFile(output);
  117. JOptionPane.showMessageDialog(null, "Done: " + output.getAbsolutePath(), "Done", 1);
  118. }
  119. catch (Throwable t) {
  120. JOptionPane.showMessageDialog(null, t, "Error", 0);
  121. t.printStackTrace();
  122. if (output != null) {
  123. output.delete();
  124. }
  125. }
  126. finally {
  127. SwingUtilities.invokeLater(new Runnable() {
  128. @Override
  129. public void run() {
  130. onDisablegui.field.setText("");
  131. onDisablegui.messagetoinject.setText("");
  132. }
  133. });
  134. }
  135. }
  136. });
  137. JPanel panel = new JPanel(new FlowLayout());
  138. panel.add(label);
  139. panel.add(onDisablegui.field);
  140. panel.add(selectButton);
  141. JPanel panel2 = new JPanel(new FlowLayout());
  142. panel2.add(startButton);
  143. JPanel panel3 = new JPanel(new FlowLayout());
  144. panel3.add(label2);
  145. panel3.add(onDisablegui.messagetoinject);
  146. JPanel border = new JPanel(new BorderLayout());
  147. border.add(panel, "North");
  148. border.add(panel3, "Center");
  149. border.add(panel2, "South");
  150. onDisablegui.getContentPane().add(border);
  151. onDisablegui.setVisible(true);
  152. }
  153. });
  154. }
  155.  
  156. private static void checkFile(File jarFile) throws Throwable {
  157. if (!jarFile.exists()) {
  158. throw new IllegalStateException("Output file not found.");
  159. }
  160. }
  161.  
  162. private static void writeToFile(ZipOutputStream outputStream, InputStream inputStream) throws Throwable {
  163. byte[] buffer = new byte[4096];
  164. try {
  165. while (inputStream.available() > 0) {
  166. int data = inputStream.read(buffer);
  167. outputStream.write(buffer, 0, data);
  168. }
  169. }
  170. finally {
  171. inputStream.close();
  172. outputStream.closeEntry();
  173. }
  174. }
  175.  
  176. private static void process(File jarFile, File outputFile, String message) throws Throwable {
  177. ZipFile zipFile = new ZipFile(jarFile);
  178. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  179. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
  180. try {
  181. while (entries.hasMoreElements()) {
  182. ZipEntry entry = (ZipEntry)entries.nextElement();
  183. if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
  184. try (InputStream in = zipFile.getInputStream(entry)) {
  185. ClassReader cr = new ClassReader(in);
  186. ClassNode classNode = new ClassNode();
  187. cr.accept(classNode, 0);
  188.  
  189. messageInjector(classNode, message);
  190.  
  191. ClassWriter cw = new ClassWriter(0);
  192. classNode.accept(cw);
  193. ZipEntry newEntry = new ZipEntry(entry.getName());
  194. newEntry.setTime(System.currentTimeMillis());
  195. out.putNextEntry(newEntry);
  196. writeToFile(out, new ByteArrayInputStream(cw.toByteArray()));
  197. }
  198. }
  199. else {
  200. entry.setTime(System.currentTimeMillis());
  201. out.putNextEntry(entry);
  202. writeToFile(out, zipFile.getInputStream(entry));
  203. }
  204. }
  205. }
  206. finally {
  207. zipFile.close();
  208. if (out != null) {
  209. out.close();
  210. }
  211. if (!messagegotinjected) {
  212. // throw new IllegalStateException("Could not find Bukkit onDisable or onLoad method.");
  213. throw new IllegalStateException("Could not find Bukkit onDisable method.");
  214. }
  215. }
  216. }
  217.  
  218. private static void messageInjector(ClassNode classNode, String message) throws Throwable {
  219. if (classNode.superName.equals("org/bukkit/plugin/java/JavaPlugin")) {
  220. for (MethodNode methodNode : classNode.methods) {
  221. if (methodNode.name.equals("onDisable") /* || methodNode.name.equals("onLoad") */) { // Meh, we don't really need to inject onLoad. Most people use onDisable anyways.
  222. InsnList instructions = new InsnList();
  223. instructions.add(new MethodInsnNode(184, "org/bukkit/Bukkit", "getConsoleSender", "()Lorg/bukkit/command/ConsoleCommandSender;", false));
  224. instructions.add(new LdcInsnNode(message));
  225. instructions.add(new MethodInsnNode(185, "org/bukkit/command/ConsoleCommandSender", "sendMessage", "(Ljava/lang/String;)V", true));
  226. methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), instructions);
  227. messagegotinjected = true;
  228. return;
  229. }
  230. }
  231. } // TODO: Bungee support
  232. }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement