diff --git a/.gitignore b/.gitignore index 2192677..d23d46a 100644 --- a/.gitignore +++ b/.gitignore @@ -369,4 +369,5 @@ FodyWeavers.xsd SharpbinV2.Server/*.db SharpbinV2.Server/logs SharpbinV2.Server/pastes +SharpbinV2.Server/.env diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs index 4435236..aa12c3f 100644 --- a/SharpbinV2.Server/Program.cs +++ b/SharpbinV2.Server/Program.cs @@ -9,6 +9,7 @@ using System.Text; using Bcrypt = BCrypt.Net.BCrypt; using System.IO.Compression; using System.Threading.Tasks; +using System.Security.Cryptography; namespace SharpbinV2.Server { @@ -18,15 +19,15 @@ namespace SharpbinV2.Server public static long MaxFileSize = 5000000; // 5MB public static string[] ValidSyntaxLanguages = { - "None", - "AutoHotkey", - "AutoIt", + "none", + "autoHotkey", + "autoIt", "bash", "c", "cpp", "csharp", "css", - "Dart", + "dart", "html", "java", "javascript", @@ -43,6 +44,7 @@ namespace SharpbinV2.Server "toml", "xml" }; + public static string HMACSecret = "thisneedstobechanged"; static async Task Main(string[] args) { @@ -52,6 +54,16 @@ namespace SharpbinV2.Server 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; + HMACSecret = Environment.GetEnvironmentVariable("HMAC_SECRET") ?? HMACSecret; + if (HMACSecret == "thisneedstobechanged") + logger.LogWarning("HMAC_SECRET is still set to default. Please change this in your .env file."); + if (HMACSecret.Length < 32 && HMACSecret != "thisneedstobechanged") + logger.LogWarning("HMAC_SECRET is less than 32 characters. Please change this in your .env file."); + if (HMACSecret.Length < 6 || HMACSecret.Length <= 0) + { + logger.LogError("HMAC_SECRET is less than 6 characters. Please change this in your .env file."); + Environment.Exit(1); + } builder.WebHost.ConfigureKestrel(options => { @@ -90,7 +102,7 @@ namespace SharpbinV2.Server app.UseResponseCompression(); #region Static Endpoints - app.MapGet("/", async (HttpContext context) => + app.MapGet("/", (HttpContext context) => { context.Response.Redirect("/index.html"); }); @@ -201,7 +213,7 @@ namespace SharpbinV2.Server await context.Response.SendFileAsync(paste.FilePath); } }); - app.MapGet("/paste.html", async (HttpContext context) => + app.MapGet("/paste.html", (HttpContext context) => { context.Response.Redirect("/"); }); @@ -280,7 +292,7 @@ namespace SharpbinV2.Server 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("@Ip", HMAC256HASH(requestdetails.Ip)); command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent); await command.ExecuteNonQueryAsync(); } @@ -369,7 +381,7 @@ namespace SharpbinV2.Server 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("@Ip", HMAC256HASH(requestdetails.Ip)); command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent); await command.ExecuteNonQueryAsync(); } @@ -395,6 +407,35 @@ namespace SharpbinV2.Server logger.LogInfo($"User {user.UUID} logged in."); return; }); + app.MapGet("/api/accounts/authorized", async (HttpContext context) => + { + var requestdetails = GetRequestDetails(context); + if (string.IsNullOrEmpty(requestdetails.Token)) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Not authorized." }); + return; + } + var user = await Database.UserFromToken(requestdetails.Token); + if (user == null) + { + context.Response.StatusCode = 400; + await context.Response.WriteAsJsonAsync(new { success = false, message = "Not authorized." }); + return; + } + var cleanuser = new User + { + UID = user.UID, + UUID = user.UUID, + Username = user.Username, + DisplayName = user.DisplayName, + Created = user.Created, + LastLogin = user.LastLogin + }; + context.Response.StatusCode = 200; + await context.Response.WriteAsJsonAsync(new { success = true, user = cleanuser }); + return; + }); app.MapPost("/api/pastes/create", async (HttpContext context) => { @@ -431,7 +472,7 @@ namespace SharpbinV2.Server paste.Views = 0; paste.Syntax = queries.ContainsKey("syntax") ? queries["syntax"].ToString() : null; - if (!string.IsNullOrEmpty(paste.Syntax) && !ValidSyntaxLanguages.Contains(paste.Syntax)) + if (!string.IsNullOrEmpty(paste.Syntax) && !ValidSyntaxLanguages.Contains(paste.Syntax.ToLower())) { context.Response.StatusCode = 400; await context.Response.WriteAsJsonAsync(new { success = false, message = "Invalid syntax language." }); @@ -615,6 +656,14 @@ namespace SharpbinV2.Server } return $"{len:0.##} {sizes[order]}"; } + public static string HMAC256HASH(string data) + { + using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(HMACSecret))) + { + byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(data)); + return Convert.ToBase64String(hashBytes); + } + } #endregion static async Task Initialize() { @@ -868,7 +917,7 @@ namespace SharpbinV2.Server using (var command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM views WHERE Ip = @Ip AND UserAgent = @UserAgent;"; - command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(10))); + command.Parameters.AddWithValue("@Ip", Program.HMAC256HASH(details.Ip)); command.Parameters.AddWithValue("@UserAgent", details.UserAgent); using (var reader = await command.ExecuteReaderAsync()) { @@ -906,7 +955,7 @@ namespace SharpbinV2.Server command.CommandText = "INSERT INTO views (UserUUID, PasteUUID, Ip, UserAgent, Created) VALUES (@UserUUID, @PasteUUID, @Ip, @UserAgent, @Created);"; command.Parameters.AddWithValue("@UserUUID", _user.UUID); command.Parameters.AddWithValue("@PasteUUID", paste.UUID); - command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(10))); + command.Parameters.AddWithValue("@Ip", Program.HMAC256HASH(details.Ip)); command.Parameters.AddWithValue("@UserAgent", details.UserAgent); command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); await command.ExecuteNonQueryAsync(); diff --git a/SharpbinV2.Server/wwwroot/css/navbar_raw.css b/SharpbinV2.Server/wwwroot/css/navbar_raw.css new file mode 100644 index 0000000..3940773 --- /dev/null +++ b/SharpbinV2.Server/wwwroot/css/navbar_raw.css @@ -0,0 +1,102 @@ +.nav-bar { + display: flex; + justify-content: space-between; + align-items: center; + padding: 1rem 2rem; + background-color: var(--neutral-900); + color: #fff; + position: fixed; + width: 100%; + top: 0; + left: 0; + right: 0; +} +.nav-link { + color: #fff; + text-decoration: none; + font-size: 1.2rem; + transition: color 0.3s; +} +.nav-link:hover { + color: var(--neutral-300); +} + +.nav-links .nav-link { + width: auto; + padding: 0 1rem; + border-bottom: none; +} + +.nav-links .nav-link:last-child { + border-bottom: none; +} + +.hamburger { + display: none; + background: none; + border: none; + font-size: 2rem; + cursor: pointer; + margin: 0 auto; +} + +.nav-links { + display: flex; + flex-direction: row; + position: static; + background-color: transparent; + padding: 0; + max-height: none; + transition: none; +} + +.nav-links.show { + display: flex; + flex-direction: column; + position: absolute; + top: 100%; + left: 0; + right: 0; + background-color: var(--neutral-900); + padding: 1rem 2rem; + max-height: 500px; + transition: max-height 0.3s ease-out; + overflow: hidden; +} + +@media (max-width: 800px) { + .nav-links { + display: none; + flex-direction: column; + width: 100%; + position: absolute; + top: 100%; + left: 0; + right: 0; + border-top: 1px solid var(--neutral-800); + background-color: var(--neutral-900); + padding: 1rem 2rem; + max-height: 0; + transition: max-height 0.3s ease-out; + overflow: hidden; + } + .nav-link{ + width: 100% !important; + padding: 1rem 0 !important; + text-align: center; + border-bottom: 1px solid var(--neutral-800) !important; + } + .nav-links.show { + display: flex; + max-height: 500px; + flex-direction: column; + align-items: center; + } + .hamburger { + display: block; + margin: 0 auto; + } + .nav-bar { + justify-content: center; + } +} \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/css/paste.css b/SharpbinV2.Server/wwwroot/css/paste.css index fd04d3f..a9211af 100644 --- a/SharpbinV2.Server/wwwroot/css/paste.css +++ b/SharpbinV2.Server/wwwroot/css/paste.css @@ -63,6 +63,7 @@ border-radius: 0px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; + display: block !important; } .paste-actions { @@ -103,7 +104,7 @@ button { } pre code.hljs { - display: block; + display: block !important; overflow-x: auto; padding: 1em } diff --git a/SharpbinV2.Server/wwwroot/css/style.css b/SharpbinV2.Server/wwwroot/css/style.css index 8a1f228..a8f04ef 100644 --- a/SharpbinV2.Server/wwwroot/css/style.css +++ b/SharpbinV2.Server/wwwroot/css/style.css @@ -125,6 +125,15 @@ select { font-size: 1rem; } +select:hover { + outline: none; + border: 1px solid var(--neutral-600); +} +select:focus { + outline: none; + border: 1px solid var(--neutral-600); +} + #syntax { padding-left: 14rem; } diff --git a/SharpbinV2.Server/wwwroot/error.html b/SharpbinV2.Server/wwwroot/error.html index 9c72989..6cafda4 100644 --- a/SharpbinV2.Server/wwwroot/error.html +++ b/SharpbinV2.Server/wwwroot/error.html @@ -20,4 +20,5 @@ + \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/index.html b/SharpbinV2.Server/wwwroot/index.html index c143330..410c92f 100644 --- a/SharpbinV2.Server/wwwroot/index.html +++ b/SharpbinV2.Server/wwwroot/index.html @@ -57,6 +57,7 @@ + \ No newline at end of file diff --git a/SharpbinV2.Server/wwwroot/js/navbar.js b/SharpbinV2.Server/wwwroot/js/navbar.js index 020d691..0af76e5 100644 --- a/SharpbinV2.Server/wwwroot/js/navbar.js +++ b/SharpbinV2.Server/wwwroot/js/navbar.js @@ -1,9 +1,22 @@ -function AddToDom() { - var nav = document.createElement("nav"); - nav.innerHTML = "Home | About | Contact"; - document.body.appendChild(nav); - console.log("Added nav to dom"); -} -document.addEventListener("DOMContentLoaded", function () { - AddToDom(); -}); \ No newline at end of file +const navbarHTML = ` + +`; + +document.addEventListener('DOMContentLoaded', () => { + document.body.insertAdjacentHTML('afterbegin', navbarHTML); +}); + +window.toggleMenu = function () { + const nav = document.querySelector('.nav-links'); + nav.classList.toggle('show'); +}; + diff --git a/SharpbinV2.Server/wwwroot/paste.html b/SharpbinV2.Server/wwwroot/paste.html index 063a300..3f9950c 100644 --- a/SharpbinV2.Server/wwwroot/paste.html +++ b/SharpbinV2.Server/wwwroot/paste.html @@ -33,10 +33,10 @@ -
Content
+
Loading...
- + \ No newline at end of file