diff options
author | 2022-07-10 16:07:45 +0200 | |
---|---|---|
committer | 2022-07-11 17:47:53 +0200 | |
commit | fa1a7037b47f2e0114d8abc5a99d29239bd3637b (patch) | |
tree | 83404acf711aa8976dce47950edcca64836e0cd8 /src/electron/ipc-api/localServer.ts | |
parent | 6.0.0-nightly.96 [skip ci] (diff) | |
download | ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.gz ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.tar.zst ferdium-app-fa1a7037b47f2e0114d8abc5a99d29239bd3637b.zip |
refactor: local server import/export
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'src/electron/ipc-api/localServer.ts')
-rw-r--r-- | src/electron/ipc-api/localServer.ts | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/src/electron/ipc-api/localServer.ts b/src/electron/ipc-api/localServer.ts index 04ddc976a..71a3003d1 100644 --- a/src/electron/ipc-api/localServer.ts +++ b/src/electron/ipc-api/localServer.ts | |||
@@ -1,9 +1,12 @@ | |||
1 | import { randomBytes } from 'crypto'; | ||
1 | import { ipcMain, BrowserWindow } from 'electron'; | 2 | import { ipcMain, BrowserWindow } from 'electron'; |
2 | import { createServer } from 'net'; | 3 | import { createServer } from 'net'; |
3 | import { LOCAL_HOSTNAME, LOCAL_PORT } from '../../config'; | 4 | import { LOCAL_HOSTNAME, LOCAL_PORT } from '../../config'; |
4 | import { userDataPath } from '../../environment-remote'; | 5 | import { userDataPath } from '../../environment-remote'; |
5 | import { server } from '../../internal-server/start'; | 6 | import { server } from '../../internal-server/start'; |
6 | 7 | ||
8 | const debug = require('../../preload-safe-debug')('Ferdium:LocalServer'); | ||
9 | |||
7 | const portInUse = (port: number): Promise<boolean> => | 10 | const portInUse = (port: number): Promise<boolean> => |
8 | new Promise(resolve => { | 11 | new Promise(resolve => { |
9 | const server = createServer(socket => { | 12 | const server = createServer(socket => { |
@@ -22,26 +25,32 @@ const portInUse = (port: number): Promise<boolean> => | |||
22 | }); | 25 | }); |
23 | 26 | ||
24 | let localServerStarted = false; | 27 | let localServerStarted = false; |
28 | let port = LOCAL_PORT; | ||
29 | let token = ''; | ||
25 | 30 | ||
26 | export default (params: { mainWindow: BrowserWindow }) => { | 31 | export default (params: { mainWindow: BrowserWindow }) => { |
27 | ipcMain.on('startLocalServer', () => { | 32 | ipcMain.on('startLocalServer', () => { |
28 | if (!localServerStarted) { | 33 | (async () => { |
29 | // Find next unused port for server | 34 | if (!localServerStarted) { |
30 | let port = LOCAL_PORT; | 35 | // Find next unused port for server |
31 | (async () => { | 36 | port = LOCAL_PORT; |
32 | // eslint-disable-next-line no-await-in-loop | 37 | // eslint-disable-next-line no-await-in-loop |
33 | while ((await portInUse(port)) && port < LOCAL_PORT + 10) { | 38 | while ((await portInUse(port)) && port < LOCAL_PORT + 10) { |
34 | port += 1; | 39 | port += 1; |
35 | } | 40 | } |
36 | console.log('Starting local server on port', port); | 41 | token = randomBytes(256 / 8).toString('base64url'); |
37 | 42 | debug('Starting local server at', `http://localhost:${port}/token/${token}`); | |
38 | server(userDataPath(), port); | 43 | await server(userDataPath(), port, token); |
44 | localServerStarted = true; | ||
45 | } | ||
39 | 46 | ||
40 | params.mainWindow.webContents.send('localServerPort', { | 47 | // Send local server parameters to the renderer even if the server is already running. |
41 | port, | 48 | params.mainWindow.webContents.send('localServerPort', { |
42 | }); | 49 | port, |
43 | })(); | 50 | token, |
44 | localServerStarted = true; | 51 | }); |
45 | } | 52 | })().catch((error) => { |
53 | console.error('Error while starting local server', error); | ||
54 | }); | ||
46 | }); | 55 | }); |
47 | }; | 56 | }; |