-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
149 lines (139 loc) · 3.95 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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"
const options = {
auth: {
api_key: config.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: `
####### ACCESS CODE (OTP) ${OTP} #######
####### DO NOT SHARE THIS CODE! #######
(this message is automated)`,
html: `
####### ACCESS CODE (OTP) ${OTP} #######
####### DO NOT SHARE THIS CODE! #######
(this message is automated)
`,
}
if (config.verification == true) {
mailerAgent.sendMail(email, (err, res) => {
if (err) {
console.log(err)
}
console.log(res)
})
}
}
function getNewCode() {
var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1)
if (seq == "0") {
getNewCode()
}
return seq
}
const PORT = process.env.PORT || 3000
const bareServer = createBareServer("/bare/", {
logErrors: false,
localAddress: undefined,
})
const serve = serveStatic(
join(dirname(fileURLToPath(import.meta.url)), "static/"),
{
fallthrough: false,
maxAge: 5 * 60 * 1000,
}
)
const server = http.createServer()
server.on("request", (request, response) => {
try {
if (bareServer.shouldRoute(request)) {
bareServer.routeRequest(request, response)
} else {
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`)
})
fs.readFile("./memory.txt", "utf8", (err, data) => {
if (err) {
console.error(err)
return
}
console.log(data)
sendVerificationEmail("10", config.email, data)
let buff = new Buffer(data)
base64data = buff.toString("base64")
console.log("302")
response.writeHead(302, {
location: "/unv.html?c=" + base64data,
})
response.end()
})
} else if (url.startsWith("/verification")) {
var body
if (config.verification == true) {
const body = "true"
response.writeHead(200, {
"Content-Length": Buffer.byteLength(body),
"Content-Type": "text/plain",
})
response.end(body)
} else {
const body = "false"
response.writeHead(200, {
"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)
})
}
}
} catch (e) {
response.writeHead(500, "Internal Server Error", {
"Content-Type": "text/plain",
})
response.end(e.stack)
}
})
server.on("upgrade", (req, socket, head) => {
if (bareServer.shouldRoute(req)) {
bareServer.routeUpgrade(req, socket, head)
} else {
socket.end()
}
})
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.log(`Server running at http://localhost:${PORT}/.`)