فهرست منبع

electron 本地检测

Michael Wang 3 سال پیش
والد
کامیت
ef73a12841
1فایلهای تغییر یافته به همراه142 افزوده شده و 0 حذف شده
  1. 142 0
      src/utils/nativeExe.ts

+ 142 - 0
src/utils/nativeExe.ts

@@ -0,0 +1,142 @@
+import { xor } from "lodash-es";
+
+export default function checkRemote(
+  exeName: string,
+  cb: () => Promise<void>
+): Promise<void> {
+  if (typeof window.nodeRequire == "undefined") {
+    logger({
+      pgu: "AUTO",
+      cnl: ["local", "server"],
+      dtl: "不在Electron中,调用 " + exeName + " 失败",
+    });
+    throw new Error("不在Electron中,调用 fs 失败");
+  }
+  return new Promise<void>((resolve) => {
+    // eslint-disable-next-line
+    window
+      .nodeRequire("node-cmd")
+      .get(
+        "cmd /c chcp 65001>nul && " + exeName,
+        async (err: Error, data: string, stderr: string) => {
+          console.log(exeName, err, data); // 未免过多日志,此处后续可以关闭
+          logger({
+            pgu: "AUTO",
+            act: exeName + " called",
+            ejn: JSON.stringify(err),
+            dtl: data,
+            ext: {
+              stderr: JSON.stringify(stderr),
+              absPath: exeName.includes(":"),
+            },
+          });
+          // 如果相对路径没找到,则通过绝对路径来执行
+          if (!exeName.includes(":") && err) {
+            const remote = window.nodeRequire("electron").remote;
+            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+            const fs: typeof import("fs") = remote.require("fs");
+
+            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+            const path: typeof import("path") = remote.require("path");
+
+            const [exePath, exeParams] = exeName.split(" ");
+            // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+            const absPath = path.join(
+              // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+              remote.app.getAppPath() as string,
+              "../../",
+              exePath
+            );
+            if (fs.existsSync(absPath)) {
+              try {
+                await checkRemote([absPath, exeParams].join(" ").trim(), cb);
+              } catch (error) {
+                console.log("second try error", absPath);
+                logger({
+                  cnl: ["local", "server"],
+                  dtl: "second try error: " + absPath,
+                });
+              }
+            }
+          }
+          await new Promise<void>((resolve2) =>
+            setTimeout(() => resolve2(), 1000)
+          );
+          try {
+            await cb();
+          } catch (e) {
+            console.log("call cb failed", e);
+            logger({
+              cnl: ["local", "server"],
+              dtl: "call cb failed: " + e,
+            });
+          } finally {
+            resolve();
+          }
+        }
+      );
+  });
+}
+
+export function fileExists(file: string): boolean {
+  if (typeof window.nodeRequire == "undefined") {
+    logger({
+      pgu: "AUTO",
+      cnl: ["local", "server"],
+      dtl: "不在Electron中,调用 fs 失败",
+    });
+    throw new Error("不在Electron中,调用 fs 失败");
+  }
+  // eslint-disable-next-line
+  return window.nodeRequire("fs").existsSync(file);
+}
+
+export function nodeCheckRemoteDesktop() {
+  logger({
+    pgu: "AUTO",
+    cnl: ["local", "server"],
+    key: "nodeCheckRemoteDesktop",
+  });
+
+  // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+  const appList: string = window
+    .nodeRequire("child_process")
+    .execSync("cmd /c chcp 65001>nul && tasklist /FO CSV")
+    .toString();
+  // console.debug(appList);
+
+  const regex = new RegExp("sunloginclient|选择免安装运行,截图识别", "gi");
+  return !!(appList?.match(regex) || []).length;
+}
+
+let previousAppList: string[] = [];
+export function nodeCheckProcess() {
+  try {
+    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+    const appListCP: string = window
+      .nodeRequire("child_process")
+      .execSync("cmd /c chcp 65001>nul && tasklist /FO CSV")
+      .toString();
+    // 不能打印,electron-log 不能接收
+    // console.log(appList);
+    let appList = appListCP.split("\r\n");
+    appList.shift();
+    appList = appList.map((v) => v.split(",")[0]);
+    const xorRes = xor(previousAppList, appList);
+    if (xorRes && xorRes.length > 0) {
+      logger({
+        pgu: "AUTO",
+        cnl: ["local", "server"],
+        key: "nodeCheckProcess",
+        dtl: xorRes.join(" ||| "),
+      });
+    }
+    previousAppList = appList;
+  } catch (error) {
+    logger({
+      cnl: ["console", "local", "server"],
+      pgu: "AUTO",
+      ejn: JSON.stringify(error),
+    });
+  }
+}