Add 15 seconds timeout to session and login

Add AutoTimeout class for use on login and session requests.
Bug report by GamerCorey7.
This commit is contained in:
ORelio 2015-01-27 20:38:59 +01:00
parent ee406b233e
commit 391eca102c
3 changed files with 72 additions and 23 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Threading;
namespace MinecraftClient
{
/// <summary>
/// Allow easy timeout on pieces of code
/// </summary>
/// <remarks>
/// By ORelio - (c) 2014 - CDDL 1.0
/// </remarks>
public class AutoTimeout
{
/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="attempts">Maximum timeout in milliseconds</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, int timeout)
{
return Perform(action, TimeSpan.FromMilliseconds(timeout));
}
/// <summary>
/// Perform the specified action with specified timeout
/// </summary>
/// <param name="action">Action to run</param>
/// <param name="attempts">Maximum timeout</param>
/// <returns>True if the action finished whithout timing out</returns>
public static bool Perform(Action action, TimeSpan timeout)
{
Thread thread = new Thread(new ThreadStart(action));
thread.Start();
bool success = thread.Join(timeout);
if (!success)
thread.Abort();
return success;
}
}
}

View file

@ -71,6 +71,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AutoTimeout.cs" />
<Compile Include="ChatBots\Alerts.cs" /> <Compile Include="ChatBots\Alerts.cs" />
<Compile Include="ChatBots\AntiAFK.cs" /> <Compile Include="ChatBots\AntiAFK.cs" />
<Compile Include="ChatBots\AutoRelog.cs" /> <Compile Include="ChatBots\AutoRelog.cs" />

View file

@ -230,30 +230,37 @@ namespace MinecraftClient.Protocol
private static int doHTTPSPost(string host, string endpoint, string request, ref string result) private static int doHTTPSPost(string host, string endpoint, string request, ref string result)
{ {
TcpClient client = ProxyHandler.newTcpClient(host, 443); string postResult = null;
SslStream stream = new SslStream(client.GetStream()); int statusCode = 520;
stream.AuthenticateAsClient(host); AutoTimeout.Perform(() =>
List<String> http_request = new List<string>();
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())));
System.IO.StreamReader sr = new System.IO.StreamReader(stream);
string raw_result = sr.ReadToEnd();
if (raw_result.StartsWith("HTTP/1.1"))
{ {
result = raw_result.Substring(raw_result.IndexOf("\r\n\r\n") + 4); TcpClient client = ProxyHandler.newTcpClient(host, 443);
return Settings.str2int(raw_result.Split(' ')[1]); SslStream stream = new SslStream(client.GetStream());
} stream.AuthenticateAsClient(host);
else return 520; //Web server is returning an unknown error
List<String> http_request = new List<string>();
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())));
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);
statusCode = Settings.str2int(raw_result.Split(' ')[1]);
}
else statusCode = 520; //Web server is returning an unknown error
}, 15000);
result = postResult;
return statusCode;
} }
/// <summary> /// <summary>