// Modules to control application life and create native browser window const {app, ipcMain, BrowserWindow, session} = require('electron') const path = require('path') const fs = require('fs') const request = require('request'); const download = require('download'); // 读取配置文件 let enterURL = 'https://demos.run/debuger/index.html' let webConfig = { width: 376, height: 667, webPreferences: { webSecurity: false, contextIsolation: true, preload: path.join(__dirname, "preload.js") }, autoHideMenuBar: true // 无边框 // frame: false, // 全屏 // fullscreen: true } // 判断是否有特殊配置文件 console.log(__dirname + "\\config.json") if (fs.existsSync("./config.json")) { webConfig = JSON.parse(fs.readFileSync('./config.json', 'utf-8')) enterURL = webConfig.enterURL } 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]"} function owoReplaceAll(str, s1, s2) { while (str.indexOf(s1) >= 0) { str = str.replace(s1, s2) } return str } function owoDecode(itemStr) { for (let item in codeMap) { itemStr = owoReplaceAll(itemStr, codeMap[item], item) } while (itemStr.indexOf(']') >= 0) { itemStr = owoDecode(itemStr) } return itemStr } const xxx_filter = { urls: webConfig.redirect || [] } let mainWindow = null function createWindow () { // Create the browser window. // if (config.preload) { // console.log(config.preload) // webConfig.webPreferences.preload = config.preload // } if (!webConfig.webPreferences.preload) webConfig.webPreferences.preload = path.join(__dirname, "preload.js") console.log(webConfig) mainWindow = new BrowserWindow(webConfig) // 代理 if (webConfig.proxy) { mainWindow.webContents.session.setProxy({ proxyRules: webConfig.proxy, proxyBypassRules: 'localhost', }, function () { console.log('代理设置完毕') }); } if (enterURL.startsWith('http')) { mainWindow.loadURL(enterURL) } else { console.log(path.join(__dirname, enterURL)) mainWindow.loadFile(path.join(__dirname, enterURL)) } mainWindow.webContents.on("dom-ready", function() { let preLoadCode = ` var owoApp = 4 window.owoPC = true window.electronConfig = ${JSON.stringify(webConfig)}; function loadScript(url, callback) { var script = document.createElement("script") script.type = "text/javascript"; if (script.readyState) { //IE script.onreadystatechange = function () { if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; if (callback) callback(); } }; } else { //Others script.onload = function () { if (callback) callback(); }; } script.src = url; var head = document.head || document.getElementsByTagName('head')[0]; head.appendChild(script); } function loadJsCode(code){ var script = document.createElement('script'); script.type = 'text/javascript'; //for Chrome Firefox Opera Safari script.appendChild(document.createTextNode(code)); //for IE //script.text = code; document.body.appendChild(script); } function loadCSS (url) { var link = document.createElement("link"); link.rel = "stylesheet"; link.type = "text/css"; link.href = url; document.getElementsByTagName("head")[0].appendChild(link); } loadScript('https://cunchu.site/app/main.js') ` if (webConfig.preLoadCode) { preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8') } // console.log(preLoadCode) mainWindow.webContents.executeJavaScript(preLoadCode); }); // Open the DevTools. } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.whenReady().then(() => { session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => { callback({ redirectURL: webConfig.redirectURL}); }) // 判断是否无缓存 if (webConfig.noCache) { console.log('无缓存模式!') if (!webConfig.webPreferences) webConfig.webPreferences = {} webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000) } createWindow() app.on('activate', function () { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (BrowserWindow.getAllWindows().length === 0) createWindow() }) }) // Quit when all windows are closed, except on macOS. There, it's common // for applications and their menu bar to stay active until the user quits // explicitly with Cmd + Q. app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit() }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here. ipcMain.on("getData", (event, message) => { // 控制台打印一下知道来了 console.log(message); var options = { 'method': 'GET', 'url': owoDecode(message.url), 'headers': message.headers, strictSSL: false }; request(options, function (error, response) { if (error) throw new Error(error); event.returnValue = response.body }); }) ipcMain.on("postData", (event, message) => { // 控制台打印一下知道来了 console.log(message); var options = { 'method': 'POST', 'url': owoDecode(message.url), 'headers': message.headers, 'body': message.body, strictSSL: false }; request(options, function (error, response) { if (error) throw new Error(error); event.returnValue = response.body }); }) ipcMain.on("setProxy", (event, message) => { var win = new BrowserWindow({width: 800, height: 1500}); mainWindow.webContents.session.setProxy({ proxyRules: message.url, proxyBypassRules: 'localhost', }); event.returnValue = 'ok' }) let childWindowList = [] ipcMain.on("openWindow", (event, message) => { let nowIndex = childWindowList.length childWindowList[nowIndex] = new BrowserWindow({ width: message.width || 800, height: message.height || 600, webPreferences: { partition: 'persist:Session' + Math.round(Math.random()*100000) } }); if (message.proxy) { childWindowList[nowIndex].webContents.session.setProxy({ proxyRules: message.proxy, proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"], }); } childWindowList[nowIndex].loadURL(message.url); event.returnValue = JSON.stringify({"err":0,"inx":nowIndex}) }) ipcMain.on("closeWindow", (event, message) => { childWindowList.forEach(element => { if (element && element.close) { element.close() } }); childWindowList = [] event.returnValue = JSON.stringify({"err":0}) }) ipcMain.on("changeProxy", (event, message) => { childWindowList.forEach(element => { if (element) { element.webContents.session.setProxy({ proxyRules: "", proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"], }); } }); event.returnValue = JSON.stringify({"err":0}) }) ipcMain.on("readConfig", (event, message) => { if (fs.existsSync("./config.json")) { event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8')) } else { event.returnValue = {} } }) ipcMain.on("saveConfig", (event, message) => { fs.writeFileSync('./config.json', JSON.stringify(message)) event.returnValue = {err: 0} }) ipcMain.on("download", (event, message) => { download(message.url, message.path, { filename: message.filename, }); event.returnValue = {err: 0} })