Removed NioTSO client and server

- NioTSO client isn't needed because we're using RayLib
- Added FreeSO's API server to handle most backend operations
This commit is contained in:
Tony Bark 2024-05-01 02:55:43 -04:00
parent f12ba1502b
commit 22191ce648
591 changed files with 53264 additions and 3362 deletions

View file

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
namespace FSO.Server.Api.Core.Utils
{
/// <summary>
/// Used to mail our users.
/// Could be more useful in the future when out of Beta.
/// </summary>
public class ApiMail
{
string subject;
string template;
Dictionary<string, string> strings;
public ApiMail(string template)
{
this.template = template;
this.strings = new Dictionary<string, string>();
}
public void AddString(string key, string value)
{
strings.Add(key, value);
}
private string ComposeBody(Dictionary<string, string> strings)
{
string baseTemplate = File.ReadAllText("./MailTemplates/MailBase.html");
string content = File.ReadAllText("./MailTemplates/" + template + ".html");
foreach (KeyValuePair<string, string> entry in strings)
{
content = content.Replace("%" + entry.Key + "%", entry.Value);
}
strings = new Dictionary<string, string>();
return baseTemplate.Replace("%content%", content);
}
public bool Send(string to, string subject)
{
Api api = Api.INSTANCE;
if(api.Config.SmtpEnabled)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress(api.Config.SmtpUser, "FreeSO Staff");
message.To.Add(to);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = ComposeBody(strings);
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = api.Config.SmtpHost;
client.Port = api.Config.SmtpPort;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(api.Config.SmtpUser, api.Config.SmtpPassword);
// Send async
client.SendMailAsync(message);
return true;
} catch(Exception e) {
return false;
}
}
return false;
}
}
}

View file

@ -0,0 +1,78 @@
using FSO.Common.Utils;
using FSO.Server.Database.DA.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using System.Xml;
namespace FSO.Server.Api.Core.Utils
{
public class ApiResponse
{
public static IActionResult Plain(HttpStatusCode code, string text)
{
return new ContentResult
{
StatusCode = (int)code,
Content = text,
ContentType = "text/plain"
};
}
public static IActionResult Json(HttpStatusCode code, object obj)
{
return new ContentResult
{
StatusCode = (int)code,
Content = Newtonsoft.Json.JsonConvert.SerializeObject(obj),
ContentType = "application/json"
};
}
public static IActionResult PagedList<T>(HttpRequest request, HttpStatusCode code, PagedList<T> list)
{
request.HttpContext.Response.Headers.Add("X-Total-Count", list.Total.ToString());
request.HttpContext.Response.Headers.Add("X-Offset", list.Offset.ToString());
return new ContentResult
{
StatusCode = (int)code,
Content = Newtonsoft.Json.JsonConvert.SerializeObject(list),
ContentType = "application/json"
};
}
public static IActionResult Xml(HttpStatusCode code, IXMLEntity xml)
{
var doc = new XmlDocument();
var firstChild = xml.Serialize(doc);
doc.AppendChild(firstChild);
return new ContentResult
{
StatusCode = (int)code,
Content = doc.OuterXml,
ContentType = "text/xml"
};
}
public static Func<IActionResult> XmlFuture(HttpStatusCode code, IXMLEntity xml)
{
var doc = new XmlDocument();
var firstChild = xml.Serialize(doc);
doc.AppendChild(firstChild);
var serialized = doc.OuterXml;
return () =>
{
return new ContentResult
{
StatusCode = (int)code,
Content = serialized,
ContentType = "text/xml"
};
};
}
}
}

View file

@ -0,0 +1,35 @@
using Microsoft.AspNetCore.Http;
using System.Linq;
namespace FSO.Server.Api.Core.Utils
{
public class ApiUtils
{
private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage =
"System.ServiceModel.Channels.RemoteEndpointMessageProperty";
private const string OwinContext = "MS_OwinContext";
public static string GetIP(HttpRequest request)
{
var api = FSO.Server.Api.Core.Api.INSTANCE;
if (!api.Config.UseProxy)
{
return request.HttpContext.Connection.RemoteIpAddress.ToString();
}
else
{
var ip = "127.0.0.1";
var xff = request.Headers["X-Forwarded-For"];
if (xff.Count != 0)
{
ip = xff.First();
ip = ip.Substring(ip.IndexOf(",") + 1);
var last = ip.LastIndexOf(":");
if (last != -1 && last < ip.Length - 5) ip = ip.Substring(0, last);
}
return ip;
}
}
}
}