/// <summary> /// Send a packet to the server. Compression and encryption will be handled automatically. /// </summary> /// <param name="packetID">packet ID</param> /// <param name="packetData">packet Data</param> private void SendPacket(int packetID, IEnumerable <byte> packetData) { //The inner packet byte[] the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(packetID), packetData.ToArray()); if (compression_treshold > 0) //Compression enabled? { if (the_packet.Length >= compression_treshold) //Packet long enough for compressing? { byte[] compressed_packet = ZlibUtils.Compress(the_packet); the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), compressed_packet); } else { byte[] uncompressed_length = dataTypes.GetVarInt(0); //Not compressed (short packet) the_packet = dataTypes.ConcatBytes(uncompressed_length, the_packet); } } socketWrapper.SendDataRAW(dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), the_packet)); }
/// <summary> /// Ping a Minecraft server to get information about the server /// </summary> /// <returns>True if ping was successful</returns> public static bool doPing(string host, int port, ref int protocolversion, ref ForgeInfo forgeInfo) { string version = ""; TcpClient tcp = ProxyHandler.newTcpClient(host, port); tcp.ReceiveBufferSize = 1024 * 1024; SocketWrapper socketWrapper = new SocketWrapper(tcp); DataTypes dataTypes = new DataTypes(MC18Version); byte[] packet_id = dataTypes.GetVarInt(0); byte[] protocol_version = dataTypes.GetVarInt(-1); byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port); byte[] next_state = dataTypes.GetVarInt(1); byte[] packet = dataTypes.ConcatBytes(packet_id, protocol_version, dataTypes.GetString(host), server_port, next_state); byte[] tosend = dataTypes.ConcatBytes(dataTypes.GetVarInt(packet.Length), packet); socketWrapper.SendDataRAW(tosend); byte[] status_request = dataTypes.GetVarInt(0); byte[] request_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(status_request.Length), status_request); socketWrapper.SendDataRAW(request_packet); int packetLength = dataTypes.ReadNextVarIntRAW(socketWrapper); if (packetLength > 0) //Read Response length { List <byte> packetData = new List <byte>(socketWrapper.ReadDataRAW(packetLength)); if (dataTypes.ReadNextVarInt(packetData) == 0x00) //Read Packet ID { string result = dataTypes.ReadNextString(packetData); //Get the Json data if (!String.IsNullOrEmpty(result) && result.StartsWith("{") && result.EndsWith("}")) { Json.JSONData jsonData = Json.ParseJson(result); if (jsonData.Type == Json.JSONData.DataType.Object && jsonData.Properties.ContainsKey("version")) { Json.JSONData versionData = jsonData.Properties["version"]; //Retrieve display name of the Minecraft version if (versionData.Properties.ContainsKey("name")) { version = versionData.Properties["name"].StringValue; } //Retrieve protocol version number for handling this server if (versionData.Properties.ContainsKey("protocol")) { protocolversion = dataTypes.Atoi(versionData.Properties["protocol"].StringValue); } // Check for forge on the server. Protocol18Forge.ServerInfoCheckForge(jsonData, ref forgeInfo); ConsoleIO.WriteLineFormatted("§8Server version : " + version + " (protocol v" + protocolversion + (forgeInfo != null ? ", with Forge)." : ").")); return(true); } } } } return(false); }