Skip to content

Commit

Permalink
🎨 Improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
xobust committed Apr 26, 2024
1 parent 031e8d6 commit 6d477c7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/CoreRCON.Tests/CsIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public async Task ConnectShouldConnectAndAuthenticate()
using RCON rcon = serverFixture.GetRconClient();
await rcon.ConnectAsync();

await rcon.ConnectAsync();

Assert.True(rcon.Connected);
Assert.True(rcon.Authenticated);
output.WriteLine("Rcon connection to server successfull");
}

[Fact]
Expand Down Expand Up @@ -92,7 +91,7 @@ public async Task TestMultipleSyncronousCommandsShouldReturn()
[Fact]
public async Task TestMultipleAsyncronousCommandsShouldReturn()
{
List<Task> tasks = new List<Task>();
List<Task> tasks = [];

using RCON rconClient = serverFixture.GetRconClient();
await rconClient.ConnectAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/CoreRCON.Tests/CsServerTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public CsServerFixture()
string authString = Convert.ToBase64String(Encoding.ASCII.GetBytes(configuration["DATHOST_API_TOKEN"]));
_client.DefaultRequestHeaders.Add("Authorization", "Basic " + authString);
// random password for the RCON server
_rconPassword = Guid.NewGuid().ToString("N").Substring(0, 10);
_rconPassword = Guid.NewGuid().ToString("N")[..10];

_retryPolicy = Policy
.Handle<HttpRequestException>()
Expand Down
1 change: 0 additions & 1 deletion src/CoreRCON/RCON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ await _tcp.SendAsync(new ArraySegment<byte>(packet.ToBytes()), SocketFlags.None)
await _tcp.SendAsync(new ArraySegment<byte>(emptyPackage.ToBytes()), SocketFlags.None)
.ConfigureAwait(false);
}

}

private void LogDisconnect(Task task)
Expand Down
32 changes: 19 additions & 13 deletions src/CoreRCON/ServerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ namespace CoreRCON
/// <summary>
/// Make a request to a server following the Source and Minecraft Server Query format.
/// </summary>
/// <see cref="https://developer.valvesoftware.com/wiki/Server_queries"/>
/// <see cref="http://wiki.vg/Query"/>
public class ServerQuery
/// <see href="https://developer.valvesoftware.com/wiki/Server_queries"/>
/// <see href="http://wiki.vg/Query"/>
/// <see href="https://wiki.vg/Server_List_Ping"/>
public static class ServerQuery
{
/// <summary>
/// The different query implementations.
Expand All @@ -27,15 +28,14 @@ public enum ServerType
/// <summary>
/// Minecraft packet types.
/// </summary>
private enum PacketType
private enum PacketType : byte
{
Handshake = 0x09,
Stat = 0x00
}

private static readonly UdpClient _client;
private static readonly byte[] _magic = [0xFE, 0xFD]; // Minecraft 'magic' bytes.
private static readonly byte[] _sessionid = [0x01, 0x02, 0x03, 0x04];
private static readonly byte[] _magic = [0xFE, 0xFD];
private static readonly byte[] _asInfoPayload = [0xFF, 0xFF, 0xFF, 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00];
private static readonly byte[] _asInfochallengeResponse = [0xFF, 0xFF, 0xFF, 0xFF, 0x41];

Expand Down Expand Up @@ -74,10 +74,16 @@ public static Task<IQueryInfo> Info(IPEndPoint host, ServerType type, TimeSpan?
}
return SourceQueryInfo.FromBytes(sourceResponse.Buffer);
case ServerType.Minecraft:
var padding = new byte[] { 0x00, 0x00, 0x00, 0x00 };
var datagram = _magic.Concat([(byte)PacketType.Stat]).Concat(_sessionid).Concat(await Challenge(host, ServerType.Minecraft)).Concat(padding).ToArray();
Random random = new();
int sessionId = random.Next() & 0x0F0F0F0F; // Minecraft does not process the higher 4-bits on each byte
byte[] sessionIdByte = BitConverter.GetBytes(sessionId);
byte[] padding = [0x00, 0x00, 0x00, 0x00];
byte[] challengeResponse = await Challenge(host, ServerType.Minecraft, sessionIdByte);
byte[] datagram = [.. _magic, (byte)PacketType.Stat, .. sessionIdByte, .. challengeResponse, .. padding];
await _client.SendAsync(datagram, datagram.Length, host);
var mcResponce = await _client.ReceiveAsync();
UdpReceiveResult mcResponce = await _client.ReceiveAsync();
return MinecraftQueryInfo.FromBytes(mcResponce.Buffer);
default:
throw new ArgumentException("type argument was invalid");
Expand All @@ -98,9 +104,9 @@ public static Task<IQueryInfo> Info(IPEndPoint host, ServerType type, TimeSpan?
/// <param name="host">Endpoint of the server.</param>
public static async Task<ServerQueryPlayer[]> Players(IPEndPoint host)
{
var challenge = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0x55 }.Concat(await Challenge(host, ServerType.Source)).ToArray();
byte[] challenge = [0xFF, 0xFF, 0xFF, 0xFF, 0x55, .. await Challenge(host, ServerType.Source)];
await _client.SendAsync(challenge, 9, host);
var response = await _client.ReceiveAsync();
UdpReceiveResult response = await _client.ReceiveAsync();
return ServerQueryPlayer.FromBytes(response.Buffer);
}

Expand All @@ -109,7 +115,7 @@ public static async Task<ServerQueryPlayer[]> Players(IPEndPoint host)
/// </summary>
/// <param name="host">Endpoint of the server.</param>
/// <returns>Challenge code to use with challenged requests.</returns>
private static async Task<byte[]> Challenge(IPEndPoint host, ServerType type)
private static async Task<byte[]> Challenge(IPEndPoint host, ServerType type, byte[] seesionId = null)
{
switch (type)
{
Expand All @@ -118,7 +124,7 @@ private static async Task<byte[]> Challenge(IPEndPoint host, ServerType type)
return (await _client.ReceiveAsync()).Buffer.Skip(5).Take(4).ToArray();
case ServerType.Minecraft:
// Create request
var datagram = _magic.Concat([(byte)PacketType.Handshake]).Concat(_sessionid).ToArray();
byte[] datagram = [.. _magic, (byte)PacketType.Handshake, .. seesionId];
await _client.SendAsync(datagram, datagram.Length, host);

// Parse challenge token
Expand Down

0 comments on commit 6d477c7

Please sign in to comment.