-
Notifications
You must be signed in to change notification settings - Fork 0
/
RconClient.vb
126 lines (118 loc) · 4.86 KB
/
RconClient.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Imports System.Net.Sockets
Imports System.Text
Public Class RCONClient
Private Client As TcpClient
Private stream As NetworkStream
Public Connected As Boolean = False
Public Authorized As Boolean = False
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
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
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
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()
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