main.js 16 KB

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