-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoldSourceRcon.cs
98 lines (90 loc) · 3.7 KB
/
GoldSourceRcon.cs
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
using System;
public class GoldSourceRcon
{
public GoldSourceRcon(string address, int port, string rconPassword, int debugFlag = 0) {
Console.Out.WriteLine("Sending GoldSourceRcon to {0}:{1}", address, port);
var remoteEP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(address), port);
udpClient = new System.Net.Sockets.UdpClient();
udpClient.Client.SendTimeout = 500;
udpClient.Client.ReceiveTimeout = 500;
udpClient.Connect(remoteEP);
rcon_password = rconPassword;
debug = debugFlag;
}
private System.Text.Encoding enc = System.Text.Encoding.UTF8;
// UDP Socket Properties
private System.Net.IPEndPoint remoteEP;
private System.Net.Sockets.UdpClient udpClient;
// Rcon Protocol Properties
private int challengeID = 0;
private string rcon_password;
// Debug
private int debug;
// Methods
private byte[] BuildPacket(string command) {
byte[] head = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF };
byte[] body = enc.GetBytes(command + Char.MinValue);
byte[] pack = new byte[head.Length + body.Length];
System.Buffer.BlockCopy(head, 0, pack, 0, head.Length);
System.Buffer.BlockCopy(body, 0, pack, head.Length, body.Length);
if ((debug & 1) == 1) { Console.WriteLine("[BuildPacket] head: " + enc.GetString(head)); }
if ((debug & 1) == 1) { Console.WriteLine("[BuildPacket] body: " + enc.GetString(body)); }
if ((debug & 1) == 1) { Console.WriteLine("[BuildPacket] pack: " + enc.GetString(pack)); }
return pack;
}
private void SendPacket(byte[] pack) {
if ((debug & 1) == 1) { Console.WriteLine("[SendPacket] " + enc.GetString(pack)); }
udpClient.Send(pack, pack.Length);
}
private byte[] ReceivePacket() {
byte[] pack = udpClient.Receive(ref remoteEP);
if ((debug & 1) == 1) { Console.WriteLine("[ReceivePacket] " + enc.GetString(pack)); }
return pack;
}
private string GetResponse(byte[] pack) {
if ((debug & 1) == 1) { Console.WriteLine("[GetResponse]"); }
if (pack.Length > 5) {
int numItems = pack.Length - 5;
byte[] body = new byte[numItems];
System.Buffer.BlockCopy(pack, 5, body, 0, numItems);
var response = enc.GetString(body);
return response;
}
return "";
}
private int InitGetChallenge() {
if ((debug & 1) == 1) { Console.WriteLine("[InitGetChallenge]"); }
var pack = BuildPacket("challenge rcon\n");
var response = SendReceive(pack);
var match = System.Text.RegularExpressions.Regex.Match(response, @"(\d+)");
if (match.Success) {
challengeID = int.Parse(match.Groups[1].Value);
}
return challengeID;
}
private string SendReceive(byte[] pack) {
SendPacket(pack);
var rPack = ReceivePacket();
var response = GetResponse(rPack);
return response;
}
public string Command(string command) {
string response = "";
try {
if (challengeID == 0) {
challengeID = InitGetChallenge();
if ((debug & 1) == 1) { Console.WriteLine("[Command] Got challengeID: " + challengeID); }
}else {
if ((debug & 1) == 1) { Console.WriteLine("[Command] Using existing challengeID: " + challengeID); }
}
if (challengeID > 0) {
var pack = BuildPacket(String.Format("rcon {0} {1} {2}", challengeID, rcon_password, command));
response = SendReceive(pack);
}
}catch(Exception ex) {
challengeID = 0;
throw ex;
}
return response;
}
}