import envConfig from '../config/envConfig' import { getRootPath } from '../utils/index' const { exec, execSync } = require('child_process') const iconv = require('iconv-lite') const path = require('path') const fs = require('fs') async function getHookPaths(): Promise<{ exePath: string dllPath: string }> { let exePath, dllPath const root_path = await getRootPath() if (envConfig.env == 'production') { try { // __dirname: __dirname 获取当前文件所在目录的绝对路径 const asarPath = path.join(__dirname, '..', '..') // const extractPath = path.join(__dirname, '..', '..', '..', 'unpacked') exePath = path.join(root_path, 'localServer', 'tools', 'inject_tool.exe') dllPath = path.join(root_path, 'localServer', 'tools', 'vworkApi.dll') if (!fs.existsSync(exePath) || !fs.existsSync(dllPath)) { // asar.extractAll(asarPath, root_path) execSync(`asar extract ${asarPath} ${root_path}`) } } catch (err) { console.log('err', err) } } else { // path.resolve 将相对路径解析为绝对路径 exePath = path.resolve('./localServer/tools', 'inject_tool.exe') dllPath = path.resolve('./localServer/tools', 'vworkApi.dll') } return { exePath, dllPath } } function formatDecode(str): string { return iconv.decode(str, 'gbk') } async function indectDll(): Promise { const { exePath } = await getHookPaths() return new Promise((resolve, reject) => { // 使用解压后的路径访问文件 // const execCommand = `${exePath} ${dllPath} ${PID}` const execCommand = `${exePath} start 6666 --my_port=7777 --key=9tJcPEVa/YhDRWAX7dSSHKC+j8gCisHaDm2X9mEi/QsDZ7jeQV+NHqKtYIFpUwrS` // console.log('execCommand', execCommand) exec(execCommand, { encoding: 'buffer' }, (error, stdout, stderr) => { try { // console.log('stdout', formatDecode(stdout)) if (formatDecode(stdout).includes('注入成功')) { resolve('success') } else { // reject('没有找到企业微信进程或企业微信没有启动') console.log('-----启动失败---', formatDecode(stderr), execCommand) if (stderr) { throw formatDecode(stderr) } if (error) { throw formatDecode(error.message) } } } catch (error) { reject(error) } }) }) } function killProcess(): Promise { // TASKKILL命令本身 /F强制 /IM映像名称 WXWork.exe /T终止 return new Promise((resolve, reject) => { exec(`taskkill /F /IM WXWork.exe /T`, (err, stdout, stderr) => { if (err) { console.log('执行命令时出错:', err) reject(err) return } if (stderr) { console.log('命令执行产生了错误输出:', err) reject(stderr) return } resolve(stdout) }) }) } // 初始化hook const initHook = async (): Promise => { try { // const PID = await getProcessId() await indectDll() } catch (error) { throw new Error(String(error)) } } export default { initHook, killProcess }