Add frontend files, improve backend.

This commit is contained in:
whoswhip
2025-01-17 21:17:37 -05:00
parent d0cf36108f
commit 01a637f5dd
13 changed files with 699 additions and 11 deletions
+22
View File
@@ -0,0 +1,22 @@
const errors = {
"400": "You've made a bad request.",
"401": "You are unauthorized to access this page.",
"403": "Forbidden! You don't have permission.",
"404": "Page not found.",
"500": "Internal server error.",
};
document.addEventListener("DOMContentLoaded", function () {
var queries = new URLSearchParams(window.location.search);
var error = queries.get("error");
var message = queries.get("message");
var errorElement = document.getElementById("error-header");
var messageElement = document.getElementById("error-message");
if (error != null) {
errorElement.innerText = errors[error];
}
if (message != null) {
messageElement.innerText = message;
}
});
+79
View File
@@ -0,0 +1,79 @@
document.addEventListener("DOMContentLoaded", async function () {
var visibility = document.getElementById("visibility");
visibility.value = "0";
});
document.getElementById("create-form").addEventListener("submit", async function (event) {
event.preventDefault();
var title = document.getElementById("title").value;
var content = document.getElementById("content").value;
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password").value;
var syntax = document.getElementById("syntax").value;
if (visibility === "2" && password !== "" && password.length >= 6) {
content = await encryptAES(content, password);
console.log(content);
}
else if (visibility === "2") {
alert("Password must be at least 6 characters long");
return;
}
fetch(`/api/pastes/create?title=${title}&visibility=${visibility}&syntax=${syntax}`, {
method: "POST",
body: content
})
.then(response => response.json())
.then(data => {
const paste = data.paste;
window.location.href = `/${paste.id}`;
});
});
function changeVisibility() {
var visibility = document.getElementById("visibility").value;
var password = document.getElementById("password");
if (visibility === "2") {
password.style.display = "block";
} else {
password.style.display = "none";
}
}
async function encryptAES(content, password) {
const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
const key = await window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: enc.encode("salt"),
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["encrypt"]
);
const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv
},
key,
enc.encode(content)
);
const encryptedArray = new Uint8Array(encryptedContent);
const encryptedString = btoa(String.fromCharCode(...iv) + String.fromCharCode(...encryptedArray));
return encryptedString;
}
+9
View File
@@ -0,0 +1,9 @@
function AddToDom() {
var nav = document.createElement("nav");
nav.innerHTML = "<a href='index.html'>Home</a> | <a href='about.html'>About</a> | <a href='contact.html'>Contact</a>";
document.body.appendChild(nav);
console.log("Added nav to dom");
}
document.addEventListener("DOMContentLoaded", function () {
AddToDom();
});
+127
View File
@@ -0,0 +1,127 @@
const visibility = [{ id: 0, name: "Public" }, { id: 1, name: "Unlisted" }, { id: 2, name: "Private" }];
document.addEventListener("DOMContentLoaded", async function () {
const id = window.location.pathname.split("/")[1];
const content = await (await fetch(`/api/pastes/${id}`)).text();
const info = await (await fetch(`/api/pastes/${id}/info`)).json();
const paste = info.paste;
const blur = document.createElement("div");
blur.id = "blur";
blur.dataset.decrypted = "false";
document.body.appendChild(blur);
const blur2 = document.getElementById("blur");
if (paste.visibility === 2) {
const password = prompt("Enter the password");
try {
const decrypted = await decryptAES(content, password);
blur2.dataset.decrypted = "true";
addContent(decrypted, paste.syntax);
} catch (error) {
alert("Failed to decrypt, invalid password, or invalid data.");
}
} else {
blur2.dataset.decrypted = "true";
addContent(content, paste.syntax);
}
document.title = `${paste.title} - Sharpbin`;
addInfo(paste);
});
function addInfo(paste) {
document.getElementById("paste-title").innerText = paste.title;
document.getElementById("paste-date").innerText = convertUnixToLocal(paste.created);
document.getElementById("paste-syntax").innerText = paste.syntax === "none" ? "" : paste.syntax || "";
document.getElementById("copy-button").addEventListener("click", function () {
navigator.clipboard.writeText(window.location.href);
});
document.getElementById("download-button").addEventListener("click", function () {
if (paste.visibility === 2) {
const content = document.getElementById("paste-content").innerText;
const blob = new Blob([content], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${paste.title}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
else {
const blob = new Blob([document.getElementById("paste-content").innerText], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `${paste.title}.txt`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
});
document.getElementById("raw-button").addEventListener("click", function () {
window.location.href = `/raw/${paste.id}`;
});
}
function addContent(content, syntax) {
if (syntax === "none" || syntax === "" || syntax === null) {
document.getElementById("paste-content").innerText = content;
} else {
var code = document.createElement("code");
code.innerText = content;
var pre = document.getElementById("paste-content");
pre.innerText = "";
pre.appendChild(code);
pre.removeAttribute("id");
code.id = "paste-content";
code.classList.add("language-" + syntax);
hljs.highlightElement(code);
}
}
async function decryptAES(content, password) {
const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey(
"raw",
enc.encode(password),
{ name: "PBKDF2" },
false,
["deriveKey"]
);
const key = await window.crypto.subtle.deriveKey(
{
name: "PBKDF2",
salt: enc.encode("salt"),
iterations: 100000,
hash: "SHA-256"
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
false,
["decrypt"]
);
const rawData = atob(content);
const rawDataArray = Uint8Array.from(rawData, c => c.charCodeAt(0));
const iv = rawDataArray.slice(0, 12);
const ciphertext = rawDataArray.slice(12);
const decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: iv
},
key,
ciphertext
);
return new TextDecoder().decode(decrypted);
}
function convertUnixToLocal(unix) {
return new Date(unix * 1000).toLocaleString();
}