mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Initial commit for new project
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
**/.classpath
|
||||||
|
**/.dockerignore
|
||||||
|
**/.env
|
||||||
|
**/.git
|
||||||
|
**/.gitignore
|
||||||
|
**/.project
|
||||||
|
**/.settings
|
||||||
|
**/.toolstarget
|
||||||
|
**/.vs
|
||||||
|
**/.vscode
|
||||||
|
**/*.*proj.user
|
||||||
|
**/*.dbmdl
|
||||||
|
**/*.jfm
|
||||||
|
**/azds.yaml
|
||||||
|
**/bin
|
||||||
|
**/charts
|
||||||
|
**/docker-compose*
|
||||||
|
**/Dockerfile*
|
||||||
|
**/node_modules
|
||||||
|
**/npm-debug.log
|
||||||
|
**/obj
|
||||||
|
**/secrets.dev.yaml
|
||||||
|
**/values.dev.yaml
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
|
!**/.gitignore
|
||||||
|
!.git/HEAD
|
||||||
|
!.git/config
|
||||||
|
!.git/packed-refs
|
||||||
|
!.git/refs/heads/**
|
||||||
+372
@@ -0,0 +1,372 @@
|
|||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Mono auto generated files
|
||||||
|
mono_crash.*
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Ww][Ii][Nn]32/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Oo]ut/
|
||||||
|
[Ll]og/
|
||||||
|
[Ll]ogs/
|
||||||
|
|
||||||
|
p/
|
||||||
|
privp/
|
||||||
|
raw/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUnit
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
nunit-*.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# ASP.NET Scaffolding
|
||||||
|
ScaffoldingReadMe.txt
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Coverlet is a free, cross platform Code Coverage Tool
|
||||||
|
coverage*.json
|
||||||
|
coverage*.xml
|
||||||
|
coverage*.info
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# NuGet Symbol Packages
|
||||||
|
*.snupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
*.appxbundle
|
||||||
|
*.appxupload
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
*- [Bb]ackup.rdl
|
||||||
|
*- [Bb]ackup ([0-9]).rdl
|
||||||
|
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
|
|
||||||
|
# BeatPulse healthcheck temp database
|
||||||
|
healthchecksdb
|
||||||
|
|
||||||
|
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||||
|
MigrationBackup/
|
||||||
|
|
||||||
|
# Ionide (cross platform F# VS Code tools) working folder
|
||||||
|
.ionide/
|
||||||
|
|
||||||
|
# Fody - auto-generated XML schema
|
||||||
|
FodyWeavers.xsd
|
||||||
|
|
||||||
|
SharpbinV2.Server/*.db
|
||||||
|
SharpbinV2.Server/logs
|
||||||
|
SharpbinV2.Server/pastes
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||||
|
USER app
|
||||||
|
WORKDIR /app
|
||||||
|
EXPOSE 8080
|
||||||
|
EXPOSE 8081
|
||||||
|
|
||||||
|
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["SharpbinV2.Server/SharpbinV2.Server.csproj", "SharpbinV2.Server/"]
|
||||||
|
RUN dotnet restore "./SharpbinV2.Server/SharpbinV2.Server.csproj"
|
||||||
|
COPY . .
|
||||||
|
WORKDIR "/src/SharpbinV2.Server"
|
||||||
|
RUN dotnet build "./SharpbinV2.Server.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||||
|
|
||||||
|
FROM build AS publish
|
||||||
|
ARG BUILD_CONFIGURATION=Release
|
||||||
|
RUN dotnet publish "./SharpbinV2.Server.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
ENTRYPOINT ["dotnet", "SharpbinV2.Server.dll"]
|
||||||
@@ -0,0 +1,859 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
using Microsoft.AspNetCore.RateLimiting;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.Extensions.Logging.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging.EventSource;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Threading.RateLimiting;
|
||||||
|
using System.Text;
|
||||||
|
using Bcrypt = BCrypt.Net.BCrypt;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace SharpbinV2.Server
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
public static string MainDatabaseConnection = "Data Source=data.db";
|
||||||
|
public static long MaxFileSize = 5000000; // 5MB
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
await Initialize();
|
||||||
|
var logger = new Logging();
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
builder.Configuration.AddEnvironmentVariables();
|
||||||
|
builder.WebHost.UseUrls($"https://*:{Environment.GetEnvironmentVariable("HTTPS_Port") ?? "5820"}", $"http://*:{Environment.GetEnvironmentVariable("HTTP_Port") ?? "5810"}");
|
||||||
|
MaxFileSize = Environment.GetEnvironmentVariable("MAX_FILE_SIZE") != null ? Convert.ToInt64(Environment.GetEnvironmentVariable("MAX_FILE_SIZE")) : MaxFileSize;
|
||||||
|
|
||||||
|
builder.WebHost.ConfigureKestrel(options =>
|
||||||
|
{
|
||||||
|
options.Limits.MaxRequestBodySize = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
builder.Services.AddCors();
|
||||||
|
builder.Services.AddResponseCaching();
|
||||||
|
builder.Services.AddResponseCompression();
|
||||||
|
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (!app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseExceptionHandler("/Error");
|
||||||
|
app.UseHsts();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//app.UseHttpsRedirection();
|
||||||
|
app.UseRouting();
|
||||||
|
|
||||||
|
//app.UseRateLimiter();
|
||||||
|
|
||||||
|
app.UseCors(builder =>
|
||||||
|
{
|
||||||
|
builder.AllowAnyOrigin();
|
||||||
|
builder.AllowAnyMethod();
|
||||||
|
builder.AllowAnyHeader();
|
||||||
|
});
|
||||||
|
app.UseResponseCaching();
|
||||||
|
app.UseResponseCompression();
|
||||||
|
|
||||||
|
#region Static Endpoints
|
||||||
|
app.MapGet("/", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
context.Response.Headers.Add("Content-Type", "text/html");
|
||||||
|
await context.Response.SendFileAsync("assets/index.html");
|
||||||
|
});
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region API
|
||||||
|
|
||||||
|
app.MapPost("/api/accounts/register", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||||
|
if (body == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Body." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var json = TryParse(body);
|
||||||
|
if (json == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid JSON." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var email = json["email"]?.ToString() ?? null;
|
||||||
|
var username = json["username"]?.ToString();
|
||||||
|
var password = json["password"]?.ToString();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Missing required fields." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var testusername = await Database.UserFromUsername(username);
|
||||||
|
if (testusername != null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Username already taken." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var testemail = await Database.UserFromEmail(email);
|
||||||
|
if (testemail != null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Email already in use." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var passwordHashed = Bcrypt.HashPassword(password, Bcrypt.GenerateSalt(12));
|
||||||
|
string token = GenerateToken();
|
||||||
|
var requestdetails = GetRequestDetails(context);
|
||||||
|
string userUUID = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO users (UUID, Username, Password, Email, Created, LastLogin) VALUES (@UUID, @Username, @Password, @Email, @Created, @LastLogin);";
|
||||||
|
command.Parameters.AddWithValue("@UUID", userUUID);
|
||||||
|
command.Parameters.AddWithValue("@Username", username);
|
||||||
|
command.Parameters.AddWithValue("@Password", passwordHashed);
|
||||||
|
command.Parameters.AddWithValue("@Email", (object)email ?? DBNull.Value);
|
||||||
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@LastLogin", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO sessions (UUID, UserUUID, Token, Created, Expirary, Ip, UserAgent) VALUES (@UUID, @UserUUID, @Token, @Created, @Expirary, @Ip, @UserAgent);";
|
||||||
|
command.Parameters.AddWithValue("@UUID", Guid.NewGuid().ToString());
|
||||||
|
command.Parameters.AddWithValue("@UserUUID", userUUID);
|
||||||
|
command.Parameters.AddWithValue("@Token", token);
|
||||||
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(requestdetails.Ip, Bcrypt.GenerateSalt(10)));
|
||||||
|
command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
await connection.CloseAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
var cookieOptions = new CookieOptions
|
||||||
|
{
|
||||||
|
HttpOnly = true,
|
||||||
|
Secure = true,
|
||||||
|
SameSite = SameSiteMode.Strict,
|
||||||
|
Expires = DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds())
|
||||||
|
};
|
||||||
|
context.Response.Cookies.Append("Authorization", token, cookieOptions);
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = true, message = "Account created." });
|
||||||
|
logger.LogInfo($"Account {username} has been created.");
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
app.MapGet("/api/accounts/login", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
var requestdetails = GetRequestDetails(context);
|
||||||
|
if (!string.IsNullOrEmpty(requestdetails.Token) && Database.UserFromToken(requestdetails.Token) != null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = true, message = "Already logged in." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||||
|
if (body == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Body." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var json = TryParse(body);
|
||||||
|
if (json == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid JSON." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var username = json["username"]?.ToString();
|
||||||
|
var email = json["email"]?.ToString();
|
||||||
|
var password = json["password"]?.ToString();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(email))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Missing required fields." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (string.IsNullOrEmpty(password))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Missing required fields." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
User user = null;
|
||||||
|
if (!string.IsNullOrEmpty(username))
|
||||||
|
user = await Database.UserFromUsername(username);
|
||||||
|
else if (!string.IsNullOrEmpty(email))
|
||||||
|
user = await Database.UserFromEmail(email);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid credentials." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!Bcrypt.Verify(password, user.Password))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid credentials." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
string token = GenerateToken();
|
||||||
|
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO sessions (UUID, UserUUID, Token, Created, Expirary, Ip, UserAgent) VALUES (@UUID, @UserUUID, @Token, @Created, @Expirary, @Ip, @UserAgent);";
|
||||||
|
command.Parameters.AddWithValue("@UUID", Guid.NewGuid().ToString());
|
||||||
|
command.Parameters.AddWithValue("@UserUUID", user.UUID);
|
||||||
|
command.Parameters.AddWithValue("@Token", token);
|
||||||
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(requestdetails.Ip, Bcrypt.GenerateSalt(10)));
|
||||||
|
command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "UPDATE users SET LastLogin = @LastLogin WHERE UUID = @UUID;";
|
||||||
|
command.Parameters.AddWithValue("@LastLogin", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
|
command.Parameters.AddWithValue("@UUID", user.UUID);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
await connection.CloseAsync();
|
||||||
|
}
|
||||||
|
var cookieOptions = new CookieOptions
|
||||||
|
{
|
||||||
|
HttpOnly = true,
|
||||||
|
Secure = true,
|
||||||
|
SameSite = SameSiteMode.Strict,
|
||||||
|
Expires = DateTimeOffset.FromUnixTimeSeconds(DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds())
|
||||||
|
};
|
||||||
|
context.Response.Cookies.Append("Authorization", token, cookieOptions);
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = true, message = "Logged in." });
|
||||||
|
logger.LogInfo($"User {user.UUID} logged in.");
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
|
||||||
|
app.MapPost("/api/pastes/create", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||||
|
if (body == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Body." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (body.Length > MaxFileSize)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "File size too large." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var paste = new Paste();
|
||||||
|
var requestdetails = GetRequestDetails(context);
|
||||||
|
var user = await Database.UserFromToken(requestdetails.Token);
|
||||||
|
var queries = context.Request.Query;
|
||||||
|
|
||||||
|
|
||||||
|
paste.Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||||
|
paste.Size = body.Length;
|
||||||
|
paste.TrueSize = body.Length;
|
||||||
|
paste.UUID = Guid.NewGuid().ToString();
|
||||||
|
paste.ID = GenerateRandomString(12);
|
||||||
|
paste.AuthorUUID = user?.UUID ?? "0"; // 0 means the user is anonymous
|
||||||
|
paste.FilePath = $"pastes/{paste.ID}.txt";
|
||||||
|
paste.Visibility = queries.ContainsKey("visibility") ? Convert.ToInt32(queries["visibility"]) : 0;
|
||||||
|
paste.Title = queries.ContainsKey("title") ? queries["title"].ToString() : $"Untitled-{await Database.EnumeratePastes()}";
|
||||||
|
paste.Edited = 0;
|
||||||
|
|
||||||
|
bool shouldCompress = await Compression.ShouldCompress(body);
|
||||||
|
if (shouldCompress)
|
||||||
|
{
|
||||||
|
var compressed = await Compression.CompressString(body);
|
||||||
|
paste.Size = compressed.Length;
|
||||||
|
paste.TrueSize = body.Length;
|
||||||
|
paste.FilePath += ".gz";
|
||||||
|
await File.WriteAllBytesAsync(paste.FilePath, compressed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await File.WriteAllTextAsync(paste.FilePath, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "INSERT INTO pastes (UUID, ID, Visibility, Title, AuthorUUID, FilePath, Created, Edited, Size, TrueSize, Views) VALUES (@UUID, @ID, @Visibility, @Title, @AuthorUUID, @FilePath, @Created, @Edited, @Size, @TrueSize, @Views);";
|
||||||
|
command.Parameters.AddWithValue("@UUID", paste.UUID);
|
||||||
|
command.Parameters.AddWithValue("@ID", paste.ID);
|
||||||
|
command.Parameters.AddWithValue("@Visibility", paste.Visibility);
|
||||||
|
command.Parameters.AddWithValue("@Title", paste.Title);
|
||||||
|
command.Parameters.AddWithValue("@AuthorUUID", paste.AuthorUUID);
|
||||||
|
command.Parameters.AddWithValue("@FilePath", paste.FilePath);
|
||||||
|
command.Parameters.AddWithValue("@Created", paste.Created);
|
||||||
|
command.Parameters.AddWithValue("@Edited", paste.Edited);
|
||||||
|
command.Parameters.AddWithValue("@Size", paste.Size);
|
||||||
|
command.Parameters.AddWithValue("@TrueSize", paste.TrueSize);
|
||||||
|
command.Parameters.AddWithValue("@Views", paste.Views);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
await connection.CloseAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = true, message = "Paste created.", paste = paste });
|
||||||
|
logger.LogInfo($"A Paste of {FormatBytes(paste.Size)} was created by {paste.UUID}");
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
app.MapGet("/api/pastes/{pasteid}/info", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
|
||||||
|
if (string.IsNullOrEmpty(pasteid))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Paste ID." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var paste = await Database.GetPasteFromID(pasteid);
|
||||||
|
if (paste == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Paste not found." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = true, paste = paste });
|
||||||
|
});
|
||||||
|
app.MapGet("/api/pastes/{pasteid}", async (HttpContext context) =>
|
||||||
|
{
|
||||||
|
var pasteid = context.Request.RouteValues["pasteid"].ToString() ?? null;
|
||||||
|
if (string.IsNullOrEmpty(pasteid))
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = 400;
|
||||||
|
await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid Paste ID." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var paste = await Database.GetPasteFromID(pasteid);
|
||||||
|
context.Response.StatusCode = 200;
|
||||||
|
context.Response.Headers.Add("Content-Type", "text/plain");
|
||||||
|
if (paste.FilePath.EndsWith(".gz"))
|
||||||
|
{
|
||||||
|
context.Response.Headers.Add("Content-Encoding", "gzip");
|
||||||
|
await context.Response.SendFileAsync(paste.FilePath);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await context.Response.SendFileAsync(paste.FilePath);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
await app.RunAsync();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Helper Functions
|
||||||
|
static JObject TryParse(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return JObject.Parse(json);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static JArray TryParseArray(string json)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return JArray.Parse(json);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static string GenerateToken()
|
||||||
|
{
|
||||||
|
string randomstring = GenerateRandomString(32);
|
||||||
|
string randomguid = Guid.NewGuid().ToString();
|
||||||
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(Bcrypt.HashPassword(randomstring + randomguid, Bcrypt.GenerateSalt(10))));
|
||||||
|
}
|
||||||
|
static string GenerateRandomString(int length)
|
||||||
|
{
|
||||||
|
var random = new Random();
|
||||||
|
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
|
||||||
|
}
|
||||||
|
static RequestDetails GetRequestDetails(HttpContext context)
|
||||||
|
{
|
||||||
|
var requestdetails = new RequestDetails();
|
||||||
|
var headers = context.Request.Headers;
|
||||||
|
if (headers.ContainsKey("User-Agent"))
|
||||||
|
requestdetails.UserAgent = headers["User-Agent"];
|
||||||
|
|
||||||
|
if (headers.ContainsKey("X-Forwarded-For"))
|
||||||
|
requestdetails.Ip = headers["X-Forwarded-For"];
|
||||||
|
else if (headers.ContainsKey("X-Real-IP"))
|
||||||
|
requestdetails.Ip = headers["X-Real-IP"];
|
||||||
|
else if (headers.ContainsKey("CF-Connecting-IP"))
|
||||||
|
requestdetails.Ip = headers["CF-Connecting-IP"];
|
||||||
|
else if (headers.ContainsKey("True-Client-IP"))
|
||||||
|
requestdetails.Ip = headers["True-Client-IP"];
|
||||||
|
else if (headers.ContainsKey("X-Cluster-Client-IP"))
|
||||||
|
requestdetails.Ip = headers["X-Cluster-Client-IP"];
|
||||||
|
else if (headers.ContainsKey("X-ProxyUser-IP"))
|
||||||
|
requestdetails.Ip = headers["X-ProxyUser-IP"];
|
||||||
|
else
|
||||||
|
requestdetails.Ip = context.Connection.RemoteIpAddress?.ToString();
|
||||||
|
|
||||||
|
if (headers.ContainsKey("Authorization"))
|
||||||
|
requestdetails.Token = headers["Authorization"];
|
||||||
|
else if (context.Request.Cookies.ContainsKey("Authorization"))
|
||||||
|
requestdetails.Token = context.Request.Cookies["Authorization"];
|
||||||
|
|
||||||
|
return requestdetails;
|
||||||
|
}
|
||||||
|
static string FormatBytes(long bytes)
|
||||||
|
{
|
||||||
|
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
|
||||||
|
double len = bytes;
|
||||||
|
int order = 0;
|
||||||
|
while (len >= 1024 && order < sizes.Length - 1)
|
||||||
|
{
|
||||||
|
order++;
|
||||||
|
len = len / 1024;
|
||||||
|
}
|
||||||
|
return $"{len:0.##} {sizes[order]}";
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
static async Task Initialize()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = @"CREATE TABLE IF NOT EXISTS users (
|
||||||
|
UID INTEGER UNIQUE,
|
||||||
|
UUID TEXT NOT NULL UNIQUE,
|
||||||
|
Type INTEGER NOT NULL DEFAULT 0,
|
||||||
|
Email TEXT UNIQUE,
|
||||||
|
Username TEXT NOT NULL UNIQUE,
|
||||||
|
DisplayName TEXT,
|
||||||
|
Password TEXT NOT NULL,
|
||||||
|
Created INTEGER NOT NULL,
|
||||||
|
LastLogin INTEGER,
|
||||||
|
PRIMARY KEY(UID AUTOINCREMENT)
|
||||||
|
);";
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
command.CommandText = @"CREATE TABLE IF NOT EXISTS pastes (
|
||||||
|
UID INTEGER NOT NULL UNIQUE,
|
||||||
|
UUID TEXT NOT NULL UNIQUE,
|
||||||
|
ID TEXT NOT NULL UNIQUE,
|
||||||
|
Visibility INTEGER,
|
||||||
|
Title TEXT,
|
||||||
|
AuthorUUID TEXT NOT NULL,
|
||||||
|
FilePath TEXT NOT NULL UNIQUE,
|
||||||
|
Created INTEGER NOT NULL,
|
||||||
|
Edited INTEGER,
|
||||||
|
Size INTEGER NOT NULL,
|
||||||
|
TrueSize INTEGER NOT NULL,
|
||||||
|
Views INTEGER NOT NULL DEFAULT 0,
|
||||||
|
PRIMARY KEY(UID AUTOINCREMENT)
|
||||||
|
);";
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
command.CommandText = @"CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
UUID TEXT NOT NULL UNIQUE,
|
||||||
|
UserUUID INTEGER NOT NULL,
|
||||||
|
Token INTEGER NOT NULL UNIQUE,
|
||||||
|
Created INTEGER NOT NULL,
|
||||||
|
Expirary INTEGER NOT NULL,
|
||||||
|
Ip TEXT NOT NULL,
|
||||||
|
UserAgent TEXT NOT NULL
|
||||||
|
);";
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
await connection.CloseAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!Directory.Exists("pastes"))
|
||||||
|
Directory.CreateDirectory("pastes");
|
||||||
|
if (!Directory.Exists("logs"))
|
||||||
|
Directory.CreateDirectory("logs");
|
||||||
|
var root = Directory.GetCurrentDirectory();
|
||||||
|
var dotenv = Path.Combine(root, ".env");
|
||||||
|
DotEnv.Load(dotenv);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
Environment.Exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Database
|
||||||
|
{
|
||||||
|
public static async Task<User> UserFromUsername(string username)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(username))
|
||||||
|
return null;
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM users WHERE Username = @Username;";
|
||||||
|
command.Parameters.AddWithValue("@Username", username);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
await reader.ReadAsync();
|
||||||
|
return new User
|
||||||
|
{
|
||||||
|
UID = reader.GetInt32(0),
|
||||||
|
UUID = reader.GetString(1),
|
||||||
|
Type = reader.GetInt32(2),
|
||||||
|
Email = reader.IsDBNull(3) ? null : reader.GetString(3),
|
||||||
|
Username = reader.GetString(4),
|
||||||
|
DisplayName = reader.IsDBNull(5) ? null : reader.GetString(5),
|
||||||
|
Password = reader.GetString(6),
|
||||||
|
Created = reader.GetInt64(7),
|
||||||
|
LastLogin = reader.GetInt64(8)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<User> UserFromUUID(string uuid)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(uuid))
|
||||||
|
return null;
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM users WHERE UUID = @UUID;";
|
||||||
|
command.Parameters.AddWithValue("@UUID", uuid);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
await reader.ReadAsync();
|
||||||
|
return new User
|
||||||
|
{
|
||||||
|
UID = reader.GetInt32(0),
|
||||||
|
UUID = reader.GetString(1),
|
||||||
|
Type = reader.GetInt32(2),
|
||||||
|
Email = reader.IsDBNull(3) ? null : reader.GetString(3),
|
||||||
|
Username = reader.GetString(4),
|
||||||
|
DisplayName = reader.IsDBNull(5) ? null : reader.GetString(5),
|
||||||
|
Password = reader.GetString(6),
|
||||||
|
Created = reader.GetInt64(7),
|
||||||
|
LastLogin = reader.GetInt64(8)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<User> UserFromEmail(string email)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(email))
|
||||||
|
return null;
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM users WHERE Email = @Email;";
|
||||||
|
command.Parameters.AddWithValue("@Email", email);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
await reader.ReadAsync();
|
||||||
|
return new User
|
||||||
|
{
|
||||||
|
UID = reader.GetInt32(0),
|
||||||
|
UUID = reader.GetString(1),
|
||||||
|
Type = reader.GetInt32(2),
|
||||||
|
Email = reader.IsDBNull(3) ? null : reader.GetString(3),
|
||||||
|
Username = reader.GetString(4),
|
||||||
|
DisplayName = reader.IsDBNull(5) ? null : reader.GetString(5),
|
||||||
|
Password = reader.GetString(6),
|
||||||
|
Created = reader.GetInt64(7),
|
||||||
|
LastLogin = reader.GetInt64(8)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<User> UserFromToken(string token)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(token))
|
||||||
|
return null;
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM sessions WHERE Token = @Token;";
|
||||||
|
command.Parameters.AddWithValue("@Token", token);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
await reader.ReadAsync();
|
||||||
|
return await UserFromUUID(reader.GetString(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<int> EnumeratePastes()
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT COUNT(*) FROM pastes;";
|
||||||
|
return Convert.ToInt32(await command.ExecuteScalarAsync());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static async Task<Paste> GetPasteFromID(string id)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(id))
|
||||||
|
return null;
|
||||||
|
using (var connection = new SqliteConnection(Program.MainDatabaseConnection))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
using (var command = connection.CreateCommand())
|
||||||
|
{
|
||||||
|
command.CommandText = "SELECT * FROM pastes WHERE ID = @ID;";
|
||||||
|
command.Parameters.AddWithValue("@ID", id);
|
||||||
|
using (var reader = await command.ExecuteReaderAsync())
|
||||||
|
{
|
||||||
|
if (!reader.HasRows)
|
||||||
|
return null;
|
||||||
|
await reader.ReadAsync();
|
||||||
|
return new Paste
|
||||||
|
{
|
||||||
|
UUID = reader.GetString(1),
|
||||||
|
ID = reader.GetString(2),
|
||||||
|
Visibility = reader.GetInt32(3),
|
||||||
|
Title = reader.IsDBNull(4) ? null : reader.GetString(4),
|
||||||
|
AuthorUUID = reader.GetString(5),
|
||||||
|
FilePath = reader.GetString(6),
|
||||||
|
Created = reader.GetInt64(7),
|
||||||
|
Edited = reader.GetInt64(8),
|
||||||
|
Size = reader.GetInt32(9),
|
||||||
|
TrueSize = reader.GetInt32(10),
|
||||||
|
Views = reader.GetInt32(11)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class Logging
|
||||||
|
{
|
||||||
|
public Logging()
|
||||||
|
{
|
||||||
|
if (!Directory.Exists("logs"))
|
||||||
|
Directory.CreateDirectory("logs");
|
||||||
|
}
|
||||||
|
public void LogInfo(string message)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[{DateTime.Now} INFO] {message}");
|
||||||
|
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} INFO] {message}\n");
|
||||||
|
}
|
||||||
|
public void LogWarning(string message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.WriteLine($"[{DateTime.Now} WARNING] {message}");
|
||||||
|
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} WARNING] {message}\n");
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
public void LogError(string message)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine($"[{DateTime.Now} ERROR] {message}");
|
||||||
|
File.AppendAllText($"logs/{DateTime.Now.ToString("yyyy-MM-dd")}.log", $"[{DateTime.Now} ERROR] {message}\n");
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// https://dusted.codes/dotenv-in-dotnet
|
||||||
|
/// </summary>
|
||||||
|
public static class DotEnv
|
||||||
|
{
|
||||||
|
public static void Load(string filePath)
|
||||||
|
{
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var line in File.ReadAllLines(filePath))
|
||||||
|
{
|
||||||
|
var parts = line.Split(
|
||||||
|
'=',
|
||||||
|
StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
if (parts.Length != 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Environment.SetEnvironmentVariable(parts[0], parts[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class Compression
|
||||||
|
{
|
||||||
|
// Taken from old branch
|
||||||
|
public static async Task<byte[]> CompressString(string text)
|
||||||
|
{
|
||||||
|
byte[] compressedBytes;
|
||||||
|
using (var ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (var cs = new GZipStream(ms, CompressionMode.Compress))
|
||||||
|
{
|
||||||
|
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
|
||||||
|
await cs.WriteAsync(inputBytes, 0, inputBytes.Length);
|
||||||
|
}
|
||||||
|
compressedBytes = ms.ToArray();
|
||||||
|
}
|
||||||
|
return compressedBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<string> DecompressByteArrayToString(byte[] compressedBytes)
|
||||||
|
{
|
||||||
|
string decompressedText;
|
||||||
|
using (var ms = new MemoryStream(compressedBytes))
|
||||||
|
{
|
||||||
|
using (var cs = new GZipStream(ms, CompressionMode.Decompress))
|
||||||
|
{
|
||||||
|
using (var sr = new StreamReader(cs))
|
||||||
|
{
|
||||||
|
decompressedText = await sr.ReadToEndAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decompressedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<byte[]> DecompressByteArray(byte[] compressedBytes)
|
||||||
|
{
|
||||||
|
byte[] decompressedBytes;
|
||||||
|
using (var ms = new MemoryStream(compressedBytes))
|
||||||
|
{
|
||||||
|
using (var cs = new GZipStream(ms, CompressionMode.Decompress))
|
||||||
|
{
|
||||||
|
using (var msDecompressed = new MemoryStream())
|
||||||
|
{
|
||||||
|
await cs.CopyToAsync(msDecompressed);
|
||||||
|
decompressedBytes = msDecompressed.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return decompressedBytes;
|
||||||
|
}
|
||||||
|
public static async Task<bool> ShouldCompress(string text, int sizeThreshold = 512, double ratioThreshold = 0.8)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
|
||||||
|
if (inputBytes.Length < sizeThreshold)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
byte[] compressedBytes = await CompressString(text).ConfigureAwait(false);
|
||||||
|
double compressionRatio = (double)compressedBytes.Length / inputBytes.Length;
|
||||||
|
|
||||||
|
return compressionRatio < ratioThreshold;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsCompressed(byte[] data)
|
||||||
|
{
|
||||||
|
return data.Length >= 2 && data[0] == 0x1F && data[1] == 0x8B;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
public int UID { get; set; }
|
||||||
|
public string UUID { get; set; }
|
||||||
|
public int Type { get; set; }
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string DisplayName { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
public long Created { get; set; }
|
||||||
|
public long LastLogin { get; set; }
|
||||||
|
}
|
||||||
|
class Paste
|
||||||
|
{
|
||||||
|
public string UUID { get; set; }
|
||||||
|
public string ID { get; set; }
|
||||||
|
public int Visibility { get; set; }
|
||||||
|
public string Title { get; set; }
|
||||||
|
public string AuthorUUID { get; set; }
|
||||||
|
public string FilePath { get; set; }
|
||||||
|
public long Created { get; set; }
|
||||||
|
public long Edited { get; set; }
|
||||||
|
public int Size { get; set; }
|
||||||
|
public int TrueSize { get; set; }
|
||||||
|
public int Views { get; set; }
|
||||||
|
}
|
||||||
|
class Session
|
||||||
|
{
|
||||||
|
public string UUID { get; set; }
|
||||||
|
public string UserUUID { get; set; }
|
||||||
|
public string Token { get; set; }
|
||||||
|
public long Created { get; set; }
|
||||||
|
public long Expirary { get; set; }
|
||||||
|
public string Ip { get; set; }
|
||||||
|
public string UserAgent { get; set; }
|
||||||
|
}
|
||||||
|
class RequestDetails
|
||||||
|
{
|
||||||
|
public string Ip { get; set; }
|
||||||
|
public string UserAgent { get; set; }
|
||||||
|
public string Token { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
{
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "http://localhost:5028"
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"applicationUrl": "https://localhost:7260;http://localhost:5028"
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Container (Dockerfile)": {
|
||||||
|
"commandName": "Docker",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||||
|
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||||
|
},
|
||||||
|
"publishAllPorts": true,
|
||||||
|
"useSSL": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:1241",
|
||||||
|
"sslPort": 44317
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<UserSecretsId>37bd710f-bebc-4da3-b1f2-d9b2885ff0a8</UserSecretsId>
|
||||||
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
|
<SignAssembly>False</SignAssembly>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="wwwroot\**" />
|
||||||
|
<Content Remove="wwwroot\**" />
|
||||||
|
<EmbeddedResource Remove="wwwroot\**" />
|
||||||
|
<None Remove="wwwroot\**" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
|
||||||
|
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.0" />
|
||||||
|
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*"
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.10.35122.118
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpbinV2.Server", "SharpbinV2.Server\SharpbinV2.Server.csproj", "{AEEAD9CC-3EB9-4576-98D0-51C04F1B8D21}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{AEEAD9CC-3EB9-4576-98D0-51C04F1B8D21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AEEAD9CC-3EB9-4576-98D0-51C04F1B8D21}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AEEAD9CC-3EB9-4576-98D0-51C04F1B8D21}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AEEAD9CC-3EB9-4576-98D0-51C04F1B8D21}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {481923D9-3518-41F9-9878-65AC793960AB}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
||||||
Reference in New Issue
Block a user