From f305cecd9909ff331f3db100bafabe6a32905b71 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 24 Jul 2026 15:42:19 +0000 Subject: [PATCH] Upload files to "/" --- README.md | 6 ++ Random.cs | 35 +++++++ ServerAPI.cs | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++ Signatures.cs | 61 ++++++++++++ Stella.csproj | 19 ++++ 5 files changed, 386 insertions(+) create mode 100644 README.md create mode 100644 Random.cs create mode 100644 ServerAPI.cs create mode 100644 Signatures.cs create mode 100644 Stella.csproj diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad6ecab --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +> [!IMPORTANT] +> this is what happens when you ban the developer that coded 90% of the server 🤦 +> this is from november 2025, so probally all the things that were in here vulnerabilities wise are patched, go ahead and try though +> oh yeah, removed all of the stuff that was rude to leak so yeah (private key, jwt token secret, mongodb connection url, ect.) +> fuck you supergamer ✌️ +> discord.gg/vanillarec diff --git a/Random.cs b/Random.cs new file mode 100644 index 0000000..6b4c37b --- /dev/null +++ b/Random.cs @@ -0,0 +1,35 @@ +using System.Text; + +namespace Stella +{ + public static class Random + { + private static readonly System.Random _random = new(); + + public static int RandomInt(int range0, int range1) + { + int result = _random.Next(range0, range1); + return result; + } + + public static long RandomLong(long range0, long range1) + { + long result = _random.NextInt64(range0, range1); + return result; + } + + public static string RandomString(int length) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}|;:',.<>?/~`"; + StringBuilder sb = new(); + + for (int i = 0; i < length; i++) + { + int index = _random.Next(chars.Length); + sb.Append(chars[index]); + } + + return sb.ToString(); + } + } +} diff --git a/ServerAPI.cs b/ServerAPI.cs new file mode 100644 index 0000000..e935e63 --- /dev/null +++ b/ServerAPI.cs @@ -0,0 +1,265 @@ +using Microsoft.AspNetCore.Http.Extensions; +using MongoDB.Driver; +using System.Linq.Expressions; +using System.Reflection; +using System.Text.Json; + +namespace Stella +{ + public class ServerAPI(WebApplication webApplication) + { + private readonly WebApplication webApplication = webApplication; + private bool hasInit = false; + + public RouteHandlerBuilder GET(string endpoint, Delegate action) => webApplication.MapGet(endpoint, action); + + public RouteHandlerBuilder POST(string endpoint, Delegate action) => webApplication.MapPost(endpoint, action); + + public RouteHandlerBuilder PUT(string endpoint, Delegate action) => webApplication.MapPut(endpoint, action); + + public RouteHandlerBuilder MAP(string endpoint, Delegate action) => webApplication.Map(endpoint, action); + + private static readonly object ConsoleLock = new(); + + public void Init() + { + webApplication.Use(async (context, next) => + { + var request = context.Request; + + context.Response.OnStarting(() => + { + lock (ConsoleLock) + { + var color = context.Response.StatusCode switch + { + 200 => ConsoleColor.Green, + 405 or 404 or 403 => ConsoleColor.Red, + 101 => ConsoleColor.Blue, + _ => ConsoleColor.White + }; + + Console.ForegroundColor = color; + Console.WriteLine($"{(context.Response.StatusCode == 101 ? "WS" : request.Method)} {request.GetEncodedPathAndQuery()} {context.Response.StatusCode}"); + if (request.Method == "POST") + { + + } + Console.ResetColor(); + } + + return Task.CompletedTask; + }); + + try + { + await next(); + } + catch (Exception ex) + { + lock (ConsoleLock) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"{request.Method} {request.GetEncodedPathAndQuery()} => {ex.Message}"); + Console.ResetColor(); + } + throw; + } + }); + + + var assembly = Assembly.GetExecutingAssembly(); + var jsonOptions = new JsonSerializerOptions + { + PropertyNamingPolicy = null, + DictionaryKeyPolicy = null, + WriteIndented = true + }; + + foreach (var type in assembly.GetTypes()) + { + if (!type.IsClass) continue; + + object? instance = null; + var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); + + foreach (var method in methods) + { + if (method.GetCustomAttribute() is { } getAttr) + { + instance ??= method.IsStatic ? null : Activator.CreateInstance(type); + + var del = CreateCompatibleDelegate(method, instance, jsonOptions); + + Console.WriteLine($"{type.FullName}.{method.Name} => GET {getAttr.Path}"); + GET(getAttr.Path, del); + } + + if (method.GetCustomAttribute() is { } postAttr) + { + instance ??= method.IsStatic ? null : Activator.CreateInstance(type); + + var del = CreateCompatibleDelegate(method, instance, jsonOptions); + + Console.WriteLine($"{type.FullName}.{method.Name} => POST {postAttr.Path}"); + POST(postAttr.Path, del); + } + + if (method.GetCustomAttribute() is { } putAttr) + { + instance ??= method.IsStatic ? null : Activator.CreateInstance(type); + + var del = CreateCompatibleDelegate(method, instance, jsonOptions); + + Console.WriteLine($"{type.FullName}.{method.Name} => POST {putAttr.Path}"); + PUT(putAttr.Path, del); + } + + if (method.GetCustomAttribute() is { } mapAttr) + { + instance ??= method.IsStatic ? null : Activator.CreateInstance(type); + + var del = CreateCompatibleDelegate(method, instance, jsonOptions); + + Console.WriteLine($"{type.FullName}.{method.Name} => POST {mapAttr.Path}"); + MAP(mapAttr.Path, del); + } + } + } + + //webApplication.UseHttpsRedirection(); + + webApplication.UseWebSockets(); // we use a custom SignalR because recrooms is so old its out of date... + + hasInit = true; + } + + public void Run(string? url = null) + { + if (!hasInit) + throw new InvalidOperationException("Call ServerAPI.Init() before Run()."); + + webApplication.Run(url); + } + + private static Delegate CreateCompatibleDelegate(MethodInfo method, object? instance, JsonSerializerOptions? options = null) + { + options ??= new JsonSerializerOptions + { + PropertyNamingPolicy = null, + DictionaryKeyPolicy = null, + WriteIndented = true + }; + + var parameters = method.GetParameters(); + bool requiresAuth = method.GetCustomAttribute() != null; + + bool isTask = typeof(Task).IsAssignableFrom(method.ReturnType); + Type? taskResultType = null; + + if (isTask && method.ReturnType.IsGenericType) + { + taskResultType = method.ReturnType.GetGenericArguments()[0]; + } + + return new Func>(async ctx => + { + IDictionary? info = null; + MongoDB.User? user = null; + + if (requiresAuth) + { + var authHeader = ctx.Request.Headers.Authorization; + + info = JsonWebToken.VerifyAndDecode(authHeader); + if (info == null) + return Results.Unauthorized(); + + long sub = long.Parse((string)info["sub"]); + + user = MongoDB.usersCollection + .Find(u => u.AccountId == sub) + .FirstOrDefault(); + + if (user == null) + return Results.Unauthorized(); + } + + object?[] args = [.. parameters.Select(p => + { + if (p.ParameterType == typeof(HttpContext)) + return ctx; + + if (requiresAuth && p.ParameterType == typeof(MongoDB.User)) + return user; + + if (ctx.Request.RouteValues.TryGetValue(p.Name, out var value)) + { + if (value == null) return null; + + if (p.ParameterType.IsEnum) + return Enum.Parse(p.ParameterType, value.ToString()!); + + return Convert.ChangeType(value, p.ParameterType); + } + + return null; + })]; + + object? result = method.Invoke(instance, args); + + if (isTask) + { + if (result is Task task) + { + await task.ConfigureAwait(false); + + if (taskResultType != null) + { + var resultProperty = task.GetType().GetProperty("Result"); + result = resultProperty?.GetValue(task); + } + else + { + result = null; + } + } + } + + if (result is IResult iResult) + return iResult; + + return Results.Json(result, options); + }); + } + + [AttributeUsage(AttributeTargets.Method)] + public class GETAttribute(string path) : Attribute + { + public string Path { get; } = path; + } + + [AttributeUsage(AttributeTargets.Method)] + public class MAPAttribute(string path) : Attribute + { + public string Path { get; } = path; + } + + [AttributeUsage(AttributeTargets.Method)] + public class POSTAttribute(string path) : Attribute + { + public string Path { get; } = path; + } + + [AttributeUsage(AttributeTargets.Method)] + public class PUTAttribute(string path) : Attribute + { + public string Path { get; } = path; + } + + [AttributeUsage(AttributeTargets.Method)] + public class UseAuthorizationAttribute() : Attribute + { + } + } +} \ No newline at end of file diff --git a/Signatures.cs b/Signatures.cs new file mode 100644 index 0000000..9a42f56 --- /dev/null +++ b/Signatures.cs @@ -0,0 +1,61 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Stella +{ + public class Signatures + { + private static RSA? _rsa; + + public static void Init() + { + try + { + string pem = File.ReadAllText(Path.Combine("Data", "PrivateKey.pem")); + + _rsa = RSA.Create(); + _rsa.ImportFromPem(pem.ToCharArray()); + } + catch (Exception ex) + { + throw new Exception("Error setting up Signatures: " + ex.Message); + } + } + + public static string Sign(byte[] data) + { + if (_rsa == null) + throw new InvalidOperationException("Signatures not initialized. Call Init() first."); + + byte[] signatureBytes = _rsa.SignData(data, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); + + return Convert.ToBase64String(signatureBytes); + } + + public static string Sign(dynamic data) + { + if (_rsa == null) + return "never called init"; + + string json = System.Text.Json.JsonSerializer.Serialize(data); + byte[] dataBytes = Encoding.UTF8.GetBytes(json); + + byte[] signature = _rsa.SignData(dataBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); + + return Convert.ToBase64String(signature); + } + + public static string Sign(string data) + { + if (_rsa == null) + return "never called init"; + + string json = System.Text.Json.JsonSerializer.Serialize(data); + byte[] dataBytes = Encoding.UTF8.GetBytes(json); + + byte[] signature = _rsa.SignData(dataBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1); + + return Convert.ToBase64String(signature); + } + } +} diff --git a/Stella.csproj b/Stella.csproj new file mode 100644 index 0000000..34240df --- /dev/null +++ b/Stella.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + +