Skip to content

Commit

Permalink
Changed the packet build routine
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaezf committed Oct 18, 2023
1 parent 08cd5ae commit ce7155c
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 42 deletions.
Binary file modified .vs/RCON/v16/.suo
Binary file not shown.
155 changes: 113 additions & 42 deletions RconClient.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,125 @@
Imports System.Text

Public Class RCONClient
Private _client As TcpClient
Private _stream As NetworkStream
Private _password As String

Public Sub New(ip As String, port As Integer, password As String)
_client = New TcpClient(ip, port)
_stream = _client.GetStream()
_password = password
End Sub

Public Function SendCommand(command As String) As String
Dim commandBytes As Byte() = Encoding.ASCII.GetBytes(command)
Dim packetLength As Integer = 4 + 4 + commandBytes.Length + 2
Dim buffer(packetLength - 1) As Byte

' Packet length
BitConverter.GetBytes(packetLength).CopyTo(buffer, 0)

' Request ID (set to 1 for simplicity)
BitConverter.GetBytes(1).CopyTo(buffer, 4)

' Type (0 for server)
BitConverter.GetBytes(0).CopyTo(buffer, 8)

' RCON command
commandBytes.CopyTo(buffer, 12)
Private Client As TcpClient
Private stream As NetworkStream
Public Connected As Boolean = False
Public Authorized As Boolean = False

' Null-terminated string
buffer(packetLength - 3) = 0
buffer(packetLength - 2) = 0

' Send the packet to the server
_stream.Write(buffer, 0, packetLength)
Public Sub Connect(ByVal server As String, ByVal port As Integer)
Try
Client = New TcpClient(server, port)
stream = Client.GetStream()
Connected = True
Catch ex As Exception
Close()
MsgBox("Could not connect to the server. Check ip/host and port.", vbCritical, "RCON.ErrConn")
End Try
End Sub

' Receive and parse the response
Dim responseBuffer(4096) As Byte
Dim bytesRead As Integer = _stream.Read(responseBuffer, 0, responseBuffer.Length)
Dim response As String = Encoding.ASCII.GetString(responseBuffer, 12, bytesRead - 12)
Public Sub Authorize(ByVal password As String)
If Connected = True Then
Dim a As Boolean = SendAuthPacket(password)
If a = True Then
Authorized = True
ElseIf a = False Then
Authorized = False
Close()
MsgBox("Invalid password or rcon_password cvar is not set.")
End If
ElseIf Connected = False Then
Authorized = False
Close()
End If
End Sub

_stream.Close()
_client.Close()
Private Function SendAuthPacket(ByVal password As String) As Boolean
If Connected = True Then
Try
Dim Packet As Byte() = New Byte(CByte((4 + 4 + 4 + password.Length + 1))) {}
Packet(0) = password.Length + 9 'Packet Size (Integer)
Packet(4) = 99 'Request Id (Integer)
Packet(8) = 3 ' 3 = SERVERDATA_AUTH
For X As Integer = 0 To password.Length - 1
Packet(12 + X) = System.Text.Encoding.UTF8.GetBytes(password(X))(0)
Next
stream.Write(Packet, 0, Packet.Length)
Dim data As Byte() = New Byte(4096) {}
Dim bytes As Integer
Do
bytes = stream.Read(data, 0, data.Length)
Loop While bytes = 0
Dim result As Byte() = New Byte(bytes - 1) {}
Array.Copy(data, 0, result, 0, bytes)
Dim ID As Integer = BitConverter.ToInt32(data, 4)
Dim Type As Integer = BitConverter.ToInt32(data, 8)
If ID = 99 And Type = 2 Then
Return True
Else
Return False
End If
Catch ex As Exception
Return False
Close()
End Try
Else
Return False
Close()
End If
End Function

Return response
Public Function SendCommand(ByVal Command As String) As String
If Connected = True And Authorized = True Then
Try
Dim Packet As Byte() = New Byte(CByte((4 + 4 + 4 + Command.Length + 1))) {}
Packet(0) = Command.Length + 9 'Packet Size (Integer)
Packet(4) = 99 'Request Id (Integer)
Packet(8) = 2 '2 = SERVERDATA_EXECCOMMAND
For X As Integer = 0 To Command.Length - 1
Packet(12 + X) = System.Text.Encoding.UTF8.GetBytes(Command(X))(0)
Next
stream.Write(Packet, 0, Packet.Length)
Dim data As Byte() = New Byte(4096) {}
Dim bytes As Integer
Do
bytes = stream.Read(data, 0, data.Length)
Loop While bytes = 0
Dim result As Byte() = New Byte(bytes - 1) {}
Array.Copy(data, 0, result, 0, bytes)
Dim size As Integer = BitConverter.ToInt32(data, 0)
Dim ID As Integer = BitConverter.ToInt32(data, 4)
Dim Typex As Integer = BitConverter.ToInt32(data, 8)
Dim Payload As String = Encoding.UTF8.GetString(data, 12, size - 10)
'Dim returndata As String = "Packet size: " & size.ToString & vbCrLf & "Packet ID: " & ID.ToString & vbCrLf & "Packet type: " & Typex.ToString & vbCrLf & "Packet body: " & Payload
'MsgBox(returndata)
If ID = 99 And Typex = 0 Then
Return Payload
Else
Return "Bad ID or Type" & vbCrLf & vbCrLf & "ID expected: 99, received: " & ID.ToString & vbCrLf & "Type expected: 0, received: " & Typex.ToString
Close()
End If
Catch ex As Exception
Return ""
Close()
End Try
Else
Close()
Return ""
End If
End Function

'Public Sub Close()
'_stream.Close()
'_client.Close()
'End Sub
Public Sub Close()
Try
If Client.Connected = True Then
Client.Close()
Client = Nothing
stream.Close()
End If
Catch ex As Exception
End Try
Connected = False
Authorized = False
End Sub

End Class
Binary file modified bin/Debug/RCONClient.dll
Binary file not shown.
Binary file modified bin/Debug/RCONClient.pdb
Binary file not shown.
Binary file modified obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
Binary file not shown.
9 changes: 9 additions & 0 deletions obj/Debug/RCON.vbproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ C:\Users\Francesc\Desktop\RCON\obj\Debug\RCON.vbproj.CoreCompileInputs.cache
C:\Users\Francesc\Desktop\RCON\obj\Debug\RCONClient.dll
C:\Users\Francesc\Desktop\RCON\obj\Debug\RCONClient.xml
C:\Users\Francesc\Desktop\RCON\obj\Debug\RCONClient.pdb
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\bin\Debug\RCONClient.dll
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\bin\Debug\RCONClient.pdb
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\bin\Debug\RCONClient.xml
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCONClient.Resources.resources
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCON.vbproj.GenerateResource.cache
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCON.vbproj.CoreCompileInputs.cache
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCONClient.dll
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCONClient.xml
F:\Visual Basic\Por acabar\Valve RCON in VB.NET\obj\Debug\RCONClient.pdb
Binary file modified obj/Debug/RCONClient.dll
Binary file not shown.
Binary file modified obj/Debug/RCONClient.pdb
Binary file not shown.

0 comments on commit ce7155c

Please sign in to comment.