Skip to content

Commit 04b6b18

Browse files
committed
[SourceRcon] Fix handling of no multipacket response for commands like 'exit'
1 parent 58b57b7 commit 04b6b18

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

Modules/SourceRcon/SourceRcon.psm1

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ function SourceRcon {
7070
}
7171
$bytes
7272
}
73-
function BytesToInt32 ($bytes) {
74-
[BitConverter]::ToInt32($bytes, 0)
73+
function BytesToInt32 ([byte[]]$bytes) {
74+
if ($bytes.Length -gt 0) {
75+
[BitConverter]::ToInt32($bytes, 0)
76+
}
7577
}
7678
function BuildPacket ([int]$ID, [int]$TYPE, [string]$BODY) {
7779
$pack = (IntToBytes $ID) + (IntToBytes $TYPE) + $enc.GetBytes($BODY) + 0 + 0
@@ -103,16 +105,18 @@ function SourceRcon {
103105
$pack
104106
}
105107
function ParsePacket ([byte[]]$pack) {
106-
$IdBytes = $pack[0..3]
107-
$typeBytes = $pack[4..7]
108-
$bodyBytes = $pack[8..($pack.Length -1 -1 -1)] # Ignore Null Character at 1) at Packet Empty String Terminator 2) end of Packet Body
109-
@{
110-
Id = BytesToInt32 $IdBytes
111-
Type = BytesToInt32 $typeBytes
112-
Body = $enc.GetString($bodyBytes)
113-
IdBytes = $IdBytes
114-
TypeBytes = $typeBytes
115-
BodyBytes = $bodyBytes
108+
if ($pack.Length -ge 10) {
109+
$IdBytes = $pack[0..3]
110+
$typeBytes = $pack[4..7]
111+
$bodyBytes = $pack[8..($pack.Length -1 -1)] # Ignore Null Character at 1) at Packet Empty String Terminator 2) end of Packet Body
112+
@{
113+
Id = BytesToInt32 $IdBytes
114+
Type = BytesToInt32 $typeBytes
115+
Body = $enc.GetString($bodyBytes)
116+
IdBytes = $IdBytes
117+
TypeBytes = $typeBytes
118+
BodyBytes = $bodyBytes
119+
}
116120
}
117121
}
118122
function Auth {
@@ -131,13 +135,17 @@ function SourceRcon {
131135
$answer = ''
132136
while ($true) {
133137
try {
134-
# Read the Size of packet
138+
# Read the size of packet
135139
$rPack = ReceivePacket 4
136140
if (!$rPack.Length) { return }
137141
$size = BytesToInt32 $rPack
138-
$rPack = ReceivePacket $size
142+
if ($size -eq 0) {
143+
# No more packets to read from socket. E.g. 'exit'
144+
break
145+
}
139146

140147
# Now read the packet
148+
$rPack = ReceivePacket $size
141149
$response = ParsePacket $rPack
142150
if ($response['ID'] -eq $packetID_MultipackDummy) {
143151
# At the end of a multiple-packet response, the dummy empty packet is finally mirrored, followed by another RESPONSE_VALUE packet containing 0x0000 0001 0000 0000 in the packet body field.

0 commit comments

Comments
 (0)