diff --git a/routes/etyd.js b/routes/etyd.js index 1d8235c..34fc166 100644 --- a/routes/etyd.js +++ b/routes/etyd.js @@ -10,7 +10,12 @@ app.get("/etyd*", (rreq,rres) => { rres.sendStatus(404) } else { dbRes.json().then(dbRes => { - rres.redirect(dbRes.content.url) + try { + rres.redirect(dbRes.content.url) // Node will crash if the Database entry is malformed + } catch (responseError) { + rres.sendStatus(500) + console.log(`${rres.get("cf-connecting-ip")} GET ${rreq.path} returned 500: ${responseError}`) + } }) } }).catch(fetchError => { diff --git a/routes/ip.js b/routes/ip.js index d955900..cb24c65 100644 --- a/routes/ip.js +++ b/routes/ip.js @@ -2,12 +2,16 @@ const { app } = require("../index.js") app.get("/ip", (rreq,rres) => { let jsonResponse = { - "IP": rreq.get("cf-connecting-ip") || "you_did", - "Country": rreq.get("cf-ipcountry") || "not_connect", - "CfRay": rreq.get("cf-ray") || "via_cloudflare" + "IP": rreq.get("cf-connecting-ip") || rreq.ip, + "Country": rreq.get("cf-ipcountry") || "not_cloudflare", + "CfRay": rreq.get("cf-ray") || "not_cloudflare" } - rres.send(jsonResponse); + rres.send(jsonResponse) +}) + +app.get("/headers", (rreq,rres) => { + rres.send(rreq.headers) }) module.exports = {app} \ No newline at end of file diff --git a/routes/mailjet.js b/routes/mailjet.js index bd2c48b..e53f3f1 100644 --- a/routes/mailjet.js +++ b/routes/mailjet.js @@ -14,42 +14,50 @@ app.post("/sendemail", (rreq,rres) => { } else { if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) { - let message = { - "Messages": [ - { - "From": { - "Email": globalConfig.mailjet.senderAddress, - "Name": globalConfig.mailjet.senderName, - }, - "To": [ - { - "Email": rreq.body.recipient.emailAddr, - "Name": rreq.body.recipient.emailName, - } - ], - - "Subject": rreq.body.message.subject, - "TextPart": rreq.body.message.content, + // 2024-05-11: Turbo bodge check to make sure request JSON is valid, probably wont work but whatever + if (rreq.body == undefined || rreq.body.recipient == undefined) { + console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 400 KEY:${rreq.get("Authorization").split("_")[1]}`) + rres.sendStatus(400) + } else { + + let message = { + "Messages": [ + { + "From": { + "Email": globalConfig.mailjet.senderAddress, + "Name": globalConfig.mailjet.senderName, + }, + "To": [ + { + "Email": rreq.body.recipient, + } + ], + + "Subject": rreq.body.subject || "Request did not include a subject.", + "TextPart": rreq.body.message || "Request did not include a message.", + } + ] + } + + fetch("https://api.mailjet.com/v3.1/send", { + method: "POST", + headers: { + "Authorization": `Basic ${btoa(globalConfig.mailjet.apiKey)}`, + "Content-Type": "application/json" + }, + body: JSON.stringify(message) + }).then(fetchRes => { + if (fetchRes.status == 200) { + console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 200 KEY:${rreq.get("Authorization").split("_")[1]}`) + rres.sendStatus(200) + } else { + console.log(`Mailjet Fetch returned result other than OK: ${fetchRes.status} ${fetchRes.statusText}`) + rres.sendStatus(500) } - ] + }) } - fetch("https://api.mailjet.com/v3.1/send", { - method: "POST", - headers: { - "Authorization": `Basic ${btoa(globalConfig.mailjet.apiKey)}`, - "Content-Type": "application/json" - }, - body: JSON.stringify(message) - }).then(fetchRes => { - if (fetchRes.status == 200) { - console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 200 KEY:${rreq.get("Authorization")}`) - rres.sendStatus(200) - } else { - console.log(`Mailjet Fetch returned result other than OK: ${fetchRes.status} ${fetchRes.statusText}`) - rres.sendStatus(500) - } - }) + } else { console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) // log ip of unauthorized requests