From 5654871a57a884e326a8ef13e711df2e8fb9107b Mon Sep 17 00:00:00 2001 From: ORelio Date: Fri, 27 Nov 2015 16:52:41 +0100 Subject: [PATCH] First attempt at Realms list retrieval > See #51 - Realms Support + Catch exception while retrieving player head --- MinecraftClient/CSharpRunner.cs | 2 +- MinecraftClient/ConsoleIcon.cs | 10 ++- MinecraftClient/Protocol/ProtocolHandler.cs | 72 ++++++++++++++++----- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/MinecraftClient/CSharpRunner.cs b/MinecraftClient/CSharpRunner.cs index 46add112..b8ec098a 100644 --- a/MinecraftClient/CSharpRunner.cs +++ b/MinecraftClient/CSharpRunner.cs @@ -210,7 +210,7 @@ namespace MinecraftClient /// /// Text to send to the server /// True if the text was sent with no error - new public bool SendText(object text) + public bool SendText(object text) { bool result = base.SendText(text is string ? (string)text : text.ToString()); tickHandler.WaitOne(); diff --git a/MinecraftClient/ConsoleIcon.cs b/MinecraftClient/ConsoleIcon.cs index 2ef20783..30dc6460 100644 --- a/MinecraftClient/ConsoleIcon.cs +++ b/MinecraftClient/ConsoleIcon.cs @@ -36,9 +36,13 @@ namespace MinecraftClient { using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse()) { - Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //Read skin from network - skin = skin.Clone(new Rectangle(8, 8, 8, 8), skin.PixelFormat); //Crop skin - SetConsoleIcon(skin.GetHicon()); //Set skin as icon + try + { + Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //Read skin from network + skin = skin.Clone(new Rectangle(8, 8, 8, 8), skin.PixelFormat); //Crop skin + SetConsoleIcon(skin.GetHicon()); //Set skin as icon + } + catch (ArgumentException) { /* Invalid image in HTTP response */ } } } catch (WebException) //Skin not found? Reset to default icon diff --git a/MinecraftClient/Protocol/ProtocolHandler.cs b/MinecraftClient/Protocol/ProtocolHandler.cs index 067ab4bf..266cbdda 100644 --- a/MinecraftClient/Protocol/ProtocolHandler.cs +++ b/MinecraftClient/Protocol/ProtocolHandler.cs @@ -231,9 +231,40 @@ namespace MinecraftClient.Protocol catch { return false; } } + public static void RealmsListWorlds(string username, string uuid, string accesstoken) + { + string result = ""; + string cookies = String.Format("sid=token:{0}:{1};user={2};version={3}", accesstoken, uuid, username, Program.MCHighestVersion); + doHTTPSGet("mcoapi.minecraft.net", "/worlds", cookies, ref result); + Console.WriteLine(result); + } + /// - /// Manual HTTPS request since we must directly use a TcpClient because of the proxy. - /// This method connects to the server, enables SSL, do the request and read the response. + /// Make a HTTPS GET request to the specified endpoint of the Mojang API + /// + /// Host to connect to + /// Endpoint for making the request + /// Cookies for making the request + /// Request result + /// HTTP Status code + + private static int doHTTPSGet(string host, string endpoint, string cookies, ref string result) + { + List http_request = new List(); + http_request.Add("GET " + endpoint + " HTTP/1.1"); + http_request.Add("Cookie: " + cookies); + http_request.Add("Cache-Control: no-cache"); + http_request.Add("Pragma: no-cache"); + http_request.Add("Host: " + host); + http_request.Add("User-Agent: Java/1.6.0_27"); + http_request.Add("Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7"); + http_request.Add("Connection: close"); + http_request.Add(""); + return doHTTPSRequest(http_request, host, ref result); + } + + /// + /// Make a HTTPS POST request to the specified endpoint of the Mojang API /// /// Host to connect to /// Endpoint for making the request @@ -242,6 +273,29 @@ namespace MinecraftClient.Protocol /// HTTP Status code private static int doHTTPSPost(string host, string endpoint, string request, ref string result) + { + List http_request = new List(); + http_request.Add("POST " + endpoint + " HTTP/1.1"); + http_request.Add("Host: " + host); + http_request.Add("User-Agent: MCC/" + Program.Version); + http_request.Add("Content-Type: application/json"); + http_request.Add("Content-Length: " + Encoding.ASCII.GetBytes(request).Length); + http_request.Add("Connection: close"); + http_request.Add(""); + http_request.Add(request); + return doHTTPSRequest(http_request, host, ref result); + } + + /// + /// Manual HTTPS request since we must directly use a TcpClient because of the proxy. + /// This method connects to the server, enables SSL, do the request and read the response. + /// + /// Request headers and optional body (POST) + /// Host to connect to + /// Request result + /// HTTP Status code + + private static int doHTTPSRequest(List headers, string host, ref string result) { string postResult = null; int statusCode = 520; @@ -250,21 +304,9 @@ namespace MinecraftClient.Protocol TcpClient client = ProxyHandler.newTcpClient(host, 443, true); SslStream stream = new SslStream(client.GetStream()); stream.AuthenticateAsClient(host); - - List http_request = new List(); - http_request.Add("POST " + endpoint + " HTTP/1.1"); - http_request.Add("Host: " + host); - http_request.Add("User-Agent: MCC/" + Program.Version); - http_request.Add("Content-Type: application/json"); - http_request.Add("Content-Length: " + Encoding.ASCII.GetBytes(request).Length); - http_request.Add("Connection: close"); - http_request.Add(""); - http_request.Add(request); - - stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", http_request.ToArray()))); + stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", headers.ToArray()))); System.IO.StreamReader sr = new System.IO.StreamReader(stream); string raw_result = sr.ReadToEnd(); - if (raw_result.StartsWith("HTTP/1.1")) { postResult = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4);