Electron: Connects to the Windows remote desktop using the RDP file

In cloud projects, remote connection can be realized in Windows, Mac and Linux. This paper mainly describes the simple usage of Windows RDP remote connection in Electron project. RDP is based on the T-120 family of protocol standards and is an extension of that. Protocols that support multiple channels allow a separate virtual channel to transmit the following information:

  • Presentation data
  • Serial device communication
  • The license information
  • Highly encrypted data

RDP is an extension of the core T. Hare protocol. Other features remain as part of RDP, such as those required to support multi-point and multi-party sessions. Multipoint data passing allows data in an application to be passed to multiple parties in real time, such as a virtual whiteboard. There is no need to send the same data separately to each session. It needs to work with Node. js and Electron.

Windows way

Reference required modules

First we need to introduce the fs module, OS module, path module and child_process module in Node.js.

const fs = require("fs");
const os = require("os");
const path = require("path");
const exec = require("child_process").execSync;
Copy the code
  1. fsThe module is used for the operation of reading and writing files, which is the key step of writing RDP files to the hard disk to run.
  2. osThe module provides some basic system manipulation functions that we will use to find the string path to the current user’s home directory.
  3. pathModule provides some widgets for handling file paths, which will work togetherfsUse together.
  4. child_processModules provide the ability to spawn child processes. We will use it to create a child process to open the RDP file and run it.

Build usage method

export const spawnUp = function(e) {
  let homedir = os.homedir();
  let apppath = path.join(homedir, "/AppData/Local/"."Filenames.rdp");
    fs.writeFile(
    apppath,
    `screen mode id:i:2
      use multimon:i:0
      desktopwidth:i:${e.width}
      desktopheight:i:${e.height}The session the BPP: I: 32 winposstr: s: 0,3,0,0,800,600 compression will: I: 1 keyboardhook: I: 2 audiocapturemode: I: 0 videoplaybackmode: I: 1  connection type:i:7 networkautodetect:i:1 username:s:${e.username}
      bandwidthautodetect:i:1
      displayconnectionbar:i:1
      enableworkspacereconnect:i:0
      disable wallpaper:i:0
      allow font smoothing:i:0
      allow desktop composition:i:0
      disable full window drag:i:1
      disable menu anims:i:1
      disable themes:i:0
      disable cursor setting:i:0
      bitmapcachepersistenable:i:1
      full address:s:${e.ip}
      audiomode:i:0
      redirectprinters:i:1
      redirectcomports:i:0
      redirectsmartcards:i:1
      redirectclipboard:i:1
      redirectposdevices:i:0
      autoreconnection enabled:i:1
      authentication level:i:2
      prompt for credentials:i:0
      negotiate security layer:i:1
      remoteapplicationmode:i:0
      alternate shell:s:
      shell working directory:s:
      gatewayhostname:s:
      gatewayusagemethod:i:4
      gatewaycredentialssource:i:4
      gatewayprofileusagemethod:i:0
      promptcredentialonce:i:0
      gatewaybrokeringtype:i:0
      use redirection server name:i:0
      rdgiskdcproxy:i:0
      kdcproxyname:s:
      `.function(err) {
      if (err) {
        return console.log(err);
      } else {
        exec("mstsc"+ apppath); }}); };Copy the code

Start with the string path to the current user’s home directory using the os.homedir() method. Call path.join(homedir, “/AppData/Local/”, “filenames.rdp”) to write out the cache location of the current user’s software files. You can store configuration files or temporary files here without worrying about other problems.

Large strings in the code are necessary data for building RDP files, many of which are parameters that can be modified and configured. For example, remote connection screen size desktopWidth and desktopheight, remote connection IPfull Address, etc. For specific configuration parameters, refer to Microsoft Remote desktop RDP file Settings

Make a call to the fs.writeFile callback to import the good child_process.execSync. Fill in the parameter “MSTSC” and the address of the executable file to complete the function.