main.js 12 KB

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