tmp.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package jrebel;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.net.URI;
  9. import java.net.http.HttpClient;
  10. import java.net.http.HttpRequest;
  11. import java.net.http.HttpResponse;
  12. import java.util.*;
  13. public class JrebelMain {
  14. private final static String url = "https://headless.zeroturnaround.com/public/api/registrations/add-jrebel-evaluation.php";
  15. public static void main(String[] args) {
  16. JFrame jf = new JFrame("JrebelEvaluation");
  17. jf.setSize(240, 320);
  18. jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  19. jf.setLocationRelativeTo(null);
  20. // 创建内容面板,指定使用 流式布局
  21. JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 100, 5));
  22. JButton button = new JButton();
  23. button.setText("Get License");
  24. button.addActionListener(new JrebelMain.ButtonClick());
  25. panel.add(button);
  26. jf.setContentPane(panel);
  27. jf.setVisible(true); // PS: 最后再设置为可显示(绘制), 所有添加的组件才会显示
  28. }
  29. public static int randNumber(int max, int min) {
  30. return min + (int) (Math.random() * (max - min + 1));
  31. }
  32. public static String uuid() {
  33. return UUID.randomUUID().toString().replaceAll("-", "");
  34. }
  35. public static String subText(String source, String startText, String endText, int offSet) {
  36. int start = source.indexOf(startText, offSet) + 1;
  37. if (start == -1) {
  38. return null;
  39. }
  40. int end = source.indexOf(endText, start + offSet + startText.length() - 1);
  41. if (end == -1) {
  42. end = source.length();
  43. }
  44. return source.substring(start + startText.length() - 1, end);
  45. }
  46. static class ButtonClick implements ActionListener {
  47. @Override
  48. public void actionPerformed(ActionEvent e) {
  49. try {
  50. action();
  51. } catch (Exception ex) {
  52. ex.printStackTrace();
  53. }
  54. }
  55. }
  56. private static void action() throws Exception {
  57. final File jrebelHome = new File(System.getProperty("user.home") + "/.jrebel");
  58. final String[] jrebelFiless = new String[]{
  59. "jrebel.prefs",
  60. "jrebel.prefs.lock",
  61. "jrebel.properties"
  62. };
  63. // 删除 jrebel 配置文件
  64. Arrays.stream(jrebelFiless).map(it -> new File(jrebelHome.getAbsolutePath() + "/" + it)).filter(it -> it.exists()).forEach(it -> it.delete());
  65. //write license jrebel.lic
  66. StringBuffer phone = new StringBuffer();
  67. for (int i = 0; i < randNumber(6, 11); i++) {
  68. phone.append(randNumber(0, 9));
  69. }
  70. Map<String, Object> body = new HashMap<>() {{
  71. put("referer_url", "IDE");
  72. put("email", uuid() + "%40qq.com");
  73. put("first_name", uuid().substring(0, randNumber(3, 5)));
  74. put("last_name", uuid().substring(0, randNumber(3, 6)));
  75. put("phone", phone.toString());
  76. put("organization", uuid().substring(0, randNumber(1, 5)));
  77. put("output_format", "json");
  78. put("client_os", "Windows+11");
  79. put("guid", uuid());
  80. put("jrebel-version", "2023.1.2");
  81. put("ide", "intellij");
  82. put("ide-product", "IU");
  83. put("ide-version", "2022.3.3");
  84. put("jvm.version", "17.0." + randNumber(0, 20));
  85. put("jvm.vendor", "JetBrains+s.r.o");
  86. put("os.name", "Windows+11");
  87. }};
  88. String queryText = String.join("&", body.entrySet().stream().map(it -> it.getKey() + "=" + it.getValue()).toArray(String[]::new));
  89. final URI uri = URI.create(url + "?" + queryText);
  90. final HttpClient httpClient = HttpClient.newBuilder()
  91. .build();
  92. final HttpRequest request = HttpRequest.newBuilder()
  93. .version(HttpClient.Version.HTTP_1_1)
  94. .GET()
  95. .uri(uri)
  96. .build();
  97. String ret = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
  98. String content = subText(ret, "content\":\"", "\"", 0);
  99. System.out.println(content);
  100. byte[] bin = Base64.getMimeDecoder().decode(content);
  101. File jrebelLicFile = new File(jrebelHome.getAbsolutePath() + "/jrebel.lic");
  102. FileOutputStream jrebelLicOutputStream = new FileOutputStream(jrebelLicFile);
  103. jrebelLicOutputStream.write(bin);
  104. jrebelLicOutputStream.flush();
  105. jrebelLicOutputStream.close();
  106. //write jrebel.properties
  107. FileOutputStream jrebelPropertiesOutputStream = new FileOutputStream(jrebelHome.getAbsolutePath() + "/jrebel.properties");
  108. jrebelPropertiesOutputStream.write(("rebel.license=" + jrebelLicFile.getAbsolutePath() + "\r\n").getBytes());
  109. jrebelPropertiesOutputStream.write(("rebel.preferred.license=0\r\n").getBytes());
  110. jrebelPropertiesOutputStream.write(("rebel.properties.version=2\r\n").getBytes());
  111. jrebelPropertiesOutputStream.flush();
  112. jrebelPropertiesOutputStream.close();
  113. JOptionPane.showMessageDialog(null, "please restart idea , license : \n" + jrebelLicFile.getAbsolutePath());
  114. }
  115. }