Fix all warnings & Trim (#2226)

* Fix AutoFishing crash
* Fix all warnings
* Remove DotNetZip.
* Fix the usage of HttpClient.
This commit is contained in:
BruceChen 2022-10-02 18:31:08 +08:00 committed by GitHub
parent 4aa6c1c99f
commit 1d52d1eadd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 2201 additions and 43564 deletions

View file

@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Net.Sockets;
using MinecraftClient.Proxy;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using MinecraftClient.Proxy;
namespace MinecraftClient.Protocol
{
@ -18,16 +17,16 @@ namespace MinecraftClient.Protocol
{
private readonly string httpVersion = "HTTP/1.0"; // Use 1.0 here because 1.1 server may send chunked data
private Uri uri;
private string host { get { return uri.Host; } }
private int port { get { return uri.Port; } }
private string path { get { return uri.PathAndQuery; } }
private bool isSecure { get { return uri.Scheme == "https"; } }
private readonly Uri uri;
private string Host { get { return uri.Host; } }
private int Port { get { return uri.Port; } }
private string Path { get { return uri.PathAndQuery; } }
private bool IsSecure { get { return uri.Scheme == "https"; } }
public NameValueCollection Headers = new NameValueCollection();
public NameValueCollection Headers = new();
public string UserAgent { get { return Headers.Get("User-Agent"); } set { Headers.Set("User-Agent", value); } }
public string Accept { get { return Headers.Get("Accept"); } set { Headers.Set("Accept", value); } }
public string UserAgent { get { return Headers.Get("User-Agent") ?? String.Empty; } set { Headers.Set("User-Agent", value); } }
public string Accept { get { return Headers.Get("Accept") ?? String.Empty; } set { Headers.Set("Accept", value); } }
public string Cookie { set { Headers.Set("Cookie", value); } }
/// <summary>
@ -57,7 +56,7 @@ namespace MinecraftClient.Protocol
/// </summary>
private void SetupBasicHeaders()
{
Headers.Add("Host", host);
Headers.Add("Host", Host);
Headers.Add("User-Agent", "MCC/" + Program.Version);
Headers.Add("Accept", "*/*");
Headers.Add("Connection", "close");
@ -94,9 +93,9 @@ namespace MinecraftClient.Protocol
/// <returns></returns>
private Response Send(string method, string body = "")
{
List<string> requestMessage = new List<string>()
List<string> requestMessage = new()
{
string.Format("{0} {1} {2}", method.ToUpper(), path, httpVersion) // Request line
string.Format("{0} {1} {2}", method.ToUpper(), Path, httpVersion) // Request line
};
foreach (string key in Headers) // Headers
{
@ -119,12 +118,12 @@ namespace MinecraftClient.Protocol
Response response = Response.Empty();
AutoTimeout.Perform(() =>
{
TcpClient client = ProxyHandler.newTcpClient(host, port, true);
TcpClient client = ProxyHandler.NewTcpClient(Host, Port, true);
Stream stream;
if (isSecure)
if (IsSecure)
{
stream = new SslStream(client.GetStream());
((SslStream)stream).AuthenticateAsClient(host, null, SslProtocols.Tls12, true); // Enable TLS 1.2. Hotfix for #1774
((SslStream)stream).AuthenticateAsClient(Host, null, SslProtocols.Tls12, true); // Enable TLS 1.2. Hotfix for #1774
}
else
{
@ -134,7 +133,7 @@ namespace MinecraftClient.Protocol
byte[] data = Encoding.ASCII.GetBytes(h);
stream.Write(data, 0, data.Length);
stream.Flush();
StreamReader sr = new StreamReader(stream);
StreamReader sr = new(stream);
string rawResult = sr.ReadToEnd();
response = ParseResponse(rawResult);
try
@ -142,8 +141,9 @@ namespace MinecraftClient.Protocol
sr.Close();
stream.Close();
client.Close();
} catch { }
},
}
catch { }
},
TimeSpan.FromSeconds(30));
return response;
}
@ -157,13 +157,13 @@ namespace MinecraftClient.Protocol
{
int statusCode;
string responseBody = "";
NameValueCollection headers = new NameValueCollection();
NameValueCollection cookies = new NameValueCollection();
NameValueCollection headers = new();
NameValueCollection cookies = new();
if (raw.StartsWith("HTTP/1.1") || raw.StartsWith("HTTP/1.0"))
{
Queue<string> msg = new Queue<string>(raw.Split(new string[] { "\r\n" }, StringSplitOptions.None));
Queue<string> msg = new(raw.Split(new string[] { "\r\n" }, StringSplitOptions.None));
statusCode = int.Parse(msg.Dequeue().Split(' ')[1]);
while (msg.Peek() != "")
{
string[] header = msg.Dequeue().Split(new char[] { ':' }, 2); // Split first ':' only
@ -183,26 +183,14 @@ namespace MinecraftClient.Protocol
}
}
msg.Dequeue();
if (msg.Count > 0)
if (msg.Count > 0)
responseBody = msg.Dequeue();
return new Response()
{
StatusCode = statusCode,
Body = responseBody,
Headers = headers,
Cookies = cookies
};
return new Response(statusCode, responseBody, headers, cookies);
}
else
{
return new Response()
{
StatusCode = 520, // 502 - Web Server Returned an Unknown Error
Body = "",
Headers = headers,
Cookies = cookies
};
return new Response(520 /* Web Server Returned an Unknown Error */, "", headers, cookies);
}
}
@ -233,19 +221,21 @@ namespace MinecraftClient.Protocol
public NameValueCollection Headers;
public NameValueCollection Cookies;
public Response(int statusCode, string body, NameValueCollection headers, NameValueCollection cookies)
{
StatusCode = statusCode;
Body = body;
Headers = headers;
Cookies = cookies;
}
/// <summary>
/// Get an empty response object
/// </summary>
/// <returns></returns>
public static Response Empty()
{
return new Response()
{
StatusCode = 204, // 204 - No content
Body = "",
Headers = new NameValueCollection(),
Cookies = new NameValueCollection()
};
return new Response(204 /* No content */, "", new NameValueCollection(), new NameValueCollection());
}
public override string ToString()
@ -274,7 +264,7 @@ namespace MinecraftClient.Protocol
sb.AppendLine("Body: (Truncated to 200 characters)");
}
else sb.AppendLine("Body: ");
sb.AppendLine(Body.Length > 200 ? Body.Substring(0, 200) + "..." : Body);
sb.AppendLine(Body.Length > 200 ? Body[..200] + "..." : Body);
}
return sb.ToString();
}