From 29ef51f0452ba4e315d8449fe72669092d3582fe Mon Sep 17 00:00:00 2001 From: Enstrayed <48845980+Enstrayed@users.noreply.github.com> Date: Sat, 10 Feb 2024 14:38:58 -0800 Subject: [PATCH] re-add /sendemail and improve cider --- config.example.json | 9 ++-- routes/cider.js | 10 +++- routes/etyd.js | 122 ++++++++++++++++++++++---------------------- routes/mailjet.js | 60 ++++++++++++++++++++++ 4 files changed, 134 insertions(+), 67 deletions(-) create mode 100644 routes/mailjet.js diff --git a/config.example.json b/config.example.json index caddf19..dc0c820 100644 --- a/config.example.json +++ b/config.example.json @@ -13,12 +13,13 @@ "authKeyInDb": "apiAuthKeys.cider" }, - "azure": { - "connectionString": "", + "mailjet": { + "apiKey": "", "senderAddress": "apinotifications@enstrayed.com", + "senderName": "API Notifications", - "authKeyInDb": "apiAuthKeys.azure", - "usageKeyPrefix": "apiUsage.azure." + "authKeyInDb": "apiAuthKeys.mailjet", + "usageKeyPrefix": "apiUsage.mailjet." }, "etyd": { diff --git a/routes/cider.js b/routes/cider.js index a58cab3..69853fd 100644 --- a/routes/cider.js +++ b/routes/cider.js @@ -19,9 +19,15 @@ app.get("/cider", (rreq,rres) => { // GET current listening from target "songName": res.info.name, "artistName": res.info.artistName, "albumName": res.info.albumName, - "artworkUrl": res.info.artwork.url, - "songLinkUrl": res.info.url.songLink + "songLinkUrl": res.info.url.songLink, + "endtimeEpochInMs": res.info.endTime }; + + // Formats info.artwork.url from upstream Cider Endpoint + let workingArtworkUrl = res.info.artwork.url + workingArtworkUrl = workingArtworkUrl.replace("{w}",res.info.artwork.width) + workingArtworkUrl = workingArtworkUrl.replace("{h}",res.info.artwork.height) + currentListening.artworkUrl = workingArtworkUrl rres.set("Access-Control-Allow-Origin","*") rres.send(currentListening) // send freshly cached json diff --git a/routes/etyd.js b/routes/etyd.js index d8d0765..5ace98a 100644 --- a/routes/etyd.js +++ b/routes/etyd.js @@ -17,83 +17,83 @@ app.options("/etydwrite", (rreq,rres) => { rres.sendStatus(204) }) -// app.post("/etydwrite", (rreq,rres) => { -// db.get(globalConfig.etyd.authKeyInDb).then(dbres => { +app.post("/etydwrite", (rreq,rres) => { + db.get(globalConfig.etyd.authKeyInDb).then(dbres => { -// if (dbres == null) { // If authkey key doesnt exist in redis then error out -// console.log("ERROR: Configured key containing etyd authkeys is null") -// rres.sendStatus(500) -// } else { // if it does exist -// let validKeys = dbres.split(",") // split the string into an array -// if (validKeys.includes(rreq.get("Authorization"))) { // check if authorization header key exists in that array + if (dbres == null) { // If authkey key doesnt exist in redis then error out + console.log("ERROR: Configured key containing etyd authkeys is null") + rres.sendStatus(500) + } else { // if it does exist + let validKeys = dbres.split(",") // split the string into an array + if (validKeys.includes(rreq.get("Authorization"))) { // check if authorization header key exists in that array -// console.log(rreq.body) + console.log(rreq.body) -// switch(rreq.body.action) { -// case "set": // Write to db + switch(rreq.body.action) { + case "set": // Write to db -// if (rreq.body.random == true) { + if (rreq.body.random == true) { -// let workingTarget = makeRandomHex() + let workingTarget = makeRandomHex() -// db.get(`/${workingTarget}`).then(dbres => { -// if (dbres != null) { -// let workingTarget = makeRandomHex() + db.get(`/${workingTarget}`).then(dbres => { + if (dbres != null) { + let workingTarget = makeRandomHex() -// db.get(`/${workingTarget}`).then(dbres => { -// if (dbres != null) { -// // well fuck -// rres.sendStatus(409) + db.get(`/${workingTarget}`).then(dbres => { + if (dbres != null) { + // well fuck + rres.sendStatus(409) -// } -// }) + } + }) -// } else { -// db.set(`/${workingTarget}`,rreq.body.value) -// rres.send(`https://etyd.cc/${workingTarget}`) -// } -// }) + } else { + db.set(`/${workingTarget}`,rreq.body.value) + rres.send(`https://etyd.cc/${workingTarget}`) + } + }) -// } else { + } else { -// db.get(rreq.body.target).then(dbres => { // check if key already exists -// if (dbres != null) { // if it does then send 409 conflict -// console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION set returned 409 KEY:${rreq.get("Authorization")}`) -// rres.sendStatus(409) -// } else { -// db.set(`/${rreq.body.target}`,rreq.body.value) -// rres.send(`https://etyd.cc/${rreq.body.target}`) -// } -// }) + db.get(rreq.body.target).then(dbres => { // check if key already exists + if (dbres != null) { // if it does then send 409 conflict + console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION set returned 409 KEY:${rreq.get("Authorization")}`) + rres.sendStatus(409) + } else { + db.set(`/${rreq.body.target}`,rreq.body.value) + rres.send(`https://etyd.cc/${rreq.body.target}`) + } + }) -// } -// break; + } + break; -// case "delete": -// db.get(`/${rreq.body.target}`).then(dbres => { -// if (dbres == null) { //if key doesnt exist then log and return 400 -// console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION delete returned 400 KEY:${rreq.get("Authorization")}`) -// rres.sendStatus(400) -// } else { -// db.del(`/${rreq.body.target}`) -// rres.sendStatus(200) -// } -// }) -// break; + case "delete": + db.get(`/${rreq.body.target}`).then(dbres => { + if (dbres == null) { //if key doesnt exist then log and return 400 + console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite ACTION delete returned 400 KEY:${rreq.get("Authorization")}`) + rres.sendStatus(400) + } else { + db.del(`/${rreq.body.target}`) + rres.sendStatus(200) + } + }) + break; -// default: -// rres.sendStatus(400) // request json didnt include a valid action -// break; -// } + default: + rres.sendStatus(400) // request json didnt include a valid action + break; + } -// } else { // if it doesnt then its a unauthorized request -// console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite returned 401`) -// rres.sendStatus(401) -// } -// } -// }) -// }) + } else { // if it doesnt then its a unauthorized request + console.log(`${rreq.get("cf-connecting-ip")} POST /etydwrite returned 401`) + rres.sendStatus(401) + } + } + }) +}) module.exports = {app} // export routes to be imported by index for execution \ No newline at end of file diff --git a/routes/mailjet.js b/routes/mailjet.js new file mode 100644 index 0000000..1fdfccf --- /dev/null +++ b/routes/mailjet.js @@ -0,0 +1,60 @@ +const { app, db, globalConfig } = require("../index.js") // Get globals from index + +app.post("/sendemail", (rreq,rres) => { + + db.get(globalConfig.mailjet.authKeyInDb).then(dbres => { + if (dbres == null) { + console.log("ERROR: Configured key containing mailjet authkeys is null") + rres.sendStatus(500) + } else { + let validKeys = dbres.split(',') + if (validKeys.includes(rreq.get("Authorization"))) { + + 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, + } + ] + } + + 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) { + db.incr(`${globalConfig.mailjet.usageKeyPrefix}${rreq.get("Authorization")}`) + 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`) + rres.sendStatus(401) + } + } + }) + +}) + +module.exports = {app} // export routes to be imported by index for execution \ No newline at end of file