服务端脚本如下:
#!/usr/bin/env -S dotnet run
#:sdk Microsoft.NET.Sdk
using System.Diagnostics;
Console.WriteLine("这是一键修改Windows平台RDP(远程桌面)默认3389为其他端口的脚本, 仅适用于Windows平台.");
Console.WriteLine("正在生成随机端口...");
Int32 PortNumber = Random.Shared.Next(10000, 60000);
Console.WriteLine($"生成的随机端口为{PortNumber}");
Console.WriteLine("请记录上述的随机端口以便登录使用");
Console.WriteLine("请输入 Y 以确认继续运行脚本(输入其他内容会停止运行脚本.)");
String? confirm = Console.ReadLine();
if (confirm != "Y" )
{
Console.WriteLine($"您输入的内容是{confirm}, 程序即将停止...");
Environment.Exit(0);
}
Console.WriteLine("启用远程桌面服务中...");
Process.Start("pwsh.exe", @"-Command Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name 'fDenyTSConnections' -Value 0").WaitForExit();
Console.WriteLine("查询当前RDP端口中...");
Process.Start("pwsh.exe", @"-Command Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name 'PortNumber'").WaitForExit();
Console.WriteLine($"修改RDP端口为{PortNumber}中...");
Process.Start("pwsh.exe", $@"-Command Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -name 'PortNumber' -Value {PortNumber}").WaitForExit();
Console.WriteLine($"防火墙放行{PortNumber}中...");
Process.Start("pwsh.exe", $@"-Command New-NetFirewallRule -DisplayName 'RdpPort-TCP' -Direction Inbound -Action Allow -Protocol TCP -LocalPort {PortNumber}").WaitForExit();
Process.Start("pwsh.exe", $@"-Command New-NetFirewallRule -DisplayName 'RdpPort-UDP' -Direction Inbound -Action Allow -Protocol UDP -LocalPort {PortNumber}").WaitForExit();
Console.WriteLine("防火墙关闭3389端口中...");
Process.Start("pwsh.exe", $@"-Command New-NetFirewallRule -DisplayName 'Block-3389-TCP' -Direction Inbound -Action Block -Protocol TCP -LocalPort 3389").WaitForExit();
Process.Start("pwsh.exe", $@"-Command New-NetFirewallRule -DisplayName 'Block-3389-UDP' -Direction Inbound -Action Block -Protocol UDP -LocalPort 3389").WaitForExit();
Console.WriteLine("脚本运行完成");
Console.WriteLine("记得要重启电脑");
下面是服务端的一些基本优化
将下述内容保存为.reg文件后, 双击运行即可.
Windows Registry Editor Version 5.00
; 解除 30 FPS 限制,提高到 60 FPS
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations]
"DWMFRAMEINTERVAL"=dword:0000000f
; 启用显卡加速及 WDDM
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services]
"bEnumerateHWBeforeSW"=dword:00000001
"fEnableWddmDriver"=dword:00000001
注意: 不要开启硬件编码以及AVC444, 开启以后远程连接到服务端有BUG, 关闭连接窗口服务端内存不释放.
客户端使用"远程桌面连接"/"mstsc"进行连接即可.
远程桌面连接-显示选项-体验-选择连接速度来优化性能 调整为: Lan 10Mbps或更高
连接成功后, 根据需要可以保存为.rdp文件.
当出现"您的凭据不工作"报错时:
cmdkey /generic:TERMSRV/域名或IP /user:用户名 /pass:密码
当出现