diff --git a/app.js b/app.js index d278fac..9700db1 100644 --- a/app.js +++ b/app.js @@ -1,149 +1,175 @@ -import createBareServer from "@tomphttp/bare-server-node" -import http from "http" -import { fileURLToPath } from "url" -import { dirname, join } from "path" -import serveStatic from "serve-static" -import { createRequire } from "module" -const require = createRequire(import.meta.url) -const config = require("./deployment.config.json") -import fs from "fs" -var base64data -import sgTransport from "nodemailer-sendgrid-transport" -import nodemailer from "nodemailer" +import createBareServer from "@tomphttp/bare-server-node"; +import http from "http"; +import {createRequire} from "module"; +import {dirname, join} from "path"; +import serveStatic from "serve-static"; +import {fileURLToPath} from "url"; +const require = createRequire(import.meta.url); +const config = require("./deployment.config.json"); +import fs from "fs"; +var base64data; +import sgTransport from "nodemailer-sendgrid-transport"; +import nodemailer from "nodemailer"; +import fetch from "node-fetch"; const options = { - auth: { - api_key: config.api_key, + auth : { + api_key : config.sendgrid_options.api_key, }, -} -const mailerAgent = nodemailer.createTransport(sgTransport(options)) -function sendVerificationEmail(UUID, mailTo, OTP) { - const email = { - to: mailTo, - from: `${config.sendFromEmail}`, - subject: `NebulaWEB personal access code ${OTP}`, - text: ` +}; +const sendgridMailerAgent = nodemailer.createTransport(sgTransport(options)); +const smtpMailerAgent = nodemailer.createTransport(config.smtp_options); +function sendVerificationEmail(UUID, OTP) { + let email = { + to : "", + from : "", + subject : `NebulaWEB personal access code ${OTP}`, + text : ` ####### ACCESS CODE (OTP) ${OTP} ####### ####### DO NOT SHARE THIS CODE! ####### (this message is automated)`, - html: ` + html : ` ####### ACCESS CODE (OTP) ${OTP} ####### ####### DO NOT SHARE THIS CODE! ####### (this message is automated) `, - } - if (config.verification == true) { - mailerAgent.sendMail(email, (err, res) => { + }; + if (config.sendgrid_verification == true) { + email.to = config.sendgrid_options.to_email + email.from = config.sendgrid_options.sendFromEmail + sendgridMailerAgent.sendMail(email, (err, res) => { if (err) { - console.log(err) + console.log(err); } - console.log(res) - }) + console.log(res); + }); + } + if (config.smtp_verification == true) { + email.to = config.smtp_options.to_email + email.from = config.smtp_options.sendFromEmail + smtpMailerAgent.sendMail(email, (err, res) => { + if (err) { + console.log(err); + } + console.log(res); + }); + } + if (config.discord_verification == true) { + fetch(config.webhook_url, { + method : "POST", + headers : { + "Content-Type" : "application/json", + }, + body : JSON.stringify({ + content : `Your NebulaWEB access code is ${OTP}`, + }), + }); } } function getNewCode() { - var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1) + var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1); if (seq == "0") { - getNewCode() + getNewCode(); } - return seq + return seq; } -const PORT = process.env.PORT || 3000 +const PORT = process.env.PORT || 3000; const bareServer = createBareServer("/bare/", { - logErrors: false, - localAddress: undefined, -}) + logErrors : false, + localAddress : undefined, +}); -const serve = serveStatic( - join(dirname(fileURLToPath(import.meta.url)), "static/"), - { - fallthrough: false, - maxAge: 5 * 60 * 1000, - } -) +const serve = + serveStatic(join(dirname(fileURLToPath(import.meta.url)), "static/"), { + fallthrough : false, + maxAge : 5 * 60 * 1000, + }); -const server = http.createServer() +const server = http.createServer(); server.on("request", (request, response) => { try { if (bareServer.shouldRoute(request)) { - bareServer.routeRequest(request, response) + bareServer.routeRequest(request, response); } else { - let base64data - const url = request.url + let base64data; + const url = request.url; if (url.startsWith("/sendNewCode")) { - const OTP = getNewCode() - fs.writeFile("./memory.txt", OTP, function (err) { - if (err) return console.log(err) - console.log(`Wrote OTP code to temp file`) - }) + const OTP = getNewCode(); + fs.writeFile("./memory.txt", OTP, function(err) { + if (err) + return console.log(err); + console.log(`Wrote OTP code to temp file`); + }); fs.readFile("./memory.txt", "utf8", (err, data) => { if (err) { - console.error(err) - return + console.error(err); + return; } - console.log(data) + console.log(data); - sendVerificationEmail("10", config.email, data) - let buff = new Buffer(data) - base64data = buff.toString("base64") - console.log("302") + sendVerificationEmail("10", data); + let buff = new Buffer(data); + base64data = buff.toString("base64"); + console.log("302"); response.writeHead(302, { - location: "/unv.html?c=" + base64data, - }) - response.end() - }) + location : "/unv.html?c=" + base64data, + }); + response.end(); + }); } else if (url.startsWith("/verification")) { - var body - if (config.verification == true) { - const body = "true" + var body; + if (config.sendgrid_verification == true || + config.discord_verification == true || + config.smtp_verificaton == true) { + const body = "true"; response.writeHead(200, { - "Content-Length": Buffer.byteLength(body), - "Content-Type": "text/plain", - }) - response.end(body) + "Content-Length" : Buffer.byteLength(body), + "Content-Type" : "text/plain", + }); + response.end(body); } else { - const body = "false" + const body = "false"; response.writeHead(200, { - "Content-Length": Buffer.byteLength(body), - "Content-Type": "text/plain", - }) - response.end(body) + "Content-Length" : Buffer.byteLength(body), + "Content-Type" : "text/plain", + }); + response.end(body); } } else { serve(request, response, (err) => { response.writeHead(err?.statusCode || 500, null, { - "Content-Type": "text/plain", - }) - response.end(err?.stack) - }) + "Content-Type" : "text/plain", + }); + response.end(err?.stack); + }); } } } catch (e) { response.writeHead(500, "Internal Server Error", { - "Content-Type": "text/plain", - }) - response.end(e.stack) + "Content-Type" : "text/plain", + }); + response.end(e.stack); } -}) +}); server.on("upgrade", (req, socket, head) => { if (bareServer.shouldRoute(req)) { - bareServer.routeUpgrade(req, socket, head) + bareServer.routeUpgrade(req, socket, head); } else { - socket.end() + socket.end(); } -}) +}); -server.listen(PORT) +server.listen(PORT); if (process.env.UNSAFE_CONTINUE) process.on("uncaughtException", (err, origin) => { - console.error(`Critical error (${origin}):`) - console.error(err) - console.error("UNSAFELY CONTINUING EXECUTION") - console.error() - }) + console.error(`Critical error (${origin}):`); + console.error(err); + console.error("UNSAFELY CONTINUING EXECUTION"); + console.error(); + }); -console.log(`Server running at http://localhost:${PORT}/.`) +console.log(`Server running at http://localhost:${PORT}/.`); diff --git a/deployment.config.json b/deployment.config.json index 8b63ec9..cadced7 100644 --- a/deployment.config.json +++ b/deployment.config.json @@ -1,7 +1,23 @@ -{ - "verification": false, - "api_key":"YOUR_SENDGRID_API_KEY", - "sendFromEmail":"THE EMAIL THE CODES WILL BE SENT FROM (MUST BE VERIFIED IN SENDGRID)", - "type": "code", - "email": "YOUR_EMAIL_HERE" -} \ No newline at end of file +{ + "sendgrid_verification": false, + "sendgrid_options": { + "api_key": "YOUR_SENDGRID_API_KEY", + "sendFromEmail": "THE EMAIL THE CODES WILL BE SENT FROM (MUST BE VERIFIED IN SENDGRID)", + "to_email": "THE EMAIL YOU WANT THE CODES SENT TO" + }, + + "discord_verification": false, + "webhook_url": "YOUR DISCORD WEBHOOK URL", + + "smtp_verification": false, + "smtp_options": { + "to_email": "THE EMAIL YOU WANT THE CODES SENT TO", + "sendFromEmail": "THE EMAIL THE CODES ARE SENT FROM", + "host": "YOUR SMTP HOST", + "port": 465, + "auth": { + "user": "SMTP USER", + "pass": "YOUR PASSWORD" + } + } +} diff --git a/package-lock.json b/package-lock.json index 578acaa..7962edb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "AGPL-3.0-only", "dependencies": { "@tomphttp/bare-server-node": "^1.2.2", + "node-fetch": "^3.3.0", "nodemailer": "^6.8.0", "nodemailer-sendgrid-transport": "^0.2.0", "serve-static": "^1.15.0" @@ -385,6 +386,14 @@ "node": ">=0.10" } }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "engines": { + "node": ">= 12" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -668,6 +677,28 @@ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "optional": true }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, "node_modules/forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", @@ -689,6 +720,17 @@ "node": ">= 0.12" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/fresh": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", @@ -1088,6 +1130,41 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.0.tgz", + "integrity": "sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/nodemailer": { "version": "6.8.0", "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.8.0.tgz", @@ -1587,6 +1664,14 @@ "extsprintf": "^1.2.0" } }, + "node_modules/web-streams-polyfill": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", + "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", + "engines": { + "node": ">= 8" + } + }, "node_modules/webidl-conversions": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-2.0.1.tgz", @@ -1937,6 +2022,11 @@ "assert-plus": "^1.0.0" } }, + "data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==" + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -2156,6 +2246,15 @@ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "optional": true }, + "fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "requires": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + } + }, "forever-agent": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz", @@ -2171,6 +2270,14 @@ "mime-types": "^2.1.12" } }, + "formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "requires": { + "fetch-blob": "^3.1.2" + } + }, "fresh": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", @@ -2512,6 +2619,21 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" }, + "node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==" + }, + "node-fetch": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.0.tgz", + "integrity": "sha512-BKwRP/O0UvoMKp7GNdwPlObhYGB5DQqwhEDQlNKuoqwVYSxkSZCSbHjnFFmUEtwSKRPU4kNK8PbDYYitwaE3QA==", + "requires": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + } + }, "nodemailer": { "version": "6.8.0", "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.8.0.tgz", @@ -2898,6 +3020,11 @@ "extsprintf": "^1.2.0" } }, + "web-streams-polyfill": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", + "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==" + }, "webidl-conversions": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-2.0.1.tgz", diff --git a/package.json b/package.json index 0a8249a..c3dd619 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "license": "AGPL-3.0-only", "dependencies": { "@tomphttp/bare-server-node": "^1.2.2", + "node-fetch": "^3.3.0", "nodemailer": "^6.8.0", "nodemailer-sendgrid-transport": "^0.2.0", "serve-static": "^1.15.0"