re-add /sendemail and improve cider

This commit is contained in:
Enstrayed
2024-02-10 14:38:58 -08:00
parent 87afed5b32
commit 29ef51f045
4 changed files with 134 additions and 67 deletions

View File

@@ -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