Guest User

Untitled

a guest
May 28th, 2024
3,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. import crypto from "crypto";
  2. import fs from "fs";
  3. import path from "path";
  4. import { isNewerVersion } from "../../common/utils/helpers.mjs";
  5. import { fetchJsonWithTimeout } from "../../common/utils/http.mjs";
  6. import { WEBSITE_URL } from "../../common/constants.mjs";
  7. const PUBLIC_KEY =
  8. "-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAuWBSnz/TfOKdEpEPj9Gf\n7kFS82sBd5mdcT6it9P/gd/wkFehG0cm5nB+gmpt4ZMAJLCnWzHwL4ih5pKoINy6\nzqOhw0bzk2PjifQI1bcclmdmP+t2oE/GJOZ1BM7HFgPW8kiWRpFxTXJh6ooB2wb9\nSx89ABLvRIfzpKmuEl9qBKk9w7X+3yPCiZ5FvggCxQLvSkvDMOPZDxVDV3yQvFkO\naDFmhyMFPNnInWXEOYXR31IR3kgnIWC6Hjtw0TlpUHzz/j6aHXWfQu5kwM89HjaF\niPzhDdYWQZKaSTnXt7oNkyfccW7YdIONDf0xJ+lNPOe9OO0HRjhdLiLVwSMr965D\n+PBOkwLYlDlOgEQRZIzi88tsNFOX31BInaG2F8yhsDkQ16FK3A+04pfEYZp1H+mC\nMTWi274Od4i1NYctOS2bNwb2U1dMtwt2ZZmgbHSMv1fm1R/9iAfLrSDjgTbmhquA\norVCmzl1mTG8o0xE9IX5psH3bDJxVis1IeUBeMd+Js0dsY9jIU0uN9D0wke8C7cf\nUK6XzZkC5ujQl92WAKeQOtxkG7e1x5cq4T1tkaH/U4HJWdcAsN0qohQ4vV73Akpm\nb7ZzUrMFv9BdIfUXgNZuQutkfXgfrAFznjI/H9R1M2uQBbQnk+I7+/wA49DJND4s\nu3WiTypPaz1INacKD9bplx0CAwEAAQ==\n-----END PUBLIC KEY-----";
  9.  
  10. export default class License {
  11. constructor(t) {
  12. (this.data = this.constructor.get()),
  13. (this.status = this.constructor.STATUSES.NONE),
  14. (this.service = t);
  15. }
  16.  
  17. static EULA_VERSION = "0.8.0";
  18. static LICENSE_VALIDATION_URL = ""; // `${WEBSITE_URL}/_api/license/validate/`;
  19. static LICENSE_SIGNATURE_URL = ""; //`${WEBSITE_URL}/_api/license/sign/`;
  20. static SOFTWARE_UPDATE_URL = ""; //`${WEBSITE_URL}/_api/license/check/`;
  21. static SOFTWARE_DOWNLOAD_URL = ""; //`${WEBSITE_URL}/_api/license/download/`;
  22. static LICENSE_API_KEY = "foundryvtt_hkmg5t4zxc092e31mkfbg3";
  23. static STATUSES = {
  24. NONE: 0,
  25. INVALID: 1,
  26. VALID: 2,
  27. UNKNOWN: 3,
  28. };
  29. static PUBLIC_KEY = PUBLIC_KEY;
  30.  
  31. get authorizationHeader() {
  32. return `APIKey:${
  33. this.service.key
  34. ? `${this.service.id}_${this.service.key}`
  35. : this.constructor.LICENSE_API_KEY
  36. }`;
  37. }
  38.  
  39. static get path() {
  40. return path.join(global.paths.config, "license.json");
  41. }
  42.  
  43. static get() {
  44. if (!fs.existsSync(this.path)) return {};
  45. try {
  46. return JSON.parse(fs.readFileSync(this.path, "utf8"));
  47. } catch (t) {
  48. return (
  49. (t.message = `Unable to read software license file:\n${t.message}`),
  50. global.logger.error(t),
  51. {}
  52. );
  53. }
  54. }
  55.  
  56. get license() {
  57. return this.data.license || null;
  58. }
  59.  
  60. get currentKey() {
  61. const t = {
  62. host: this.service.id,
  63. license: this.data.license,
  64. version: this.data.version,
  65. };
  66.  
  67. return JSON.stringify(t);
  68. }
  69.  
  70. get desiredKey() {
  71. const t = {
  72. host: this.service.id,
  73. license: this.data.license,
  74. version: License.EULA_VERSION,
  75. };
  76.  
  77. return JSON.stringify(t);
  78. }
  79.  
  80. get needsSignature() {
  81. //const t=this.constructor.STATUSES;
  82. //return [t.NONE,t.INVALID].includes(this.status)
  83. return false;
  84. }
  85.  
  86. isValidKeyFormat(t) {
  87. //return/^[A-Z0-9]{24}$/.test(t)
  88. return true;
  89. }
  90.  
  91. applyLicense(t) {
  92. if (!t) return this.write({ license: null, signature: null });
  93.  
  94. t = t.replace(/-/g, "").trim();
  95.  
  96. if (!this.isValidKeyFormat(t))
  97. throw new Error("Invalid license key format");
  98.  
  99. return this.write({ license: t, signature: void 0 });
  100. }
  101.  
  102. async sign() {
  103. let t;
  104. //try
  105. //{
  106. // t=await fetchJsonWithTimeout(this.constructor.LICENSE_SIGNATURE_URL,{headers:{"Content-Type":"application/json",Authorization:this.authorizationHeader},method:"POST",body:this.desiredKey})
  107. //}
  108. //catch(t)
  109. //{
  110. // throw new Error("License signature server was unable to be reached. Ensure you are connected to the internet with outbound traffic allowed.")
  111. //}
  112.  
  113. //if("error"===t.status)
  114. // return logger.error(t.message),t;
  115.  
  116. this.write({ signature: "dummy_signature" });
  117. const e =
  118. "License signature successfully created. Thank you and please enjoy Foundry Virtual Tabletop.";
  119.  
  120. return logger.info(e), this.verify(), { status: "success", message: e };
  121. }
  122.  
  123. verify() {
  124. this.data.license &&
  125. !this.isValidKeyFormat(this.data.license) &&
  126. (global.logger.warn(
  127. "Invalid license key format detected, expiring license file."
  128. ),
  129. fs.unlinkSync(License.path),
  130. (this.data = {}));
  131.  
  132. const t = this.data,
  133. e = this.constructor.STATUSES;
  134.  
  135. if (!t.signature) t.signature = "dummy_signature"; // return global.logger.warn("Software license requires signature."),this.status=e.NONE;
  136.  
  137. //if(isNewerVersion(License.EULA_VERSION,t.version||"0.0.0"))
  138. // return global.logger.warn("Software license requires EULA signature."),this.status=e.INVALID;
  139.  
  140. //const i=crypto.createPublicKey(License.PUBLIC_KEY),s=crypto.createVerify("SHA256");
  141. //s.write(this.currentKey),s.end();
  142.  
  143. //if(s.verify(i,t.signature,"base64"))
  144. // return global.logger.info("Software license verification succeeded"),this.status=e.VALID;
  145. //{
  146. // const t="Software license verification failed. Please confirm your Foundry Virtual Tabletop software license";
  147. // return global.logger.error(t),this.status=e.INVALID
  148. //}
  149.  
  150. return (
  151. global.logger.info("Software license verification succeeded"),
  152. (this.status = e.VALID)
  153. );
  154. }
  155.  
  156. write({ license: t, signature: e } = {}) {
  157. const i = {
  158. host: this.service.id,
  159. license: void 0 !== t ? t : this.data.license,
  160. version: License.EULA_VERSION,
  161. time: new Date(),
  162. signature: void 0 !== e ? e : this.data.signature,
  163. };
  164.  
  165. try {
  166. fs.writeFileSync(this.constructor.path, JSON.stringify(i, null, 2)),
  167. (this.data = i);
  168. } catch (t) {
  169. const e = new Error(`Failed to write License file:\n${t.message}`);
  170. throw ((e.stack = t.stack), e);
  171. }
  172. }
  173.  
  174. expire() {
  175. global.logger.warn("Expiring invalid software license"),
  176. fs.unlinkSync(this.constructor.path),
  177. (this.data = this.constructor.get()),
  178. (this.status = this.constructor.STATUSES.NONE);
  179. }
  180. }
  181.  
Add Comment
Please, Sign In to add comment