import CONFIG from './const'; import mkdirp from 'mkdirp'; import fs from 'fs'; import path from 'path'; import sudo from 'sudo-prompt'; import { clipboard, dialog } from 'electron'; export function checkCertInstalled() { return fs.existsSync(CONFIG.INSTALL_CERT_FLAG); } export async function installCert(checkInstalled = true) { if (checkInstalled && checkCertInstalled()) { return; } mkdirp.sync(path.dirname(CONFIG.INSTALL_CERT_FLAG)); if (process.platform === 'darwin') { return new Promise((resolve, reject) => { clipboard.writeText( `echo "输入本地登录密码" && sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain "${CONFIG.CERT_PUBLIC_PATH}" && touch ${CONFIG.INSTALL_CERT_FLAG} && echo "安装完成"`, ); dialog.showMessageBoxSync({ type: 'info', message: `命令已复制到剪贴板,粘贴命令到终端并运行以安装并信任证书`, }); reject(); }); } else { return new Promise((resolve, reject) => { sudo.exec( `${CONFIG.WIN_CERT_INSTALL_HELPER} -c -add ${CONFIG.CERT_PUBLIC_PATH} -s root`, { name: CONFIG.APP_EN_NAME }, (error, stdout) => { if (error) { reject(error); } else { fs.writeFileSync(CONFIG.INSTALL_CERT_FLAG, ''); resolve(stdout); } }, ); }); } }