Skip to content

Commit c519cd3

Browse files
committed
opti: using enums as protocol verbs
1 parent 539cf5b commit c519cd3

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

PowerRemoteDesktop_Server/PowerRemoteDesktop_Server.psm1

+27-12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ enum ClipboardMode {
6666
Both = 4
6767
}
6868

69+
enum ProtocolCommand {
70+
Success = 1
71+
Fail = 2
72+
RequestSession = 3
73+
AttachToSession = 4
74+
BadRequest = 5
75+
ResourceFound = 6
76+
ResourceNotFound = 7
77+
}
78+
79+
enum WorkerKind {
80+
Desktop = 1
81+
Events = 2
82+
}
83+
6984
function Write-Banner
7085
{
7186
<#
@@ -1411,13 +1426,13 @@ class ClientIO {
14111426
# Challenge solution is a Sha512 Hash so comparison doesn't need to be sensitive (-ceq or -cne)
14121427
if ($challengeReply -ne $challengeSolution)
14131428
{
1414-
$this.Writer.WriteLine("KO.")
1429+
$this.Writer.WriteLine(([ProtocolCommand]::Fail))
14151430

14161431
throw "Client challenge solution does not match our solution."
14171432
}
14181433
else
14191434
{
1420-
$this.Writer.WriteLine("OK.")
1435+
$this.Writer.WriteLine(([ProtocolCommand]::Success))
14211436

14221437
Write-Verbose "Password Authentication Success"
14231438

@@ -2098,7 +2113,7 @@ class SessionManager {
20982113

20992114
$this.Sessions.Add($session)
21002115

2101-
$client.WriteLine("OK")
2116+
$client.WriteLine((([ProtocolCommand]::Success)))
21022117
}
21032118
catch
21042119
{
@@ -2130,27 +2145,27 @@ class SessionManager {
21302145
$session = $this.GetSession($Client.ReadLine(5 * 1000))
21312146
if (-not $session)
21322147
{
2133-
$Client.WriteLine("SESS_NOTFOUND")
2148+
$Client.WriteLine(([ProtocolCommand]::ResourceNotFound))
21342149

21352150
throw "Session object matchin given id could not be find in active session pool."
21362151
}
21372152

21382153
Write-Verbose "Client successfully attached to session: ""$($session.id)"""
21392154

2140-
$Client.WriteLine("SESS_OK")
2155+
$Client.WriteLine(([ProtocolCommand]::ResourceFound))
21412156

21422157
$workerKind = $Client.ReadLine(5 * 1000)
21432158

2144-
switch ($workerKind)
2159+
switch ([WorkerKind] $workerKind)
21452160
{
2146-
"WRKER_DESKTOP"
2161+
(([WorkerKind]::Desktop))
21472162
{
21482163
$session.NewDesktopWorker($Client)
21492164

21502165
break
21512166
}
21522167

2153-
"WRKER_EVENT"
2168+
(([WorkerKind]::Events))
21542169
{
21552170
$session.NewEventWorker($Client) # I/O
21562171

@@ -2189,16 +2204,16 @@ class SessionManager {
21892204

21902205
$requestMode = $client.ReadLine(5 * 1000)
21912206

2192-
switch ($requestMode)
2207+
switch ([ProtocolCommand] $requestMode)
21932208
{
2194-
"REQ_SESSION"
2209+
([ProtocolCommand]::RequestSession)
21952210
{
21962211
$this.ProceedNewSessionRequest($client)
21972212

21982213
break
21992214
}
22002215

2201-
"REQ_ATTACH"
2216+
([ProtocolCommand]::AttachToSession)
22022217
{
22032218
$this.ProceedAttachRequest($client)
22042219

@@ -2207,7 +2222,7 @@ class SessionManager {
22072222

22082223
default:
22092224
{
2210-
$client.WriteLine("BAD_REQ")
2225+
$client.WriteLine(([ProtocolCommand]::BadRequest))
22112226

22122227
throw "Bad request."
22132228
}

PowerRemoteDesktop_Viewer/PowerRemoteDesktop_Viewer.psm1

+26-11
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ enum ClipboardMode {
7272
Both = 4
7373
}
7474

75+
enum ProtocolCommand {
76+
Success = 1
77+
Fail = 2
78+
RequestSession = 3
79+
AttachToSession = 4
80+
BadRequest = 5
81+
ResourceFound = 6
82+
ResourceNotFound = 7
83+
}
84+
85+
enum WorkerKind {
86+
Desktop = 1
87+
Events = 2
88+
}
89+
7590
function Write-Banner
7691
{
7792
<#
@@ -551,7 +566,7 @@ class ClientIO {
551566
$this.Writer.WriteLine($challengeSolution)
552567

553568
$result = $this.Reader.ReadLine()
554-
if ($result -eq "OK.")
569+
if ($result -eq [ProtocolCommand]::Success)
555570
{
556571
Write-Verbose "Solution accepted. Authentication success."
557572
}
@@ -752,7 +767,7 @@ class ViewerSession
752767

753768
Write-Verbose "Request session..."
754769

755-
$client.WriteLine("REQ_SESSION")
770+
$client.WriteLine(([ProtocolCommand]::RequestSession))
756771

757772
$this.ServerInformation = $client.ReadJson()
758773

@@ -921,7 +936,7 @@ class ViewerSession
921936

922937
$client.WriteJson($viewerExpectation)
923938

924-
if ($client.ReadLine(5 * 1000) -cne "OK")
939+
if ($client.ReadLine(5 * 1000) -cne [ProtocolCommand]::Success)
925940
{
926941
throw "Remote server did not respond to our expectation in time."
927942
}
@@ -941,7 +956,7 @@ class ViewerSession
941956
}
942957
}
943958

944-
[ClientIO] ConnectWorker([string] $WorkerKind)
959+
[ClientIO] ConnectWorker([WorkerKind] $WorkerKind)
945960
{
946961
Write-Verbose "Connect new worker: ""$WorkerKind""..."
947962

@@ -954,15 +969,15 @@ class ViewerSession
954969

955970
$client.Authentify($this.SecurePassword)
956971

957-
$client.WriteLine("REQ_ATTACH")
972+
$client.WriteLine(([ProtocolCommand]::AttachToSession))
958973

959974
Write-Verbose "Attach worker to remote session ""$($this.ServerInformation.SessionId)"""
960975

961976
$client.WriteLine($this.ServerInformation.SessionId)
962977

963-
switch ($client.ReadLine(5 * 1000))
978+
switch ([ProtocolCommand] $client.ReadLine(5 * 1000))
964979
{
965-
"SESS_OK"
980+
([ProtocolCommand]::ResourceFound)
966981
{
967982
Write-Verbose "Worker successfully attached to session, define which kind of worker we are..."
968983

@@ -973,14 +988,14 @@ class ViewerSession
973988
break
974989
}
975990

976-
"SESS_NOTFOUND"
991+
([ProtocolCommand]::ResourceNotFound)
977992
{
978993
throw "Server could not locate session."
979994
}
980995

981996
default
982997
{
983-
throw "Unexpected answer from remote server for ""REQ_ATTACH""."
998+
throw "Unexpected answer from remote server for session attach."
984999
}
9851000
}
9861001

@@ -1001,14 +1016,14 @@ class ViewerSession
10011016
{
10021017
Write-Verbose "Create new desktop streaming worker..."
10031018

1004-
$this.ClientDesktop = $this.ConnectWorker("WRKER_DESKTOP")
1019+
$this.ClientDesktop = $this.ConnectWorker([WorkerKind]::Desktop)
10051020
}
10061021

10071022
[void] ConnectEventsWorker()
10081023
{
10091024
Write-Verbose "Create new event event (in/out) worker..."
10101025

1011-
$this.ClientEvents = $this.ConnectWorker("WRKER_EVENT")
1026+
$this.ClientEvents = $this.ConnectWorker([WorkerKind]::Events)
10121027
}
10131028

10141029
[bool] HasSession()

0 commit comments

Comments
 (0)