Skip to content

Commit

Permalink
Add MongoDB api side isSafe support
Browse files Browse the repository at this point in the history
  • Loading branch information
oriionn committed Aug 10, 2023
1 parent a3e92be commit 4814ae1
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ app.get('/', (req, res) => {

let lookup;
async function checkURL(url){
if (config.SAFE_BROWSING_APIKEY === "") return undefined;
if(!lookup) lookup = require('safe-browse-url-lookup')({ apiKey: config.SAFE_BROWSING_APIKEY });
return await lookup.checkSingle(url)
return !(await lookup.checkSingle(url))
}

function generateCode() {
Expand Down Expand Up @@ -126,8 +127,8 @@ app.get("/s/:code", async (req, res) => {
if (password) {
let hashPass = SHA256(password).toString();
if (hashPass === db[code].password) {
let isSafe = !(await checkURL(db[code].link));
if (isSafe) res.redirect(db[code].link);
let isSafe = (await checkURL(db[code].link));
if (isSafe || isSafe === undefined) res.redirect(db[code].link);
else res.redirect(`/warning?link=${Base64.encode(db[code].link)}`);
} else {
res.redirect(`/s/${code}`)
Expand All @@ -136,13 +137,13 @@ app.get("/s/:code", async (req, res) => {
res.sendFile(__dirname + '/public/password.html');
}
} else {
let isSafe = !(await checkURL(db[code].link));
if (isSafe) res.redirect(db[code].link);
let isSafe = (await checkURL(db[code].link));
if (isSafe || isSafe === undefined) res.redirect(db[code].link);
else res.redirect(`/warning?link=${Base64.encode(db[code].link)}`);
}
} else {
let isSafe = !(await checkURL(db[code]));
if (isSafe) res.redirect(db[code]);
let isSafe = (await checkURL(db[code]));
if (isSafe || isSafe === undefined) res.redirect(db[code]);
else res.redirect(`/warning?link=${Base64.encode(db[code])}`);
}
} else if (isMongoDB) {
Expand All @@ -164,8 +165,8 @@ app.get("/s/:code", async (req, res) => {
if (password) {
let hashPass = SHA256(password).toString();
if (hashPass === filtered[0].password) {
let isSafe = !(await checkURL(filtered[0].link));
if (isSafe) res.redirect(filtered[0].link);
let isSafe = (await checkURL(filtered[0].link));
if (isSafe || isSafe === undefined) res.redirect(filtered[0].link);
else res.redirect(`/warning?link=${Base64.encode(filtered[0].link)}`);
} else {
res.redirect(`/s/${code}`)
Expand All @@ -174,8 +175,8 @@ app.get("/s/:code", async (req, res) => {
res.sendFile(__dirname + '/public/password.html');
}
} else {
let isSafe = !(await checkURL(filtered[0].link));
if (isSafe) res.redirect(filtered[0].link);
let isSafe = (await checkURL(filtered[0].link));
if (isSafe || isSafe === undefined) res.redirect(filtered[0].link);
else res.redirect(`/warning?link=${Base64.encode(filtered[0].link)}`);
}
}
Expand Down Expand Up @@ -204,7 +205,7 @@ app.get("/api/s/:code", async (req, res) => {
if (password) {
let hashPass = SHA256(password).toString();
if (hashPass === db[code].password) {
let isSafe = !(await checkURL(db[code].link));
let isSafe = (await checkURL(db[code].link));
res.json({status: 200, data: {original: db[code].link, shorten: `${config.DOMAIN}/s/${code}`, safe: isSafe }});
} else {
res.json({status: 401, data: { original: "Error: Unauthorized", shorten: `${config.DOMAIN}/s/${code}` }})
Expand All @@ -213,11 +214,11 @@ app.get("/api/s/:code", async (req, res) => {
res.json({status: 401, data: { original: "Error: Unauthorized", shorten: `${config.DOMAIN}/s/${code}` }})
}
} else {
let isSafe = !(await checkURL(db[code].link));
let isSafe = (await checkURL(db[code].link));
res.json({status: 200, data: {original: db[code].link, shorten: `${config.DOMAIN}/s/${code}`, safe: isSafe }});
}
} else {
let isSafe = !(await checkURL(db[code]));
let isSafe = (await checkURL(db[code]));
res.json({status: 200, data: {original: db[code], shorten: `${config.DOMAIN}/s/${code}`, safe: isSafe }});
}
} else if (isMongoDB) {
Expand All @@ -239,15 +240,17 @@ app.get("/api/s/:code", async (req, res) => {
if (password) {
let hashPass = SHA256(password).toString();
if (hashPass === filtered[0].password) {
res.json({status: 200, data: {original: filtered[0].link, shorten: `${config.DOMAIN}/s/${code}`}});
let isSafe = (await checkURL(filtered[0].link));
res.json({status: 200, data: {original: filtered[0].link, shorten: `${config.DOMAIN}/s/${code}`, safe: isSafe}});
} else {
res.json({status: 401, data: { original: "Error: Unauthorized", shorten: `${config.DOMAIN}/s/${code}` }})
}
} else {
res.json({status: 401, data: { original: "Error: Unauthorized", shorten: `${config.DOMAIN}/s/${code}` }});
}
} else {
res.json({status: 200, data: {original: filtered[0].link, shorten: `${config.DOMAIN}/s/${code}`}});
let isSafe = (await checkURL(filtered[0].link));
res.json({status: 200, data: {original: filtered[0].link, shorten: `${config.DOMAIN}/s/${code}`, safe: isSafe }});
}
}
});
Expand Down

0 comments on commit 4814ae1

Please sign in to comment.