diff --git a/.vs/RCON/v16/.suo b/.vs/RCON/v16/.suo index b1d0e97..af7ee2e 100644 Binary files a/.vs/RCON/v16/.suo and b/.vs/RCON/v16/.suo differ diff --git a/RconClient.vb b/RconClient.vb index d1b3b5d..234d671 100644 --- a/RconClient.vb +++ b/RconClient.vb @@ -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 diff --git a/bin/Debug/RCONClient.dll b/bin/Debug/RCONClient.dll index c2b4027..6c38e81 100644 Binary files a/bin/Debug/RCONClient.dll and b/bin/Debug/RCONClient.dll differ diff --git a/bin/Debug/RCONClient.pdb b/bin/Debug/RCONClient.pdb index 585d251..fa6888b 100644 Binary files a/bin/Debug/RCONClient.pdb and b/bin/Debug/RCONClient.pdb differ diff --git a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 341d95a..aa6e024 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/obj/Debug/RCON.vbproj.FileListAbsolute.txt b/obj/Debug/RCON.vbproj.FileListAbsolute.txt index 6d272b8..387890f 100644 --- a/obj/Debug/RCON.vbproj.FileListAbsolute.txt +++ b/obj/Debug/RCON.vbproj.FileListAbsolute.txt @@ -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 diff --git a/obj/Debug/RCONClient.dll b/obj/Debug/RCONClient.dll index c2b4027..6c38e81 100644 Binary files a/obj/Debug/RCONClient.dll and b/obj/Debug/RCONClient.dll differ diff --git a/obj/Debug/RCONClient.pdb b/obj/Debug/RCONClient.pdb index 585d251..fa6888b 100644 Binary files a/obj/Debug/RCONClient.pdb and b/obj/Debug/RCONClient.pdb differ