main.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // Modules to control application life and create native browser window
  2. const {app, ipcMain, BrowserWindow, session} = require('electron')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const iconv = require('iconv-lite');
  6. const request = require('request');
  7. const download = require('download');
  8. // 读取配置文件
  9. let enterURL = 'https://demos.run/debuger/index.html'
  10. let webConfig = {
  11. width: 376,
  12. height: 667,
  13. webPreferences: {
  14. webSecurity: false,
  15. contextIsolation: true,
  16. nodeIntegration: false,
  17. allowRunningInsecureContent: true, // 允许不安全内容
  18. plugins: true,
  19. scrollBounce: true,
  20. preload: path.join(__dirname, "preload.js")
  21. },
  22. autoHideMenuBar: true
  23. // 无边框
  24. // frame: false,
  25. // 全屏
  26. // fullscreen: true
  27. }
  28. // 判断是否有特殊配置文件
  29. console.log(__dirname + "\\config.json")
  30. if (fs.existsSync("./config.json")) {
  31. let configStr = fs.readFileSync('./config.json', 'utf-8')
  32. // 特殊符号表示运行目录
  33. configStr = configStr.replaceAll('<dir>', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
  34. webConfig = JSON.parse(configStr)
  35. enterURL = webConfig.enterURL
  36. }
  37. console.log(webConfig)
  38. 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]"}
  39. function owoReplaceAll(str, s1, s2) {
  40. while (str.indexOf(s1) >= 0) {
  41. str = str.replace(s1, s2)
  42. }
  43. return str
  44. }
  45. function owoDecode(itemStr) {
  46. for (let item in codeMap) {
  47. itemStr = owoReplaceAll(itemStr, codeMap[item], item)
  48. }
  49. while (itemStr.indexOf(']') >= 0) {
  50. itemStr = owoDecode(itemStr)
  51. }
  52. return itemStr
  53. }
  54. let mainWindow = null
  55. let preLoadCode = `
  56. var owoApp = 5
  57. window.owoPC = true
  58. window.electronConfig = ${JSON.stringify(webConfig)};
  59. function loadScript(url, callback) {
  60. var script = document.createElement("script")
  61. script.type = "text/javascript";
  62. if (script.readyState) { //IE
  63. script.onreadystatechange = function () {
  64. if (script.readyState == "loaded" || script.readyState == "complete") {
  65. script.onreadystatechange = null;
  66. if (callback) callback();
  67. }
  68. };
  69. } else { //Others
  70. script.onload = function () {
  71. if (callback) callback();
  72. };
  73. }
  74. script.src = url;
  75. var head = document.head || document.getElementsByTagName('head')[0];
  76. head.appendChild(script);
  77. }
  78. function loadJsCode(code){
  79. var script = document.createElement('script');
  80. script.type = 'text/javascript';
  81. //for Chrome Firefox Opera Safari
  82. script.appendChild(document.createTextNode(code));
  83. //for IE
  84. //script.text = code;
  85. document.body.appendChild(script);
  86. }
  87. function loadCSS (url) {
  88. var link = document.createElement("link");
  89. link.rel = "stylesheet";
  90. link.type = "text/css";
  91. link.href = url;
  92. document.getElementsByTagName("head")[0].appendChild(link);
  93. }
  94. loadScript('https://cunchu.site/app/main.js');
  95. `
  96. // 拦截数据
  97. function setupDebuggerInterceptor(webContents, interceptor) {
  98. webContents.debugger.attach('1.3');
  99. const pendingRequests = new Map();
  100. webContents.debugger.on('message', (event, method, params) => {
  101. // 存储请求信息
  102. if (method === 'Network.requestWillBeSent') {
  103. pendingRequests.set(params.requestId, {
  104. url: params.request.url,
  105. method: params.request.method,
  106. headers: params.request.headers,
  107. postData: params.request.postData,
  108. timestamp: Date.now()
  109. });
  110. }
  111. // 响应接收时记录信息
  112. if (method === 'Network.responseReceived') {
  113. const { requestId, response } = params;
  114. let hookData = false;
  115. interceptor.forEach(hookURL => {
  116. if (response.url.includes(hookURL) || response.url === hookURL) {
  117. hookData = true;
  118. }
  119. });
  120. if (hookData) {
  121. console.log('拦截到响应:', response.url, response.status);
  122. // 存储响应信息,等待 loadingFinished
  123. const requestInfo = pendingRequests.get(requestId) || {};
  124. pendingRequests.set(requestId, {
  125. ...requestInfo,
  126. responseReceived: true,
  127. response,
  128. hookData: true
  129. });
  130. }
  131. }
  132. // 在资源加载完成时获取响应体
  133. if (method === 'Network.loadingFinished') {
  134. const { requestId } = params;
  135. const requestInfo = pendingRequests.get(requestId);
  136. if (requestInfo && requestInfo.hookData) {
  137. // 延迟获取响应体,确保数据可用
  138. setTimeout(() => {
  139. webContents.debugger.sendCommand('Network.getResponseBody', { requestId })
  140. .then(({ body }) => {
  141. // 获取请求体(如果有)
  142. let requestBody = null;
  143. if (requestInfo.postData) {
  144. try {
  145. // 尝试解析为JSON,如果不是JSON则保持原样
  146. requestBody = JSON.parse(requestInfo.postData);
  147. } catch (e) {
  148. requestBody = requestInfo.postData;
  149. }
  150. }
  151. const interceptedData = {
  152. url: requestInfo.response.url,
  153. method: requestInfo.method,
  154. statusCode: requestInfo.response.status,
  155. requestHeaders: requestInfo.headers,
  156. requestBody: requestBody,
  157. responseBody: body,
  158. responseHeaders: requestInfo.response.headers,
  159. requestId: requestId,
  160. timestamp: requestInfo.timestamp
  161. };
  162. console.log('拦截到的完整数据:', interceptedData.url);
  163. console.log(interceptedData);
  164. if (mainWindow && !mainWindow.isDestroyed()) {
  165. mainWindow.webContents.executeJavaScript(`
  166. if (window.onInterceptedData) {
  167. window.onInterceptedData(${JSON.stringify(interceptedData)});
  168. }
  169. `).catch(console.error);
  170. }
  171. // 清理缓存
  172. pendingRequests.delete(requestId);
  173. })
  174. .catch(error => {
  175. console.warn(`无法获取响应体 ${requestInfo.response.url}:`, error.message);
  176. pendingRequests.delete(requestId);
  177. });
  178. }, 100); // 添加小延迟
  179. }
  180. }
  181. // 清理已完成或失败的请求
  182. if (method === 'Network.loadingFailed' || method === 'Network.requestServedFromCache') {
  183. const { requestId } = params;
  184. pendingRequests.delete(requestId);
  185. }
  186. });
  187. // 启用网络跟踪
  188. webContents.debugger.sendCommand('Network.enable');
  189. }
  190. // 代码注入到iframe
  191. // 主进程中的递归注入函数
  192. function injectToAllNestedFrames(frame, injectedSet = new Set()) {
  193. try {
  194. // 安全检查
  195. if (!frame || frame.isDestroyed()) {
  196. return injectedSet;
  197. }
  198. const frameId = `${frame.processId}-${frame.routingId}`;
  199. // 避免重复注入
  200. if (injectedSet.has(frameId)) {
  201. return injectedSet;
  202. }
  203. // 注入当前框架
  204. if (!frame.isMainFrame) {
  205. try {
  206. frame.executeJavaScript(`
  207. if (!window.__electronDeepInjected) {
  208. ${preLoadCode}
  209. window.__electronDeepInjected = true;
  210. console.log('深层 iframe 注入成功,深度: ${frame.depth || 0}');
  211. }
  212. `, true).then(() => {
  213. console.log(`注入成功: ${frame.url ? frame.url.substring(0, 50) : '未知URL'} [深度: ${frame.depth || 0}]`);
  214. }).catch(err => {
  215. // 忽略无害错误
  216. });
  217. injectedSet.add(frameId);
  218. } catch (error) {
  219. // 忽略注入错误
  220. }
  221. }
  222. // 递归注入子框架
  223. try {
  224. if (frame.children && frame.children.length > 0) {
  225. frame.children.forEach(childFrame => {
  226. injectToAllNestedFrames(childFrame, injectedSet);
  227. });
  228. }
  229. // 也可以通过 frames 属性获取
  230. if (frame.frames && frame.frames.length > 0) {
  231. frame.frames.forEach(childFrame => {
  232. if (childFrame !== frame) { // 避免重复
  233. injectToAllNestedFrames(childFrame, injectedSet);
  234. }
  235. });
  236. }
  237. } catch (childError) {
  238. // 忽略子框架访问错误
  239. }
  240. } catch (error) {
  241. console.warn('递归注入出错:', error.message);
  242. }
  243. return injectedSet;
  244. }
  245. // 设置深层 iframe 注入
  246. function setupDeepFrameInjection(webContents) {
  247. console.log('设置深层 iframe 注入...');
  248. const injectedFrames = new Set();
  249. // 方法1:监听框架创建事件(包含嵌套框架)
  250. webContents.on('frame-created', (event, { frame, originalEvent }) => {
  251. console.log(`框架创建: ${frame.routingId}, 父框架: ${frame.parent ? frame.parent.routingId : '无'}`);
  252. setTimeout(() => {
  253. injectToAllNestedFrames(frame, injectedFrames);
  254. }, 500);
  255. });
  256. // 方法2:监听框架导航完成
  257. webContents.on('did-frame-finish-load', (event, isMainFrame, frameProcessId, frameRoutingId) => {
  258. setTimeout(() => {
  259. try {
  260. // 查找框架
  261. const findFrameRecursive = (parentFrame, targetId) => {
  262. if (parentFrame.routingId === targetId) {
  263. return parentFrame;
  264. }
  265. if (parentFrame.children) {
  266. for (const child of parentFrame.children) {
  267. const found = findFrameRecursive(child, targetId);
  268. if (found) return found;
  269. }
  270. }
  271. return null;
  272. };
  273. const frame = findFrameRecursive(webContents.mainFrame, frameRoutingId);
  274. if (frame) {
  275. injectToAllNestedFrames(frame, injectedFrames);
  276. }
  277. } catch (error) {
  278. // 忽略错误
  279. }
  280. }, 600);
  281. });
  282. // 方法3:定期深度扫描所有框架
  283. let deepScanInterval = setInterval(() => {
  284. if (webContents.isDestroyed()) {
  285. clearInterval(deepScanInterval);
  286. return;
  287. }
  288. console.log('执行深度框架扫描...');
  289. deepScanAllFrames(webContents.mainFrame, injectedFrames);
  290. }, 3000);
  291. // 初始扫描
  292. setTimeout(() => {
  293. deepScanAllFrames(webContents.mainFrame, injectedFrames);
  294. }, 2000);
  295. // 清理
  296. webContents.on('destroyed', () => {
  297. clearInterval(deepScanInterval);
  298. });
  299. }
  300. // 深度扫描所有框架(递归)
  301. function deepScanAllFrames(startFrame, injectedSet) {
  302. try {
  303. if (!startFrame || startFrame.isDestroyed()) {
  304. return;
  305. }
  306. // 注入当前框架
  307. if (!startFrame.isMainFrame) {
  308. const frameId = `${startFrame.processId}-${startFrame.routingId}`;
  309. if (!injectedSet.has(frameId)) {
  310. try {
  311. startFrame.executeJavaScript(`
  312. if (!window.__electronDeepInjected) {
  313. ${preLoadCode}
  314. window.__electronDeepInjected = true;
  315. }
  316. `, true);
  317. injectedSet.add(frameId);
  318. console.log(`深度扫描注入: ${startFrame.url ? startFrame.url.substring(0, 50) : '未知URL'} [深度: ${getFrameDepth(startFrame)}]`);
  319. } catch (error) {
  320. // 忽略注入错误
  321. }
  322. }
  323. }
  324. // 递归处理子框架
  325. if (startFrame.children && startFrame.children.length > 0) {
  326. startFrame.children.forEach(child => {
  327. deepScanAllFrames(child, injectedSet);
  328. });
  329. }
  330. // 处理同级框架(如果有的话)
  331. if (startFrame.frames && startFrame.frames.length > 0) {
  332. startFrame.frames.forEach(sibling => {
  333. if (sibling !== startFrame) {
  334. deepScanAllFrames(sibling, injectedSet);
  335. }
  336. });
  337. }
  338. } catch (error) {
  339. console.warn('深度扫描出错:', error.message);
  340. }
  341. }
  342. // 获取框架深度
  343. function getFrameDepth(frame) {
  344. let depth = 0;
  345. let current = frame;
  346. while (current && current.parent) {
  347. depth++;
  348. current = current.parent;
  349. }
  350. return depth;
  351. }
  352. function createWindow (partitionSession) {
  353. // console.log(webConfig)
  354. mainWindow = new BrowserWindow(webConfig)
  355. // 代理
  356. if (webConfig.proxy) {
  357. partitionSession.setProxy({
  358. proxyRules: webConfig.proxy,
  359. proxyBypassRules: 'localhost',
  360. }, function () {
  361. console.log('代理设置完毕')
  362. });
  363. }
  364. // 拦截到新窗口打开请求
  365. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  366. console.log('拦截到新窗口打开请求:', url);
  367. mainWindow.loadURL(url); // 当前窗口跳转
  368. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  369. });
  370. // 使用纯 Chrome User Agent,完全移除 Electron 相关标识
  371. 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');
  372. if (enterURL.startsWith('http')) {
  373. mainWindow.loadURL(enterURL)
  374. } else {
  375. console.log(path.join(__dirname, enterURL))
  376. mainWindow.loadFile(enterURL)
  377. }
  378. if (webConfig.preLoadFile) {
  379. console.log(`Load additional JS: ${webConfig.preLoadFile}`)
  380. preLoadCode += fs.readFileSync(webConfig.preLoadFile, 'utf-8')
  381. }
  382. mainWindow.webContents.on("dom-ready", function() {
  383. mainWindow.webContents.executeJavaScript(preLoadCode);
  384. });
  385. // 只触发一次!
  386. mainWindow.webContents.once("did-finish-load", function() {
  387. if (webConfig.interceptor && webConfig.interceptor.length > 0) {
  388. console.log('Need to intercept data!')
  389. setupDebuggerInterceptor(mainWindow.webContents, webConfig.interceptor);
  390. }
  391. });
  392. // 打开新窗口触发
  393. mainWindow.webContents.on("did-create-window", function(neWindow) {
  394. neWindow.webContents.on("dom-ready", function() {
  395. neWindow.webContents.executeJavaScript(preLoadCode);
  396. });
  397. });
  398. setupDeepFrameInjection(mainWindow.webContents);
  399. }
  400. function setupCSPRemoval(ses) {
  401. ses.webRequest.onHeadersReceived((details, callback) => {
  402. const responseHeaders = details.responseHeaders || {};
  403. const cspHeaders = [
  404. 'content-security-policy',
  405. 'Content-Security-Policy',
  406. 'content-security-policy-report-only',
  407. 'x-content-security-policy',
  408. 'x-webkit-csp'
  409. ];
  410. cspHeaders.forEach(header => {
  411. if (responseHeaders[header]) {
  412. // console.log('Removing', header, 'from', details.url);
  413. delete responseHeaders[header];
  414. }
  415. });
  416. callback({ cancel: false, responseHeaders });
  417. });
  418. }
  419. // This method will be called when Electron has finished
  420. // initialization and is ready to create browser windows.
  421. // Some APIs can only be used after this event occurs.
  422. app.whenReady().then(() => {
  423. // 判断是否无缓存
  424. if (webConfig.noCache) {
  425. console.log('无缓存模式!')
  426. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  427. } else {
  428. webConfig.webPreferences.partition = 'persist:owoApp'
  429. }
  430. // 去掉安全措施
  431. const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
  432. setupCSPRemoval(partitionSession);
  433. createWindow(partitionSession)
  434. app.on('activate', function () {
  435. // On macOS it's common to re-create a window in the app when the
  436. // dock icon is clicked and there are no other windows open.
  437. if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
  438. })
  439. })
  440. // Quit when all windows are closed, except on macOS. There, it's common
  441. // for applications and their menu bar to stay active until the user quits
  442. // explicitly with Cmd + Q.
  443. app.on('window-all-closed', function () {
  444. if (process.platform !== 'darwin') app.quit()
  445. })
  446. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  447. // 阻止默认的错误处理
  448. event.preventDefault();
  449. // 允许证书错误
  450. callback(true);
  451. // 如果需要,可以记录日志
  452. console.warn(`忽略证书错误: ${error} - ${url}`);
  453. });
  454. // In this file you can include the rest of your app's specific main process
  455. // code. You can also put them in separate files and require them here.
  456. ipcMain.on("getData", (event, message) => {
  457. // 控制台打印一下知道来了
  458. console.log(message);
  459. var options = {
  460. 'method': 'GET',
  461. 'url': owoDecode(message.url),
  462. 'headers': message.headers,
  463. strictSSL: false
  464. };
  465. request(options, function (error, response) {
  466. if (error) throw new Error(error);
  467. event.returnValue = response.body
  468. });
  469. })
  470. ipcMain.on("postData", (event, message) => {
  471. // 控制台打印一下知道来了
  472. console.log(message);
  473. var options = {
  474. 'method': 'POST',
  475. 'url': owoDecode(message.url),
  476. 'headers': message.headers,
  477. 'body': message.body,
  478. strictSSL: false
  479. };
  480. request(options, function (error, response) {
  481. if (error) throw new Error(error);
  482. event.returnValue = response.body
  483. });
  484. })
  485. ipcMain.on("setProxy", (event, message) => {
  486. var win = new BrowserWindow({width: 800, height: 1500});
  487. mainWindow.webContents.session.setProxy({
  488. proxyRules: message.url,
  489. proxyBypassRules: 'localhost',
  490. });
  491. event.returnValue = 'ok'
  492. })
  493. // 添加注入代码
  494. ipcMain.on("addPreLoadCode", (event, message) => {
  495. if (message.data) preLoadCode += message.data
  496. })
  497. // 通用保存数据
  498. let dataStor = {}
  499. ipcMain.on("setStoData", (event, message) => {
  500. dataStor[message.key] = message.value
  501. event.returnValue = '{"err":0}'
  502. })
  503. ipcMain.on("getStoData", (event, message) => {
  504. event.returnValue = dataStor[message.key]
  505. })
  506. let maxWindowOpenNum = 10
  507. let nowWindowInd = 0
  508. let childWindowList = []
  509. 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;}
  510. ipcMain.handle("openWindow", async (event, message) => {
  511. console.log(message)
  512. let nowIndex = nowWindowInd++
  513. // 判断是否达到了最大窗口数量
  514. let nowWindowNumTemp = 0
  515. for (let index = 0; index < childWindowList.length; index++) {
  516. const element = childWindowList[index];
  517. if (element) nowWindowNumTemp++
  518. }
  519. if (nowWindowNumTemp > maxWindowOpenNum) {
  520. for (let index = 0; index < childWindowList.length; index++) {
  521. const element = childWindowList[index];
  522. if (element) {
  523. childWindowList[index].close()
  524. childWindowList[index] = null
  525. }
  526. }
  527. }
  528. // 获取对应 session
  529. let partitionName = 'persist:owoAppChild'
  530. if (message.noCache) {
  531. partitionName = 'persist:Session' + Math.round(Math.random()*100000)
  532. }
  533. const customSession = session.fromPartition(partitionName);
  534. if (message.proxy) {
  535. // 设置代理(等待设置完成)
  536. await customSession.setProxy({
  537. proxyRules: message.proxy,
  538. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  539. });
  540. }
  541. let childWindowPreferences = webConfig.webPreferences
  542. if (message.webPreferences) childWindowPreferences = message.webPreferences
  543. childWindowPreferences.partition = partitionName
  544. // 创建新窗口
  545. childWindowList[nowIndex] = new BrowserWindow({
  546. width: message.width || 800,
  547. height: message.height || 600,
  548. autoHideMenuBar: message.autoHideMenuBar || true,
  549. // 无边框
  550. frame: message.autoHideMenuBar || false,
  551. // 全屏
  552. fullscreen: message.autoHideMenuBar || false,
  553. webPreferences: childWindowPreferences
  554. });
  555. // 判断是否静音
  556. if (message.muted) {
  557. // 设置静音
  558. childWindowList[nowIndex].webContents.setAudioMuted(true);
  559. }
  560. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  561. console.log("dom-ready")
  562. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  563. });
  564. childWindowList[nowIndex].on('closed', () => {
  565. childWindowList[nowIndex] = null;
  566. });
  567. // 拦截到新窗口打开请求
  568. childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
  569. console.log('拦截到新窗口打开请求:', url);
  570. childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
  571. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  572. });
  573. childWindowList[nowIndex].loadURL(message.url, {
  574. 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"
  575. });
  576. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  577. })
  578. ipcMain.on("closeWindow", (event, message) => {
  579. if (message && message.key) {
  580. message.key = parseInt(message.key)
  581. setTimeout(() => {
  582. if (childWindowList[message.key]) {
  583. childWindowList[message.key].close()
  584. }
  585. setTimeout(() => {
  586. childWindowList[message.key] = null
  587. }, 0);
  588. }, message.time || 0);
  589. }
  590. event.returnValue = JSON.stringify({"err":0})
  591. })
  592. ipcMain.on("closeAllWindow", (event, message) => {
  593. for (let index = 0; index < childWindowList.length; index++) {
  594. const element = childWindowList[index];
  595. if (element && element.close) {
  596. try {
  597. element.close()
  598. } catch (error) {
  599. console.log(error)
  600. }
  601. }
  602. }
  603. setTimeout(() => {
  604. childWindowList = []
  605. }, 0);
  606. event.returnValue = JSON.stringify({"err":0})
  607. })
  608. ipcMain.on("changeProxy", (event, message) => {
  609. if (message && message.key) {
  610. message.key = parseInt(message.key)
  611. childWindowList[message.key].webContents.session.setProxy({
  612. proxyRules: "",
  613. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  614. });
  615. } else {
  616. for (let index = 0; index < childWindowList.length; index++) {
  617. const element = childWindowList[index];
  618. if (element) {
  619. element.webContents.session.setProxy({
  620. proxyRules: "",
  621. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  622. });
  623. }
  624. }
  625. }
  626. event.returnValue = JSON.stringify({"err":0})
  627. })
  628. ipcMain.on("readConfig", (event, message) => {
  629. if (fs.existsSync("./config.json")) {
  630. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  631. } else {
  632. event.returnValue = {}
  633. }
  634. })
  635. ipcMain.handle("saveFile", async (event, message) => {
  636. try {
  637. // 确保download目录存在
  638. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  639. if (!fs.existsSync(downloadDir)) {
  640. fs.mkdirSync(downloadDir, { recursive: true });
  641. console.log('创建download目录:', downloadDir);
  642. }
  643. // 构建完整的文件路径
  644. const filePath = path.join(downloadDir, message.filename);
  645. // 将字符串编码为缓冲区
  646. const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
  647. fs.writeFileSync(filePath, fileBuffer);
  648. console.log('文件保存成功:', filePath);
  649. return { success: true, path: filePath };
  650. } catch (error) {
  651. console.error('保存文件失败:', error);
  652. return { success: false, error: error.message };
  653. }
  654. });
  655. ipcMain.handle("readFile", async (event, message) => {
  656. try {
  657. // 确保download目录存在
  658. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  659. if (!fs.existsSync(downloadDir)) {
  660. fs.mkdirSync(downloadDir, { recursive: true });
  661. console.log('创建download目录:', downloadDir);
  662. }
  663. // 构建完整的文件路径
  664. const filePath = path.join(downloadDir, message.filename);
  665. // 检查文件是否存在
  666. if (!fs.existsSync(filePath)) {
  667. console.log('文件不存在,返回空内容:', filePath);
  668. return { success: true, content: '', exists: false };
  669. }
  670. // 读取文件内容
  671. const buffer = fs.readFileSync(filePath);
  672. const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
  673. console.log('文件读取成功:', filePath);
  674. return { success: true, content: content, exists: true };
  675. } catch (error) {
  676. console.error('读取文件失败:', error);
  677. return { success: false, error: error.message, exists: false };
  678. }
  679. });
  680. ipcMain.on("saveConfig", (event, message) => {
  681. fs.writeFileSync('./config.json', JSON.stringify(message))
  682. event.returnValue = {err: 0}
  683. })
  684. // 设置最大打开窗口数量
  685. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  686. if (message.value) {
  687. maxWindowOpenNum = parseInt(message.value)
  688. }
  689. })
  690. ipcMain.on("readdir", (event, directoryPath) => {
  691. fs.readdir(directoryPath, (err, files) => {
  692. if (err) {
  693. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  694. return;
  695. }
  696. event.returnValue = {err: 0, files}
  697. });
  698. })
  699. ipcMain.on("download", (event, message) => {
  700. download(message.url, message.path, {
  701. filename: message.filename,
  702. });
  703. event.returnValue = {err: 0}
  704. })
  705. // 窗口间通信
  706. ipcMain.on('broadcast-message', (event, message) => {
  707. // 获取发送者
  708. console.log(message)
  709. mainWindow.send('message-broadcast', message);
  710. // 广播给所有其他窗口
  711. for (const win of childWindowList) {
  712. if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
  713. }
  714. });