Server list ping packet done

(+) Thanks dogwatch for pointing out the mistake in int->varint
converter !
This commit is contained in:
ORelio 2014-01-08 20:54:42 +01:00
parent 5fb54882ca
commit bee1893f75
2 changed files with 40 additions and 42 deletions

View file

@ -127,7 +127,7 @@ namespace MinecraftClient
public bool HasBeenKicked { get { return connectionlost; } } public bool HasBeenKicked { get { return connectionlost; } }
bool connectionlost = false; bool connectionlost = false;
bool encrypted = false; bool encrypted = false;
byte protocolversion; int protocolversion;
public bool Update() public bool Update()
{ {
@ -471,6 +471,7 @@ namespace MinecraftClient
bytes.Add((byte)(paramInt & 127 | 128)); bytes.Add((byte)(paramInt & 127 | 128));
paramInt = (int)(((uint)paramInt) >> 7); paramInt = (int)(((uint)paramInt) >> 7);
} }
bytes.Add((byte)paramInt);
return bytes.ToArray(); return bytes.ToArray();
} }
private static byte[] concatBytes(params byte[][] bytes) private static byte[] concatBytes(params byte[][] bytes)
@ -480,6 +481,10 @@ namespace MinecraftClient
result.AddRange(array); result.AddRange(array);
return result.ToArray(); return result.ToArray();
} }
private static int atoi(string str)
{
return int.Parse(new string(str.Trim().TakeWhile(char.IsDigit).ToArray()));
}
private static void setcolor(char c) private static void setcolor(char c)
{ {
@ -552,7 +557,7 @@ namespace MinecraftClient
return results[0]; return results[0];
} }
public void setVersion(byte ver) { protocolversion = ver; } public void setVersion(int ver) { protocolversion = ver; }
public void setClient(TcpClient n) { c = n; } public void setClient(TcpClient n) { c = n; }
private void setEncryptedClient(Crypto.AesStream n) { s = n; encrypted = true; } private void setEncryptedClient(Crypto.AesStream n) { s = n; encrypted = true; }
private void Receive(byte[] buffer, int start, int offset, SocketFlags f) private void Receive(byte[] buffer, int start, int offset, SocketFlags f)
@ -573,7 +578,7 @@ namespace MinecraftClient
else c.Client.Send(buffer); else c.Client.Send(buffer);
} }
public static bool GetServerInfo(string serverIP, ref byte protocolversion, ref string version) public static bool GetServerInfo(string serverIP, ref int protocolversion, ref string version)
{ {
try try
{ {
@ -614,17 +619,26 @@ namespace MinecraftClient
MinecraftCom ComTmp = new MinecraftCom(); MinecraftCom ComTmp = new MinecraftCom();
ComTmp.setClient(tcp); ComTmp.setClient(tcp);
if (ComTmp.readNextVarInt() > 0) //Read Response length //<- STUCK HERE (NO ANSWER FROM SERVER) if (ComTmp.readNextVarInt() > 0) //Read Response length
{ {
if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID if (ComTmp.readNextVarInt() == 0x00) //Read Packet ID
{ {
string result = ComTmp.readNextString(); string result = ComTmp.readNextString(); //Get the Json data
Console.ForegroundColor = ConsoleColor.DarkGray; if (result[0] == '{' && result.Contains("protocol\":") && result.Contains("name\":\""))
Console.WriteLine(result); {
//Console.WriteLine("Server version : MC " + version + " (protocol v" + protocolversion + ").");*/ string[] tmp_ver = result.Split(new string[] { "protocol\":" }, StringSplitOptions.None);
Console.ForegroundColor = ConsoleColor.Gray; string[] tmp_name = result.Split(new string[] { "name\":\"" }, StringSplitOptions.None);
//return true; if (tmp_ver.Length >= 2 && tmp_name.Length >= 2)
return false; //MC 1.7+ not supported {
protocolversion = atoi(tmp_ver[1]);
version = tmp_name[1].Split('"')[0];
Console.ForegroundColor = ConsoleColor.DarkGray;
//Console.WriteLine(result); //Debug: show the full Json string
Console.WriteLine("Server version : " + version + " (protocol v" + protocolversion + ").");
Console.ForegroundColor = ConsoleColor.Gray;
return true;
}
}
} }
} }
Console.ForegroundColor = ConsoleColor.DarkGray; Console.ForegroundColor = ConsoleColor.DarkGray;
@ -642,39 +656,23 @@ namespace MinecraftClient
} }
public bool Handshake(string username, string sessionID, ref string serverID, ref byte[] token, string host, int port) public bool Handshake(string username, string sessionID, ref string serverID, ref byte[] token, string host, int port)
{ {
//array byte[] packet_id = getVarInt(0);
byte[] data = new byte[10 + (username.Length + host.Length) * 2]; byte[] protocol_version = getVarInt(4);
byte[] server_adress_val = Encoding.UTF8.GetBytes(host);
byte[] server_adress_len = getVarInt(server_adress_val.Length);
byte[] server_port = BitConverter.GetBytes((ushort)port); Array.Reverse(server_port);
byte[] next_state = getVarInt(1);
byte[] handshake_packet = concatBytes(packet_id, protocol_version, server_adress_len, server_adress_val, server_port, next_state);
byte[] handshake_packet_tosend = concatBytes(getVarInt(handshake_packet.Length), handshake_packet);
//packet id Send(handshake_packet_tosend);
data[0] = (byte)2;
//Protocol Version byte[] username_val = Encoding.UTF8.GetBytes(username);
data[1] = protocolversion; byte[] username_len = getVarInt(username_val.Length);
byte[] login_packet = concatBytes(packet_id, username_len, username_val);
byte[] login_packet_tosend = concatBytes(getVarInt(login_packet.Length), login_packet);
//short len Send(login_packet_tosend);
byte[] sh = BitConverter.GetBytes((short)username.Length);
Array.Reverse(sh);
sh.CopyTo(data, 2);
//username
byte[] bname = Encoding.BigEndianUnicode.GetBytes(username);
bname.CopyTo(data, 4);
//short len
sh = BitConverter.GetBytes((short)host.Length);
Array.Reverse(sh);
sh.CopyTo(data, 4 + (username.Length * 2));
//host
byte[] bhost = Encoding.BigEndianUnicode.GetBytes(host);
bhost.CopyTo(data, 6 + (username.Length * 2));
//port
sh = BitConverter.GetBytes(port);
Array.Reverse(sh);
sh.CopyTo(data, 6 + (username.Length * 2) + (host.Length * 2));
Send(data);
byte[] pid = new byte[1]; byte[] pid = new byte[1];
Receive(pid, 0, 1, SocketFlags.None); Receive(pid, 0, 1, SocketFlags.None);

View file

@ -207,7 +207,7 @@ namespace MinecraftClient
//Get server version //Get server version
Console.WriteLine("Retrieving Server Info..."); Console.WriteLine("Retrieving Server Info...");
byte protocolversion = 0; string version = ""; int protocolversion = 0; string version = "";
if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version)) if (MinecraftCom.GetServerInfo(Settings.ServerIP, ref protocolversion, ref version))
{ {
//Supported protocol version ? //Supported protocol version ?