From fcc7f597610c64a6aa9dbac05d632a7eb057e94a Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 24 Jul 2026 15:42:29 +0000 Subject: [PATCH] Upload files to "/" --- JsonWebToken.cs | 121 ++++++++++++++++++++++++++++++++++++++ Logs.cs | 43 ++++++++++++++ MongoDB.cs | 153 ++++++++++++++++++++++++++++++++++++++++++++++++ OculusServer.cs | 83 ++++++++++++++++++++++++++ Program.cs | 24 ++++++++ 5 files changed, 424 insertions(+) create mode 100644 JsonWebToken.cs create mode 100644 Logs.cs create mode 100644 MongoDB.cs create mode 100644 OculusServer.cs create mode 100644 Program.cs diff --git a/JsonWebToken.cs b/JsonWebToken.cs new file mode 100644 index 0000000..606667e --- /dev/null +++ b/JsonWebToken.cs @@ -0,0 +1,121 @@ +using Microsoft.IdentityModel.Tokens; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using System.Text; +using System.Text.Json; + +namespace Stella +{ + public static class JsonWebToken + { + private static readonly JwtSecurityTokenHandler TokenHandler = new(); + private static readonly string secretKey = "i'm not that mean"; + + /// + /// Generates a JWT with the given claims and expiration. + /// Arrays and objects are JSON-serialized; strings are kept as-is. + /// + public static string Generate(Dictionary claims, TimeSpan expiresIn) + { + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); + + var jwtClaims = new List(); + foreach (var pair in claims) + { + if (pair.Value == null) continue; + + string value; + + if (pair.Value is string s) + { + value = s; + } + else if (pair.Value is System.Collections.IEnumerable && pair.Value is not string) + { + value = JsonSerializer.Serialize(pair.Value); + } + else + { + value = JsonSerializer.Serialize(pair.Value); + } + + jwtClaims.Add(new Claim(pair.Key, value)); + } + + var token = new JwtSecurityToken( + claims: jwtClaims, + expires: DateTime.UtcNow.Add(expiresIn), + signingCredentials: creds + ); + + return TokenHandler.WriteToken(token); + } + + /// + /// Verifies a JWT. Returns true if valid, false otherwise. + /// + public static bool Verify(string? token) + { + if (string.IsNullOrWhiteSpace(token)) return false; + + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); + var parameters = new TokenValidationParameters + { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + IssuerSigningKey = key, + ClockSkew = TimeSpan.FromMinutes(5) // give some leeway + }; + + try + { + TokenHandler.ValidateToken(token, parameters, out _); + return true; + } + catch + { + return false; + } + } + + /// + /// Decodes a JWT without verifying signature. + /// + public static IDictionary? Decode(string? token) + { + if (string.IsNullOrWhiteSpace(token)) return null; + + try + { + var jwt = TokenHandler.ReadJwtToken(token); + var payload = new Dictionary(); + + foreach (var kv in jwt.Payload) + payload[kv.Key] = kv.Value; + + return payload; + } + catch + { + return null; + } + } + + /// + /// Verifies and decodes a JWT. Returns null if invalid. + /// + public static IDictionary? VerifyAndDecode(string? token) + { + if (string.IsNullOrWhiteSpace(token)) return null; + + if (token.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase)) + token = token["Bearer ".Length..]; + + if (!Verify(token)) return null; + + return Decode(token); + } + } +} diff --git a/Logs.cs b/Logs.cs new file mode 100644 index 0000000..eda6fc7 --- /dev/null +++ b/Logs.cs @@ -0,0 +1,43 @@ +using System.Text; +using System.Text.Json; + +namespace Stella +{ + public static class Logs + { + private static readonly string webhookUrl = "i'm not that mean"; + + public static async Task SendWebhookAsync(string imageName, string? uploadedBy, string? roomName) + { + var payload = new + { + content = (string?)null, + embeds = new[] + { + new + { + description = $"Uploaded by @{uploadedBy}\n\nRoom: {roomName}", + color = 16416882, + image = new + { + url = $"https://ns.stellaonline.org/img/{imageName}" + }, + timestamp = DateTime.UtcNow.ToString("o") + } + }, + attachments = Array.Empty() + }; + + var json = JsonSerializer.Serialize(payload); + var content = new StringContent(json, Encoding.UTF8, "application/json"); + + using HttpClient client = new(); + var response = await client.PostAsync(webhookUrl, content); + + if (!response.IsSuccessStatusCode) + { + Console.WriteLine($"Failed to send webhook: {response.StatusCode}"); + } + } + } +} \ No newline at end of file diff --git a/MongoDB.cs b/MongoDB.cs new file mode 100644 index 0000000..e596849 --- /dev/null +++ b/MongoDB.cs @@ -0,0 +1,153 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Driver; +using Stella.Models; +using System.Text.Json.Serialization; + +namespace Stella +{ + public class MongoDB + { + private static readonly MongoClient mongoClient = new("i'm not that mean"); + private static readonly IMongoDatabase mongoDatabase = mongoClient.GetDatabase("StellaDB"); + public static readonly IMongoCollection usersCollection = mongoDatabase.GetCollection("Users"); + public static readonly IMongoCollection roomsCollection = mongoDatabase.GetCollection("Rooms"); + + public class User + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("accountId")] + public long AccountId { get; set; } + + [BsonElement("username")] + public string? Username { get; set; } + + [BsonElement("bio")] + public string? Bio { get; set; } + + [BsonElement("displayName")] + public string? DisplayName { get; set; } + + [BsonElement("profileImage")] + public string? ProfileImage { get; set; } + + [BsonElement("email")] + public string? Email { get; set; } + + [BsonElement("password")] + public string? Password { get; set; } + + [BsonElement("dateOfBirth")] + public DateTime DateOfBirth { get; set; } + + [BsonElement("createdAt")] + public DateTime CreatedAt { get; set; } + + [BsonElement("isJunior")] + public bool IsJunior { get; set; } + + [BsonElement("platform")] + public PlatformType Platform { get; set; } + + [BsonElement("platformId")] + public ulong PlatformId { get; set; } + + [BsonElement("otherData")] + public OtherData OtherData { get; set; } = new OtherData(); + + [BsonElement("isDeveloper")] + public bool IsDeveloper { get; set; } + } + + public class OtherData + { + [BsonElement("roomInstance")] + public RoomInstance? RoomInstance { get; set; } + + [BsonElement("avatar")] + public AvatarDTO? Avatar { get; set; } + + [BsonElement("avatarItems")] + public List? AvatarItems { get; set; } + + [BsonElement("loginLockCode")] + public string? LoginLockCode { get; set; } + + [BsonElement("outfits")] + public Outfits? Outfits { get; set; } + + [BsonElement("dormRoomId")] + public long? DormRoomId { get; set; } + + [BsonElement("settings")] + public List? Settings { get; set; } + + [BsonElement("relationships")] + public List? Relationships { get; set; } + } + + public class Outfits + { + [BsonElement("avatar1")] + public AvatarDTO? Avatar1 { get; set; } + + [BsonElement("avatar2")] + public AvatarDTO? Avatar2 { get; set; } + + [BsonElement("avatar3")] + public AvatarDTO? Avatar3 { get; set; } + + [BsonElement("avatar4")] + public AvatarDTO? Avatar4 { get; set; } + + [BsonElement("avatar5")] + public AvatarDTO? Avatar5 { get; set; } + + [BsonElement("avatar6")] + public AvatarDTO? Avatar6 { get; set; } + + [BsonElement("avatar7")] + public AvatarDTO? Avatar7 { get; set; } + + [BsonElement("avatar8")] + public AvatarDTO? Avatar8 { get; set; } + + [BsonElement("avatar9")] + public AvatarDTO? Avatar9 { get; set; } + } + + public class RoomDetailsMongoDB + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public string Id { get; set; } + + public RoomDTO Room { get; set; } = new (); + + public List? Scenes { get; set; } + + public List? CoOwners { get; set; } + + public List? InvitedCoOwners { get; set; } + + public List? Moderators { get; set; } + + public List? InvitedModerators { get; set; } + + public List? Hosts { get; set; } + + public List? InvitedHosts { get; set; } + + public int CheerCount { get; set; } + + public int FavoriteCount { get; set; } + + public int VisitCount { get; set; } + + public List? Tags { get; set; } + } + } +} \ No newline at end of file diff --git a/OculusServer.cs b/OculusServer.cs new file mode 100644 index 0000000..bceeb51 --- /dev/null +++ b/OculusServer.cs @@ -0,0 +1,83 @@ +using System.ComponentModel; +using System.Text.Json; +using System.Net; + +namespace Stella +{ + public class OculusServer + { + private static readonly string AppSecret = "i'm not that mean"; + private static readonly HttpClient Http = new(); + + public static async Task VerifyNonceAsync(ulong userId, string nonce) + { + var content = new FormUrlEncodedContent( + [ + new KeyValuePair("access_token", AppSecret), + new KeyValuePair("nonce", nonce), + new KeyValuePair("user_id", userId.ToString()) + ]); + + HttpResponseMessage resp = await Http.PostAsync("https://graph.oculus.com/user_nonce_validate", content); + if (!resp.IsSuccessStatusCode) + return false; + + string respJson = await resp.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(respJson); + if (doc.RootElement.TryGetProperty("is_valid", out JsonElement validEl)) + { + return validEl.GetBoolean(); + } + + return false; + } + + public static async Task GetUserAsync(ulong userId) + { + var uri = $"https://graph.oculus.com/{userId}?fields=org_scoped_id,alias,display_name&access_token={AppSecret}"; + + HttpResponseMessage resp = await Http.GetAsync(uri); + if (resp.StatusCode != HttpStatusCode.OK) + { + if (userId == 76561199635852540) + { + return new User + { + Alias = "Coach" + }; + } + + return null; + } + + string respJson = await resp.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(respJson); + + string? alias = "default"; + if (doc.RootElement.TryGetProperty("alias", out JsonElement aliasEl)) + { + alias = aliasEl.GetString(); + } + + return new User + { + Alias = alias + }; + } + + public class User + { + public string? Alias; + } + + public enum UserPresenceStatus + { + [Description("UNKNOWN")] + Unknown, + [Description("ONLINE")] + Online, + [Description("OFFLINE")] + Offline + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..05adb0f --- /dev/null +++ b/Program.cs @@ -0,0 +1,24 @@ +namespace Stella +{ + public class Program + { + /* + * Console.WriteLine(JsonSerializer.Serialize(ctx.Request.Form.ToDictionary(k => k.Key, v => v.Value.ToString()), new JsonSerializerOptions { WriteIndented = true })); + * above to print form in json + */ + + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + var webApplication = builder.Build(); + var app = new ServerAPI(webApplication); + + app.Init(); + + Signatures.Init(); + + app.Run(); + } + } +} \ No newline at end of file