Advertisement
Stillkill

ServerPing

Apr 15th, 2019
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using Newtonsoft.Json;
  8. using System.Globalization;
  9. using Newtonsoft.Json.Converters;
  10. using System.Diagnostics;
  11.  
  12. namespace TFFServerManager.ServerHandler
  13. {
  14.     class ServerPing
  15.     {
  16.  
  17.         private NetworkStream _stream;
  18.         private List<byte> _buffer;
  19.         private int _offset;
  20.         public ServerHandler.Response ServerResponsePacket;
  21.         public ServerPing()
  22.         {
  23.             Console.Title = "Minecraft Server Ping";
  24.  
  25.             var client = new TcpClient();
  26.             var task = client.ConnectAsync("<IP redacted>", 25565);
  27.             Console.WriteLine("Connecting to Minecraft server..");
  28.  
  29.             while (!task.IsCompleted)
  30.             {
  31. #if DEBUG
  32.                 Debug.WriteLine("Connecting..");
  33. #endif
  34.                 Thread.Sleep(250);
  35.             }
  36.  
  37.             if (!client.Connected)
  38.             {
  39.                 Console.ForegroundColor = ConsoleColor.Red;
  40.                 Console.WriteLine("Unable to connect to the server");
  41.                 Console.ResetColor();
  42.                 Console.ReadKey(true);
  43.                 Environment.Exit(1);
  44.             }
  45.  
  46.             _buffer = new List<byte>();
  47.             _stream = client.GetStream();
  48.             Console.WriteLine("Sending status request");
  49.  
  50.  
  51.             /*
  52.              * Send a "Handshake" packet
  53.              * http://wiki.vg/Server_List_Ping#Ping_Process
  54.              */
  55.             WriteVarInt(47);
  56.             WriteString("mc.gamesnfriends.com");
  57.             WriteShort(25565);
  58.             WriteVarInt(1);
  59.             Flush(0);
  60.  
  61.             /*
  62.              * Send a "Status Request" packet
  63.              * http://wiki.vg/Server_List_Ping#Ping_Process
  64.              */
  65.             Flush(0);
  66.  
  67.             var buffer = new byte[Int16.MaxValue];
  68.             // var buffer = new byte[4096];
  69.             _stream.Read(buffer, 0, buffer.Length);
  70.  
  71.             try
  72.             {
  73.                 var length = ReadVarInt(buffer);
  74.                 var packet = ReadVarInt(buffer);
  75.                 var jsonLength = ReadVarInt(buffer);
  76.                 Console.WriteLine("Received packet 0x{0} with a length of {1}", packet.ToString("X2"), length);
  77.                 var json = ReadString(buffer, jsonLength);
  78.  
  79.                 this.ServerResponsePacket = Response.FromJson(json);
  80.                 File.WriteAllText("Output.json", Serialize.ToJson(ServerResponsePacket));
  81.             }
  82.             catch (IOException ex)
  83.             {
  84.                 /*
  85.                  * If an IOException is thrown then the server didn't
  86.                  * send us a VarInt or sent us an invalid one.
  87.                  */
  88.                 Console.ForegroundColor = ConsoleColor.Red;
  89.                 Console.WriteLine("Unable to read packet length from server,");
  90.                 Console.WriteLine("are you sure it's a Minecraft server?");
  91.                 Console.WriteLine("Here are the details:");
  92.                 Console.WriteLine(ex.ToString());
  93.                 Console.ResetColor();
  94.             }
  95.         }
  96.  
  97.         #region Read/Write methods
  98.         internal byte ReadByte(byte[] buffer)
  99.         {
  100.             var b = buffer[this._offset];
  101.             _offset += 1;
  102.             return b;
  103.         }
  104.  
  105.         internal byte[] Read(byte[] buffer, int length)
  106.         {
  107.             var data = new byte[length];
  108.             Array.Copy(buffer, _offset, data, 0, length);
  109.             _offset += length;
  110.             return data;
  111.         }
  112.  
  113.         internal int ReadVarInt(byte[] buffer)
  114.         {
  115.             var value = 0;
  116.             var size = 0;
  117.             int b;
  118.             while (((b = ReadByte(buffer)) & 0x80) == 0x80)
  119.             {
  120.                 value |= (b & 0x7F) << (size++ * 7);
  121.                 if (size > 5)
  122.                 {
  123.                     throw new IOException("This VarInt is an imposter!");
  124.                 }
  125.             }
  126.             return value | ((b & 0x7F) << (size * 7));
  127.         }
  128.  
  129.         internal string ReadString(byte[] buffer, int length)
  130.         {
  131.             var data = Read(buffer, length);
  132.             return Encoding.UTF8.GetString(data);
  133.         }
  134.  
  135.         internal void WriteVarInt(int value)
  136.         {
  137.             while ((value & 128) != 0)
  138.             {
  139.                 _buffer.Add((byte)(value & 127 | 128));
  140.                 value = (int)((uint)value) >> 7;
  141.             }
  142.             _buffer.Add((byte)value);
  143.         }
  144.  
  145.         internal void WriteShort(short value)
  146.         {
  147.             _buffer.AddRange(BitConverter.GetBytes(value));
  148.         }
  149.  
  150.         internal void WriteString(string data)
  151.         {
  152.             var buffer = Encoding.UTF8.GetBytes(data);
  153.             WriteVarInt(buffer.Length);
  154.             _buffer.AddRange(buffer);
  155.         }
  156.  
  157.         internal void Write(byte b)
  158.         {
  159.             _stream.WriteByte(b);
  160.         }
  161.  
  162.         internal void Flush(int id = -1)
  163.         {
  164.             var buffer = _buffer.ToArray();
  165.             _buffer.Clear();
  166.  
  167.             var add = 0;
  168.             var packetData = new[] { (byte)0x00 };
  169.             if (id >= 0)
  170.             {
  171.                 WriteVarInt(id);
  172.                 packetData = _buffer.ToArray();
  173.                 add = packetData.Length;
  174.                 _buffer.Clear();
  175.             }
  176.  
  177.             WriteVarInt(buffer.Length + add);
  178.             var bufferLength = _buffer.ToArray();
  179.             _buffer.Clear();
  180.  
  181.             _stream.Write(bufferLength, 0, bufferLength.Length);
  182.             _stream.Write(packetData, 0, packetData.Length);
  183.             _stream.Write(buffer, 0, buffer.Length);
  184.         }
  185.         #endregion
  186.     }
  187.  
  188.     //Packet Objects
  189.     public partial class Response
  190.     {
  191.         [JsonProperty("description")]
  192.         public Description Description { get; set; }
  193.  
  194.         [JsonProperty("players")]
  195.         public Players Players { get; set; }
  196.  
  197.         [JsonProperty("version")]
  198.         public Version Version { get; set; }
  199.     }
  200.  
  201.     public partial class Description
  202.     {
  203.         [JsonProperty("text")]
  204.         public string Text { get; set; }
  205.     }
  206.  
  207.     public partial class Players
  208.     {
  209.         [JsonProperty("max")]
  210.         public long Max { get; set; }
  211.  
  212.         [JsonProperty("online")]
  213.         public long Online { get; set; }
  214.  
  215.         [JsonProperty("sample")]
  216.         public Sample[] Sample { get; set; }
  217.     }
  218.  
  219.     public partial class Sample
  220.     {
  221.         [JsonProperty("id")]
  222.         public Guid Id { get; set; }
  223.  
  224.         [JsonProperty("name")]
  225.         public string Name { get; set; }
  226.     }
  227.  
  228.     public partial class Version
  229.     {
  230.         [JsonProperty("name")]
  231.         public string Name { get; set; }
  232.  
  233.         [JsonProperty("protocol")]
  234.         public long Protocol { get; set; }
  235.     }
  236.  
  237.     public partial class Response
  238.     {
  239.         public static Response FromJson(string json) => JsonConvert.DeserializeObject<Response>(json, Converter.Settings);
  240.     }
  241.  
  242.     public static class Serialize
  243.     {
  244.         public static string ToJson(this Response self) => JsonConvert.SerializeObject(self, Formatting.Indented, Converter.Settings);
  245.     }
  246.  
  247.     internal static class Converter
  248.     {
  249.         public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
  250.         {
  251.             MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
  252.             DateParseHandling = DateParseHandling.None,
  253.             Converters =
  254.             {
  255.                 new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
  256.             },
  257.         };
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement