diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/Compression.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/Compression.cs new file mode 100644 index 0000000..ce43d6b --- /dev/null +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/Compression.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Text; + +namespace LightReflectiveMirror.Compression +{ + internal static class StringCompressor + { + /// + /// Compresses the string. + /// + /// The text. + /// + public static string Compress(this string text) + { + byte[] buffer = Encoding.UTF8.GetBytes(text); + var memoryStream = new MemoryStream(); + using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) + { + gZipStream.Write(buffer, 0, buffer.Length); + } + + memoryStream.Position = 0; + + var compressedData = new byte[memoryStream.Length]; + memoryStream.Read(compressedData, 0, compressedData.Length); + + var gZipBuffer = new byte[compressedData.Length + 4]; + Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length); + Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4); + return Convert.ToBase64String(gZipBuffer); + } + + /// + /// Decompresses the string. + /// + /// The compressed text. + /// + public static string Decompress(this string compressedText) + { + byte[] gZipBuffer = Convert.FromBase64String(compressedText); + using (var memoryStream = new MemoryStream()) + { + int dataLength = BitConverter.ToInt32(gZipBuffer, 0); + memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4); + + var buffer = new byte[dataLength]; + + memoryStream.Position = 0; + using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress)) + { + gZipStream.Read(buffer, 0, buffer.Length); + } + + return Encoding.UTF8.GetString(buffer); + } + } + } +} diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs index 1518683..e434d53 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/Endpoint.cs @@ -1,90 +1,101 @@ -using Grapevine; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using System; -using System.Threading.Tasks; - -namespace LightReflectiveMirror.Endpoints -{ - [Serializable] - struct RelayStats - { - public int ConnectedClients; - public int RoomCount; - public int PublicRoomCount; - public TimeSpan Uptime; - } - - [RestResource] - public class Endpoint - { - [RestRoute("Get", "/api/stats")] - public async Task Stats(IHttpContext context) - { - RelayStats stats = new RelayStats - { - ConnectedClients = Program.instance.GetConnections(), - RoomCount = Program.instance.GetRooms().Count, - PublicRoomCount = Program.instance.GetPublicRoomCount(), - Uptime = Program.instance.GetUptime() - }; - - string json = JsonConvert.SerializeObject(stats, Formatting.Indented); - await context.Response.SendResponseAsync(json); - } - - [RestRoute("Get", "/api/servers")] - public async Task ServerList(IHttpContext context) - { - if (Program.conf.EndpointServerList) - { - string json = JsonConvert.SerializeObject(Program.instance.GetRooms(), Formatting.Indented); - await context.Response.SendResponseAsync(json); - } - else - { - await context.Response.SendResponseAsync("Access Denied"); - } - } - } - - public class EndpointServer - { - public bool Start(ushort port = 8080) - { - try - { - var config = new ConfigurationBuilder() - .SetBasePath(System.IO.Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); - - Action configServices = (services) => - { - services.AddLogging(configure => configure.AddConsole()); - services.Configure(options => options.MinLevel = LogLevel.None); - }; - - Action configServer = (server) => - { - server.Prefixes.Add($"http://*:{port}/"); - }; - - var server = new RestServerBuilder(new ServiceCollection(), config, configServices, configServer).Build(); - server.Router.Options.SendExceptionMessages = false; - - server.Start(); - - return true; - } - catch - { - return false; - } - - } - } -} - +using Grapevine; +using LightReflectiveMirror.Compression; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace LightReflectiveMirror.Endpoints +{ + [Serializable] + struct RelayStats + { + public int ConnectedClients; + public int RoomCount; + public int PublicRoomCount; + public TimeSpan Uptime; + } + + [RestResource] + public class Endpoint + { + private List _rooms { get => Program.instance.GetRooms(); } + + private RelayStats _stats { get => new RelayStats + { + ConnectedClients = Program.instance.GetConnections(), + RoomCount = Program.instance.GetRooms().Count, + PublicRoomCount = Program.instance.GetPublicRoomCount(), + Uptime = Program.instance.GetUptime() + }; } + + [RestRoute("Get", "/api/stats")] + public async Task Stats(IHttpContext context) + { + string json = JsonConvert.SerializeObject(_stats, Formatting.Indented); + await context.Response.SendResponseAsync(json); + } + + [RestRoute("Get", "/api/servers")] + public async Task ServerList(IHttpContext context) + { + if (Program.conf.EndpointServerList) + { + string json = JsonConvert.SerializeObject(_rooms, Formatting.Indented); + await context.Response.SendResponseAsync(json); + } + else + await context.Response.SendResponseAsync(HttpStatusCode.Forbidden); + } + + [RestRoute("Get", "/api/compressed/servers")] + public async Task ServerListCompressed(IHttpContext context) + { + if (Program.conf.EndpointServerList) + { + string json = JsonConvert.SerializeObject(_rooms); + await context.Response.SendResponseAsync(json.Compress()); + } + else + await context.Response.SendResponseAsync(HttpStatusCode.Forbidden); + } + } + + public class EndpointServer + { + public bool Start(ushort port = 8080) + { + try + { + var config = new ConfigurationBuilder() + .SetBasePath(System.IO.Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .Build(); + + var server = new RestServerBuilder(new ServiceCollection(), config, + (services) => + { + services.AddLogging(configure => configure.AddConsole()); + services.Configure(options => options.MinLevel = LogLevel.None); + }, (server) => + { + server.Prefixes.Add($"http://*:{port}/"); + }).Build(); + + server.Router.Options.SendExceptionMessages = false; + server.Start(); + + return true; + } + catch + { + return false; + } + + } + } +} + diff --git a/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs b/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs index 1cae8e0..66c26e4 100644 --- a/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs +++ b/ServerProject-DONT-IMPORT-INTO-UNITY/Program.cs @@ -163,15 +163,16 @@ namespace LightReflectiveMirror try { natThread.Start(); - }catch(Exception err) + } + catch(Exception e) { - WriteLogMessage("FAILED\n" + err, ConsoleColor.DarkRed); + WriteLogMessage("FAILED\n" + e, ConsoleColor.DarkRed); } } - catch(Exception error) + catch(Exception e) { WriteLogMessage("FAILED\nCheck if port is in use.", ConsoleColor.DarkRed, true); - Console.WriteLine(error); + Console.WriteLine(e); } } } @@ -285,7 +286,7 @@ namespace LightReflectiveMirror string load = $"Chimp Event Listener Initializing... OK" + "\nHarambe Memorial Initializing... OK" + - "\nBananas initializing... OK\n"; + "\nBananas Initializing... OK\n"; WriteLogMessage(t, ConsoleColor.Green); WriteLogMessage(load, ConsoleColor.Cyan);