move mailjet to couchdb + change cider

This commit is contained in:
Enstrayed
2024-03-27 22:45:05 -07:00
parent e3ccd4b3c7
commit 18b7bb431a
3 changed files with 19 additions and 18 deletions

View File

@@ -11,10 +11,9 @@
}, },
"cider": { "cider": {
"targetHost": "localhost", "targetHosts": ["localhost:10769"],
"targetPort": 10769,
"authKeyInDb": "apiAuthKeys.cider" "authKeysDoc": "cider"
}, },
"mailjet": { "mailjet": {
@@ -22,8 +21,7 @@
"senderAddress": "apinotifications@enstrayed.com", "senderAddress": "apinotifications@enstrayed.com",
"senderName": "API Notifications", "senderName": "API Notifications",
"authKeyInDb": "apiAuthKeys.mailjet", "authKeysDoc": "mailjet"
"usageKeyPrefix": "apiUsage.mailjet."
}, },
"etyd": { "etyd": {

View File

@@ -39,19 +39,19 @@ app.get("/cider", (rreq,rres) => { // GET current listening from target
app.post("/cider", (rreq,rres) => { // POST stop listening on cider target app.post("/cider", (rreq,rres) => { // POST stop listening on cider target
fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/cider`, { fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/${globalConfig.cider.authKeysDoc}`, {
headers: { headers: {
"Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}` "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
} }
}).then(dbRes => dbRes.json()).then(dbRes => { }).then(dbRes => dbRes.json()).then(dbRes => {
if (dbRes.status == 404) { // If document containing cider auth keys does not exist if (dbRes.status == 404) { // If document containing cider auth keys does not exist
console.log("ERROR: Could not find apiauthkeys/cider") console.log(`ERROR: Could not find apiauthkeys/${globalConfig.mailjet.authKeysDoc}`)
rres.sendStatus(500) // Refuse request rres.sendStatus(500) // Refuse request
} else { } else {
if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) { if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) {
fetch(`http://${globalConfig.cider.targetHost}:${globalConfig.cider.targetPort}/stop`).then(fres => { // send GET /stop to cider target fetch(`http://${globalConfig.cider.targetHosts[0]}/stop`).then(fres => { // send GET /stop to cider target
if (fres.status == 204) { if (fres.status == 204) {
console.log(`${rreq.get("cf-connecting-ip")} POST /cider returned 200 KEY:${rreq.get("Authorization")}`) console.log(`${rreq.get("cf-connecting-ip")} POST /cider returned 200 KEY:${rreq.get("Authorization")}`)
rres.sendStatus(200) // if that works then 200 rres.sendStatus(200) // if that works then 200
@@ -73,7 +73,7 @@ app.post("/cider", (rreq,rres) => { // POST stop listening on cider target
async function getCurrentListening() { // async function to actually get and return the json (this is just adapted from the original gist) async function getCurrentListening() { // async function to actually get and return the json (this is just adapted from the original gist)
timeSinceLastCiderQuery = Date.now(); // update last query time timeSinceLastCiderQuery = Date.now(); // update last query time
return await fetch(`http://${globalConfig.cider.targetHost}:${globalConfig.cider.targetPort}/currentPlayingSong`).then(res => res.json()).catch(err => { // fetch, format and return JSON return await fetch(`http://${globalConfig.cider.targetHosts[0]}/currentPlayingSong`).then(res => res.json()).catch(err => { // fetch, format and return JSON
return "unreachable" return "unreachable"
}) })

View File

@@ -2,13 +2,17 @@ const { app, db, globalConfig } = require("../index.js") // Get globals from ind
app.post("/sendemail", (rreq,rres) => { app.post("/sendemail", (rreq,rres) => {
db.get(globalConfig.mailjet.authKeyInDb).then(dbres => { fetch(`http://${globalConfig.couchdb.host}/apiauthkeys/${globalConfig.mailjet.authKeysDoc}`, {
if (dbres == null) { headers: {
console.log("ERROR: Configured key containing mailjet authkeys is null") "Authorization": `Basic ${btoa(globalConfig.couchdb.authorization)}`
rres.sendStatus(500) }
}).then(dbRes => dbRes.json()).then(dbRes => {
if (dbRes.status == 404) { // If document containing mailjet auth keys does not exist
console.log(`ERROR: Could not find apiauthkeys/${globalConfig.mailjet.authKeysDoc}`)
rres.sendStatus(500) // Refuse request
} else { } else {
let validKeys = dbres.split(',') if (dbRes["content"][rreq.get("Authorization").split("_")[0]] === rreq.get("Authorization").split("_")[1]) {
if (validKeys.includes(rreq.get("Authorization"))) {
let message = { let message = {
"Messages": [ "Messages": [
@@ -39,7 +43,6 @@ app.post("/sendemail", (rreq,rres) => {
body: JSON.stringify(message) body: JSON.stringify(message)
}).then(fetchRes => { }).then(fetchRes => {
if (fetchRes.status == 200) { 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")}`) console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 200 KEY:${rreq.get("Authorization")}`)
rres.sendStatus(200) rres.sendStatus(200)
} else { } else {
@@ -49,8 +52,8 @@ app.post("/sendemail", (rreq,rres) => {
}) })
} else { } else {
console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) console.log(`${rreq.get("cf-connecting-ip")} POST /sendemail returned 401`) // log ip of unauthorized requests
rres.sendStatus(401) rres.sendStatus(401) // received auth key was not in database
} }
} }
}) })