First commit
This commit is contained in:
commit
0ac6ff9196
26 changed files with 2836 additions and 0 deletions
180
LarpixServer/Program.cs
Normal file
180
LarpixServer/Program.cs
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using LarpixServer.Account;
|
||||
using LarpixServer.Filesystem;
|
||||
using LarpixServer.Utils.Jsons;
|
||||
|
||||
namespace LarpixServer;
|
||||
|
||||
using static LarpixServer.Utils.Utils;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
await LoadEnv();
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddHostedService<BackgroundDelete>();
|
||||
builder.Logging.ClearProviders();
|
||||
|
||||
builder.WebHost.ConfigureKestrel(serverOptions =>
|
||||
{
|
||||
serverOptions.AddServerHeader = false;
|
||||
serverOptions.ListenAnyIP(PORT);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseWebSockets();
|
||||
|
||||
app.Use(async (HttpContext context, Func<Task> next) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
context.Response.ContentType = mimeTypes["txt"];
|
||||
context.Response.StatusCode = 200;
|
||||
var path = context.Request.Path.Value;
|
||||
var url = $"{context.Request.Scheme}://{context.Request.Host}";
|
||||
if (path == "" || path.Length > 512)
|
||||
{
|
||||
context.Response.Redirect(url + "/");
|
||||
return;
|
||||
}
|
||||
int index = path.LastIndexOf('.');
|
||||
string fileExtension = index >= 0 ? path[(index + 1)..] : "";
|
||||
IQueryCollection query = context.Request.Query;
|
||||
using StreamReader reader = new StreamReader(context.Request.Body);
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case "/_larpix/serverinfo":
|
||||
ServerInfo serverInfo = new ServerInfo();
|
||||
serverInfo.domain = DOMAIN;
|
||||
serverInfo.owner_contact = OWNER_CONTACT;
|
||||
|
||||
string registrationString = Encoding.UTF8.GetString(await Fs.ReadFile($"{ACCOUNTS_DIR}/registration"));
|
||||
if (registrationString.StartsWith("first;"))
|
||||
{
|
||||
serverInfo.registration = "enabled";
|
||||
}
|
||||
if (registrationString.StartsWith("1;"))
|
||||
{
|
||||
serverInfo.registration = "enabled";
|
||||
}
|
||||
if (registrationString.StartsWith("0;"))
|
||||
{
|
||||
serverInfo.registration = "disabled";
|
||||
}
|
||||
if (registrationString.StartsWith("code;"))
|
||||
{
|
||||
serverInfo.registration = "code";
|
||||
}
|
||||
|
||||
var serializedPayload = JsonSerializer.Serialize(
|
||||
serverInfo,
|
||||
AppJsonSerializerContext.Default.ServerInfo
|
||||
);
|
||||
context.Response.ContentType = mimeTypes["json"];
|
||||
await context.Response.WriteAsync(serializedPayload);
|
||||
return;
|
||||
}
|
||||
|
||||
//if (!context.Request.Headers.TryGetValue("username", out var username)) how to header
|
||||
|
||||
switch (path)
|
||||
{
|
||||
case "/restart":
|
||||
if (!query.TryGetValue("skey", out var skey))
|
||||
{
|
||||
context.Response.ContentType = mimeTypes["html"];
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("Forbidden");
|
||||
return;
|
||||
}
|
||||
if (skey != SERVER_KEY)
|
||||
{
|
||||
context.Response.ContentType = mimeTypes["html"];
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("Forbidden");
|
||||
return;
|
||||
}
|
||||
if (!query.TryGetValue("f", out var force))
|
||||
{
|
||||
force = "false";
|
||||
}
|
||||
if (force == "true")
|
||||
{
|
||||
await context.Response.WriteAsync("[FORCE] Shutting down...");
|
||||
Console.WriteLine("[FORCE] Shutting down...");
|
||||
Environment.Exit(0);
|
||||
return;
|
||||
}
|
||||
await context.Response.WriteAsync("[SAFE] Shutting down...");
|
||||
Console.WriteLine("[SAFE] Shutting down...");
|
||||
var lifetime = context.RequestServices.GetRequiredService<IHostApplicationLifetime>();
|
||||
lifetime.StopApplication();
|
||||
return;
|
||||
case "/clearcache": //kiedys bedzie endpoint cache
|
||||
if (!query.TryGetValue("skey", out skey))
|
||||
{
|
||||
context.Response.ContentType = mimeTypes["html"];
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("Forbidden");
|
||||
return;
|
||||
}
|
||||
if (skey != SERVER_KEY)
|
||||
{
|
||||
context.Response.ContentType = mimeTypes["html"];
|
||||
context.Response.StatusCode = 403;
|
||||
await context.Response.WriteAsync("Forbidden");
|
||||
return;
|
||||
}
|
||||
if (!query.TryGetValue("p", out var p))
|
||||
{
|
||||
p = "";
|
||||
}
|
||||
ulong removed = Fs.ClearCache(p);
|
||||
await context.Response.WriteAsync($"Cache cleared with pattern: {p}. {removed} elements removed");
|
||||
return;
|
||||
case "/createaccount":
|
||||
await Account.Requests.Create(context, next, query, reader);
|
||||
return;
|
||||
case "/deleteaccount":
|
||||
await Account.Requests.Delete(context, next, query, reader);
|
||||
return;
|
||||
case "/auth":
|
||||
await Account.Requests.Auth(context, next, query, reader);
|
||||
return;
|
||||
case "/chpass":
|
||||
await Account.Requests.ChangePassword(context, next, query, reader);
|
||||
return;
|
||||
case "/chname":
|
||||
await Account.Requests.ChangeUsername(context, next, query, reader);
|
||||
return;
|
||||
case "/correctedname":
|
||||
await Account.Requests.CorrectedName(context, next, query);
|
||||
return;
|
||||
case "/nextnonce":
|
||||
await Account.Requests.NextNonce(context, next, query);
|
||||
return;
|
||||
case "/encryptedrequest":
|
||||
await Account.Requests.EncryptedRequest(context, next, query, reader);
|
||||
return;
|
||||
case "/user/key/getpublic":
|
||||
await Account.Requests.GetUserPublicKey(context, next, query, reader);
|
||||
return;
|
||||
case "/user/storage/public/getentry":
|
||||
await Account.Requests.GetUserPublicStorageEntry(context, next, query, reader);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Console.WriteLine("Starting server at port: " + PORT);
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue