Skip to content

Commit

Permalink
Add google API check
Browse files Browse the repository at this point in the history
  • Loading branch information
NayamAmarshe committed Feb 7, 2024
1 parent e32e6ed commit 06d356d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ API_KEY=""
# CAN BE ANYTHING, THIS IS FOR BLOCKING API

SECRET_KEY=""
SAFE_BROWSING_API_KEY=""

# ENTER YOUR WEBSITE DOMAIN NAME HERE

NEXT_PUBLIC_BASE_URL="https://website.com/"

# DEFAULT CONFIG

RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false
RECOIL_DUPLICATE_ATOM_KEY_CHECKING_ENABLED=false
50 changes: 49 additions & 1 deletion pages/api/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ import CryptoJS from "crypto-js";
import { StatusCodes } from "http-status-codes";

const regex =
/(magnet:\?xt=urn:btih:[a-zA-Z0-9]*)|(^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,8}(:[0-9]{1,5})?(\/.*)?$)/;
/^(\S+:\/\/)[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,16}(:[0-9]{1,5})?(\/.*)?$/;

const slugRegex = /^[a-z0-9](-?[a-z0-9])*$/;

export default async function handler(req, res) {
const { slug, link, password } = req.body;
const apiKey = process.env.SAFE_BROWSING_API_KEY;

if (!apiKey) {
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.json({ message: "API not available" });
}

const collectionName =
process.env.NODE_ENV === "production" ? "links" : "testLinks";
Expand Down Expand Up @@ -42,6 +50,45 @@ export default async function handler(req, res) {
});
}

try {
const url = new URL(link).hostname;
const response = await fetch(
"https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" + apiKey,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
client: {
clientId: "maglit-website",
clientVersion: "1.0.0",
},
threatInfo: {
threatTypes: [
"MALWARE",
"SOCIAL_ENGINEERING",
"UNWANTED_SOFTWARE",
"POTENTIALLY_HARMFUL_APPLICATION",
],
platformTypes: ["ANY_PLATFORM"],
threatEntryTypes: ["URL"],
threatEntries: [{ url: `${url}` }],
},
}),
}
);

const data = await response.json();

if (data && data?.matches?.length > 0) {
// Handle error cases where the URL might not be checked by Safe Browsing
res.status(200).json({ message: "Malicious link entered!" });
}
} catch (error) {
res.status(500).json({ error: "Failed to check the URL." });
}

try {
// check firebase if slug exists
const documentRef = doc(db, collectionName, slug);
Expand All @@ -65,6 +112,7 @@ export default async function handler(req, res) {
const docRef = setDoc(doc(collection(db, collectionName), slug), {
link: encryptedLink,
slug: slug,
created: new Date(),
protected: !(password === ""),
});

Expand Down
17 changes: 7 additions & 10 deletions pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import LinkOptionsModal from "../components/home/LinkOptionsModal";
import TopRightButtons from "../components/home/TopRightButtons";
import { cardsOpenState } from "../atoms/cardsOpenState";
import LinkClipboard from "../components/home/LinkClipboard";
import { AnimatePresence, motion } from "framer-motion";
import { AnimatePresence } from "framer-motion";
import { toast, ToastContainer } from "react-toastify";
import { AiFillCloseCircle } from "react-icons/ai";
import { Backdrop } from "../components/Backdrop";
import { navbarState } from "../atoms/navbarAtom";
import { linksState } from "../atoms/linksState";
import { RiArrowUpSLine } from "react-icons/ri";
Expand All @@ -15,9 +13,7 @@ import { BsArchiveFill } from "react-icons/bs";
import MainLogo from "../components/home/MainLogo";
import * as Monkey from "monkey-typewriter";
import { BASE_URL } from "../utils/config";
import { FiCopy } from "react-icons/fi";
import { useRecoilState } from "recoil";
import { useTheme } from "next-themes";
import Form from "../components/home/Form";
import { useEffect } from "react";
import { useState } from "react";
Expand All @@ -40,8 +36,9 @@ export default function Home() {
const [locked, setLocked] = useState(false);
const [customSlug, setCustomSlug] = useState("");
const slugRegex = /^[a-z0-9](-?[a-z0-9])*$/;

const linkRegex =
/(magnet:\?xt=urn:btih:[a-zA-Z0-9]*)|(^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,8}(:[0-9]{1,5})?(\/.*)?$)/;
/^(\S+:\/\/)[a-zA-Z0-9]+([\-\.]{1}[a-zA-Z0-9]+)*\.[a-zA-Z]{2,16}(:[0-9]{1,5})?(\/.*)?$/;

useEffect(() => {
const linksInStorage = JSON.parse(localStorage.getItem("links")) || [];
Expand Down Expand Up @@ -120,10 +117,6 @@ export default function Home() {
setPassword("");
}

const slug = await generateSlug();

const customOrDefaultSlug = customSlug.length == 0 ? slug : customSlug;

if (magnetLink.length < 1) {
toast.error("You entered an invalid link");
return;
Expand All @@ -136,6 +129,10 @@ export default function Home() {
return;
}

const slug = await generateSlug();

const customOrDefaultSlug = customSlug.length == 0 ? slug : customSlug;

if (slug.length < 1) {
toast.error("Invalid Slug!");
return;
Expand Down

0 comments on commit 06d356d

Please sign in to comment.