Improve title handling, token retrieval, and logging

## COMMIT MESSAGE GENERATED BY COPILOT

- Initialize and update `title` variable based on `paste.Title`.
- Update HTML template to use `{pastetitleattribute}` and `{pastetitle}`.
- Retrieve `token` from `Authorization` header or `token` cookie.
- Use `token` in `IsLoggedInAsync` and `GetLoggedInUserAsync` methods.
- Add logging for paste deletion, including log file and response.
- Update user type and UUID check in verification process.
- Log paste deletion details: timestamp, paste ID, user info, request.
This commit is contained in:
whoswhip
2024-09-02 13:42:38 -04:00
parent 05cb8f0afc
commit 1c73e8caa4
2 changed files with 20 additions and 10 deletions
+19 -9
View File
@@ -375,6 +375,8 @@ class Program
var userReader = await userCommand.ExecuteReaderAsync(); var userReader = await userCommand.ExecuteReaderAsync();
await userReader.ReadAsync(); await userReader.ReadAsync();
string title = paste.Title;
var user = new User(); var user = new User();
if (userReader.HasRows) if (userReader.HasRows)
{ {
@@ -402,15 +404,17 @@ class Program
if (paste.Title == "" || string.IsNullOrEmpty(paste.Title)) if (paste.Title == "" || string.IsNullOrEmpty(paste.Title))
{ {
paste.Title = $"Untitled {reader.GetString(0)}"; paste.Title = $"Untitled {reader.GetString(0)}";
title = $"Untitled {reader.GetString(0)}";
} }
if (paste.Title.Length > 40) if (paste.Title.Length > 40)
{ {
paste.Title = paste.Title.Substring(0, 50) + "..."; title = paste.Title.Substring(0, 40) + "...";
} }
var html = File.ReadAllText("assets\\paste.html"); var html = File.ReadAllText("assets\\paste.html");
html = html.Replace("{pastetitle}", paste.Title); html = html.Replace("{pastetitleattribute}", paste.Title);
html = html.Replace("{pastetitle}", title);
html = html.Replace("{content}", content); html = html.Replace("{content}", content);
html = html.Replace("{pasteid}", paste.Id); html = html.Replace("{pasteid}", paste.Id);
html = html.Replace("{username}", user.Username); html = html.Replace("{username}", user.Username);
@@ -496,7 +500,7 @@ class Program
} }
if (paste.Title.Length > 40) if (paste.Title.Length > 40)
{ {
title = paste.Title.Substring(0, 50) + "..."; title = paste.Title.Substring(0, 40) + "...";
} }
var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds())); var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds()));
@@ -563,7 +567,7 @@ class Program
} }
if (paste.Title.Length > 40) if (paste.Title.Length > 40)
{ {
title = paste.Title.Substring(0, 50) + "..."; title = paste.Title.Substring(0, 40) + "...";
} }
var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds())); var difference = GetTimeDifference(DateTimeOffset.FromUnixTimeSeconds(paste.UnixDate ?? 0), DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.Now.ToUnixTimeSeconds()));
pastes += $"<a title=\"{paste.Title}\" href=\"/{paste.Id}\"><li>{title} {difference} {ConvertToBytes(paste.Size)}</li></a>"; pastes += $"<a title=\"{paste.Title}\" href=\"/{paste.Id}\"><li>{title} {difference} {ConvertToBytes(paste.Size)}</li></a>";
@@ -1001,6 +1005,9 @@ class Program
string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
string ip = context.Request.Headers["X-Forwarded-For"].ToString() ?? context.Connection.RemoteIpAddress.ToString(); string ip = context.Request.Headers["X-Forwarded-For"].ToString() ?? context.Connection.RemoteIpAddress.ToString();
string uploaderUUID = string.Empty; string uploaderUUID = string.Empty;
string token = context.Request.Cookies["token"] ?? context.Request.Headers["Authorization"];
if (string.IsNullOrEmpty(requestBody)) if (string.IsNullOrEmpty(requestBody))
{ {
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
@@ -1008,18 +1015,18 @@ class Program
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No content uploaded" }.ToString()); await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No content uploaded" }.ToString());
return; return;
} }
if (await IsLoggedInAsync(context.Request.Cookies["token"])) if (await IsLoggedInAsync(token))
{ {
using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
{ {
await connection.OpenAsync(); await connection.OpenAsync();
var command = connection.CreateCommand(); var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM logins WHERE Token = @Token"; command.CommandText = "SELECT * FROM logins WHERE Token = @Token";
command.Parameters.AddWithValue("@Token", context.Request.Cookies["token"]); command.Parameters.AddWithValue("@Token", token);
var reader = await command.ExecuteReaderAsync(); var reader = await command.ExecuteReaderAsync();
await reader.ReadAsync(); await reader.ReadAsync();
uploaderUUID = reader.GetString(0); uploaderUUID = reader.GetString(0);
var user = await GetLoggedInUserAsync(context.Request.Cookies["token"]); var user = await GetLoggedInUserAsync(token);
if (user.State != 0) if (user.State != 0)
{ {
context.Response.StatusCode = 401; context.Response.StatusCode = 401;
@@ -1095,6 +1102,7 @@ class Program
app.MapPost("/api/pastes/delete", async (HttpContext context) => app.MapPost("/api/pastes/delete", async (HttpContext context) =>
{ {
string token = context.Request.Cookies["token"] ?? context.Request.Headers["Authorization"]; string token = context.Request.Cookies["token"] ?? context.Request.Headers["Authorization"];
var log = await Logging.LogRequestAsync(context);
string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync();
if (!await IsLoggedInAsync(token)) if (!await IsLoggedInAsync(token))
{ {
@@ -1147,9 +1155,8 @@ class Program
user = await GetLoggedInUserAsync(token); user = await GetLoggedInUserAsync(token);
} }
if (verifyReader.GetString(0) != UUID || user.Type != 255) if (user.Type != 255 && verifyReader.GetString(0) != UUID )
{ {
Console.WriteLine(verifyReader.GetString(1) + " " + UUID);
context.Response.StatusCode = 401; context.Response.StatusCode = 401;
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString()); await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString());
@@ -1162,6 +1169,9 @@ class Program
deleteCommand.Parameters.AddWithValue("@ID", id); deleteCommand.Parameters.AddWithValue("@ID", id);
await deleteCommand.ExecuteNonQueryAsync(); await deleteCommand.ExecuteNonQueryAsync();
File.Delete($"pastes/{id}.txt"); File.Delete($"pastes/{id}.txt");
Console.WriteLine($"[{log.Now}] Paste {id} deleted by {user.Username}({user.UUID})");
await File.AppendAllTextAsync("logs.log", $"[{log.Now}] PASTE DELETED {id} {log.IP} {log.Method} {log.Path} {log.UserAgent} {log.Referer} {log.Body} {user.UUID}\n");
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Paste deleted" }.ToString()); await context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Paste deleted" }.ToString());
} }
+1 -1
View File
@@ -16,7 +16,7 @@
</div> </div>
<div class="container-wrapper"> <div class="container-wrapper">
<div class="paste-container"> <div class="paste-container">
<h1 id="paste-title" title="{pastetitle}">{pastetitle}</h1> <h1 id="paste-title" title="{pastetitleattribute}">{pastetitle}</h1>
<h3 class="uploader-text">{username} <a id="creationdate">{creationdate}</a> {pastesize}</h3> <h3 class="uploader-text">{username} <a id="creationdate">{creationdate}</a> {pastesize}</h3>
<div class="paste-wrapper"> <div class="paste-wrapper">
<p>{content}</p> <p>{content}</p>