Clean up
This commit is contained in:
259
Server/Util.cs
259
Server/Util.cs
@ -1,5 +1,4 @@
|
||||
global using System.Collections.Generic;
|
||||
using Lidgren.Network;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -7,148 +6,146 @@ using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
namespace RageCoop.Server
|
||||
{
|
||||
internal static partial class Util
|
||||
{
|
||||
using Lidgren.Network;
|
||||
|
||||
public static string DownloadString(string url)
|
||||
namespace RageCoop.Server;
|
||||
|
||||
internal static class Util
|
||||
{
|
||||
public static string DownloadString(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
// TLS only
|
||||
ServicePointManager.Expect100Continue = true;
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
|
||||
HttpClient client = new();
|
||||
HttpRequestMessage request = new(HttpMethod.Get, url);
|
||||
var response = client.Send(request);
|
||||
using var reader = new StreamReader(response.Content.ReadAsStream());
|
||||
var responseBody = reader.ReadToEnd();
|
||||
|
||||
return responseBody;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static List<NetConnection> Exclude(this IEnumerable<NetConnection> connections, NetConnection toExclude)
|
||||
{
|
||||
return new List<NetConnection>(connections.Where(e => e != toExclude));
|
||||
}
|
||||
|
||||
public static T Read<T>(string file) where T : new()
|
||||
{
|
||||
XmlSerializer ser = new(typeof(T));
|
||||
|
||||
XmlWriterSettings settings = new()
|
||||
{
|
||||
Indent = true,
|
||||
IndentChars = "\t",
|
||||
OmitXmlDeclaration = true
|
||||
};
|
||||
|
||||
var path = AppContext.BaseDirectory + file;
|
||||
T data;
|
||||
|
||||
if (File.Exists(path))
|
||||
try
|
||||
{
|
||||
// TLS only
|
||||
ServicePointManager.Expect100Continue = true;
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
using (var stream = XmlReader.Create(path))
|
||||
{
|
||||
data = (T)ser.Deserialize(stream);
|
||||
}
|
||||
|
||||
HttpClient client = new();
|
||||
HttpRequestMessage request = new(HttpMethod.Get, url);
|
||||
HttpResponseMessage response = client.Send(request);
|
||||
using var reader = new StreamReader(response.Content.ReadAsStream());
|
||||
string responseBody = reader.ReadToEnd();
|
||||
|
||||
return responseBody;
|
||||
using (var stream = XmlWriter.Create(path, settings))
|
||||
{
|
||||
ser.Serialize(stream, data);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
public static List<NetConnection> Exclude(this IEnumerable<NetConnection> connections, NetConnection toExclude)
|
||||
{
|
||||
return new(connections.Where(e => e != toExclude));
|
||||
}
|
||||
|
||||
public static T Read<T>(string file) where T : new()
|
||||
{
|
||||
XmlSerializer ser = new(typeof(T));
|
||||
|
||||
XmlWriterSettings settings = new()
|
||||
{
|
||||
Indent = true,
|
||||
IndentChars = ("\t"),
|
||||
OmitXmlDeclaration = true
|
||||
};
|
||||
|
||||
string path = AppContext.BaseDirectory + file;
|
||||
T data;
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
try
|
||||
{
|
||||
using (XmlReader stream = XmlReader.Create(path))
|
||||
{
|
||||
data = (T)ser.Deserialize(stream);
|
||||
}
|
||||
|
||||
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
||||
{
|
||||
ser.Serialize(stream, data);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
||||
{
|
||||
ser.Serialize(stream, data = new T());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
using (XmlWriter stream = XmlWriter.Create(path, settings))
|
||||
using (var stream = XmlWriter.Create(path, settings))
|
||||
{
|
||||
ser.Serialize(stream, data = new T());
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public static T Next<T>(this T[] values)
|
||||
{
|
||||
return values[new Random().Next(values.Length - 1)];
|
||||
}
|
||||
|
||||
public static string GetFinalRedirect(string url)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
return url;
|
||||
|
||||
int maxRedirCount = 8; // prevent infinite loops
|
||||
string newUrl = url;
|
||||
do
|
||||
else
|
||||
using (var stream = XmlWriter.Create(path, settings))
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpClientHandler handler = new()
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
};
|
||||
HttpClient client = new(handler);
|
||||
HttpRequestMessage request = new(HttpMethod.Head, url);
|
||||
HttpResponseMessage response = client.Send(request);
|
||||
ser.Serialize(stream, data = new T());
|
||||
}
|
||||
|
||||
switch (response.StatusCode)
|
||||
{
|
||||
case HttpStatusCode.OK:
|
||||
return newUrl;
|
||||
case HttpStatusCode.Redirect:
|
||||
case HttpStatusCode.MovedPermanently:
|
||||
case HttpStatusCode.RedirectKeepVerb:
|
||||
case HttpStatusCode.RedirectMethod:
|
||||
newUrl = response.Headers.Location.ToString();
|
||||
if (newUrl == null)
|
||||
return url;
|
||||
|
||||
string newUrlString = newUrl;
|
||||
|
||||
if (!newUrlString.Contains("://"))
|
||||
{
|
||||
// Doesn't have a URL Schema, meaning it's a relative or absolute URL
|
||||
Uri u = new Uri(new Uri(url), newUrl);
|
||||
newUrl = u.ToString();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
url = newUrl;
|
||||
}
|
||||
catch (WebException)
|
||||
{
|
||||
// Return the last known good URL
|
||||
return newUrl;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
} while (maxRedirCount-- > 0);
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public static T Next<T>(this T[] values)
|
||||
{
|
||||
return values[new Random().Next(values.Length - 1)];
|
||||
}
|
||||
|
||||
public static string GetFinalRedirect(string url)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
return url;
|
||||
|
||||
var maxRedirCount = 8; // prevent infinite loops
|
||||
var newUrl = url;
|
||||
do
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpClientHandler handler = new()
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
};
|
||||
HttpClient client = new(handler);
|
||||
HttpRequestMessage request = new(HttpMethod.Head, url);
|
||||
var response = client.Send(request);
|
||||
|
||||
switch (response.StatusCode)
|
||||
{
|
||||
case HttpStatusCode.OK:
|
||||
return newUrl;
|
||||
case HttpStatusCode.Redirect:
|
||||
case HttpStatusCode.MovedPermanently:
|
||||
case HttpStatusCode.RedirectKeepVerb:
|
||||
case HttpStatusCode.RedirectMethod:
|
||||
newUrl = response.Headers.Location.ToString();
|
||||
if (newUrl == null)
|
||||
return url;
|
||||
|
||||
var newUrlString = newUrl;
|
||||
|
||||
if (!newUrlString.Contains("://"))
|
||||
{
|
||||
// Doesn't have a URL Schema, meaning it's a relative or absolute URL
|
||||
var u = new Uri(new Uri(url), newUrl);
|
||||
newUrl = u.ToString();
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
return newUrl;
|
||||
}
|
||||
|
||||
url = newUrl;
|
||||
}
|
||||
catch (WebException)
|
||||
{
|
||||
// Return the last known good URL
|
||||
return newUrl;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
} while (maxRedirCount-- > 0);
|
||||
|
||||
return newUrl;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user