Upload files to "/"

This commit is contained in:
2026-07-24 15:42:29 +00:00
parent f305cecd99
commit fcc7f59761
5 changed files with 424 additions and 0 deletions

121
JsonWebToken.cs Normal file
View File

@@ -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";
/// <summary>
/// Generates a JWT with the given claims and expiration.
/// Arrays and objects are JSON-serialized; strings are kept as-is.
/// </summary>
public static string Generate(Dictionary<string, object?> claims, TimeSpan expiresIn)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var jwtClaims = new List<Claim>();
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);
}
/// <summary>
/// Verifies a JWT. Returns true if valid, false otherwise.
/// </summary>
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;
}
}
/// <summary>
/// Decodes a JWT without verifying signature.
/// </summary>
public static IDictionary<string, object>? Decode(string? token)
{
if (string.IsNullOrWhiteSpace(token)) return null;
try
{
var jwt = TokenHandler.ReadJwtToken(token);
var payload = new Dictionary<string, object>();
foreach (var kv in jwt.Payload)
payload[kv.Key] = kv.Value;
return payload;
}
catch
{
return null;
}
}
/// <summary>
/// Verifies and decodes a JWT. Returns null if invalid.
/// </summary>
public static IDictionary<string, object>? 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);
}
}
}

43
Logs.cs Normal file
View File

@@ -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<object>()
};
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}");
}
}
}
}

153
MongoDB.cs Normal file
View File

@@ -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<User> usersCollection = mongoDatabase.GetCollection<User>("Users");
public static readonly IMongoCollection<RoomDetailsMongoDB> roomsCollection = mongoDatabase.GetCollection<RoomDetailsMongoDB>("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<UnlockedAvatarItem>? 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<SettingDTO>? Settings { get; set; }
[BsonElement("relationships")]
public List<RelationshipDTO>? 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<SceneDTO>? Scenes { get; set; }
public List<int>? CoOwners { get; set; }
public List<int>? InvitedCoOwners { get; set; }
public List<int>? Moderators { get; set; }
public List<int>? InvitedModerators { get; set; }
public List<int>? Hosts { get; set; }
public List<int>? InvitedHosts { get; set; }
public int CheerCount { get; set; }
public int FavoriteCount { get; set; }
public int VisitCount { get; set; }
public List<TagDTO>? Tags { get; set; }
}
}
}

83
OculusServer.cs Normal file
View File

@@ -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<bool> VerifyNonceAsync(ulong userId, string nonce)
{
var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("access_token", AppSecret),
new KeyValuePair<string, string>("nonce", nonce),
new KeyValuePair<string, string>("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<User?> 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
}
}
}

24
Program.cs Normal file
View File

@@ -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();
}
}
}