Forbid web redirect

This commit is contained in:
Keannu Bernasol 2023-07-23 21:49:36 +08:00
parent 0a289cdb0c
commit a34872affe

View file

@ -6,7 +6,7 @@ import { useState, useEffect } from "react";
const router = createHashRouter([ const router = createHashRouter([
{ {
path: "redirect/:name/:slug1/:slug2", path: "redirect/:name/:slug1/:slug2/:slug3",
element: <RedirectPage />, element: <RedirectPage />,
}, },
]); ]);
@ -23,11 +23,16 @@ function App() {
return <RouterProvider router={router} />; return <RouterProvider router={router} />;
} }
function RedirectPage() { function RedirectPage() {
const { name, slug1, slug2 } = useParams(); const forbid_web_links = true;
const [color, setColor] = useState<CircularProgressColor>("inherit"); const { name, slug1, slug2, slug3 } = useParams(); // URL Params
const [feedback, setFeedback] = useState(""); const [color, setColor] = useState<CircularProgressColor>("inherit"); // Spinner color
const [feedback, setFeedback] = useState(
"Opening " + name + " on your mobile device "
); // Feedback to display to user
const [error, setError] = useState(false); const [error, setError] = useState(false);
const url = name + "://" + slug1 + "/" + slug2; const [forbidden, setForbidden] = useState(false); // If link is forbidden (HTTP/HTTPS)
const [checked, setChecked] = useState(false); // If linked has been checked already
const url = name + "://" + slug1 + "/" + slug2 + "/" + slug3; // URL to redirect to
function ManualButton() { function ManualButton() {
if (error) { if (error) {
return ( return (
@ -43,6 +48,18 @@ function RedirectPage() {
return <view />; return <view />;
} }
useEffect(() => { useEffect(() => {
if (
(name === "http" ||
name === "HTTP" ||
name === "https" ||
name === "HTTPS") &&
forbid_web_links
) {
setFeedback("Simple redirect is not allowed to redirect HTTP links");
setColor("error");
setForbidden(true);
}
if (!forbidden && checked) {
setTimeout(() => { setTimeout(() => {
window.open(url, "_blank"); window.open(url, "_blank");
}, 2000); }, 2000);
@ -57,10 +74,12 @@ function RedirectPage() {
); );
setError(true); setError(true);
}, 5000); }, 5000);
}, []); }
setChecked(true);
}, [forbidden, name, url, checked, forbid_web_links]);
return ( return (
<view> <view>
<CircularProgress size={128} color={color} /> <CircularProgress size={error ? 128 : 0} color={color} />
<view <view
style={{ style={{
display: "flex", display: "flex",
@ -72,10 +91,8 @@ function RedirectPage() {
<view style={{ padding: 16 }} /> <view style={{ padding: 16 }} />
<h5>{"URL Payload 2:" + slug2}</h5> <h5>{"URL Payload 2:" + slug2}</h5>
</view> </view>
<h1 style={{ color: "white", textAlign: "center" }}> <h5>{"URL Payload 3:" + slug3}</h5>
{"Opening " + name + " on your mobile device "} <h2 style={{ color: "white", textAlign: "center" }}>{feedback}</h2>
</h1>
<h2>{feedback}</h2>
<ManualButton /> <ManualButton />
</view> </view>
); );