main.js 13 KB

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