Skip to content

Commit 195416b

Browse files
authored
Merge pull request #16 from DarkCoderSc/dev
1.0.6 Stable
2 parents 99db5af + d78345a commit 195416b

File tree

5 files changed

+137
-171
lines changed

5 files changed

+137
-171
lines changed
Binary file not shown.

PowerRemoteDesktop_Server/PowerRemoteDesktop_Server.psm1

+43-91
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,14 @@ Add-Type -Assembly System.Windows.Forms
5252
Add-Type -Assembly System.Drawing
5353
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern bool SetProcessDPIAware();[DllImport("User32.dll")] public static extern int LoadCursorA(int hInstance, int lpCursorName);[DllImport("User32.dll")] public static extern bool GetCursorInfo(IntPtr pci);' -Name User32 -Namespace W;
5454

55-
$global:PowerRemoteDesktopVersion = "1.0.5.beta.6"
55+
$global:PowerRemoteDesktopVersion = "1.0.6"
5656

5757
$global:HostSyncHash = [HashTable]::Synchronized(@{
5858
host = $host
5959
ClipboardText = (Get-Clipboard -Raw)
60+
RunningSession = $false
6061
})
6162

62-
enum TransportMode {
63-
Raw = 1
64-
Base64 = 2
65-
}
66-
6763
enum ClipboardMode {
6864
Disabled = 1
6965
Receive = 2
@@ -600,14 +596,12 @@ class ClientIO {
600596
[System.IO.StreamWriter] $Writer = $null
601597
[System.IO.StreamReader] $Reader = $null
602598
[System.Net.Security.SslStream] $SSLStream = $null
603-
[TransportMode] $TransportMode
604599

605600

606601
ClientIO(
607602
[System.Net.Sockets.TcpClient] $Client,
608603
[System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate,
609-
[bool] $TLSv1_3,
610-
[TransportMode] $TransportMode
604+
[bool] $TLSv1_3
611605
) {
612606
<#
613607
.SYNOPSIS
@@ -621,9 +615,6 @@ class ClientIO {
621615
622616
.PARAMETER TLSv1_3
623617
Define whether or not SSL/TLS v1.3 must be used.
624-
625-
.PARAMETER TransportMode
626-
Define transport method for streams (Base64 or Raw)
627618
#>
628619

629620
if ((-not $Client) -or (-not $Certificate))
@@ -632,7 +623,6 @@ class ClientIO {
632623
}
633624

634625
$this.Client = $Client
635-
$this.TransportMode = $TransportMode
636626

637627
Write-Verbose "Create new SSL Stream..."
638628

@@ -783,7 +773,6 @@ class ClientIO {
783773

784774
$sessionInformation = Get-LocalMachineInformation
785775

786-
$sessionInformation | Add-Member -MemberType NoteProperty -Name "TransportMode" -Value $this.TransportMode
787776
$sessionInformation | Add-Member -MemberType NoteProperty -Name "SessionId" -Value $session.Id
788777
$sessionInformation | Add-Member -MemberType NoteProperty -Name "Version" -Value $global:PowerRemoteDesktopVersion
789778
$sessionInformation | Add-Member -MemberType NoteProperty -Name "ViewOnly" -Value $ViewOnly
@@ -854,7 +843,6 @@ class ServerIO {
854843
[string] $ListenAddress = "127.0.0.1"
855844
[int] $ListenPort = 2801
856845
[bool] $TLSv1_3 = $false
857-
[TransportMode] $TransportMode
858846
[string] $Password
859847
[bool] $ViewOnly = $false
860848

@@ -888,17 +876,13 @@ class ServerIO {
888876
.PARAMETER TLSv1_3
889877
Define if TLS v1.3 must be used.
890878
891-
.PARAMETER TransportMode
892-
Define stream transport method.
893-
894879
.PARAMETER ViewOnly
895880
Define if mouse / keyboard is authorized.
896881
#>
897882

898883
[string] $ListenAddress,
899884
[int] $ListenPort,
900885
[string] $Password,
901-
[TransportMode] $TransportMode,
902886
[System.Security.Cryptography.X509Certificates.X509Certificate2] $Certificate,
903887
[bool] $TLSv1_3,
904888
[bool] $ViewOnly
@@ -913,7 +897,6 @@ class ServerIO {
913897
$this.ListenPort = $ListenPort
914898
$this.TLSv1_3 = $TLSv1_3
915899
$this.Password = $Password
916-
$this.TransportMode = $TransportMode
917900
$this.ViewOnly = $ViewOnly
918901

919902
if (-not $Certificate)
@@ -997,8 +980,7 @@ class ServerIO {
997980
$client = [ClientIO]::New(
998981
$socket,
999982
$this.Certificate,
1000-
$this.TLSv1_3,
1001-
$this.TransportMode
983+
$this.TLSv1_3
1002984
)
1003985
try
1004986
{
@@ -1059,11 +1041,6 @@ class ServerIO {
10591041

10601042
$global:DesktopStreamScriptBlock = {
10611043

1062-
enum TransportMode {
1063-
Raw = 1
1064-
Base64 = 2
1065-
}
1066-
10671044
function Get-DesktopImage {
10681045
<#
10691046
.SYNOPSIS
@@ -1093,11 +1070,16 @@ $global:DesktopStreamScriptBlock = {
10931070
$Screen.Bounds.Location.Y
10941071
)
10951072

1096-
$bitmap = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
1073+
$bitmap = New-Object System.Drawing.Bitmap(
1074+
$size.Width,
1075+
$size.Height,
1076+
[System.Drawing.Imaging.PixelFormat]::Format24bppRgb
1077+
)
1078+
10971079
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
10981080

10991081
$graphics.CopyFromScreen($location, [System.Drawing.Point]::Empty, $size)
1100-
1082+
11011083
return $bitmap
11021084
}
11031085
catch
@@ -1130,13 +1112,13 @@ $global:DesktopStreamScriptBlock = {
11301112
$encoderParameters = New-Object System.Drawing.Imaging.EncoderParameters(1)
11311113
$encoderParameters.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $imageQuality)
11321114

1133-
$packetSize = 4096
1134-
1135-
while ($true)
1136-
{
1115+
$packetSize = 9216 # 9KiB
1116+
1117+
while ($global:HostSyncHash.RunningSession)
1118+
{
11371119
try
11381120
{
1139-
$desktopImage = Get-DesktopImage -Screen $Param.Screen
1121+
$desktopImage = Get-DesktopImage -Screen $Param.Screen
11401122

11411123
$imageStream = New-Object System.IO.MemoryStream
11421124

@@ -1163,50 +1145,20 @@ $global:DesktopStreamScriptBlock = {
11631145
{
11641146
$imageStream.position = 0
11651147
try
1166-
{
1167-
switch ([TransportMode] $Param.Client.TransportMode)
1168-
{
1169-
([TransportMode]::Raw)
1170-
{
1171-
$Param.Client.SSLStream.Write([BitConverter]::GetBytes([int32] $imageStream.Length) , 0, 4) # SizeOf(Int32)
1172-
1173-
$totalBytesSent = 0
1174-
1175-
$buffer = New-Object -TypeName byte[] -ArgumentList $packetSize
1176-
do
1177-
{
1178-
$bufferSize = ($imageStream.Length - $totalBytesSent)
1179-
if ($bufferSize -gt $packetSize)
1180-
{
1181-
$bufferSize = $packetSize
1182-
}
1183-
else
1184-
{
1185-
# Save some memory operations for creating objects.
1186-
# Usually, bellow code is call when last chunk is being sent.
1187-
$buffer = New-Object -TypeName byte[] -ArgumentList $bufferSize
1188-
}
1189-
1190-
# (OPTIMIZATION IDEA): Try with BinaryStream to save the need of "byte[]"" buffer.
1191-
$null = $imageStream.Read($buffer, 0, $buffer.Length)
1192-
1193-
$Param.Client.SSLStream.Write($buffer, 0, $buffer.Length)
1194-
1195-
$totalBytesSent += $bufferSize
1196-
} until ($totalBytesSent -eq $imageStream.Length)
1197-
1198-
break
1199-
}
1200-
1201-
([TransportMode]::Base64)
1202-
{
1203-
$Param.Client.Writer.WriteLine(
1204-
[System.Convert]::ToBase64String($imageStream.ToArray())
1205-
)
1148+
{
1149+
$Param.Client.SSLStream.Write([BitConverter]::GetBytes([int32] $imageStream.Length) , 0, 4) # SizeOf(Int32)
1150+
1151+
$binaryReader = New-Object System.IO.BinaryReader($imageStream)
1152+
do
1153+
{
1154+
$bufferSize = ($imageStream.Length - $imageStream.Position)
1155+
if ($bufferSize -gt $packetSize)
1156+
{
1157+
$bufferSize = $packetSize
1158+
}
12061159

1207-
break
1208-
}
1209-
}
1160+
$Param.Client.SSLStream.Write($binaryReader.ReadBytes($bufferSize), 0, $bufferSize)
1161+
} until ($imageStream.Position -eq $imageStream.Length)
12101162
}
12111163
catch
12121164
{ break }
@@ -1323,7 +1275,7 @@ $global:IngressEventScriptBlock = {
13231275

13241276
$keyboardSim = [KeyboardSim]::New()
13251277

1326-
while ($true)
1278+
while ($global:HostSyncHash.RunningSession)
13271279
{
13281280
try
13291281
{
@@ -1626,7 +1578,7 @@ $global:EgressEventScriptBlock = {
16261578

16271579
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()
16281580

1629-
while ($true)
1581+
while ($global:HostSyncHash.RunningSession)
16301582
{
16311583
# Events that occurs every seconds needs to be placed bellow.
16321584
# If no event has occured during this second we send a Keep-Alive signal to
@@ -1792,10 +1744,6 @@ function Invoke-RemoteDesktopServer
17921744
.PARAMETER EncodedCertificate
17931745
A valid X509 Certificate (With Private Key) encoded as a Base64 String.
17941746
1795-
.PARAMETER TransportMode
1796-
Tell server how to send desktop image to remote viewer. Best method is Raw Bytes but I decided to keep
1797-
the Base64 transport method as an alternative.
1798-
17991747
.PARAMETER TLSv1_3
18001748
Define whether or not TLS v1.3 must be used for communication with Viewer.
18011749
@@ -1828,7 +1776,6 @@ function Invoke-RemoteDesktopServer
18281776
# Or
18291777
[string] $EncodedCertificate = "", # 2
18301778

1831-
[TransportMode] $TransportMode = [TransportMode]::Raw,
18321779
[switch] $TLSv1_3,
18331780
[switch] $DisableVerbosity,
18341781
[int] $ImageQuality = 100,
@@ -1911,13 +1858,11 @@ function Invoke-RemoteDesktopServer
19111858
}
19121859
}
19131860

1914-
Write-Verbose $TransportMode
19151861
# Create new server and listen
19161862
$server = [ServerIO]::New(
19171863
$ListenAddress,
19181864
$ListenPort,
19191865
$Password,
1920-
$TransportMode,
19211866
$Certificate,
19221867
$TLSv1_3,
19231868
$ViewOnly
@@ -1929,6 +1874,8 @@ function Invoke-RemoteDesktopServer
19291874
{
19301875
try
19311876
{
1877+
$global:HostSyncHash.RunningSession = $false
1878+
19321879
Write-Verbose "Server waiting for new incomming session..."
19331880

19341881
# Establish a new Remote Desktop Session.
@@ -1939,7 +1886,9 @@ function Invoke-RemoteDesktopServer
19391886
# Otherwise a Timeout Exception will be raised.
19401887
# Actually, if someone else decide to connect in the mean time it will interrupt the whole session,
19411888
# Remote Viewer will then need to establish a new session from scratch.
1942-
$clientEvents = $server.PullClient(10 * 1000);
1889+
$clientEvents = $server.PullClient(10 * 1000);
1890+
1891+
$global:HostSyncHash.RunningSession = $true
19431892

19441893
# Grab desired screen to capture
19451894
$screen = [System.Windows.Forms.Screen]::AllScreens | Where-Object -FilterScript { $_.DeviceName -eq $server.Session.Screen }
@@ -1983,16 +1932,19 @@ function Invoke-RemoteDesktopServer
19831932
# Waiting for Runspaces to finish their jobs.
19841933
while ($true)
19851934
{
1986-
$completed = $true
1935+
$completed = $true
19871936

19881937
# Probe each existing runspaces
19891938
foreach ($runspace in $runspaces)
19901939
{
19911940
if (-not $runspace.AsyncResult.IsCompleted)
19921941
{
1993-
$completed = $false
1994-
1995-
break
1942+
$completed = $false
1943+
}
1944+
elseif ($global:HostSyncHash.RunningSession)
1945+
{
1946+
# Notifying other runspaces that a session integrity was lost
1947+
$global:HostSyncHash.RunningSession = $false
19961948
}
19971949
}
19981950

Binary file not shown.

0 commit comments

Comments
 (0)