main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. mainWindow.webContents.on("dom-ready", function() {
  115. if (webConfig.preLoadCode) {
  116. preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8')
  117. }
  118. // console.log(preLoadCode)
  119. mainWindow.webContents.executeJavaScript(preLoadCode);
  120. });
  121. // Open the DevTools.
  122. }
  123. // This method will be called when Electron has finished
  124. // initialization and is ready to create browser windows.
  125. // Some APIs can only be used after this event occurs.
  126. app.whenReady().then(() => {
  127. session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => {
  128. callback({ redirectURL: webConfig.redirectURL});
  129. })
  130. // 判断是否无缓存
  131. if (webConfig.noCache) {
  132. console.log('无缓存模式!')
  133. if (!webConfig.webPreferences) webConfig.webPreferences = {}
  134. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  135. }
  136. createWindow()
  137. app.on('activate', function () {
  138. // On macOS it's common to re-create a window in the app when the
  139. // dock icon is clicked and there are no other windows open.
  140. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  141. })
  142. })
  143. // Quit when all windows are closed, except on macOS. There, it's common
  144. // for applications and their menu bar to stay active until the user quits
  145. // explicitly with Cmd + Q.
  146. app.on('window-all-closed', function () {
  147. if (process.platform !== 'darwin') app.quit()
  148. })
  149. // In this file you can include the rest of your app's specific main process
  150. // code. You can also put them in separate files and require them here.
  151. ipcMain.on("getData", (event, message) => {
  152. // 控制台打印一下知道来了
  153. console.log(message);
  154. var options = {
  155. 'method': 'GET',
  156. 'url': owoDecode(message.url),
  157. 'headers': message.headers,
  158. strictSSL: false
  159. };
  160. request(options, function (error, response) {
  161. if (error) throw new Error(error);
  162. event.returnValue = response.body
  163. });
  164. })
  165. ipcMain.on("postData", (event, message) => {
  166. // 控制台打印一下知道来了
  167. console.log(message);
  168. var options = {
  169. 'method': 'POST',
  170. 'url': owoDecode(message.url),
  171. 'headers': message.headers,
  172. 'body': message.body,
  173. strictSSL: false
  174. };
  175. request(options, function (error, response) {
  176. if (error) throw new Error(error);
  177. event.returnValue = response.body
  178. });
  179. })
  180. ipcMain.on("setProxy", (event, message) => {
  181. var win = new BrowserWindow({width: 800, height: 1500});
  182. mainWindow.webContents.session.setProxy({
  183. proxyRules: message.url,
  184. proxyBypassRules: 'localhost',
  185. });
  186. event.returnValue = 'ok'
  187. })
  188. // 添加注入代码
  189. ipcMain.on("addPreLoadCode", (event, message) => {
  190. if (message.data) preLoadCode += message.data
  191. })
  192. // 通用保存数据
  193. let dataStor = {}
  194. ipcMain.on("setStoData", (event, message) => {
  195. dataStor[message.key] = message.value
  196. event.returnValue = '{"err":0}'
  197. })
  198. ipcMain.on("getStoData", (event, message) => {
  199. event.returnValue = dataStor[message.key]
  200. })
  201. let maxWindowOpenNum = 10
  202. let nowWindowInd = 0
  203. let childWindowList = []
  204. 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;}
  205. ipcMain.on("openWindow", (event, message) => {
  206. let nowIndex = nowWindowInd++
  207. // 判断是否达到了最大窗口数量
  208. let nowWindowNumTemp = 0
  209. for (let index = 0; index < childWindowList.length; index++) {
  210. const element = childWindowList[index];
  211. if (element) nowWindowNumTemp++
  212. }
  213. if (nowWindowNumTemp > maxWindowOpenNum) {
  214. for (let index = 0; index < childWindowList.length; index++) {
  215. const element = childWindowList[index];
  216. if (element) {
  217. childWindowList[index].close()
  218. childWindowList[index] = null
  219. }
  220. }
  221. }
  222. // 创建新窗口
  223. childWindowList[nowIndex] = new BrowserWindow({
  224. width: message.width || 800,
  225. height: message.height || 600,
  226. webPreferences: {
  227. webSecurity: false,
  228. contextIsolation: true,
  229. preload: path.join(__dirname, "preload.js"),
  230. partition: 'persist:Session' + Math.round(Math.random()*100000)
  231. }
  232. });
  233. if (message.proxy) {
  234. childWindowList[nowIndex].webContents.session.setProxy({
  235. proxyRules: message.proxy,
  236. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  237. });
  238. }
  239. childWindowList[nowIndex].loadURL(message.url);
  240. childWindowList[nowIndex].webContents.on("dom-ready", function() {
  241. childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
  242. });
  243. event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
  244. })
  245. ipcMain.on("closeWindow", (event, message) => {
  246. if (message && message.key) {
  247. message.key = parseInt(message.key)
  248. setTimeout(() => {
  249. if (childWindowList[message.key]) {
  250. childWindowList[message.key].close()
  251. }
  252. childWindowList[message.key] = null
  253. }, message.time || 0);
  254. } else {
  255. for (let index = 0; index < childWindowList.length; index++) {
  256. const element = childWindowList[index];
  257. if (element && element.close) {
  258. try {
  259. element.close()
  260. } catch (error) {
  261. console.log(error)
  262. }
  263. }
  264. }
  265. childWindowList = []
  266. }
  267. event.returnValue = JSON.stringify({"err":0})
  268. })
  269. ipcMain.on("changeProxy", (event, message) => {
  270. if (message && message.key) {
  271. message.key = parseInt(message.key)
  272. childWindowList[message.key].webContents.session.setProxy({
  273. proxyRules: "",
  274. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  275. });
  276. } else {
  277. for (let index = 0; index < childWindowList.length; index++) {
  278. const element = childWindowList[index];
  279. if (element) {
  280. element.webContents.session.setProxy({
  281. proxyRules: "",
  282. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  283. });
  284. }
  285. }
  286. }
  287. event.returnValue = JSON.stringify({"err":0})
  288. })
  289. ipcMain.on("readConfig", (event, message) => {
  290. if (fs.existsSync("./config.json")) {
  291. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  292. } else {
  293. event.returnValue = {}
  294. }
  295. })
  296. ipcMain.on("saveConfig", (event, message) => {
  297. fs.writeFileSync('./config.json', JSON.stringify(message))
  298. event.returnValue = {err: 0}
  299. })
  300. // 设置最大打开窗口数量
  301. ipcMain.on("setMaxWindowOpenNum", (event, message) => {
  302. if (message.value) {
  303. maxWindowOpenNum = parseInt(message.value)
  304. }
  305. })
  306. ipcMain.on("readdir", (event, directoryPath) => {
  307. fs.readdir(directoryPath, (err, files) => {
  308. if (err) {
  309. event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
  310. return;
  311. }
  312. event.returnValue = {err: 0, files}
  313. });
  314. })
  315. ipcMain.on("download", (event, message) => {
  316. download(message.url, message.path, {
  317. filename: message.filename,
  318. });
  319. event.returnValue = {err: 0}
  320. })