main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 request = require('request');
  6. const download = require('download');
  7. // 读取配置文件
  8. let enterURL = 'https://demos.run/debuger/index.html'
  9. let webConfig = {
  10. width: 376,
  11. height: 667,
  12. webPreferences: {
  13. webSecurity: false,
  14. contextIsolation: true,
  15. nodeIntegration: false,
  16. allowRunningInsecureContent: true, // 允许不安全内容
  17. plugins: true,
  18. scrollBounce: true,
  19. preload: path.join(__dirname, "preload.js")
  20. },
  21. autoHideMenuBar: true
  22. // 无边框
  23. // frame: false,
  24. // 全屏
  25. // fullscreen: true
  26. }
  27. // 判断是否有特殊配置文件
  28. console.log(__dirname + "\\config.json")
  29. if (fs.existsSync("./config.json")) {
  30. let configStr = fs.readFileSync('./config.json', 'utf-8')
  31. // 特殊符号表示运行目录
  32. configStr = configStr.replaceAll('<dir>', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
  33. webConfig = JSON.parse(configStr)
  34. enterURL = webConfig.enterURL
  35. }
  36. console.log(webConfig)
  37. 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]"}
  38. function owoReplaceAll(str, s1, s2) {
  39. while (str.indexOf(s1) >= 0) {
  40. str = str.replace(s1, s2)
  41. }
  42. return str
  43. }
  44. function owoDecode(itemStr) {
  45. for (let item in codeMap) {
  46. itemStr = owoReplaceAll(itemStr, codeMap[item], item)
  47. }
  48. while (itemStr.indexOf(']') >= 0) {
  49. itemStr = owoDecode(itemStr)
  50. }
  51. return itemStr
  52. }
  53. let mainWindow = null
  54. let preLoadCode = `
  55. var owoApp = 5
  56. window.owoPC = true
  57. window.electronConfig = ${JSON.stringify(webConfig)};
  58. function loadScript(url, callback) {
  59. var script = document.createElement("script")
  60. script.type = "text/javascript";
  61. if (script.readyState) { //IE
  62. script.onreadystatechange = function () {
  63. if (script.readyState == "loaded" || script.readyState == "complete") {
  64. script.onreadystatechange = null;
  65. if (callback) callback();
  66. }
  67. };
  68. } else { //Others
  69. script.onload = function () {
  70. if (callback) callback();
  71. };
  72. }
  73. script.src = url;
  74. var head = document.head || document.getElementsByTagName('head')[0];
  75. head.appendChild(script);
  76. }
  77. function loadJsCode(code){
  78. var script = document.createElement('script');
  79. script.type = 'text/javascript';
  80. //for Chrome Firefox Opera Safari
  81. script.appendChild(document.createTextNode(code));
  82. //for IE
  83. //script.text = code;
  84. document.body.appendChild(script);
  85. }
  86. function loadCSS (url) {
  87. var link = document.createElement("link");
  88. link.rel = "stylesheet";
  89. link.type = "text/css";
  90. link.href = url;
  91. document.getElementsByTagName("head")[0].appendChild(link);
  92. }
  93. loadScript('https://cunchu.site/app/main.js');
  94. `
  95. // 拦截数据
  96. function setupDebuggerInterceptor(webContents) {
  97. webContents.debugger.attach('1.3');
  98. webContents.debugger.on('message', (event, method, params) => {
  99. if (method === 'Network.responseReceived') {
  100. const { requestId, response } = params;
  101. // 检查是否是目标URL
  102. if (response.url.includes('video_billboard')) {
  103. console.log('拦截到响应:', response.url, response.status);
  104. // 获取响应体(需要额外处理)
  105. webContents.debugger.sendCommand('Network.getResponseBody', { requestId })
  106. .then(({ body }) => {
  107. const interceptedData = {
  108. url: response.url,
  109. statusCode: response.status,
  110. responseBody: body,
  111. headers: response.headers
  112. };
  113. console.log('hook data:')
  114. console.log(interceptedData)
  115. if (mainWindow && !mainWindow.isDestroyed()) {
  116. mainWindow.webContents.executeJavaScript(`
  117. if (window.onInterceptedData) {
  118. window.onInterceptedData(${JSON.stringify(interceptedData)});
  119. }
  120. `).catch(console.error);
  121. }
  122. })
  123. .catch(console.error);
  124. }
  125. }
  126. });
  127. // 启用网络跟踪
  128. webContents.debugger.sendCommand('Network.enable');
  129. }
  130. function createWindow (partitionSession) {
  131. // console.log(webConfig)
  132. mainWindow = new BrowserWindow(webConfig)
  133. // 代理
  134. if (webConfig.proxy) {
  135. partitionSession.setProxy({
  136. proxyRules: webConfig.proxy,
  137. proxyBypassRules: 'localhost',
  138. }, function () {
  139. console.log('代理设置完毕')
  140. });
  141. }
  142. // 拦截到新窗口打开请求
  143. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  144. console.log('拦截到新窗口打开请求:', url);
  145. mainWindow.loadURL(url); // 当前窗口跳转
  146. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  147. });
  148. if (enterURL.startsWith('http')) {
  149. mainWindow.loadURL(enterURL)
  150. } else {
  151. console.log(path.join(__dirname, enterURL))
  152. mainWindow.loadFile(enterURL)
  153. }
  154. if (webConfig.preLoadFile) {
  155. console.log(`加载额外JS: ${webConfig.preLoadFile}`)
  156. preLoadCode += fs.readFileSync(webConfig.preLoadFile, 'utf-8')
  157. }
  158. mainWindow.webContents.on("dom-ready", function() {
  159. mainWindow.webContents.executeJavaScript(preLoadCode);
  160. });
  161. mainWindow.webContents.on("did-finish-load", function() {
  162. if (webConfig.interceptor && webConfig.interceptor.length > 0) {
  163. setupDebuggerInterceptor(mainWindow.webContents);
  164. }
  165. });
  166. // 打开新窗口触发
  167. mainWindow.webContents.on("did-create-window", function(neWindow) {
  168. neWindow.webContents.on("dom-ready", function() {
  169. neWindow.webContents.executeJavaScript(preLoadCode);
  170. });
  171. });
  172. }
  173. function setupCSPRemoval(ses) {
  174. ses.webRequest.onHeadersReceived((details, callback) => {
  175. const responseHeaders = details.responseHeaders || {};
  176. const cspHeaders = [
  177. 'content-security-policy',
  178. 'Content-Security-Policy',
  179. 'content-security-policy-report-only',
  180. 'x-content-security-policy',
  181. 'x-webkit-csp'
  182. ];
  183. cspHeaders.forEach(header => {
  184. if (responseHeaders[header]) {
  185. // console.log('Removing', header, 'from', details.url);
  186. delete responseHeaders[header];
  187. }
  188. });
  189. callback({ cancel: false, responseHeaders });
  190. });
  191. }
  192. // This method will be called when Electron has finished
  193. // initialization and is ready to create browser windows.
  194. // Some APIs can only be used after this event occurs.
  195. app.whenReady().then(() => {
  196. // 判断是否无缓存
  197. if (webConfig.noCache) {
  198. console.log('无缓存模式!')
  199. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  200. } else {
  201. webConfig.webPreferences.partition = 'persist:owoApp'
  202. }
  203. // 去掉安全措施
  204. const partitionSession = session.fromPartition(webConfig.webPreferences.partition);
  205. setupCSPRemoval(partitionSession);
  206. createWindow(partitionSession)
  207. app.on('activate', function () {
  208. // On macOS it's common to re-create a window in the app when the
  209. // dock icon is clicked and there are no other windows open.
  210. if (BrowserWindow.getAllWindows().length === 0) createWindow(partitionSession)
  211. })
  212. })
  213. // Quit when all windows are closed, except on macOS. There, it's common
  214. // for applications and their menu bar to stay active until the user quits
  215. // explicitly with Cmd + Q.
  216. app.on('window-all-closed', function () {
  217. if (process.platform !== 'darwin') app.quit()
  218. })
  219. // In this file you can include the rest of your app's specific main process
  220. // code. You can also put them in separate files and require them here.
  221. ipcMain.on("getData", (event, message) => {
  222. // 控制台打印一下知道来了
  223. console.log(message);
  224. var options = {
  225. 'method': 'GET',
  226. 'url': owoDecode(message.url),
  227. 'headers': message.headers,
  228. strictSSL: false
  229. };
  230. request(options, function (error, response) {
  231. if (error) throw new Error(error);
  232. event.returnValue = response.body
  233. });
  234. })
  235. ipcMain.on("postData", (event, message) => {
  236. // 控制台打印一下知道来了
  237. console.log(message);
  238. var options = {
  239. 'method': 'POST',
  240. 'url': owoDecode(message.url),
  241. 'headers': message.headers,
  242. 'body': message.body,
  243. strictSSL: false
  244. };
  245. request(options, function (error, response) {
  246. if (error) throw new Error(error);
  247. event.returnValue = response.body
  248. });
  249. })
  250. ipcMain.on("setProxy", (event, message) => {
  251. var win = new BrowserWindow({width: 800, height: 1500});
  252. mainWindow.webContents.session.setProxy({
  253. proxyRules: message.url,
  254. proxyBypassRules: 'localhost',
  255. });
  256. event.returnValue = 'ok'
  257. })
  258. // 添加注入代码
  259. ipcMain.on("addPreLoadCode", (event, message) => {
  260. if (message.data) preLoadCode += message.data
  261. })
  262. // 通用保存数据
  263. let dataStor = {}
  264. ipcMain.on("setStoData", (event, message) => {
  265. dataStor[message.key] = message.value
  266. event.returnValue = '{"err":0}'
  267. })
  268. ipcMain.on("getStoData", (event, message) => {
  269. event.returnValue = dataStor[message.key]
  270. })
  271. let maxWindowOpenNum = 10
  272. let nowWindowInd = 0
  273. let childWindowList = []
  274. 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;}
  275. ipcMain.handle("openWindow", async (event, message) => {
  276. console.log(message)
  277. let nowIndex = nowWindowInd++
  278. // 判断是否达到了最大窗口数量
  279. let nowWindowNumTemp = 0
  280. for (let index = 0; index < childWindowList.length; index++) {
  281. const element = childWindowList[index];
  282. if (element) nowWindowNumTemp++
  283. }
  284. if (nowWindowNumTemp > maxWindowOpenNum) {
  285. for (let index = 0; index < childWindowList.length; index++) {
  286. const element = childWindowList[index];
  287. if (element) {
  288. childWindowList[index].close()
  289. childWindowList[index] = null
  290. }
  291. }
  292. }
  293. // 获取对应 session
  294. let partitionName = 'persist:owoAppChild'
  295. if (message.noCache) {
  296. partitionName = 'persist:Session' + Math.round(Math.random()*100000)
  297. }
  298. const customSession = session.fromPartition(partitionName);
  299. if (message.proxy) {
  300. // 设置代理(等待设置完成)
  301. await customSession.setProxy({
  302. proxyRules: message.proxy,
  303. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  304. });
  305. }
  306. let childWindowPreferences = webConfig.webPreferences
  307. if (message.webPreferences) childWindowPreferences = message.webPreferences
  308. childWindowPreferences.partition = partitionName
  309. // 创建新窗口
  310. childWindowList[nowIndex] = new BrowserWindow({
  311. width: message.width || 800,
  312. height: message.height || 600,
  313. webPreferences: childWindowPreferences
  314. });
  315. // 判断是否静音
  316. if (message.muted) {
  317. // 设置静音
  318. childWindowList[nowIndex].webContents.setAudioMuted(true);
  319. }
  320. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  321. console.log("dom-ready")
  322. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  323. });
  324. childWindowList[nowIndex].on('closed', () => {
  325. childWindowList[nowIndex] = null;
  326. });
  327. // 拦截到新窗口打开请求
  328. childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
  329. console.log('拦截到新窗口打开请求:', url);
  330. childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
  331. return { action: 'deny' }; // 阻止 Electron 弹出窗口
  332. });
  333. childWindowList[nowIndex].loadURL(message.url, {
  334. 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"
  335. });
  336. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  337. })
  338. ipcMain.on("closeWindow", (event, message) => {
  339. if (message && message.key) {
  340. message.key = parseInt(message.key)
  341. setTimeout(() => {
  342. if (childWindowList[message.key]) {
  343. childWindowList[message.key].close()
  344. }
  345. setTimeout(() => {
  346. childWindowList[message.key] = null
  347. }, 0);
  348. }, message.time || 0);
  349. }
  350. event.returnValue = JSON.stringify({"err":0})
  351. })
  352. ipcMain.on("closeAllWindow", (event, message) => {
  353. for (let index = 0; index < childWindowList.length; index++) {
  354. const element = childWindowList[index];
  355. if (element && element.close) {
  356. try {
  357. element.close()
  358. } catch (error) {
  359. console.log(error)
  360. }
  361. }
  362. }
  363. setTimeout(() => {
  364. childWindowList = []
  365. }, 0);
  366. event.returnValue = JSON.stringify({"err":0})
  367. })
  368. ipcMain.on("changeProxy", (event, message) => {
  369. if (message && message.key) {
  370. message.key = parseInt(message.key)
  371. childWindowList[message.key].webContents.session.setProxy({
  372. proxyRules: "",
  373. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  374. });
  375. } else {
  376. for (let index = 0; index < childWindowList.length; index++) {
  377. const element = childWindowList[index];
  378. if (element) {
  379. element.webContents.session.setProxy({
  380. proxyRules: "",
  381. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  382. });
  383. }
  384. }
  385. }
  386. event.returnValue = JSON.stringify({"err":0})
  387. })
  388. ipcMain.on("readConfig", (event, message) => {
  389. if (fs.existsSync("./config.json")) {
  390. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  391. } else {
  392. event.returnValue = {}
  393. }
  394. })
  395. ipcMain.handle("saveFile", async (event, message) => {
  396. try {
  397. // 确保download目录存在
  398. const downloadDir = path.join(__dirname, 'download');
  399. if (!fs.existsSync(downloadDir)) {
  400. fs.mkdirSync(downloadDir, { recursive: true });
  401. console.log('创建download目录:', downloadDir);
  402. }
  403. // 构建完整的文件路径
  404. const filePath = path.join(downloadDir, message.filename);
  405. // 保存文件内容
  406. fs.writeFileSync(filePath, message.content);
  407. console.log('文件保存成功:', filePath);
  408. return { success: true, path: filePath };
  409. } catch (error) {
  410. console.error('保存文件失败:', error);
  411. return { success: false, error: error.message };
  412. }
  413. });
  414. ipcMain.on("saveConfig", (event, message) => {
  415. fs.writeFileSync('./config.json', JSON.stringify(message))
  416. event.returnValue = {err: 0}
  417. })
  418. // 设置最大打开窗口数量
  419. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  420. if (message.value) {
  421. maxWindowOpenNum = parseInt(message.value)
  422. }
  423. })
  424. ipcMain.on("readdir", (event, directoryPath) => {
  425. fs.readdir(directoryPath, (err, files) => {
  426. if (err) {
  427. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  428. return;
  429. }
  430. event.returnValue = {err: 0, files}
  431. });
  432. })
  433. ipcMain.on("download", (event, message) => {
  434. download(message.url, message.path, {
  435. filename: message.filename,
  436. });
  437. event.returnValue = {err: 0}
  438. })
  439. // 窗口间通信
  440. ipcMain.on('broadcast-message', (event, message) => {
  441. // 获取发送者
  442. console.log(message)
  443. mainWindow.send('message-broadcast', message);
  444. // 广播给所有其他窗口
  445. for (const win of childWindowList) {
  446. if (win && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
  447. }
  448. });