using Microsoft.AspNetCore.RateLimiting;
using Microsoft.Data.Sqlite;
using System.Threading.RateLimiting;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Text;
using System.Globalization;
using Sharpbin;
class Program
{
private static string salt = string.Empty;
public static async Task Main(string[] args)
{
await InitializeAsync();
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddCors();
builder.Services.AddRateLimiter(_ => _
.AddFixedWindowLimiter(policyName: "fixed", options =>
{
options.PermitLimit = 4;
options.Window = TimeSpan.FromSeconds(12);
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 2;
}));
builder.Services.AddRateLimiter(_ => _
.AddFixedWindowLimiter(policyName: "fixed-bigger", options =>
{
options.PermitLimit = 8;
options.Window = TimeSpan.FromSeconds(6);
options.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
options.QueueLimit = 4;
}));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseRateLimiter();
app.UseAuthorization();
app.MapRazorPages();
#region Frontend
app.MapGet("/", async (HttpContext context) =>
{
var html = File.ReadAllText("assets\\index.html");
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);
return;
});
app.MapGet("/paste-not-found", async (HttpContext context) =>
{
context.Response.StatusCode = 404;
var error_html = File.ReadAllText("assets\\error.html");
error_html = error_html.Replace("{code}", "404");
error_html = error_html.Replace("{message}", "Paste not found");
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(error_html);
return;
});
app.MapGet("/{id}", async (HttpContext context) =>
{
var id = context.Request?.RouteValues?["id"]?.ToString();
if (!File.Exists($"pastes/{id}.txt"))
{
context.Response.StatusCode = 404;
return;
}
var content = File.ReadAllText($"pastes/{id}.txt");
content = content.Replace("\n", "
").Replace("\r", "");
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
{
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM pastes WHERE ID = @ID";
command.Parameters.AddWithValue("@ID", id);
var reader = await command.ExecuteReaderAsync();
await reader.ReadAsync();
if(!reader.HasRows)
{
context.Response.StatusCode = 404;
context.Response.Redirect("/paste-not-found");
return;
}
var paste = new Paste
{
Title = reader.GetString(1),
Date = reader.GetString(2),
Size = reader.GetString(3),
Visibility = reader.GetString(4),
Id = id,
};
if(paste.Visibility == "Private")
{
// WIP Private pastes will be implemented soon enough, most likely with a password or longer ID
context.Response.StatusCode = 404;
return;
}
if (paste.Title == "" || string.IsNullOrEmpty(paste.Title))
{
paste.Title = $"Untitled {reader.GetString(0)}";
}
if (paste.Title.Length > 40)
{
paste.Title = paste.Title.Substring(0, 50) + "...";
}
var html = File.ReadAllText("assets\\paste.html");
html = html.Replace("{pastetitle}", paste.Title);
html = html.Replace("{content}", content);
html = html.Replace("{pasteid}", paste.Id);
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);
return;
}
});
app.MapGet("/raw/{id}", async (HttpContext context) =>
{
var id = context.Request?.RouteValues?["id"]?.ToString();
if (!File.Exists($"pastes/{id}.txt"))
{
context.Response.StatusCode = 404;
return;
}
var content = File.ReadAllText($"pastes/{id}.txt");
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync(content);
return;
});
app.MapGet("/pastes", async (HttpContext context) =>
{
var html = File.ReadAllText("assets\\recent_pastes.html");
using (var connection = new SqliteConnection("Data Source=pastes.sqlite"))
{
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = "SELECT * FROM pastes ORDER BY UID DESC LIMIT 10";
var reader = await command.ExecuteReaderAsync();
string pastes = string.Empty;
while (await reader.ReadAsync())
{
var paste = new Paste
{
Title = reader.GetString(1),
Date = reader.GetString(2),
Size = reader.GetString(3),
Visibility = reader.GetString(4),
Id = reader.GetString(5),
};
if(paste.Visibility == "Private" || paste.Visibility == "Unlisted")
{
continue;
}
if(paste.Title == "" || string.IsNullOrEmpty(paste.Title))
{
paste.Title = $"Untitled {reader.GetString(0)}";
}
if (paste.Title.Length > 40)
{
paste.Title = paste.Title.Substring(0, 50) + "...";
}
DateTime date = DateTime.ParseExact(paste.Date, "dd/MM/yyyy-HH:mm:ss", CultureInfo.InvariantCulture);
DateTime now = DateTime.Now;
var difference = GetTimeDifference(date, now);
pastes += $"