|
|
@@ -0,0 +1,827 @@
|
|
|
+// Modules to control application life and create native browser window
|
|
|
+const {app, ipcMain, BrowserWindow, session} = require('electron')
|
|
|
+const path = require('path')
|
|
|
+const fs = require('fs')
|
|
|
+const iconv = require('iconv-lite');
|
|
|
+const request = require('request');
|
|
|
+const download = require('download');
|
|
|
+// 读取配置文件
|
|
|
+let enterURL = 'https://demos.run/debuger/index.html'
|
|
|
+let webConfig = {
|
|
|
+ width: 376,
|
|
|
+ height: 667,
|
|
|
+ webPreferences: {
|
|
|
+ webSecurity: false,
|
|
|
+ contextIsolation: true,
|
|
|
+ nodeIntegration: false,
|
|
|
+ allowRunningInsecureContent: true, // 允许不安全内容
|
|
|
+ plugins: true,
|
|
|
+ scrollBounce: true,
|
|
|
+ preload: path.join(__dirname, "preload.js")
|
|
|
+ },
|
|
|
+ autoHideMenuBar: true
|
|
|
+ // 无边框
|
|
|
+ // frame: false,
|
|
|
+ // 全屏
|
|
|
+ // fullscreen: true
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// 判断是否有特殊配置文件
|
|
|
+console.log(__dirname + "\\config.json")
|
|
|
+if (fs.existsSync("./config.json")) {
|
|
|
+ let configStr = fs.readFileSync('./config.json', 'utf-8')
|
|
|
+ // 特殊符号表示运行目录
|
|
|
+ configStr = configStr.replaceAll('<dir>', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
|
|
|
+ webConfig = JSON.parse(configStr)
|
|
|
+ enterURL = webConfig.enterURL
|
|
|
+}
|
|
|
+console.log(webConfig)
|
|
|
+const codeMap = {"fc":"[re]","ep":"[Af]","rj":"[M_]","sp":"[sW]","ws":"[Pj]","mb":"[^~]","ww":"[Dp]","wh":"[ZH]","ph":"[b+]","hk":"[3b]","mc":"[%)]","fm":"[$4]","nm":"[T!]","ei":"[J3]","pd":"[(A]","ef":"[%t]","xf":"[n_]","na":"[W6]","mr":"[dn]","km":"[b*]","aw":"[#*]","sj":"[~6]","ry":"[t#]","sd":"[$R]","eh":"[!!]","wp":"[TE]","fy":"[s6]","ex":"[EE]","ce":"[PS]","xr":"[~z]","cj":"[xh]","am":"[(G]","kw":"[Nr]","hj":"[p@]","ia":"[jO]","mp":"[75]","py":"[6C]","hc":"[46]","sk":"[(8]","hp":"[SB]","my":"[pq]","wk":"[Xd]","bk":"[Q^]","ak":"[)J]","cw":"[ai]","ym":"[Te]","yh":"[Cd]","xb":"[R5]","yy":"[#H]","nt":"[4)]","bc":"[#J]","fe":"[2+]","ni":"[f@]","bb":"[!k]","jc":"[$Q]","an":"[m$]","ee":"[RH]","nn":"[n$]","jr":"[5F]","pp":"[JQ]","fx":"[86]","2":"[)h]","3":"[iL]","4":"[r2]","5":"[Ys]","6":"[7p]","7":"[!5]","8":"[@A]","A":"[_W]","B":"[Kt]","C":"[m#]","D":"[A!]","E":"[M!]","F":"[xG]","G":"[k@]","H":"[_!]","J":"[rP]","K":"[z#]","M":"[r$]","N":"[rN]","P":"[t$]","Q":"[3(]","R":"[fF]","S":"[H)]","T":"[J@]","W":"[83]","X":"[t5]","Y":"[T_]","Z":"[CT]","a":"[Jt]","b":"[Ks]","c":"[yn]","d":"[2r]","e":"[#2]","f":"[yM]","h":"[)m]","i":"[mx]","j":"[YV]","k":"[$j]","m":"[Xy]","n":"[Bk]","p":"[5$]","r":"[EH]","s":"[Pw]","t":"[j(]","w":"[p7]","x":"[a+]","y":"[B2]","z":"[4n]","~":"[~C]","!":"[iw]","@":"[SK]","#":"[Pf]","$":"[de]","%":"[3t]","^":"[H_]","&":"[WA]","*":"[!A]","(":"[z*]",")":"[)n]","_":"[&k]","+":"[*F]"}
|
|
|
+
|
|
|
+function owoReplaceAll(str, s1, s2) {
|
|
|
+ while (str.indexOf(s1) >= 0) {
|
|
|
+ str = str.replace(s1, s2)
|
|
|
+ }
|
|
|
+ return str
|
|
|
+}
|
|
|
+
|
|
|
+function owoDecode(itemStr) {
|
|
|
+ for (let item in codeMap) {
|
|
|
+ itemStr = owoReplaceAll(itemStr, codeMap[item], item)
|
|
|
+ }
|
|
|
+ while (itemStr.indexOf(']') >= 0) {
|
|
|
+ itemStr = owoDecode(itemStr)
|
|
|
+ }
|
|
|
+ return itemStr
|
|
|
+}
|
|
|
+
|
|
|
+let mainWindow = null
|
|
|
+let preLoadCode = `
|
|
|
+ var owoApp = 5
|
|
|
+ window.owoPC = true
|
|
|
+ window.electronConfig = ${JSON.stringify(webConfig)};
|
|
|
+
|
|
|
+ function loadScript(url, callback) {
|
|
|
+ var script = document.createElement("script")
|
|
|
+ script.type = "text/javascript";
|
|
|
+ if (script.readyState) { //IE
|
|
|
+ script.onreadystatechange = function () {
|
|
|
+ if (script.readyState == "loaded" || script.readyState == "complete") {
|
|
|
+ script.onreadystatechange = null;
|
|
|
+ if (callback) callback();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ } else { //Others
|
|
|
+ script.onload = function () {
|
|
|
+ if (callback) callback();
|
|
|
+ };
|
|
|
+ }
|
|
|
+ script.src = url;
|
|
|
+ var head = document.head || document.getElementsByTagName('head')[0];
|
|
|
+ head.appendChild(script);
|
|
|
+ }
|
|
|
+ function loadJsCode(code){
|
|
|
+ var script = document.createElement('script');
|
|
|
+ script.type = 'text/javascript';
|
|
|
+ //for Chrome Firefox Opera Safari
|
|
|
+ script.appendChild(document.createTextNode(code));
|
|
|
+ //for IE
|
|
|
+ //script.text = code;
|
|
|
+ document.body.appendChild(script);
|
|
|
+ }
|
|
|
+ function loadCSS (url) {
|
|
|
+ var link = document.createElement("link");
|
|
|
+ link.rel = "stylesheet";
|
|
|
+ link.type = "text/css";
|
|
|
+ link.href = url;
|
|
|
+ document.getElementsByTagName("head")[0].appendChild(link);
|
|
|
+ }
|
|
|
+
|
|
|
+ loadScript('https://cunchu.site/app/main.js');
|
|
|
+`
|
|
|
+
|
|
|
+// 拦截数据
|
|
|
+function setupDebuggerInterceptor(webContents, interceptor) {
|
|
|
+ webContents.debugger.attach('1.3');
|
|
|
+
|
|
|
+ const pendingRequests = new Map();
|
|
|
+
|
|
|
+ webContents.debugger.on('message', (event, method, params) => {
|
|
|
+ // 存储请求信息
|
|
|
+ if (method === 'Network.requestWillBeSent') {
|
|
|
+ pendingRequests.set(params.requestId, {
|
|
|
+ url: params.request.url,
|
|
|
+ method: params.request.method,
|
|
|
+ headers: params.request.headers,
|
|
|
+ postData: params.request.postData,
|
|
|
+ timestamp: Date.now()
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应接收时记录信息
|
|
|
+ if (method === 'Network.responseReceived') {
|
|
|
+ const { requestId, response } = params;
|
|
|
+ let hookData = false;
|
|
|
+
|
|
|
+ interceptor.forEach(hookURL => {
|
|
|
+ if (response.url.includes(hookURL) || response.url === hookURL) {
|
|
|
+ hookData = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (hookData) {
|
|
|
+ console.log('拦截到响应:', response.url, response.status);
|
|
|
+
|
|
|
+ // 存储响应信息,等待 loadingFinished
|
|
|
+ const requestInfo = pendingRequests.get(requestId) || {};
|
|
|
+ pendingRequests.set(requestId, {
|
|
|
+ ...requestInfo,
|
|
|
+ responseReceived: true,
|
|
|
+ response,
|
|
|
+ hookData: true
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 在资源加载完成时获取响应体
|
|
|
+ if (method === 'Network.loadingFinished') {
|
|
|
+ const { requestId } = params;
|
|
|
+ const requestInfo = pendingRequests.get(requestId);
|
|
|
+
|
|
|
+ if (requestInfo && requestInfo.hookData) {
|
|
|
+ // 延迟获取响应体,确保数据可用
|
|
|
+ setTimeout(() => {
|
|
|
+ webContents.debugger.sendCommand('Network.getResponseBody', { requestId })
|
|
|
+ .then(({ body }) => {
|
|
|
+ // 获取请求体(如果有)
|
|
|
+ let requestBody = null;
|
|
|
+ if (requestInfo.postData) {
|
|
|
+ try {
|
|
|
+ // 尝试解析为JSON,如果不是JSON则保持原样
|
|
|
+ requestBody = JSON.parse(requestInfo.postData);
|
|
|
+ } catch (e) {
|
|
|
+ requestBody = requestInfo.postData;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const interceptedData = {
|
|
|
+ url: requestInfo.response.url,
|
|
|
+ method: requestInfo.method,
|
|
|
+ statusCode: requestInfo.response.status,
|
|
|
+ requestHeaders: requestInfo.headers,
|
|
|
+ requestBody: requestBody,
|
|
|
+ responseBody: body,
|
|
|
+ responseHeaders: requestInfo.response.headers,
|
|
|
+ requestId: requestId,
|
|
|
+ timestamp: requestInfo.timestamp
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('拦截到的完整数据:', interceptedData.url);
|
|
|
+ console.log(interceptedData);
|
|
|
+
|
|
|
+ if (mainWindow && !mainWindow.isDestroyed()) {
|
|
|
+ mainWindow.webContents.executeJavaScript(`
|
|
|
+ if (window.onInterceptedData) {
|
|
|
+ window.onInterceptedData(${JSON.stringify(interceptedData)});
|
|
|
+ }
|
|
|
+ `).catch(console.error);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清理缓存
|
|
|
+ pendingRequests.delete(requestId);
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ console.warn(`无法获取响应体 ${requestInfo.response.url}:`, error.message);
|
|
|
+ pendingRequests.delete(requestId);
|
|
|
+ });
|
|
|
+ }, 100); // 添加小延迟
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清理已完成或失败的请求
|
|
|
+ if (method === 'Network.loadingFailed' || method === 'Network.requestServedFromCache') {
|
|
|
+ const { requestId } = params;
|
|
|
+ pendingRequests.delete(requestId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 启用网络跟踪
|
|
|
+ webContents.debugger.sendCommand('Network.enable');
|
|
|
+}
|
|
|
+
|
|
|
+// 代码注入到iframe
|
|
|
+// 主进程中的递归注入函数
|
|
|
+function injectToAllNestedFrames(frame, injectedSet = new Set()) {
|
|
|
+ try {
|
|
|
+ // 安全检查
|
|
|
+ if (!frame || frame.isDestroyed()) {
|
|
|
+ return injectedSet;
|
|
|
+ }
|
|
|
+
|
|
|
+ const frameId = `${frame.processId}-${frame.routingId}`;
|
|
|
+
|
|
|
+ // 避免重复注入
|
|
|
+ if (injectedSet.has(frameId)) {
|
|
|
+ return injectedSet;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注入当前框架
|
|
|
+ if (!frame.isMainFrame) {
|
|
|
+ try {
|
|
|
+ frame.executeJavaScript(`
|
|
|
+ if (!window.__electronDeepInjected) {
|
|
|
+ ${preLoadCode}
|
|
|
+ window.__electronDeepInjected = true;
|
|
|
+ console.log('深层 iframe 注入成功,深度: ${frame.depth || 0}');
|
|
|
+ }
|
|
|
+ `, true).then(() => {
|
|
|
+ console.log(`注入成功: ${frame.url ? frame.url.substring(0, 50) : '未知URL'} [深度: ${frame.depth || 0}]`);
|
|
|
+ }).catch(err => {
|
|
|
+ // 忽略无害错误
|
|
|
+ });
|
|
|
+
|
|
|
+ injectedSet.add(frameId);
|
|
|
+ } catch (error) {
|
|
|
+ // 忽略注入错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归注入子框架
|
|
|
+ try {
|
|
|
+ if (frame.children && frame.children.length > 0) {
|
|
|
+ frame.children.forEach(childFrame => {
|
|
|
+ injectToAllNestedFrames(childFrame, injectedSet);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 也可以通过 frames 属性获取
|
|
|
+ if (frame.frames && frame.frames.length > 0) {
|
|
|
+ frame.frames.forEach(childFrame => {
|
|
|
+ if (childFrame !== frame) { // 避免重复
|
|
|
+ injectToAllNestedFrames(childFrame, injectedSet);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (childError) {
|
|
|
+ // 忽略子框架访问错误
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.warn('递归注入出错:', error.message);
|
|
|
+ }
|
|
|
+
|
|
|
+ return injectedSet;
|
|
|
+}
|
|
|
+
|
|
|
+// 设置深层 iframe 注入
|
|
|
+function setupDeepFrameInjection(webContents) {
|
|
|
+ console.log('设置深层 iframe 注入...');
|
|
|
+ const injectedFrames = new Set();
|
|
|
+
|
|
|
+ // 方法1:监听框架创建事件(包含嵌套框架)
|
|
|
+ webContents.on('frame-created', (event, { frame, originalEvent }) => {
|
|
|
+ console.log(`框架创建: ${frame.routingId}, 父框架: ${frame.parent ? frame.parent.routingId : '无'}`);
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ injectToAllNestedFrames(frame, injectedFrames);
|
|
|
+ }, 500);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 方法2:监听框架导航完成
|
|
|
+ webContents.on('did-frame-finish-load', (event, isMainFrame, frameProcessId, frameRoutingId) => {
|
|
|
+ setTimeout(() => {
|
|
|
+ try {
|
|
|
+ // 查找框架
|
|
|
+ const findFrameRecursive = (parentFrame, targetId) => {
|
|
|
+ if (parentFrame.routingId === targetId) {
|
|
|
+ return parentFrame;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentFrame.children) {
|
|
|
+ for (const child of parentFrame.children) {
|
|
|
+ const found = findFrameRecursive(child, targetId);
|
|
|
+ if (found) return found;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ };
|
|
|
+
|
|
|
+ const frame = findFrameRecursive(webContents.mainFrame, frameRoutingId);
|
|
|
+ if (frame) {
|
|
|
+ injectToAllNestedFrames(frame, injectedFrames);
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ // 忽略错误
|
|
|
+ }
|
|
|
+ }, 600);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 方法3:定期深度扫描所有框架
|
|
|
+ let deepScanInterval = setInterval(() => {
|
|
|
+ if (webContents.isDestroyed()) {
|
|
|
+ clearInterval(deepScanInterval);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('执行深度框架扫描...');
|
|
|
+ deepScanAllFrames(webContents.mainFrame, injectedFrames);
|
|
|
+ }, 3000);
|
|
|
+
|
|
|
+ // 初始扫描
|
|
|
+ setTimeout(() => {
|
|
|
+ deepScanAllFrames(webContents.mainFrame, injectedFrames);
|
|
|
+ }, 2000);
|
|
|
+
|
|
|
+ // 清理
|
|
|
+ webContents.on('destroyed', () => {
|
|
|
+ clearInterval(deepScanInterval);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+// 深度扫描所有框架(递归)
|
|
|
+function deepScanAllFrames(startFrame, injectedSet) {
|
|
|
+ try {
|
|
|
+ if (!startFrame || startFrame.isDestroyed()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 注入当前框架
|
|
|
+ if (!startFrame.isMainFrame) {
|
|
|
+ const frameId = `${startFrame.processId}-${startFrame.routingId}`;
|
|
|
+ if (!injectedSet.has(frameId)) {
|
|
|
+ try {
|
|
|
+ startFrame.executeJavaScript(`
|
|
|
+ if (!window.__electronDeepInjected) {
|
|
|
+ ${preLoadCode}
|
|
|
+ window.__electronDeepInjected = true;
|
|
|
+ }
|
|
|
+ `, true);
|
|
|
+ injectedSet.add(frameId);
|
|
|
+ console.log(`深度扫描注入: ${startFrame.url ? startFrame.url.substring(0, 50) : '未知URL'} [深度: ${getFrameDepth(startFrame)}]`);
|
|
|
+ } catch (error) {
|
|
|
+ // 忽略注入错误
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 递归处理子框架
|
|
|
+ if (startFrame.children && startFrame.children.length > 0) {
|
|
|
+ startFrame.children.forEach(child => {
|
|
|
+ deepScanAllFrames(child, injectedSet);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理同级框架(如果有的话)
|
|
|
+ if (startFrame.frames && startFrame.frames.length > 0) {
|
|
|
+ startFrame.frames.forEach(sibling => {
|
|
|
+ if (sibling !== startFrame) {
|
|
|
+ deepScanAllFrames(sibling, injectedSet);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.warn('深度扫描出错:', error.message);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 获取框架深度
|
|
|
+function getFrameDepth(frame) {
|
|
|
+ let depth = 0;
|
|
|
+ let current = frame;
|
|
|
+
|
|
|
+ while (current && current.parent) {
|
|
|
+ depth++;
|
|
|
+ current = current.parent;
|
|
|
+ }
|
|
|
+
|
|
|
+ return depth;
|
|
|
+}
|
|
|
+
|
|
|
+function createWindow (partitionSession) {
|
|
|
+
|
|
|
+ // console.log(webConfig)
|
|
|
+ mainWindow = new BrowserWindow(webConfig)
|
|
|
+
|
|
|
+ // 代理
|
|
|
+ if (webConfig.proxy) {
|
|
|
+ partitionSession.setProxy({
|
|
|
+ proxyRules: webConfig.proxy,
|
|
|
+ proxyBypassRules: 'localhost',
|
|
|
+ }, function () {
|
|
|
+ console.log('代理设置完毕')
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 拦截到新窗口打开请求
|
|
|
+ mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
+ console.log('拦截到新窗口打开请求:', url);
|
|
|
+ mainWindow.loadURL(url); // 当前窗口跳转
|
|
|
+ return { action: 'deny' }; // 阻止 Electron 弹出窗口
|
|
|
+ });
|
|
|
+
|
|
|
+ // 使用纯 Chrome User Agent,完全移除 Electron 相关标识
|
|
|
+
|
|
|
+ mainWindow.webContents.setUserAgent(webConfig.userAgent ? webConfig.userAgent : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
|
|
|
+ if (enterURL.startsWith('http')) {
|
|
|
+ mainWindow.loadURL(enterURL)
|
|
|
+ } else {
|
|
|
+ console.log(path.join(__dirname, enterURL))
|
|
|
+ mainWindow.loadFile(enterURL)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (webConfig.preLoadFile) {
|
|
|
+ console.log(`Load additional JS: ${webConfig.preLoadFile}`)
|
|
|
+ preLoadCode += fs.readFileSync(webConfig.preLoadFile, 'utf-8')
|
|
|
+ }
|
|
|
+ mainWindow.webContents.on("dom-ready", function() {
|
|
|
+ mainWindow.webContents.executeJavaScript(preLoadCode);
|
|
|
+ });
|
|
|
+ // 只触发一次!
|
|
|
+ mainWindow.webContents.once("did-finish-load", function() {
|
|
|
+ if (webConfig.interceptor && webConfig.interceptor.length > 0) {
|
|
|
+ console.log('Need to intercept data!')
|
|
|
+ setupDebuggerInterceptor(mainWindow.webContents, webConfig.interceptor);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 打开新窗口触发
|
|
|
+ mainWindow.webContents.on("did-create-window", function(neWindow) {
|
|
|
+ neWindow.webContents.on("dom-ready", function() {
|
|
|
+ neWindow.webContents.executeJavaScript(preLoadCode);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ setupDeepFrameInjection(mainWindow.webContents);
|
|
|
+}
|
|
|
+
|
|
|
+function setupCSPRemoval(ses) {
|
|
|
+ ses.webRequest.onHeadersReceived((details, callback) => {
|
|
|
+ const responseHeaders = details.responseHeaders || {};
|
|
|
+
|
|
|
+ const cspHeaders = [
|
|
|
+ 'content-security-policy',
|
|
|
+ 'Content-Security-Policy',
|
|
|
+ 'content-security-policy-report-only',
|
|
|
+ 'x-content-security-policy',
|
|
|
+ 'x-webkit-csp'
|
|
|
+ ];
|
|
|
+
|
|
|
+ cspHeaders.forEach(header => {
|
|
|
+ if (responseHeaders[header]) {
|
|
|
+ // console.log('Removing', header, 'from', details.url);
|
|
|
+ delete responseHeaders[header];
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ callback({ cancel: false, responseHeaders });
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// This method will be called when Electron has finished
|
|
|
+// initialization and is ready to create browser windows.
|
|
|
+// Some APIs can only be used after this event occurs.
|
|
|
+app.whenReady().then(() => {
|
|
|
+ // 判断是否无缓存
|
|
|
+ if (webConfig.noCache) {
|
|
|
+ console.log('无缓存模式!')
|
|
|
+ webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
|
|
|
+
|
|
|
+ } else {
|
|
|
+ webConfig.webPreferences.partition = 'persist:owoApp'
|
|
|
+ }
|
|
|
+ // 去掉安全措施
|
|
|
+ const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
|
|
|
+ setupCSPRemoval(partitionSession);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ createWindow(partitionSession)
|
|
|
+
|
|
|
+ app.on('activate', function () {
|
|
|
+ // On macOS it's common to re-create a window in the app when the
|
|
|
+ // dock icon is clicked and there are no other windows open.
|
|
|
+ if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// Quit when all windows are closed, except on macOS. There, it's common
|
|
|
+// for applications and their menu bar to stay active until the user quits
|
|
|
+// explicitly with Cmd + Q.
|
|
|
+app.on('window-all-closed', function () {
|
|
|
+ if (process.platform !== 'darwin') app.quit()
|
|
|
+})
|
|
|
+
|
|
|
+app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
|
|
|
+ // 阻止默认的错误处理
|
|
|
+ event.preventDefault();
|
|
|
+ // 允许证书错误
|
|
|
+ callback(true);
|
|
|
+
|
|
|
+ // 如果需要,可以记录日志
|
|
|
+ console.warn(`忽略证书错误: ${error} - ${url}`);
|
|
|
+});
|
|
|
+
|
|
|
+// In this file you can include the rest of your app's specific main process
|
|
|
+// code. You can also put them in separate files and require them here.
|
|
|
+
|
|
|
+ipcMain.on("getData", (event, message) => {
|
|
|
+ // 控制台打印一下知道来了
|
|
|
+ console.log(message);
|
|
|
+ var options = {
|
|
|
+ 'method': 'GET',
|
|
|
+ 'url': owoDecode(message.url),
|
|
|
+ 'headers': message.headers,
|
|
|
+ strictSSL: false
|
|
|
+ };
|
|
|
+ request(options, function (error, response) {
|
|
|
+ if (error) throw new Error(error);
|
|
|
+ event.returnValue = response.body
|
|
|
+ });
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("postData", (event, message) => {
|
|
|
+ // 控制台打印一下知道来了
|
|
|
+ console.log(message);
|
|
|
+ var options = {
|
|
|
+ 'method': 'POST',
|
|
|
+ 'url': owoDecode(message.url),
|
|
|
+ 'headers': message.headers,
|
|
|
+ 'body': message.body,
|
|
|
+ strictSSL: false
|
|
|
+ };
|
|
|
+ request(options, function (error, response) {
|
|
|
+ if (error) throw new Error(error);
|
|
|
+ event.returnValue = response.body
|
|
|
+ });
|
|
|
+})
|
|
|
+
|
|
|
+
|
|
|
+ipcMain.on("setProxy", (event, message) => {
|
|
|
+ var win = new BrowserWindow({width: 800, height: 1500});
|
|
|
+ mainWindow.webContents.session.setProxy({
|
|
|
+ proxyRules: message.url,
|
|
|
+ proxyBypassRules: 'localhost',
|
|
|
+ });
|
|
|
+ event.returnValue = 'ok'
|
|
|
+})
|
|
|
+
|
|
|
+// 添加注入代码
|
|
|
+ipcMain.on("addPreLoadCode", (event, message) => {
|
|
|
+ if (message.data) preLoadCode += message.data
|
|
|
+})
|
|
|
+
|
|
|
+// 通用保存数据
|
|
|
+let dataStor = {}
|
|
|
+ipcMain.on("setStoData", (event, message) => {
|
|
|
+ dataStor[message.key] = message.value
|
|
|
+ event.returnValue = '{"err":0}'
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("getStoData", (event, message) => {
|
|
|
+ event.returnValue = dataStor[message.key]
|
|
|
+})
|
|
|
+let maxWindowOpenNum = 10
|
|
|
+let nowWindowInd = 0
|
|
|
+let childWindowList = []
|
|
|
+function randomString(n){const str = 'abcdefghijklmnopqrstuvwxyz9876543210';let tmp = '',i = 0,l = str.length;for (i = 0; i < n; i++) {tmp += str.charAt(Math.floor(Math.random() * l));}return tmp;}
|
|
|
+ipcMain.handle("openWindow", async (event, message) => {
|
|
|
+ console.log(message)
|
|
|
+ let nowIndex = nowWindowInd++
|
|
|
+ // 判断是否达到了最大窗口数量
|
|
|
+ let nowWindowNumTemp = 0
|
|
|
+ for (let index = 0; index < childWindowList.length; index++) {
|
|
|
+ const element = childWindowList[index];
|
|
|
+ if (element) nowWindowNumTemp++
|
|
|
+ }
|
|
|
+ if (nowWindowNumTemp > maxWindowOpenNum) {
|
|
|
+ for (let index = 0; index < childWindowList.length; index++) {
|
|
|
+ const element = childWindowList[index];
|
|
|
+ if (element) {
|
|
|
+ childWindowList[index].close()
|
|
|
+ childWindowList[index] = null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取对应 session
|
|
|
+ let partitionName = 'persist:owoAppChild'
|
|
|
+ if (message.noCache) {
|
|
|
+ partitionName = 'persist:Session' + Math.round(Math.random()*100000)
|
|
|
+ }
|
|
|
+ const customSession = session.fromPartition(partitionName);
|
|
|
+ if (message.proxy) {
|
|
|
+ // 设置代理(等待设置完成)
|
|
|
+ await customSession.setProxy({
|
|
|
+ proxyRules: message.proxy,
|
|
|
+ proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
|
|
|
+ });
|
|
|
+ }
|
|
|
+ let childWindowPreferences = webConfig.webPreferences
|
|
|
+ if (message.webPreferences) childWindowPreferences = message.webPreferences
|
|
|
+ childWindowPreferences.partition = partitionName
|
|
|
+ // 创建新窗口
|
|
|
+ childWindowList[nowIndex] = new BrowserWindow({
|
|
|
+ width: message.width || 800,
|
|
|
+ height: message.height || 600,
|
|
|
+ autoHideMenuBar: message.autoHideMenuBar || true,
|
|
|
+ // 无边框
|
|
|
+ frame: message.autoHideMenuBar || false,
|
|
|
+ // 全屏
|
|
|
+ fullscreen: message.autoHideMenuBar || false,
|
|
|
+ webPreferences: childWindowPreferences
|
|
|
+ });
|
|
|
+ // 判断是否静音
|
|
|
+ if (message.muted) {
|
|
|
+ // 设置静音
|
|
|
+ childWindowList[nowIndex].webContents.setAudioMuted(true);
|
|
|
+ }
|
|
|
+ childWindowList[nowIndex].webContents.on("dom-ready", function() {
|
|
|
+ console.log("dom-ready")
|
|
|
+ childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
|
|
|
+ });
|
|
|
+ childWindowList[nowIndex].on('closed', () => {
|
|
|
+ childWindowList[nowIndex] = null;
|
|
|
+ });
|
|
|
+
|
|
|
+ // 拦截到新窗口打开请求
|
|
|
+ childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
|
|
|
+ console.log('拦截到新窗口打开请求:', url);
|
|
|
+ childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
|
|
|
+ return { action: 'deny' }; // 阻止 Electron 弹出窗口
|
|
|
+ });
|
|
|
+ childWindowList[nowIndex].loadURL(message.url, {
|
|
|
+ userAgent: message.userAgent ? message.userAgent : "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
|
|
|
+ });
|
|
|
+
|
|
|
+ event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("closeWindow", (event, message) => {
|
|
|
+ if (message && message.key) {
|
|
|
+ message.key = parseInt(message.key)
|
|
|
+ setTimeout(() => {
|
|
|
+ if (childWindowList[message.key]) {
|
|
|
+ childWindowList[message.key].close()
|
|
|
+ }
|
|
|
+ setTimeout(() => {
|
|
|
+ childWindowList[message.key] = null
|
|
|
+ }, 0);
|
|
|
+ }, message.time || 0);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ event.returnValue = JSON.stringify({"err":0})
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("closeAllWindow", (event, message) => {
|
|
|
+ for (let index = 0; index < childWindowList.length; index++) {
|
|
|
+ const element = childWindowList[index];
|
|
|
+ if (element && element.close) {
|
|
|
+ try {
|
|
|
+ element.close()
|
|
|
+ } catch (error) {
|
|
|
+ console.log(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setTimeout(() => {
|
|
|
+ childWindowList = []
|
|
|
+ }, 0);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ event.returnValue = JSON.stringify({"err":0})
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("changeProxy", (event, message) => {
|
|
|
+ if (message && message.key) {
|
|
|
+ message.key = parseInt(message.key)
|
|
|
+ childWindowList[message.key].webContents.session.setProxy({
|
|
|
+ proxyRules: "",
|
|
|
+ proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ for (let index = 0; index < childWindowList.length; index++) {
|
|
|
+ const element = childWindowList[index];
|
|
|
+ if (element) {
|
|
|
+ element.webContents.session.setProxy({
|
|
|
+ proxyRules: "",
|
|
|
+ proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ event.returnValue = JSON.stringify({"err":0})
|
|
|
+})
|
|
|
+
|
|
|
+
|
|
|
+ipcMain.on("readConfig", (event, message) => {
|
|
|
+ if (fs.existsSync("./config.json")) {
|
|
|
+ event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
|
|
|
+ } else {
|
|
|
+ event.returnValue = {}
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.handle("saveFile", async (event, message) => {
|
|
|
+ try {
|
|
|
+ // 确保download目录存在
|
|
|
+ const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
|
|
|
+ if (!fs.existsSync(downloadDir)) {
|
|
|
+ fs.mkdirSync(downloadDir, { recursive: true });
|
|
|
+ console.log('创建download目录:', downloadDir);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整的文件路径
|
|
|
+ const filePath = path.join(downloadDir, message.filename);
|
|
|
+
|
|
|
+ // 将字符串编码为缓冲区
|
|
|
+ const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
|
|
|
+ fs.writeFileSync(filePath, fileBuffer);
|
|
|
+
|
|
|
+ console.log('文件保存成功:', filePath);
|
|
|
+ return { success: true, path: filePath };
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存文件失败:', error);
|
|
|
+ return { success: false, error: error.message };
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+ipcMain.handle("readFile", async (event, message) => {
|
|
|
+ try {
|
|
|
+ // 确保download目录存在
|
|
|
+ const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
|
|
|
+ if (!fs.existsSync(downloadDir)) {
|
|
|
+ fs.mkdirSync(downloadDir, { recursive: true });
|
|
|
+ console.log('创建download目录:', downloadDir);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建完整的文件路径
|
|
|
+ const filePath = path.join(downloadDir, message.filename);
|
|
|
+
|
|
|
+ // 检查文件是否存在
|
|
|
+ if (!fs.existsSync(filePath)) {
|
|
|
+ console.log('文件不存在,返回空内容:', filePath);
|
|
|
+ return { success: true, content: '', exists: false };
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取文件内容
|
|
|
+ const buffer = fs.readFileSync(filePath);
|
|
|
+ const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
|
|
|
+
|
|
|
+ console.log('文件读取成功:', filePath);
|
|
|
+ return { success: true, content: content, exists: true };
|
|
|
+ } catch (error) {
|
|
|
+ console.error('读取文件失败:', error);
|
|
|
+ return { success: false, error: error.message, exists: false };
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+ipcMain.on("saveConfig", (event, message) => {
|
|
|
+ fs.writeFileSync('./config.json', JSON.stringify(message))
|
|
|
+ event.returnValue = {err: 0}
|
|
|
+})
|
|
|
+
|
|
|
+// 设置最大打开窗口数量
|
|
|
+ipcMain.on("setMaxWindowOpenNum", (event, message) => {
|
|
|
+ if (message.value) {
|
|
|
+ maxWindowOpenNum = parseInt(message.value)
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("readdir", (event, directoryPath) => {
|
|
|
+ fs.readdir(directoryPath, (err, files) => {
|
|
|
+ if (err) {
|
|
|
+ event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ event.returnValue = {err: 0, files}
|
|
|
+ });
|
|
|
+
|
|
|
+})
|
|
|
+
|
|
|
+ipcMain.on("download", (event, message) => {
|
|
|
+ download(message.url, message.path, {
|
|
|
+ filename: message.filename,
|
|
|
+ });
|
|
|
+ event.returnValue = {err: 0}
|
|
|
+})
|
|
|
+
|
|
|
+// 窗口间通信
|
|
|
+ipcMain.on('broadcast-message', (event, message) => {
|
|
|
+ // 获取发送者
|
|
|
+ console.log(message)
|
|
|
+ mainWindow.send('message-broadcast', message);
|
|
|
+ // 广播给所有其他窗口
|
|
|
+ for (const win of childWindowList) {
|
|
|
+ if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|