import { HOST_FILE_HASH_MAP } from "@/constants/constants"; export function setUUID() { if (!localStorage.getItem("uuidForEcs")) { const uuidForEcs = "" + Date.now() + Math.random(); localStorage.setItem("uuidForEcs", uuidForEcs); } } export function isElectron() { return typeof window.nodeRequire != "undefined"; } export function existsSync(path: string) { const fs: typeof import("fs") = window.nodeRequire("fs"); return fs.existsSync(path); } export function checkMainExe() { try { let iid: string = window.nodeRequire("process").pid; const cp: typeof import("child_process") = window.nodeRequire("child_process"); iid = cp .execSync( `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get parentprocessid /value` ) .toString(); iid = iid.replace("ParentProcessId=", "").trim(); console.log(iid); logger({ cnl: ["local", "console", "server"], key: "checkMainExe", dtl: `iid1: ${iid}`, }); iid = cp .execSync( `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get parentprocessid /value` ) .toString(); iid = iid.replace("ParentProcessId=", "").trim(); logger({ cnl: ["local", "console", "server"], key: "checkMainExe", dtl: `iid2: ${iid}`, }); const executablePathBuffer = cp.execSync( `cmd /c chcp 65001>nul && C:\\Windows\\System32\\wbem\\wmic process where ^(processid^=${iid}^) get executablepath /value` ); console.log(executablePathBuffer); const encoding = window.nodeRequire("encoding"); // eslint-disable-next-line @typescript-eslint/no-unsafe-call let executablePath: string = encoding .convert(executablePathBuffer, "utf8", "gbk") .toString(); logger({ cnl: ["local", "console", "server"], key: "checkMainExe", dtl: executablePath, }); executablePath = executablePath .replace("ExecutablePath=", "") .trim() .replace(/&/g, "&"); if (executablePath === eval(`process.env.PORTABLE_EXECUTABLE_FILE`)) { const crypto: typeof import("crypto") = window.nodeRequire("crypto"); const fs: typeof import("fs") = window.nodeRequire("fs"); const getHash = crypto .createHmac("sha256", "abcdefg") .update(fs.readFileSync(executablePath)) .digest("hex"); console.log("the hash: ", getHash); logger({ cnl: ["local", "console", "server"], key: "checkMainExe", dtl: `the hash: ${getHash}`, }); if (HOST_FILE_HASH_MAP.get(window.location.hostname) === getHash) { return true; } } // check filepath executablePath md5 } catch (error) { console.log(error); logger({ cnl: ["local", "console", "server"], key: "checkMainExe", ejn: JSON.stringify(error), }); } return false; }