From d43185f5c856a09108d08f7b88e9582b6214ff59 Mon Sep 17 00:00:00 2001 From: mhmdkrmabd Date: Tue, 15 Oct 2024 18:18:52 +0300 Subject: [PATCH] Attempt to reduce the number of auth requests. #470 --- custom_node_modules/main/pty.js | 48 +++++++++++++++++++++++++++---- main/main.js | 50 ++++++++++++++++++++++++--------- 2 files changed, 78 insertions(+), 20 deletions(-) diff --git a/custom_node_modules/main/pty.js b/custom_node_modules/main/pty.js index 3a779bd..e2e87aa 100755 --- a/custom_node_modules/main/pty.js +++ b/custom_node_modules/main/pty.js @@ -24,11 +24,6 @@ * Creates pseudo terminal, it returns a terminal object that allows interaction - reads and writes - with that terminal */ const PTY = require('node-pty'), - /** - * Node.js OS module - * Used for operating system-related utilities and properties - */ - OS = require('os'), // Strip all special characters in a given string StripChar = require('stripchar').StripChar, // Return the absolute system-dependant path for the place where applications should store their data for the current user @@ -1205,8 +1200,49 @@ let bashSession = (window, data) => { }) } +let runKeysGenerator = (callback) => { + // Create the pty instance and set the docker project folder as the current working directory + let process = PTY.spawn(Shell, [], { + cwd: CWD, + useConpty: false + }), + output = '' + + process.write('cd keys_generator && ./keys_generator' + OS.EOL) + + // When receiving any data from the instance send it to the provided window + process.on('data', function(_data) { + output += `${_data}` + + try { + let publicKey = output.match(/-+\s*BEGIN\s*PUBLIC\s*KEY-+[\s\S]+-+END\s*PUBLIC\s*KEY-+/gi)[0] + + callback(publicKey) + + try { + process.destroy() + Kill(process.pid, 'SIGKILL') + } catch (e) {} + + return + } catch (e) {} + + try { + let processFinished = output.match(/.+(keys_generator.*)$/gi)[0] + + callback(null) + + try { + process.destroy() + Kill(process.pid, 'SIGKILL') + } catch (e) {} + } catch (e) {} + }) +} + module.exports = { Pty, testConnectionWithCluster, - bashSession + bashSession, + runKeysGenerator } diff --git a/main/main.js b/main/main.js index 35941ec..b3229c7 100755 --- a/main/main.js +++ b/main/main.js @@ -89,6 +89,12 @@ global.FS = require('fs-extra') */ global.Path = require('path') +/** + * Node.js OS module + * Used for operating system-related utilities and properties + */ +global.OS = require('os') + /** * Import extra modules needed in the main thread * @@ -792,24 +798,40 @@ App.on('second-instance', () => { // Request to get the public key from the keys generator tool IPCMain.on('public-key:get', (_, id) => { - // Define the bin folder path - // let binFolder = Path.join((extraResourcesPath != null ? Path.join(extraResourcesPath, 'main') : Path.join(__dirname)), 'bin') - let binFolder = Path.join((extraResourcesPath != null ? Path.join(__dirname, '..', '..', 'main') : Path.join(__dirname)), 'bin', 'keys_generator') + let runKeysGenerator = () => { + // Define the bin folder path + // let binFolder = Path.join((extraResourcesPath != null ? Path.join(extraResourcesPath, 'main') : Path.join(__dirname)), 'bin') + let binFolder = Path.join((extraResourcesPath != null ? Path.join(__dirname, '..', '..', 'main') : Path.join(__dirname)), 'bin', 'keys_generator') - // Switch to the single-file mode - try { - if (!FS.lstatSync(binFolder).isDirectory()) - binFolder = Path.join(binFolder, '..') - } catch (e) {} + // Switch to the single-file mode + try { + if (!FS.lstatSync(binFolder).isDirectory()) + binFolder = Path.join(binFolder, '..') + } catch (e) {} + + // Run the keys generator tool + let binCall = `./keys_generator` - // Run the keys generator tool - let binCall = `./keys_generator` + // If the host is Windows + binCall = (process.platform == 'win32') ? `keys_generator.exe` : binCall - // If the host is Windows - binCall = (process.platform == 'win32') ? `keys_generator.exe` : binCall + // Execute the command, get the public key, and send it to the renderer thread + Terminal.run(`cd "${binFolder}" && ${binCall}`, (err, publicKey, stderr) => views.main.webContents.send(`public-key:${id}`, (err || stderr) ? '' : publicKey)) + } + + try { + if (OS.platform() != 'darwin') { + runKeysGenerator() + throw 0 + } + + Modules.Pty.runKeysGenerator((publicKey) => { + if (publicKey != null) + return views.main.webContents.send(`public-key:${id}`, publicKey) - // Execute the command, get the public key, and send it to the renderer thread - Terminal.run(`cd "${binFolder}" && ${binCall}`, (err, publicKey, stderr) => views.main.webContents.send(`public-key:${id}`, (err || stderr) ? '' : publicKey)) + runKeysGenerator() + }) + } catch (e) {} }) /**