Files
RAGECOOP-V/RageCoop.Server/Util.cs

155 lines
4.9 KiB
C#
Raw Normal View History

global using System.Collections.Generic;
using Lidgren.Network;
2022-09-08 12:41:56 -07:00
using System;
using System.IO;
2021-08-18 11:47:59 +02:00
using System.Linq;
2022-06-03 16:28:02 +08:00
using System.Net;
2022-08-12 20:48:31 +02:00
using System.Net.Http;
2022-09-08 12:41:56 -07:00
using System.Xml;
using System.Xml.Serialization;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Server
2021-07-07 13:36:25 +02:00
{
2022-09-08 12:41:56 -07:00
internal static partial class Util
2021-07-07 13:36:25 +02:00
{
2022-06-03 16:28:02 +08:00
public static string DownloadString(string url)
{
try
{
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
2022-08-12 20:48:31 +02:00
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;
2022-06-03 16:28:02 +08:00
}
catch
{
return "";
}
}
2022-09-08 12:41:56 -07:00
public static List<NetConnection> Exclude(this IEnumerable<NetConnection> connections, NetConnection toExclude)
2021-08-18 11:47:59 +02:00
{
return new(connections.Where(e => e != toExclude));
2021-08-18 11:47:59 +02:00
}
2022-09-08 12:41:56 -07:00
2021-07-07 13:36:25 +02:00
public static T Read<T>(string file) where T : new()
{
XmlSerializer ser = new(typeof(T));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = ("\t"),
OmitXmlDeclaration = true
};
2021-07-12 01:47:01 +02:00
string path = AppContext.BaseDirectory + file;
2021-08-14 21:49:23 +02:00
T data;
2021-07-07 13:36:25 +02:00
if (File.Exists(path))
{
2022-06-27 14:42:57 +08:00
try
2021-07-07 13:36:25 +02:00
{
2022-06-27 14:42:57 +08:00
using (XmlReader stream = XmlReader.Create(path))
{
data = (T)ser.Deserialize(stream);
}
2021-07-07 13:36:25 +02:00
2022-06-27 14:42:57 +08:00
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data);
}
}
catch
2021-07-07 13:36:25 +02:00
{
2022-06-27 14:42:57 +08:00
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data = new T());
}
2021-07-07 13:36:25 +02:00
}
}
else
{
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data = new T());
}
2021-07-07 13:36:25 +02:00
}
2021-08-14 21:49:23 +02:00
return data;
2021-07-07 13:36:25 +02:00
}
2022-07-10 16:13:08 +08:00
public static T Next<T>(this T[] values)
{
2022-09-08 12:41:56 -07:00
return values[new Random().Next(values.Length - 1)];
2022-07-10 16:13:08 +08:00
}
2022-07-11 13:26:28 +08:00
public static string GetFinalRedirect(string url)
{
if (string.IsNullOrWhiteSpace(url))
return url;
int maxRedirCount = 8; // prevent infinite loops
string newUrl = url;
do
{
try
{
2022-08-12 20:48:31 +02:00
HttpClientHandler handler = new()
{
AllowAutoRedirect = false
};
HttpClient client = new(handler);
HttpRequestMessage request = new(HttpMethod.Head, url);
HttpResponseMessage response = client.Send(request);
switch (response.StatusCode)
2022-07-11 13:26:28 +08:00
{
case HttpStatusCode.OK:
return newUrl;
case HttpStatusCode.Redirect:
case HttpStatusCode.MovedPermanently:
case HttpStatusCode.RedirectKeepVerb:
case HttpStatusCode.RedirectMethod:
2022-08-12 20:48:31 +02:00
newUrl = response.Headers.Location.ToString();
2022-07-11 13:26:28 +08:00
if (newUrl == null)
return url;
2022-08-12 20:48:31 +02:00
string newUrlString = newUrl;
if (!newUrlString.Contains("://"))
2022-07-11 13:26:28 +08:00
{
// 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;
}
2022-08-12 20:48:31 +02:00
2022-07-11 13:26:28 +08:00
url = newUrl;
}
catch (WebException)
{
// Return the last known good URL
return newUrl;
}
catch
{
return null;
}
} while (maxRedirCount-- > 0);
return newUrl;
}
2021-07-07 13:36:25 +02:00
}
}