main.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. // 判断是否注入iframe
  399. if (webConfig.frameInjection) {
  400. setupDeepFrameInjection(mainWindow.webContents);
  401. }
  402. }
  403. function setupCSPRemoval(ses) {
  404. ses.webRequest.onHeadersReceived((details, callback) => {
  405. const responseHeaders = details.responseHeaders || {};
  406. const cspHeaders = [
  407. 'content-security-policy',
  408. 'Content-Security-Policy',
  409. 'content-security-policy-report-only',
  410. 'x-content-security-policy',
  411. 'x-webkit-csp'
  412. ];
  413. cspHeaders.forEach(header => {
  414. if (responseHeaders[header]) {
  415. // console.log('Removing', header, 'from', details.url);
  416. delete responseHeaders[header];
  417. }
  418. });
  419. callback({ cancel: false, responseHeaders });
  420. });
  421. }
  422. // This method will be called when Electron has finished
  423. // initialization and is ready to create browser windows.
  424. // Some APIs can only be used after this event occurs.
  425. app.whenReady().then(() => {
  426. // 判断是否无缓存
  427. if (webConfig.noCache) {
  428. console.log('无缓存模式!')
  429. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  430. } else {
  431. webConfig.webPreferences.partition = 'persist:owoApp'
  432. }
  433. // 去掉安全措施
  434. const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
  435. setupCSPRemoval(partitionSession);
  436. createWindow(partitionSession)
  437. app.on('activate', function () {
  438. // On macOS it's common to re-create a window in the app when the
  439. // dock icon is clicked and there are no other windows open.
  440. if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
  441. })
  442. })
  443. // Quit when all windows are closed, except on macOS. There, it's common
  444. // for applications and their menu bar to stay active until the user quits
  445. // explicitly with Cmd + Q.
  446. app.on('window-all-closed', function () {
  447. if (process.platform !== 'darwin') app.quit()
  448. })
  449. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  450. // 阻止默认的错误处理
  451. event.preventDefault();
  452. // 允许证书错误
  453. callback(true);
  454. // 如果需要,可以记录日志
  455. console.warn(`忽略证书错误: ${error} - ${url}`);
  456. });
  457. // In this file you can include the rest of your app's specific main process
  458. // code. You can also put them in separate files and require them here.
  459. ipcMain.on("getData", (event, message) => {
  460. // 控制台打印一下知道来了
  461. console.log(message);
  462. var options = {
  463. 'method': 'GET',
  464. 'url': owoDecode(message.url),
  465. 'headers': message.headers,
  466. strictSSL: false
  467. };
  468. request(options, function (error, response) {
  469. if (error) throw new Error(error);
  470. event.returnValue = response.body
  471. });
  472. })
  473. ipcMain.on("postData", (event, message) => {
  474. // 控制台打印一下知道来了
  475. console.log(message);
  476. var options = {
  477. 'method': 'POST',
  478. 'url': owoDecode(message.url),
  479. 'headers': message.headers,
  480. 'body': message.body,
  481. strictSSL: false
  482. };
  483. request(options, function (error, response) {
  484. if (error) throw new Error(error);
  485. event.returnValue = response.body
  486. });
  487. })
  488. ipcMain.on("setProxy", (event, message) => {
  489. var win = new BrowserWindow({width: 800, height: 1500});
  490. mainWindow.webContents.session.setProxy({
  491. proxyRules: message.url,
  492. proxyBypassRules: 'localhost',
  493. });
  494. event.returnValue = 'ok'
  495. })
  496. // 添加注入代码
  497. ipcMain.on("addPreLoadCode", (event, message) => {
  498. if (message.data) preLoadCode += message.data
  499. })
  500. // 通用保存数据
  501. let dataStor = {}
  502. ipcMain.on("setStoData", (event, message) => {
  503. dataStor[message.key] = message.value
  504. event.returnValue = '{"err":0}'
  505. })
  506. ipcMain.on("getStoData", (event, message) => {
  507. event.returnValue = dataStor[message.key]
  508. })
  509. let maxWindowOpenNum = 10
  510. let nowWindowInd = 0
  511. let childWindowList = []
  512. 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;}
  513. ipcMain.handle("openWindow", async (event, message) => {
  514. console.log(message)
  515. let nowIndex = nowWindowInd++
  516. // 判断是否达到了最大窗口数量
  517. let nowWindowNumTemp = 0
  518. for (let index = 0; index < childWindowList.length; index++) {
  519. const element = childWindowList[index];
  520. if (element) nowWindowNumTemp++
  521. }
  522. if (nowWindowNumTemp > maxWindowOpenNum) {
  523. for (let index = 0; index < childWindowList.length; index++) {
  524. const element = childWindowList[index];
  525. if (element) {
  526. childWindowList[index].close()
  527. childWindowList[index] = null
  528. }
  529. }
  530. }
  531. // 获取对应 session
  532. let partitionName = 'persist:owoAppChild'
  533. if (message.noCache) {
  534. partitionName = 'persist:Session' + Math.round(Math.random()*100000)
  535. }
  536. const customSession = session.fromPartition(partitionName);
  537. if (message.proxy) {
  538. // 设置代理(等待设置完成)
  539. await customSession.setProxy({
  540. proxyRules: message.proxy,
  541. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  542. });
  543. }
  544. let childWindowPreferences = webConfig.webPreferences
  545. if (message.webPreferences) childWindowPreferences = message.webPreferences
  546. childWindowPreferences.partition = partitionName
  547. // 创建新窗口
  548. childWindowList[nowIndex] = new BrowserWindow({
  549. width: message.width || 800,
  550. height: message.height || 600,
  551. autoHideMenuBar: message.autoHideMenuBar || true,
  552. // 无边框
  553. frame: message.autoHideMenuBar || false,
  554. // 全屏
  555. fullscreen: message.autoHideMenuBar || false,
  556. webPreferences: childWindowPreferences
  557. });
  558. // 判断是否静音
  559. if (message.muted) {
  560. // 设置静音
  561. childWindowList[nowIndex].webContents.setAudioMuted(true);
  562. }
  563. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  564. console.log("dom-ready")
  565. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  566. });
  567. childWindowList[nowIndex].on('closed', () => {
  568. childWindowList[nowIndex] = null;
  569. });
  570. // 拦截到新窗口打开请求
  571. childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
  572. console.log('拦截到新窗口打开请求:', url);
  573. childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
  574. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  575. });
  576. childWindowList[nowIndex].loadURL(message.url, {
  577. 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"
  578. });
  579. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  580. })
  581. ipcMain.on("closeWindow", (event, message) => {
  582. if (message && message.key) {
  583. message.key = parseInt(message.key)
  584. setTimeout(() => {
  585. if (childWindowList[message.key]) {
  586. childWindowList[message.key].close()
  587. }
  588. setTimeout(() => {
  589. childWindowList[message.key] = null
  590. }, 0);
  591. }, message.time || 0);
  592. }
  593. event.returnValue = JSON.stringify({"err":0})
  594. })
  595. ipcMain.on("closeAllWindow", (event, message) => {
  596. for (let index = 0; index < childWindowList.length; index++) {
  597. const element = childWindowList[index];
  598. if (element && element.close) {
  599. try {
  600. element.close()
  601. } catch (error) {
  602. console.log(error)
  603. }
  604. }
  605. }
  606. setTimeout(() => {
  607. childWindowList = []
  608. }, 0);
  609. event.returnValue = JSON.stringify({"err":0})
  610. })
  611. ipcMain.on("changeProxy", (event, message) => {
  612. if (message && message.key) {
  613. message.key = parseInt(message.key)
  614. childWindowList[message.key].webContents.session.setProxy({
  615. proxyRules: "",
  616. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  617. });
  618. } else {
  619. for (let index = 0; index < childWindowList.length; index++) {
  620. const element = childWindowList[index];
  621. if (element) {
  622. element.webContents.session.setProxy({
  623. proxyRules: "",
  624. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  625. });
  626. }
  627. }
  628. }
  629. event.returnValue = JSON.stringify({"err":0})
  630. })
  631. ipcMain.on("readConfig", (event, message) => {
  632. if (fs.existsSync("./config.json")) {
  633. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  634. } else {
  635. event.returnValue = {}
  636. }
  637. })
  638. ipcMain.handle("saveFile", async (event, message) => {
  639. try {
  640. // 确保download目录存在
  641. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  642. if (!fs.existsSync(downloadDir)) {
  643. fs.mkdirSync(downloadDir, { recursive: true });
  644. console.log('创建download目录:', downloadDir);
  645. }
  646. // 构建完整的文件路径
  647. const filePath = path.join(downloadDir, message.filename);
  648. // 将字符串编码为缓冲区
  649. const fileBuffer = iconv.encode(message.content, message.encoding ? message.encoding : 'utf8');
  650. fs.writeFileSync(filePath, fileBuffer);
  651. console.log('文件保存成功:', filePath);
  652. return { success: true, path: filePath };
  653. } catch (error) {
  654. console.error('保存文件失败:', error);
  655. return { success: false, error: error.message };
  656. }
  657. });
  658. ipcMain.handle("readFile", async (event, message) => {
  659. try {
  660. // 确保download目录存在
  661. const downloadDir = path.join(__dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''), 'download');
  662. if (!fs.existsSync(downloadDir)) {
  663. fs.mkdirSync(downloadDir, { recursive: true });
  664. console.log('创建download目录:', downloadDir);
  665. }
  666. // 构建完整的文件路径
  667. const filePath = path.join(downloadDir, message.filename);
  668. // 检查文件是否存在
  669. if (!fs.existsSync(filePath)) {
  670. console.log('文件不存在,返回空内容:', filePath);
  671. return { success: true, content: '', exists: false };
  672. }
  673. // 读取文件内容
  674. const buffer = fs.readFileSync(filePath);
  675. const content = iconv.decode(buffer, message.encoding ? message.encoding : 'utf8');
  676. console.log('文件读取成功:', filePath);
  677. return { success: true, content: content, exists: true };
  678. } catch (error) {
  679. console.error('读取文件失败:', error);
  680. return { success: false, error: error.message, exists: false };
  681. }
  682. });
  683. ipcMain.handle("downloadFile", async (event, message) => {
  684. try {
  685. const { url, filename, path: customPath, encoding = 'utf8' } = message;
  686. if (!url || !filename) {
  687. return {
  688. success: false,
  689. error: '缺少必要参数: url 和 filename'
  690. };
  691. }
  692. // 解码 URL(如果需要)
  693. const decodedUrl = owoDecode(url);
  694. // 确定保存路径
  695. let saveDir;
  696. if (customPath) {
  697. saveDir = customPath;
  698. } else {
  699. // 关键修复:使用正确的路径获取方式
  700. const baseDir = app.isPackaged
  701. ? process.resourcesPath // 打包后:resources目录
  702. : process.cwd(); // 开发环境:当前工作目录
  703. saveDir = path.join(baseDir, 'downloads'); // 使用downloads而不是download
  704. }
  705. // 确保路径格式正确(Windows下使用path.normalize)
  706. saveDir = path.normalize(saveDir);
  707. console.log('保存目录:', saveDir);
  708. // 确保目录存在
  709. if (!fs.existsSync(saveDir)) {
  710. try {
  711. fs.mkdirSync(saveDir, {
  712. recursive: true,
  713. mode: 0o755 // 设置目录权限
  714. });
  715. console.log('创建目录成功:', saveDir);
  716. } catch (mkdirError) {
  717. console.error('创建目录失败:', mkdirError);
  718. throw new Error(`无法创建目录: ${mkdirError.message}`);
  719. }
  720. }
  721. // 构建完整的文件路径
  722. const filePath = path.join(saveDir, filename);
  723. console.log(`开始下载: ${decodedUrl} -> ${filePath}`);
  724. // 使用 download 模块下载文件
  725. const data = await download(decodedUrl);
  726. // 保存文件
  727. fs.writeFileSync(filePath, data);
  728. console.log('文件下载成功:', filePath, '大小:', data.length, '字节');
  729. return {
  730. success: true,
  731. path: filePath,
  732. size: data.length,
  733. url: decodedUrl
  734. };
  735. } catch (error) {
  736. console.error('下载文件失败:', error);
  737. return {
  738. success: false,
  739. error: error.message,
  740. url: message.url
  741. };
  742. }
  743. });
  744. ipcMain.on("saveConfig", (event, message) => {
  745. fs.writeFileSync('./config.json', JSON.stringify(message))
  746. event.returnValue = {err: 0}
  747. })
  748. // 设置最大打开窗口数量
  749. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  750. if (message.value) {
  751. maxWindowOpenNum = parseInt(message.value)
  752. }
  753. })
  754. ipcMain.on("readdir", (event, directoryPath) => {
  755. fs.readdir(directoryPath, (err, files) => {
  756. if (err) {
  757. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  758. return;
  759. }
  760. event.returnValue = {err: 0, files}
  761. });
  762. })
  763. ipcMain.on("download", (event, message) => {
  764. download(message.url, message.path, {
  765. filename: message.filename,
  766. });
  767. event.returnValue = {err: 0}
  768. })
  769. // 窗口间通信
  770. ipcMain.on('broadcast-message', (event, message) => {
  771. // 获取发送者
  772. console.log(message)
  773. mainWindow.send('message-broadcast', message);
  774. // 广播给所有其他窗口
  775. for (const win of childWindowList) {
  776. if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
  777. }
  778. });