utils.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { HOST_FILE_HASH_MAP } from "@/constants/constants";
  2. export function setUUID() {
  3. if (!localStorage.getItem("uuidForEcs")) {
  4. const uuidForEcs = "" + Date.now() + Math.random();
  5. localStorage.setItem("uuidForEcs", uuidForEcs);
  6. }
  7. }
  8. export function isElectron() {
  9. return typeof window.nodeRequire != "undefined";
  10. }
  11. export function existsSync(path: string) {
  12. const fs: typeof import("fs") = window.nodeRequire("fs");
  13. return fs.existsSync(path);
  14. }
  15. export function checkMainExe() {
  16. try {
  17. let iid: string = window.nodeRequire("process").pid;
  18. const cp: typeof import("child_process") =
  19. window.nodeRequire("child_process");
  20. iid = cp
  21. .execSync(
  22. `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get parentprocessid /value`
  23. )
  24. .toString();
  25. iid = iid.replace("ParentProcessId=", "").trim();
  26. console.log(iid);
  27. logger({
  28. cnl: ["local", "console", "server"],
  29. key: "checkMainExe",
  30. dtl: `iid1: ${iid}`,
  31. });
  32. iid = cp
  33. .execSync(
  34. `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get parentprocessid /value`
  35. )
  36. .toString();
  37. iid = iid.replace("ParentProcessId=", "").trim();
  38. logger({
  39. cnl: ["local", "console", "server"],
  40. key: "checkMainExe",
  41. dtl: `iid2: ${iid}`,
  42. });
  43. const executablePathBuffer = cp.execSync(
  44. `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get executablepath /value`
  45. );
  46. console.log(executablePathBuffer);
  47. const encoding = window.nodeRequire("encoding");
  48. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  49. let executablePath: string = encoding
  50. .convert(executablePathBuffer, "utf8", "gbk")
  51. .toString();
  52. logger({
  53. cnl: ["local", "console", "server"],
  54. key: "checkMainExe",
  55. dtl: executablePath,
  56. });
  57. executablePath = executablePath
  58. .replace("ExecutablePath=", "")
  59. .trim()
  60. .replace(/&/g, "&");
  61. if (executablePath === eval(`process.env.PORTABLE_EXECUTABLE_FILE`)) {
  62. const crypto: typeof import("crypto") = window.nodeRequire("crypto");
  63. const fs: typeof import("fs") = window.nodeRequire("fs");
  64. const getHash = crypto
  65. .createHmac("sha256", "abcdefg")
  66. .update(fs.readFileSync(executablePath))
  67. .digest("hex");
  68. console.log("the hash: ", getHash);
  69. logger({
  70. cnl: ["local", "console", "server"],
  71. key: "checkMainExe",
  72. dtl: `the hash: ${getHash}`,
  73. });
  74. if (HOST_FILE_HASH_MAP.get(window.location.hostname) === getHash) {
  75. return true;
  76. }
  77. }
  78. // check filepath executablePath md5
  79. } catch (error) {
  80. console.log(error);
  81. logger({
  82. cnl: ["local", "console", "server"],
  83. key: "checkMainExe",
  84. ejn: JSON.stringify(error),
  85. });
  86. }
  87. return false;
  88. }