main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. 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. mainWindow = new BrowserWindow(webConfig)
  58. // 代理
  59. if (webConfig.proxy) {
  60. mainWindow.webContents.session.setProxy({
  61. proxyRules: webConfig.proxy,
  62. proxyBypassRules: 'localhost',
  63. }, function () {
  64. console.log('代理设置完毕')
  65. });
  66. }
  67. if (enterURL.startsWith('http')) {
  68. mainWindow.loadURL(enterURL)
  69. } else {
  70. console.log(path.join(__dirname, enterURL))
  71. mainWindow.loadFile(path.join(__dirname, enterURL))
  72. }
  73. mainWindow.webContents.on("dom-ready", function() {
  74. let preLoadCode = `
  75. var owoApp = 4
  76. window.owoPC = true
  77. window.electronConfig = ${JSON.stringify(webConfig)};
  78. function loadScript(url, callback) {
  79. var script = document.createElement("script")
  80. script.type = "text/javascript";
  81. if (script.readyState) { //IE
  82. script.onreadystatechange = function () {
  83. if (script.readyState == "loaded" || script.readyState == "complete") {
  84. script.onreadystatechange = null;
  85. if (callback) callback();
  86. }
  87. };
  88. } else { //Others
  89. script.onload = function () {
  90. if (callback) callback();
  91. };
  92. }
  93. script.src = url;
  94. var head = document.head || document.getElementsByTagName('head')[0];
  95. head.appendChild(script);
  96. }
  97. function loadJsCode(code){
  98. var script = document.createElement('script');
  99. script.type = 'text/javascript';
  100. //for Chrome Firefox Opera Safari
  101. script.appendChild(document.createTextNode(code));
  102. //for IE
  103. //script.text = code;
  104. document.body.appendChild(script);
  105. }
  106. function loadCSS (url) {
  107. var link = document.createElement("link");
  108. link.rel = "stylesheet";
  109. link.type = "text/css";
  110. link.href = url;
  111. document.getElementsByTagName("head")[0].appendChild(link);
  112. }
  113. let needLoad_App = null
  114. if (window && window.localStorage) {
  115. needLoad_App = localStorage.getItem('needLoad_App')
  116. }
  117. function owoReload (data) {
  118. data.forEach(element => {
  119. if (element.type == 'app') {
  120. let scriptList = element.script
  121. if (typeof scriptList == 'string') {
  122. scriptList = JSON.parse(scriptList)
  123. }
  124. if (typeof element.style == 'string') {
  125. element.style = JSON.parse(element.style)
  126. }
  127. scriptList.forEach(scriptItem => {
  128. loadScript(scriptItem)
  129. });
  130. element.style.forEach(styleItem => {
  131. loadCSS(styleItem)
  132. });
  133. if (element.data) loadJsCode(element.data)
  134. }
  135. });
  136. }
  137. if (needLoad_App) {
  138. owoReload(JSON.parse(needLoad_App))
  139. } else {
  140. fetch("https://going.run/assist?route=app&username=${webConfig.username}", {
  141. method: 'POST',
  142. headers: {
  143. "Content-Type": "application/json"
  144. },
  145. body: JSON.stringify({
  146. "url": location.href,
  147. "edition": 1
  148. }),
  149. redirect: 'follow'
  150. }).then(response => response.json())
  151. .then(result => {
  152. if (result['err'] == 0) {
  153. owoReload(result['data'])
  154. localStorage.setItem('needLoad_App', JSON.stringify(result['data']))
  155. }
  156. })
  157. .catch(error => console.log('error', error))
  158. }
  159. `
  160. if (webConfig.preLoadCode) {
  161. preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8')
  162. }
  163. // console.log(preLoadCode)
  164. mainWindow.webContents.executeJavaScript(preLoadCode);
  165. });
  166. // Open the DevTools.
  167. }
  168. // This method will be called when Electron has finished
  169. // initialization and is ready to create browser windows.
  170. // Some APIs can only be used after this event occurs.
  171. app.whenReady().then(() => {
  172. session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => {
  173. callback({ redirectURL: webConfig.redirectURL});
  174. })
  175. // 判断是否无缓存
  176. if (webConfig.noCache) {
  177. console.log('无缓存模式!')
  178. if (!webConfig.webPreferences) webConfig.webPreferences = {}
  179. webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
  180. }
  181. createWindow()
  182. app.on('activate', function () {
  183. // On macOS it's common to re-create a window in the app when the
  184. // dock icon is clicked and there are no other windows open.
  185. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  186. })
  187. })
  188. // Quit when all windows are closed, except on macOS. There, it's common
  189. // for applications and their menu bar to stay active until the user quits
  190. // explicitly with Cmd + Q.
  191. app.on('window-all-closed', function () {
  192. if (process.platform !== 'darwin') app.quit()
  193. })
  194. // In this file you can include the rest of your app's specific main process
  195. // code. You can also put them in separate files and require them here.
  196. ipcMain.on("getData", (event, message) => {
  197. // 控制台打印一下知道来了
  198. console.log(message);
  199. var options = {
  200. 'method': 'GET',
  201. 'url': owoDecode(message.url),
  202. 'headers': message.headers,
  203. strictSSL: false
  204. };
  205. request(options, function (error, response) {
  206. if (error) throw new Error(error);
  207. event.returnValue = response.body
  208. });
  209. })
  210. ipcMain.on("postData", (event, message) => {
  211. // 控制台打印一下知道来了
  212. console.log(message);
  213. var options = {
  214. 'method': 'POST',
  215. 'url': owoDecode(message.url),
  216. 'headers': message.headers,
  217. 'body': message.body,
  218. strictSSL: false
  219. };
  220. request(options, function (error, response) {
  221. if (error) throw new Error(error);
  222. event.returnValue = response.body
  223. });
  224. })
  225. ipcMain.on("setProxy", (event, message) => {
  226. var win = new BrowserWindow({width: 800, height: 1500});
  227. mainWindow.webContents.session.setProxy({
  228. proxyRules: message.url,
  229. proxyBypassRules: 'localhost',
  230. });
  231. event.returnValue = 'ok'
  232. })
  233. let childWindowList = []
  234. ipcMain.on("openWindow", (event, message) => {
  235. let nowIndex = childWindowList.length
  236. childWindowList[nowIndex] = new BrowserWindow({
  237. width: message.width || 800,
  238. height: message.height || 600,
  239. webPreferences: {
  240. partition: 'persist:Session' + Math.round(Math.random()*100000)
  241. }
  242. });
  243. if (message.proxy) {
  244. childWindowList[nowIndex].webContents.session.setProxy({
  245. proxyRules: message.proxy,
  246. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  247. });
  248. }
  249. childWindowList[nowIndex].loadURL(message.url);
  250. event.returnValue = JSON.stringify({"err":0,"inx":nowIndex})
  251. })
  252. ipcMain.on("closeWindow", (event, message) => {
  253. childWindowList.forEach(element => {
  254. if (element && element.close) {
  255. element.close()
  256. }
  257. });
  258. childWindowList = []
  259. event.returnValue = JSON.stringify({"err":0})
  260. })
  261. ipcMain.on("changeProxy", (event, message) => {
  262. childWindowList.forEach(element => {
  263. if (element) {
  264. element.webContents.session.setProxy({
  265. proxyRules: "",
  266. proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
  267. });
  268. }
  269. });
  270. event.returnValue = JSON.stringify({"err":0})
  271. })
  272. ipcMain.on("readConfig", (event, message) => {
  273. if (fs.existsSync("./config.json")) {
  274. event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
  275. } else {
  276. event.returnValue = {}
  277. }
  278. })
  279. ipcMain.on("saveConfig", (event, message) => {
  280. fs.writeFileSync('./config.json', JSON.stringify(message))
  281. event.returnValue = {err: 0}
  282. })
  283. ipcMain.on("download", (event, message) => {
  284. download(message.url, message.path, {
  285. filename: message.filename,
  286. });
  287. event.returnValue = {err: 0}
  288. })