Skip to content

Commit

Permalink
Merge pull request #3 from DarkCoderSc/dev
Browse files Browse the repository at this point in the history
1.0.2 Beta 3
  • Loading branch information
DarkCoderSc committed Jan 12, 2022
2 parents 484f357 + 9173ead commit 16ef456
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 52 deletions.
Binary file modified PowerRemoteDesktop_Server/PowerRemoteDesktop_Server.psd1
Binary file not shown.
77 changes: 37 additions & 40 deletions PowerRemoteDesktop_Server/PowerRemoteDesktop_Server.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
Add-Type -Assembly System.Windows.Forms
Add-Type -Assembly System.Drawing
Add-Type -MemberDefinition '[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);' -Name GDI32 -Namespace W;
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);' -Name User32 -Namespace W;
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);[DllImport("User32.dll")] public static extern bool SetProcessDPIAware();' -Name User32 -Namespace W;

$global:PowerRemoteDesktopVersion = "1.0.beta.2"
$global:PowerRemoteDesktopVersion = "1.0.beta.3"

enum TransportMode {
Raw = 1
Expand Down Expand Up @@ -501,6 +501,24 @@ function Resolve-AuthenticationChallenge
return $solution
}

function Get-ResolutionScaleFactor
{
<#
.SYNOPSIS
Return current screen scale factor
#>

$hdc = [W.User32]::GetDC(0)
try
{
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
}
finally
{
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
}
}

function Get-LocalMachineInformation
{
<#
Expand All @@ -521,10 +539,10 @@ function Get-LocalMachineInformation
WindowsVersion = [Environment]::OSVersion.VersionString

ScreenInformation = New-Object -TypeName PSCustomObject -Property @{
Width = $screenBounds.Width
Width = $screenBounds.Width
Height = $screenBounds.Height
X = $screenBounds.X
Y = $screenBounds.Y
X = $screenBounds.X
Y = $screenBounds.Y
}
}
}
Expand Down Expand Up @@ -1018,25 +1036,7 @@ $global:DesktopStreamScriptBlock = {
.PARAMETER syncHash.Param.Client
A ClientIO Object containing an active connection. This is where, desktop updates will be
sent over network.
#>

function Get-ResolutionScaleFactor
{
<#
.SYNOPSIS
Return the scale factor of target screen to capture.
#>

$hdc = [W.User32]::GetDC(0)
try
{
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
}
finally
{
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
}
}
#>

function Get-DesktopImage {
<#
Expand All @@ -1048,25 +1048,19 @@ $global:DesktopStreamScriptBlock = {
At this time, PowerRemoteDesktop only supports PrimaryScreen.
Even if multi-screen capture is a very easy feature to implement, It will probably be present
in final version 1.0
.PARAMETER ScaleFactor
Define target monitor scale factor to adjust bounds.
#>
param (
[int] $ScaleFactor = 1
)
try
{
$primaryDesktop = [System.Windows.Forms.Screen]::PrimaryScreen

$size = New-Object System.Drawing.Size(
($primaryDesktop.Bounds.Size.Width * $ScaleFactor),
($primaryDesktop.Bounds.Size.Height * $ScaleFactor)
$primaryDesktop.Bounds.Size.Width,
$primaryDesktop.Bounds.Size.Height
)

$location = New-Object System.Drawing.Point(
($primaryDesktop.Bounds.Location.X * $ScaleFactor),
($primaryDesktop.Bounds.Location.Y * $ScaleFactor)
$primaryDesktop.Bounds.Location.X,
$primaryDesktop.Bounds.Location.Y
)

$bitmap = New-Object System.Drawing.Bitmap($size.Width, $size.Height)
Expand Down Expand Up @@ -1102,15 +1096,13 @@ $global:DesktopStreamScriptBlock = {
$encoderParameters = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParameters.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $imageQuality)

$scaleFactor = Get-ResolutionScaleFactor

$packetSize = 4096
$packetSize = 4096

while ($true)
{
try
{
$desktopImage = Get-DesktopImage -ScaleFactor 1
$desktopImage = Get-DesktopImage

$imageStream = New-Object System.IO.MemoryStream

Expand Down Expand Up @@ -1534,6 +1526,7 @@ function Invoke-RemoteDesktopServer
[switch] $DisableVerbosity
)


[System.Collections.Generic.List[PSCustomObject]]$runspaces = @()

$oldErrorActionPreference = $ErrorActionPreference
Expand All @@ -1553,6 +1546,11 @@ function Invoke-RemoteDesktopServer

Write-Banner

if (Get-ResolutionScaleFactor -ne 1)
{
[W.User32]::SetProcessDPIAware()
}

if (-not (Test-Administrator) -and -not $CertificateFile -and -not $EncodedCertificate)
{
throw "Insuficient Privilege`r`n`
Expand Down Expand Up @@ -1638,8 +1636,7 @@ function Invoke-RemoteDesktopServer
$clientControl = $server.PullClient(10 * 1000);

# Create Runspace #1 for Desktop Streaming.
$param = New-Object -TypeName PSCustomObject -Property @{
TransportMode = $TransportMode
$param = New-Object -TypeName PSCustomObject -Property @{
Client = $clientDesktop
}

Expand Down
Binary file modified PowerRemoteDesktop_Viewer/PowerRemoteDesktop_Viewer.psd1
Binary file not shown.
61 changes: 53 additions & 8 deletions PowerRemoteDesktop_Viewer/PowerRemoteDesktop_Viewer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
-------------------------------------------------------------------------------#>

Add-Type -Assembly System.Windows.Forms
Add-Type -MemberDefinition '[DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);' -Name GDI32 -Namespace W;
Add-Type -MemberDefinition '[DllImport("User32.dll")] public static extern int GetDC(IntPtr hWnd);[DllImport("User32.dll")] public static extern int ReleaseDC(IntPtr hwnd, int hdc);[DllImport("User32.dll")] public static extern bool SetProcessDPIAware();' -Name User32 -Namespace W;

$global:PowerRemoteDesktopVersion = "1.0.beta.2"
$global:PowerRemoteDesktopVersion = "1.0.beta.3"

function Write-Banner
{
Expand Down Expand Up @@ -80,6 +82,24 @@ function Write-Banner
Write-Host ""
}

function Get-ResolutionScaleFactor
{
<#
.SYNOPSIS
Return current screen scale factor
#>

$hdc = [W.User32]::GetDC(0)
try
{
return [W.GDI32]::GetDeviceCaps($hdc, 117) / [W.GDI32]::GetDeviceCaps($hdc, 10)
}
finally
{
[W.User32]::ReleaseDC(0, $hdc) | Out-Null
}
}

function Get-SHA512FromString
{
<#
Expand Down Expand Up @@ -860,6 +880,11 @@ function Invoke-RemoteDesktopViewer
$VerbosePreference = "continue"

Write-Banner

if (Get-ResolutionScaleFactor -ne 1)
{
[W.User32]::SetProcessDPIAware()
}

Write-Verbose "Server address: ""${ServerAddress}:${ServerPort}"""

Expand All @@ -885,9 +910,13 @@ function Invoke-RemoteDesktopViewer
$screenRect = $virtualDesktopForm.Form.RectangleToScreen($virtualDesktopForm.Form.ClientRectangle)
$captionHeight = $screenRect.Top - $virtualDesktopForm.Form.Top

$localScreenWidth = $locationResolutionInformation.WorkingArea.Width
$localScreenHeight = $locationResolutionInformation.WorkingArea.Height
$localScreenHeight -= $captionHeight

$requireResize = (
($locationResolutionInformation.WorkingArea.Width -le $session.SessionInformation.ScreenInformation.Width) -or
(($locationResolutionInformation.WorkingArea.Height - $captionHeight) -le $session.SessionInformation.ScreenInformation.Height)
($localScreenWidth -le $session.SessionInformation.ScreenInformation.Width) -or
($localScreenHeight -le $session.SessionInformation.ScreenInformation.Height)
)

$virtualDesktopWidth = 0
Expand All @@ -897,11 +926,27 @@ function Invoke-RemoteDesktopViewer

if ($requireResize)
{
$virtualDesktopWidth = [math]::Round(($session.SessionInformation.ScreenInformation.Width * $resizeRatio) / 100)
$virtualDesktopHeight = [math]::Round(($session.SessionInformation.ScreenInformation.Height * $resizeRatio) / 100)
$adjustVertically = $localScreenWidth -gt $localScreenHeight

if ($adjustVertically)
{
$virtualDesktopWidth = [math]::Round(($localScreenWidth * $resizeRatio) / 100)

$remoteResizedRatio = [math]::Round(($virtualDesktopWidth * 100) / $session.SessionInformation.ScreenInformation.Width)

$virtualDesktopHeight = [math]::Round(($session.SessionInformation.ScreenInformation.Height * $remoteResizedRatio) / 100)
}
else
{
$virtualDesktopHeight = [math]::Round(($localScreenHeight * $resizeRatio) / 100)

$remoteResizedRatio = [math]::Round(($virtualDesktopHeight * 100) / $session.SessionInformation.ScreenInformation.Height)

$virtualDesktopWidth = [math]::Round(($session.SessionInformation.ScreenInformation.Width * $remoteResizedRatio) / 100)
}
}
else
{
{
$virtualDesktopWidth = $session.SessionInformation.ScreenInformation.Width
$virtualDesktopHeight = $session.SessionInformation.ScreenInformation.Height
}
Expand All @@ -911,8 +956,8 @@ function Invoke-RemoteDesktopViewer

# Center Virtual Desktop Form
$virtualDesktopForm.Form.Location = [System.Drawing.Point]::new(
(($locationResolutionInformation.WorkingArea.Width - $virtualDesktopForm.Form.Width) / 2),
(($locationResolutionInformation.WorkingArea.Height - $virtualDesktopForm.Form.Height) / 2)
(($localScreenWidth - $virtualDesktopForm.Form.Width) / 2),
(($localScreenHeight - $virtualDesktopForm.Form.Height) / 2)
)

# WinForms Events (If enabled, I recommend to disable control when testing on local machine to avoid funny things)
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# PowerRemoteDesktop

<img src="Screenshot 2022-01-07 at 16.43.53.png" width="100%"/>
<img src="demo.png" width="100%"/>

*Power Remote Desktop* is a fully functional Remote Desktop Application entirely coded in PowerShell.

Expand All @@ -19,7 +19,7 @@ Tested on:

## Features

* Captures Remote Desktop Image with support of HDPI (Beta).
* Captures Remote Desktop Image with support of HDPI.
* Supports Mouse Click (Left, Right, Middle), Mouse Moves and Mouse Wheel.
* Supports Keystrokes Simulation (Sending remote key strokes) and few useful shortcuts.
* Traffic is encrypted by default using TLSv1.2 and optionnally using TLSv1.3 (TLS 1.3 might not be possible on older systems).
Expand Down Expand Up @@ -220,7 +220,7 @@ Then pass the encoded string to parameter `EncodedCertificate`.

## Changelog

### 11 January 2021
### 11 January 2021 (1.0.1 Beta 2)

* Desktop images are now transported in raw bytes instead of base64 string thus slightly improving performances. Base64 Transport Method is still available through an option but disabled by default.
* Protocol has drastically changed. It is smoother to read and less prone to errors.
Expand All @@ -230,6 +230,10 @@ Then pass the encoded string to parameter `EncodedCertificate`.
* Possibility to disable verbose.
* Server & Viewer version synchronization. Same version must be used between the two.

### 12 January 2021 (1.0.2 Beta 3)

* HDPI is completely supported.

### List of ideas and TODO

* 🟢 Do a deep investigation about SecureString and if it applies to current project (to protect password)
Expand All @@ -243,7 +247,6 @@ Then pass the encoded string to parameter `EncodedCertificate`.
* 🟠 Server Concurrency.
* 🟠 Listen for local/remote screen resolution update event.
* 🟠 Multiple Monitor Support.
* 🟠 Improve HDPI Scaling / Quality.
* 🔴 Motion Update for Desktop Streaming (Only send and update changing parts of desktop).

🟢 = Easy
Expand Down
Binary file removed Screenshot 2022-01-07 at 16.43.53.png
Binary file not shown.
Binary file added demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 16ef456

Please sign in to comment.