mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
First attempt at Realms list retrieval
> See #51 - Realms Support + Catch exception while retrieving player head
This commit is contained in:
parent
6dd003d04c
commit
5654871a57
3 changed files with 65 additions and 19 deletions
|
|
@ -210,7 +210,7 @@ namespace MinecraftClient
|
|||
/// </summary>
|
||||
/// <param name="text">Text to send to the server</param>
|
||||
/// <returns>True if the text was sent with no error</returns>
|
||||
new public bool SendText(object text)
|
||||
public bool SendText(object text)
|
||||
{
|
||||
bool result = base.SendText(text is string ? (string)text : text.ToString());
|
||||
tickHandler.WaitOne();
|
||||
|
|
|
|||
|
|
@ -35,11 +35,15 @@ namespace MinecraftClient
|
|||
try
|
||||
{
|
||||
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
|
||||
{
|
||||
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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <param name="host">Host to connect to</param>
|
||||
/// <param name="endpoint">Endpoint for making the request</param>
|
||||
/// <param name="cookies">Cookies for making the request</param>
|
||||
/// <param name="result">Request result</param>
|
||||
/// <returns>HTTP Status code</returns>
|
||||
|
||||
private static int doHTTPSGet(string host, string endpoint, string cookies, ref string result)
|
||||
{
|
||||
List<String> http_request = new List<string>();
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make a HTTPS POST request to the specified endpoint of the Mojang API
|
||||
/// </summary>
|
||||
/// <param name="host">Host to connect to</param>
|
||||
/// <param name="endpoint">Endpoint for making the request</param>
|
||||
|
|
@ -243,14 +274,6 @@ namespace MinecraftClient.Protocol
|
|||
|
||||
private static int doHTTPSPost(string host, string endpoint, string request, ref string result)
|
||||
{
|
||||
string postResult = null;
|
||||
int statusCode = 520;
|
||||
AutoTimeout.Perform(() =>
|
||||
{
|
||||
TcpClient client = ProxyHandler.newTcpClient(host, 443, true);
|
||||
SslStream stream = new SslStream(client.GetStream());
|
||||
stream.AuthenticateAsClient(host);
|
||||
|
||||
List<String> http_request = new List<string>();
|
||||
http_request.Add("POST " + endpoint + " HTTP/1.1");
|
||||
http_request.Add("Host: " + host);
|
||||
|
|
@ -260,11 +283,30 @@ namespace MinecraftClient.Protocol
|
|||
http_request.Add("Connection: close");
|
||||
http_request.Add("");
|
||||
http_request.Add(request);
|
||||
return doHTTPSRequest(http_request, host, ref result);
|
||||
}
|
||||
|
||||
stream.Write(Encoding.ASCII.GetBytes(String.Join("\r\n", http_request.ToArray())));
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="headers">Request headers and optional body (POST)</param>
|
||||
/// <param name="host">Host to connect to</param>
|
||||
/// <param name="result">Request result</param>
|
||||
/// <returns>HTTP Status code</returns>
|
||||
|
||||
private static int doHTTPSRequest(List<string> headers, string host, ref string result)
|
||||
{
|
||||
string postResult = null;
|
||||
int statusCode = 520;
|
||||
AutoTimeout.Perform(() =>
|
||||
{
|
||||
TcpClient client = ProxyHandler.newTcpClient(host, 443, true);
|
||||
SslStream stream = new SslStream(client.GetStream());
|
||||
stream.AuthenticateAsClient(host);
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue