|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | +const { spawn, spawnSync } = require('../helpers/spawn-promise'); |
| 6 | +const { shell } = require('electron'); |
| 7 | +const fs = require('fs'); |
| 8 | +const which = require('which'); |
| 9 | +const { isMac, isWindows } = require('../helpers/platform.js'); |
| 10 | +const store = require('./electron-store-manager'); |
| 11 | + |
| 12 | +// RDP Client Configuration |
| 13 | +const RDP_CLIENTS = [ |
| 14 | + { |
| 15 | + value: 'mstsc', |
| 16 | + isAvailable: async () => { |
| 17 | + if (!isWindows()) return false; |
| 18 | + try { |
| 19 | + const mstscPath = await which('mstsc', { nothrow: true }); |
| 20 | + return Boolean(mstscPath); |
| 21 | + } catch { |
| 22 | + return false; |
| 23 | + } |
| 24 | + }, |
| 25 | + }, |
| 26 | + { |
| 27 | + value: 'windows-app', |
| 28 | + isAvailable: () => { |
| 29 | + if (!isMac()) return false; |
| 30 | + try { |
| 31 | + // Check for Windows App |
| 32 | + if (fs.existsSync('/Applications/Windows App.app')) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + // Fallback: Use mdfind for Microsoft Remote Desktop |
| 37 | + else { |
| 38 | + const result = spawnSync( |
| 39 | + ['kMDItemCFBundleIdentifier == "com.microsoft.rdc.macos"'], |
| 40 | + {}, |
| 41 | + 'mdfind', |
| 42 | + ); |
| 43 | + // `mdfind` returns an object with stdout containing the path of the app if found |
| 44 | + // Example: '/Applications/Windows App.app\n' |
| 45 | + return result.stdout && result.stdout.trim().length > 0; |
| 46 | + } |
| 47 | + } catch { |
| 48 | + return false; |
| 49 | + } |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + value: 'none', |
| 54 | + isAvailable: () => true, |
| 55 | + }, |
| 56 | +]; |
| 57 | + |
| 58 | +class RdpClientManager { |
| 59 | + // Track active RDP processes for cleanup |
| 60 | + #activeProcesses = []; |
| 61 | + /** |
| 62 | + * Gets all available RDP clients on the current system |
| 63 | + * @returns {Promise<Array>} Array of available RDP client values |
| 64 | + */ |
| 65 | + async getAvailableRdpClients() { |
| 66 | + const available = []; |
| 67 | + for (const client of RDP_CLIENTS) { |
| 68 | + if (await client.isAvailable()) { |
| 69 | + available.push(client.value); |
| 70 | + } |
| 71 | + } |
| 72 | + return available; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets the best default RDP client (first available that's not 'none') |
| 77 | + * @returns {Promise<string>} Best RDP client value or 'none' |
| 78 | + */ |
| 79 | + async getBestDefaultRdpClient() { |
| 80 | + const availableClients = await this.getAvailableRdpClients(); |
| 81 | + const bestClient = availableClients.find((client) => client !== 'none'); |
| 82 | + return bestClient ?? 'none'; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets the user's preferred RDP client, auto-detecting if not set |
| 87 | + * @returns {Promise<string>} Preferred RDP client value |
| 88 | + */ |
| 89 | + async getPreferredRdpClient() { |
| 90 | + let preferredClient = store.get('preferredRdpClient'); |
| 91 | + |
| 92 | + if (!preferredClient) { |
| 93 | + // Auto-detect and set the best available client |
| 94 | + preferredClient = await this.getBestDefaultRdpClient(); |
| 95 | + store.set('preferredRdpClient', preferredClient); |
| 96 | + } |
| 97 | + return preferredClient; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Sets the user's preferred RDP client |
| 102 | + * @param {string} preferredClient - The RDP client value to set as preferred |
| 103 | + */ |
| 104 | + setPreferredRdpClient(preferredClient) { |
| 105 | + if (!preferredClient) { |
| 106 | + store.set('preferredRdpClient', 'none'); |
| 107 | + } else { |
| 108 | + store.set('preferredRdpClient', preferredClient); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Launches RDP connection with the specified address and port |
| 114 | + * @param {string} address - Target address |
| 115 | + * @param {number} port - Target port |
| 116 | + */ |
| 117 | + async launchRdpConnection(address, port) { |
| 118 | + if (isWindows()) { |
| 119 | + // Launch Windows mstsc and track it for cleanup |
| 120 | + const mstscArgs = [`/v:${address}:${port}`]; |
| 121 | + const { childProcess } = await spawn(mstscArgs, {}, 'mstsc'); |
| 122 | + // Add to activeProcesses array for cleanup |
| 123 | + this.#activeProcesses.push(childProcess); |
| 124 | + } else if (isMac()) { |
| 125 | + // Launch macOS RDP URL - no process to track as it's handled by the system |
| 126 | + const fullAddress = `${address}:${port}`; |
| 127 | + const encoded = encodeURIComponent(`full address=s:${fullAddress}`); |
| 128 | + const rdpUrl = `rdp://${encoded}`; |
| 129 | + await shell.openExternal(rdpUrl); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Launches RDP client using session ID |
| 135 | + * Retrieves session object from session manager and launches appropriate RDP client |
| 136 | + * @param {string} sessionId - The session ID to get session for |
| 137 | + * @param {Object} sessionManager - Session manager instance to get session from |
| 138 | + */ |
| 139 | + async launchRdpClient(sessionId, sessionManager) { |
| 140 | + // Get session object from session manager |
| 141 | + const session = sessionManager.getSessionById(sessionId); |
| 142 | + |
| 143 | + if (!session) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + const { |
| 148 | + proxyDetails: { address, port }, |
| 149 | + } = session; |
| 150 | + // Launch RDP connection |
| 151 | + await this.launchRdpConnection(address, port); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Stop all active RDP processes |
| 156 | + */ |
| 157 | + stopAll() { |
| 158 | + for (const process of this.#activeProcesses) { |
| 159 | + if (!process.killed) { |
| 160 | + process.kill(); |
| 161 | + } |
| 162 | + } |
| 163 | + // Clear the active processes array after stopping all processes |
| 164 | + this.#activeProcesses = []; |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +module.exports = new RdpClientManager(); |
0 commit comments