?在Electron中使用windows远程连接桌面

Electron:使用rdp文件实现连接windows远程桌面

在云类型项目中,实现远程连接共有在windows、Mac、Linux三种情况,本文主要阐述了在Electron项目中windows RDP 远程连接的简单用法。RDP 基于 T-120 系列协议标准,它是其扩展。 支持多通道的协议允许单独的虚拟通道用于传输以下信息:

  • 演示文稿数据
  • 串行设备通信
  • 许可信息
  • 高度加密的数据

如键盘、鼠标活动
RDP 是核心 T.Share 协议的扩展。 其他一些功能作为 RDP 的一部分保留,例如支持多点和多方会话所需的 (功能) 。 多点数据传递允许将应用程序中的数据实时传递给多个方,如虚拟白板。 不需要将相同的数据分别发送到每个会话。需要配合node.jsElectron使用。

windows方式

引用所需模块

首先我们需要引入node.js中的fs模块、os模块、path模块与child_process模块、

const fs = require("fs");
const os = require("os");
const path = require("path");
const exec = require("child_process").execSync;
复制代码
  1. fs模块用于读写文件的操作,是在硬盘中写入RDP文件从而运行的关键步骤。
  2. os模块提供了一些基本的系统操作函数,我们将用它查找当前用户的主目录的字符串路径。
  3. path模块提供了一些用于处理文件路径的小工具,它将配合fs一起使用。
  4. child_process模块提供了衍生子进程的能力。我们将用它创建子进程打开RDP文件从而运行的功效。

构建使用方法

export const spawnUp = function(e) {
  let homedir = os.homedir();
  let apppath = path.join(homedir, "/AppData/Local/", "文件名.rdp");
    fs.writeFile(
    apppath,
    `screen mode id:i:2
      use multimon:i:0
      desktopwidth:i:${e.width}
      desktopheight:i:${e.height}
      session bpp:i:32
      winposstr:s:0,3,0,0,800,600
      compression: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);
      }
    }
  );
};
复制代码

首先使用os.homedir()方法当前用户的主目录的字符串路径。通过调用path.join(homedir, "/AppData/Local/", "文件名.rdp")的方法写出当前用户的软件文件缓存位置,可以将软件的配置文件或者临时文件存放在此处而不用担心其他问题。

代码中大段的字符串为构建RDP文件必要的数据,其中很多是可以修改与配置的参数。例如远程连接屏幕大小desktopwidthdesktopheight,连接的远程IPfull address等等,具体的配置参数可以参考 microsoft远程桌面 RDP 文件设置

最后在fs.writeFile的回调函数中调用引入好的child_process.execSync,填入参数”mstsc”与执行文件地址即可完成整体功能。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享