Skip to content

Commit 99301fb

Browse files
committed
Managed to get RCON to work
1 parent 23e4c86 commit 99301fb

File tree

3 files changed

+77
-50
lines changed

3 files changed

+77
-50
lines changed

MCQuery-Console/Program.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ class Program
77
{
88
static void Main(string[] args)
99
{
10-
Console.WriteLine("Server IP Address: ");
11-
string ipAddress = Console.ReadLine();
12-
13-
Console.WriteLine("Server Port: ");
14-
string portString = Console.ReadLine();
10+
// Console.WriteLine("Server IP Address: ");
11+
// string ipAddress = Console.ReadLine();
12+
//
13+
// Console.WriteLine("Server Port: ");
14+
// string portString = Console.ReadLine();
15+
string ipAddress = "192.168.0.7";
16+
string portString = "25575";
1517

1618
int.TryParse(portString, out int port);
1719

@@ -29,6 +31,13 @@ static void Main(string[] args)
2931
//serverQuery.Close();
3032
Rcon rconServer = new Rcon(ipAddress, port, "yolo");
3133
rconServer.Login();
34+
// rconServer.SendCommand("time set 0");
35+
36+
while (true)
37+
{
38+
string input = Console.ReadLine();
39+
rconServer.SendCommand(input);
40+
}
3241

3342
//Console.WriteLine("Printing out server info: ");
3443
//Console.WriteLine("Server MOTD: {0}", basicServer.Motd);

MCQuery/Connection.cs

+12-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
53
using System.Net.Sockets;
64
using System.Text;
7-
using System.Timers;
85

96
namespace MCQuery
107
{
@@ -53,10 +50,7 @@ protected byte[] SendByUdp(string address, int port, byte[] data)
5350
{
5451
return new byte[] { };
5552
}
56-
else
57-
{
58-
return receiveData;
59-
}
53+
return receiveData;
6054
}
6155
catch (SocketException exception)
6256
{
@@ -82,10 +76,7 @@ protected bool IsDomainAddress(string address)
8276
{
8377
return true;
8478
}
85-
else
86-
{
87-
return false;
88-
}
79+
return false;
8980
}
9081
catch (SocketException exception)
9182
{
@@ -102,23 +93,23 @@ protected byte[] SendByTcp(string address, int port, byte[] data)
10293
if (_tcpClient == null)
10394
{
10495
_tcpClient = new TcpClient(address, port);
105-
NetworkStream networkStream = _tcpClient.GetStream();
96+
_tcpClient.ReceiveTimeout = 5000;
97+
}
10698

107-
byte[] buffer = new byte[1024];
99+
NetworkStream networkStream = _tcpClient.GetStream();
108100

109-
networkStream.Write(data, 0, data.Length);
110-
int byteCount = networkStream.Read(buffer, 0, buffer.Length);
101+
byte[] buffer = new byte[1024];
111102

112-
string receivedText = Encoding.ASCII.GetString(buffer, 0, byteCount);
113-
}
103+
networkStream.Write(data, 0, data.Length);
104+
int byteCount = networkStream.Read(buffer, 0, buffer.Length);
105+
106+
return BitConverter.GetBytes(byteCount);
114107
}
115108
catch (SocketException exception)
116109
{
117110
Console.WriteLine(exception);
118111
throw;
119112
}
120-
121-
return new byte[] { };
122113
}
123114

124115
public virtual void Close()
@@ -151,6 +142,8 @@ protected virtual void Dispose(bool disposing)
151142

152143
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
153144
// TODO: set large fields to null.
145+
_tcpClient = null;
146+
_udpClient = null;
154147

155148
disposedValue = true;
156149
}

MCQuery/Rcon.cs

+51-26
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,82 @@
44

55
namespace MCQuery
66
{
7-
public class Rcon : Connection
8-
{
9-
private readonly byte[] _requestId = { 0x01, 0x01, 0x01, 0x01 };
7+
public class Rcon : Connection
8+
{
9+
private readonly int _requestId = 1;
1010

11-
private readonly byte[] _loginType = { 0x03 };
12-
private readonly byte[] _commandType = { 0x02 };
13-
private readonly byte[] _multiType = { 0x00 };
11+
private readonly int _loginType = 3;
12+
private readonly int _commandType = 2;
13+
private readonly int _multiType = 0;
1414
private readonly byte[] _twoBytePad = { 0x00, 0x00 };
1515

1616
private string _password;
1717

1818
public Rcon(string address, int port, string password) : base(address, port)
19-
{
19+
{
2020
_password = password;
21-
}
21+
//#();
22+
}
2223

2324
public bool Login()
2425
{
26+
//Int32 = 4 byte array
2527
//1. Reminder Lenght = int
2628
//2. Request Id = int
2729
//3. Type = int
2830
//4. Payload = byte[]
2931
//5. 2-byte pad = byte, byte
3032

31-
// List<byte> message = new List<byte>();
32-
// message.AddRange(new byte[] { 0x08 });
33-
// message.AddRange(new byte[] { 0x01 });
34-
// message.AddRange(_loginType);
35-
// message.AddRange(Encoding.ASCII.GetBytes(_password));
36-
// message.AddRange(_twoBytePad);
37-
// byte[] loginMessage = message.ToArray();
38-
39-
var command = BitConverter.GetBytes(10 + Encoding.UTF8.GetByteCount(_password));
4033
List<byte> message = new List<byte>();
41-
message.AddRange(command);
42-
message.AddRange(BitConverter.GetBytes(1));
43-
message.AddRange(BitConverter.GetBytes(3));
34+
35+
//Reminder = requestId (int32=4) + loginType (int32=4) + twoBytePad (2) => 10
36+
var reminder = BitConverter.GetBytes(10 + Encoding.UTF8.GetByteCount(_password));
37+
message.AddRange(reminder);
38+
message.AddRange(BitConverter.GetBytes(_requestId));
39+
message.AddRange(BitConverter.GetBytes(_loginType));
4440
message.AddRange(Encoding.UTF8.GetBytes(_password));
4541
message.AddRange(_twoBytePad);
4642

4743
byte[] response = SendByTcp(_address, _port, message.ToArray());
44+
bool authenticated = false;
45+
46+
foreach (byte item in response)
47+
{
48+
if (reminder[0] == item)
49+
{
50+
authenticated = true;
51+
break;
52+
}
53+
}
54+
55+
return authenticated;
56+
}
57+
58+
public bool SendCommand(string command)
59+
{
60+
List<byte> message = new List<byte>();
61+
62+
//Reminder = requestId (int32=4) + commandType (int32=4) + twoBytePad (2) => 10
63+
var reminder = BitConverter.GetBytes(10 + Encoding.UTF8.GetByteCount(command));
64+
message.AddRange(reminder);
65+
message.AddRange(BitConverter.GetBytes(_requestId));
66+
message.AddRange(BitConverter.GetBytes(_commandType));
67+
message.AddRange(Encoding.UTF8.GetBytes(command));
68+
message.AddRange(_twoBytePad);
69+
70+
byte[] response = SendByTcp(_address, _port, message.ToArray());
71+
bool didSucceeed = false;
4872

4973
foreach (byte item in response)
5074
{
51-
//if (item == _requestId)
52-
//{
53-
// return true;
54-
//}
75+
if (reminder[0] == item)
76+
{
77+
didSucceeed = true;
78+
break;
79+
}
5580
}
5681

57-
return false;
82+
return didSucceeed;
5883
}
59-
}
84+
}
6085
}

0 commit comments

Comments
 (0)