Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package me.absurd.oxalixhub.license;
  2.  
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.time.chrono.HijrahEra;
  6. import java.util.Scanner;
  7. import java.util.UUID;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.plugin.Plugin;
  11.  
  12. public class AdvancedLicense {
  13.  
  14. private String licenseKey;
  15. private Plugin plugin;
  16. private String validationServer;
  17. private LogType logType = LogType.NORMAL;
  18. private String securityKey = "YecoF0I6M05thxLeokoHuW8iUhTdIUInjkfF";
  19. private boolean debug = false;
  20.  
  21. public AdvancedLicense(String licenseKey, String validationServer, Plugin plugin){
  22. this.licenseKey = licenseKey;
  23. this.plugin = plugin;
  24. this.validationServer = validationServer;
  25. }
  26.  
  27. public AdvancedLicense setSecurityKey(String securityKey){
  28. this.securityKey = securityKey;
  29. return this;
  30. }
  31.  
  32. public AdvancedLicense setConsoleLog(LogType logType){
  33. this.logType = logType;
  34. return this;
  35. }
  36.  
  37. public AdvancedLicense debug(){
  38. debug = true;
  39. return this;
  40. }
  41.  
  42. public boolean register(){
  43. log(0, "[]==========[License-System]==========[]");
  44. log(0, "Connecting to License-Server...");
  45. ValidationType vt = isValid();
  46. if(vt == ValidationType.VALID){
  47. log(1, "License valid!");
  48. log(0, "[]==========[License-System]==========[]");
  49. return true;
  50. }else{
  51. log(1, "License is NOT valid!");
  52. log(1, "Failed as a result of "+vt.toString());
  53. log(1, "Disabling plugin!");
  54. log(0, "[]==========[License-System]==========[]");
  55.  
  56.  
  57. Bukkit.getScheduler().cancelTasks(plugin);
  58. Bukkit.getPluginManager().disablePlugin(plugin);
  59. return false;
  60. }
  61. }
  62.  
  63. public boolean isValidSimple(){
  64. return (isValid() == ValidationType.VALID);
  65. }
  66.  
  67. public ValidationType isValid(){
  68. String rand = toBinary(UUID.randomUUID().toString());
  69. String sKey = toBinary(securityKey);
  70. String key = toBinary(licenseKey);
  71.  
  72. try{
  73. URL url = new URL(validationServer+"?v1="+xor(rand, sKey)+"&v2="+xor(rand, key)+"&pl="+plugin.getName());
  74. if(debug) System.out.println("RequestURL -> "+url.toString());
  75. Scanner s = new Scanner(url.openStream());
  76. if(s.hasNext()){
  77. String response = s.next();
  78. s.close();
  79. try{
  80. return ValidationType.valueOf(response);
  81. }catch(IllegalArgumentException exc){
  82. String respRand = xor(xor(response, key), sKey);
  83. if(rand.substring(0, respRand.length()).equals(respRand)) return ValidationType.VALID;
  84. else return ValidationType.WRONG_RESPONSE;
  85. }
  86. }else{
  87. s.close();
  88. return ValidationType.PAGE_ERROR;
  89. }
  90. }catch(IOException exc){
  91. if(debug) exc.printStackTrace();
  92. return ValidationType.URL_ERROR;
  93. }
  94. }
  95.  
  96.  
  97. //
  98. // Cryptographic
  99. //
  100.  
  101. private static String xor(String s1, String s2){
  102. String s0 = "";
  103. for(int i = 0; i < (s1.length() < s2.length() ? s1.length() : s2.length()) ; i++) s0 += Byte.valueOf(""+s1.charAt(i))^Byte.valueOf(""+s2.charAt(i));
  104. return s0;
  105. }
  106.  
  107. //
  108. // Enums
  109. //
  110.  
  111. public enum LogType{
  112. NORMAL, LOW, NONE;
  113. }
  114.  
  115. public static enum ValidationType{
  116. WRONG_RESPONSE, PAGE_ERROR, URL_ERROR, KEY_OUTDATED, KEY_NOT_FOUND, NOT_VALID_IP, INVALID_PLUGIN, VALID;
  117. }
  118.  
  119. //
  120. // Binary methods
  121. //
  122.  
  123. private String toBinary(String s){
  124. byte[] bytes = s.getBytes();
  125. StringBuilder binary = new StringBuilder();
  126. for (byte b : bytes)
  127. {
  128. int val = b;
  129. for (int i = 0; i < 8; i++)
  130. {
  131. binary.append((val & 128) == 0 ? 0 : 1);
  132. val <<= 1;
  133. }
  134. }
  135. return binary.toString();
  136. }
  137.  
  138. //
  139. // Console-Log
  140. //
  141.  
  142. private void log(int type, String message){
  143. if(logType == LogType.NONE || ( logType == LogType.LOW && type == 0 )) return;
  144. System.out.println(message);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement