Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using Newtonsoft.Json;
- using System.Globalization;
- using Newtonsoft.Json.Converters;
- using System.Diagnostics;
- namespace TFFServerManager.ServerHandler
- {
- class ServerPing
- {
- private NetworkStream _stream;
- private List<byte> _buffer;
- private int _offset;
- public ServerHandler.Response ServerResponsePacket;
- public ServerPing()
- {
- Console.Title = "Minecraft Server Ping";
- var client = new TcpClient();
- var task = client.ConnectAsync("<IP redacted>", 25565);
- Console.WriteLine("Connecting to Minecraft server..");
- while (!task.IsCompleted)
- {
- #if DEBUG
- Debug.WriteLine("Connecting..");
- #endif
- Thread.Sleep(250);
- }
- if (!client.Connected)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Unable to connect to the server");
- Console.ResetColor();
- Console.ReadKey(true);
- Environment.Exit(1);
- }
- _buffer = new List<byte>();
- _stream = client.GetStream();
- Console.WriteLine("Sending status request");
- /*
- * Send a "Handshake" packet
- * http://wiki.vg/Server_List_Ping#Ping_Process
- */
- WriteVarInt(47);
- WriteString("mc.gamesnfriends.com");
- WriteShort(25565);
- WriteVarInt(1);
- Flush(0);
- /*
- * Send a "Status Request" packet
- * http://wiki.vg/Server_List_Ping#Ping_Process
- */
- Flush(0);
- var buffer = new byte[Int16.MaxValue];
- // var buffer = new byte[4096];
- _stream.Read(buffer, 0, buffer.Length);
- try
- {
- var length = ReadVarInt(buffer);
- var packet = ReadVarInt(buffer);
- var jsonLength = ReadVarInt(buffer);
- Console.WriteLine("Received packet 0x{0} with a length of {1}", packet.ToString("X2"), length);
- var json = ReadString(buffer, jsonLength);
- this.ServerResponsePacket = Response.FromJson(json);
- File.WriteAllText("Output.json", Serialize.ToJson(ServerResponsePacket));
- }
- catch (IOException ex)
- {
- /*
- * If an IOException is thrown then the server didn't
- * send us a VarInt or sent us an invalid one.
- */
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Unable to read packet length from server,");
- Console.WriteLine("are you sure it's a Minecraft server?");
- Console.WriteLine("Here are the details:");
- Console.WriteLine(ex.ToString());
- Console.ResetColor();
- }
- }
- #region Read/Write methods
- internal byte ReadByte(byte[] buffer)
- {
- var b = buffer[this._offset];
- _offset += 1;
- return b;
- }
- internal byte[] Read(byte[] buffer, int length)
- {
- var data = new byte[length];
- Array.Copy(buffer, _offset, data, 0, length);
- _offset += length;
- return data;
- }
- internal int ReadVarInt(byte[] buffer)
- {
- var value = 0;
- var size = 0;
- int b;
- while (((b = ReadByte(buffer)) & 0x80) == 0x80)
- {
- value |= (b & 0x7F) << (size++ * 7);
- if (size > 5)
- {
- throw new IOException("This VarInt is an imposter!");
- }
- }
- return value | ((b & 0x7F) << (size * 7));
- }
- internal string ReadString(byte[] buffer, int length)
- {
- var data = Read(buffer, length);
- return Encoding.UTF8.GetString(data);
- }
- internal void WriteVarInt(int value)
- {
- while ((value & 128) != 0)
- {
- _buffer.Add((byte)(value & 127 | 128));
- value = (int)((uint)value) >> 7;
- }
- _buffer.Add((byte)value);
- }
- internal void WriteShort(short value)
- {
- _buffer.AddRange(BitConverter.GetBytes(value));
- }
- internal void WriteString(string data)
- {
- var buffer = Encoding.UTF8.GetBytes(data);
- WriteVarInt(buffer.Length);
- _buffer.AddRange(buffer);
- }
- internal void Write(byte b)
- {
- _stream.WriteByte(b);
- }
- internal void Flush(int id = -1)
- {
- var buffer = _buffer.ToArray();
- _buffer.Clear();
- var add = 0;
- var packetData = new[] { (byte)0x00 };
- if (id >= 0)
- {
- WriteVarInt(id);
- packetData = _buffer.ToArray();
- add = packetData.Length;
- _buffer.Clear();
- }
- WriteVarInt(buffer.Length + add);
- var bufferLength = _buffer.ToArray();
- _buffer.Clear();
- _stream.Write(bufferLength, 0, bufferLength.Length);
- _stream.Write(packetData, 0, packetData.Length);
- _stream.Write(buffer, 0, buffer.Length);
- }
- #endregion
- }
- //Packet Objects
- public partial class Response
- {
- [JsonProperty("description")]
- public Description Description { get; set; }
- [JsonProperty("players")]
- public Players Players { get; set; }
- [JsonProperty("version")]
- public Version Version { get; set; }
- }
- public partial class Description
- {
- [JsonProperty("text")]
- public string Text { get; set; }
- }
- public partial class Players
- {
- [JsonProperty("max")]
- public long Max { get; set; }
- [JsonProperty("online")]
- public long Online { get; set; }
- [JsonProperty("sample")]
- public Sample[] Sample { get; set; }
- }
- public partial class Sample
- {
- [JsonProperty("id")]
- public Guid Id { get; set; }
- [JsonProperty("name")]
- public string Name { get; set; }
- }
- public partial class Version
- {
- [JsonProperty("name")]
- public string Name { get; set; }
- [JsonProperty("protocol")]
- public long Protocol { get; set; }
- }
- public partial class Response
- {
- public static Response FromJson(string json) => JsonConvert.DeserializeObject<Response>(json, Converter.Settings);
- }
- public static class Serialize
- {
- public static string ToJson(this Response self) => JsonConvert.SerializeObject(self, Formatting.Indented, Converter.Settings);
- }
- internal static class Converter
- {
- public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
- {
- MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
- DateParseHandling = DateParseHandling.None,
- Converters =
- {
- new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
- },
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement