|
| 1 | +'use strict'; |
| 2 | +const app = require('app'); |
| 3 | +const BrowserWindow = require('browser-window'); |
| 4 | + |
| 5 | +// adds debug features like hotkeys for triggering dev tools and reload |
| 6 | +require('electron-debug')(); |
| 7 | + |
| 8 | +let setup = require('proxy'); |
| 9 | +let http = require('http'); |
| 10 | +let ipc = require('ipc'); |
| 11 | +let dns = require('dns'); |
| 12 | + |
| 13 | +let defaultPort = 8888; |
| 14 | +let mainWindow; |
| 15 | +let server = false; |
| 16 | + |
| 17 | +function onClosed() { |
| 18 | + mainWindow = null; |
| 19 | +} |
| 20 | + |
| 21 | +function createMainWindow() { |
| 22 | + const win = new BrowserWindow({ |
| 23 | + width: 600, |
| 24 | + height: 400 |
| 25 | + }); |
| 26 | + |
| 27 | + win.loadUrl(`file://${__dirname}/index.html`); |
| 28 | + win.on('closed', onClosed); |
| 29 | + |
| 30 | + return win; |
| 31 | +} |
| 32 | + |
| 33 | +app.on('window-all-closed', () => { |
| 34 | + if (server) { |
| 35 | + server.close(); |
| 36 | + } |
| 37 | + if (process.platform !== 'darwin') { |
| 38 | + app.quit(); |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +app.on('activate-with-no-open-windows', () => { |
| 43 | + if (!mainWindow) { |
| 44 | + mainWindow = createMainWindow(); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +app.on('ready', () => { |
| 49 | + mainWindow = createMainWindow(); |
| 50 | + mainWindow.openDevTools(); |
| 51 | +}); |
| 52 | + |
| 53 | +ipc.on('connect', function(event) { |
| 54 | + dns.lookup(require('os').hostname(), function (err, address, fam) { |
| 55 | + event.sender.send('set-ip', address); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +ipc.on('start-proxy', function(event, port) { |
| 60 | + if (server) { |
| 61 | + server.close(); |
| 62 | + } |
| 63 | + |
| 64 | + server = setup(http.createServer()); |
| 65 | + server.listen(port, function() { |
| 66 | + var port = server.address().port; |
| 67 | + console.log('HTTP(s) proxy server listening on port %d', port); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +ipc.on('stop-proxy', function(event, port) { |
| 72 | + if (server) { |
| 73 | + server.close(function() { |
| 74 | + console.log('proxy stopped') |
| 75 | + }); |
| 76 | + server = false; |
| 77 | + } |
| 78 | +}); |
0 commit comments