main.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. nativeWindowOpen: false,
  15. contextIsolation: true,
  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. webConfig = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  28. enterURL = webConfig.enterURL
  29. }
  30. 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]"}
  31. function owoReplaceAll(str, s1, s2) {
  32. while (str.indexOf(s1) >= 0) {
  33. str = str.replace(s1, s2)
  34. }
  35. return str
  36. }
  37. function owoDecode(itemStr) {
  38. for (let item in codeMap) {
  39. itemStr = owoReplaceAll(itemStr, codeMap[item], item)
  40. }
  41. while (itemStr.indexOf(']') >= 0) {
  42. itemStr = owoDecode(itemStr)
  43. }
  44. return itemStr
  45. }
  46. const xxx_filter = {
  47. urls: webConfig.redirect || []
  48. }
  49. function createWindow () {
  50. // Create the browser window.
  51. // if (config.preload) {
  52. // console.log(config.preload)
  53. // webConfig.webPreferences.preload = config.preload
  54. // }
  55. if (!webConfig.webPreferences.preload) webConfig.webPreferences.preload = path.join(__dirname, "preload.js")
  56. console.log(webConfig)
  57. const mainWindow = new BrowserWindow(webConfig)
  58. // and load the index.html of the app.
  59. // mainWindow.loadFile('./resources/01.html')
  60. // mainWindow.webContents.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1 electron'
  61. // mainWindow.webContents.openDevTools()
  62. if (enterURL.startsWith('http')) {
  63. mainWindow.loadURL(enterURL)
  64. } else {
  65. console.log(path.join(__dirname, enterURL))
  66. mainWindow.loadFile(path.join(__dirname, enterURL))
  67. }
  68. mainWindow.webContents.on("dom-ready", function() {
  69. // 代理
  70. if (webConfig.proxy) {
  71. mainWindow.webContents.session.setProxy({
  72. proxyRules: webConfig.proxy,
  73. proxyBypassRules: 'localhost',
  74. }, function () {
  75. console.log('代理设置完毕')
  76. });
  77. }
  78. let preLoadCode = `
  79. var owoApp = 4
  80. window.owoPC = true
  81. window.electronConfig = ${JSON.stringify(webConfig)};
  82. function loadScript(url, callback) {
  83. var script = document.createElement("script")
  84. script.type = "text/javascript";
  85. if (script.readyState) { //IE
  86. script.onreadystatechange = function () {
  87. if (script.readyState == "loaded" || script.readyState == "complete") {
  88. script.onreadystatechange = null;
  89. if (callback) callback();
  90. }
  91. };
  92. } else { //Others
  93. script.onload = function () {
  94. if (callback) callback();
  95. };
  96. }
  97. script.src = url;
  98. var head = document.head || document.getElementsByTagName('head')[0];
  99. head.appendChild(script);
  100. }
  101. function loadJsCode(code){
  102. var script = document.createElement('script');
  103. script.type = 'text/javascript';
  104. //for Chrome Firefox Opera Safari
  105. script.appendChild(document.createTextNode(code));
  106. //for IE
  107. //script.text = code;
  108. document.body.appendChild(script);
  109. }
  110. function loadCSS (url) {
  111. var link = document.createElement("link");
  112. link.rel = "stylesheet";
  113. link.type = "text/css";
  114. link.href = url;
  115. document.getElementsByTagName("head")[0].appendChild(link);
  116. }
  117. let needLoad_App = localStorage.getItem('needLoad_App')
  118. needLoad_App = null
  119. function owoReload (data) {
  120. data.forEach(element => {
  121. if (element.type == 'app') {
  122. let scriptList = element.script
  123. if (typeof scriptList == 'string') {
  124. scriptList = JSON.parse(scriptList)
  125. }
  126. if (typeof element.style == 'string') {
  127. element.style = JSON.parse(element.style)
  128. }
  129. scriptList.forEach(scriptItem => {
  130. loadScript(scriptItem)
  131. });
  132. element.style.forEach(styleItem => {
  133. loadCSS(styleItem)
  134. });
  135. if (element.data) loadJsCode(element.data)
  136. }
  137. });
  138. }
  139. if (needLoad_App) {
  140. owoReload(JSON.parse(needLoad_App))
  141. } else {
  142. fetch("https://going.run/assist?route=app&username=${webConfig.username}", {
  143. method: 'POST',
  144. headers: {
  145. "Content-Type": "application/json"
  146. },
  147. body: JSON.stringify({
  148. "url": location.href,
  149. "edition": 1
  150. }),
  151. redirect: 'follow'
  152. }).then(response => response.json())
  153. .then(result => {
  154. if (result['err'] == 0) {
  155. owoReload(result['data'])
  156. localStorage.setItem('needLoad_App', JSON.stringify(result['data']))
  157. }
  158. })
  159. .catch(error => console.log('error', error))
  160. }
  161. `
  162. if (webConfig.preLoadCode) {
  163. preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8')
  164. }
  165. // console.log(preLoadCode)
  166. mainWindow.webContents.executeJavaScript(preLoadCode);
  167. });
  168. // Open the DevTools.
  169. }
  170. // This method will be called when Electron has finished
  171. // initialization and is ready to create browser windows.
  172. // Some APIs can only be used after this event occurs.
  173. app.whenReady().then(() => {
  174. session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => {
  175. callback({ redirectURL: webConfig.redirectURL});
  176. })
  177. // 判断是否无缓存
  178. if (webConfig.noCache) {
  179. console.log('无缓存模式!')
  180. if (!webConfig.webPreferences) webConfig.webPreferences = {}
  181. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  182. }
  183. createWindow()
  184. app.on('activate', function () {
  185. // On macOS it's common to re-create a window in the app when the
  186. // dock icon is clicked and there are no other windows open.
  187. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  188. })
  189. })
  190. // Quit when all windows are closed, except on macOS. There, it's common
  191. // for applications and their menu bar to stay active until the user quits
  192. // explicitly with Cmd + Q.
  193. app.on('window-all-closed', function () {
  194. if (process.platform !== 'darwin') app.quit()
  195. })
  196. // In this file you can include the rest of your app's specific main process
  197. // code. You can also put them in separate files and require them here.
  198. ipcMain.on("getData", (event, message) => {
  199. // 控制台打印一下知道来了
  200. console.log(message);
  201. var options = {
  202. 'method': 'GET',
  203. 'url': owoDecode(message.url),
  204. 'headers': message.headers,
  205. strictSSL: false
  206. };
  207. request(options, function (error, response) {
  208. if (error) throw new Error(error);
  209. event.returnValue = response.body
  210. });
  211. })
  212. ipcMain.on("postData", (event, message) => {
  213. // 控制台打印一下知道来了
  214. console.log(message);
  215. var options = {
  216. 'method': 'POST',
  217. 'url': owoDecode(message.url),
  218. 'headers': message.headers,
  219. 'body': message.body,
  220. strictSSL: false
  221. };
  222. request(options, function (error, response) {
  223. if (error) throw new Error(error);
  224. event.returnValue = response.body
  225. });
  226. })
  227. ipcMain.on("readConfig", (event, message) => {
  228. if (fs.existsSync("./config.json")) {
  229. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  230. } else {
  231. event.returnValue = {}
  232. }
  233. })
  234. ipcMain.on("saveConfig", (event, message) => {
  235. fs.writeFileSync('./config.json', JSON.stringify(message))
  236. event.returnValue = {err: 0}
  237. })
  238. ipcMain.on("download", (event, message) => {
  239. download(message.url, message.path, {
  240. filename: message.filename,
  241. });
  242. event.returnValue = {err: 0}
  243. })