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,24 +48,38 @@ function RedirectPage() {
return <view />; return <view />;
} }
useEffect(() => { useEffect(() => {
setTimeout(() => { if (
window.open(url, "_blank"); (name === "http" ||
}, 2000); name === "HTTP" ||
setTimeout(() => { name === "https" ||
setColor("warning"); name === "HTTPS") &&
setFeedback("Link redirection is taking longer than usual"); forbid_web_links
}, 3000); ) {
setTimeout(() => { setFeedback("Simple redirect is not allowed to redirect HTTP links");
setColor("error"); setColor("error");
setFeedback( setForbidden(true);
"Unable to open link automatically. Please open using the link below" }
); if (!forbidden && checked) {
setError(true); setTimeout(() => {
}, 5000); window.open(url, "_blank");
}, []); }, 2000);
setTimeout(() => {
setColor("warning");
setFeedback("Link redirection is taking longer than usual");
}, 3000);
setTimeout(() => {
setColor("error");
setFeedback(
"Unable to open link automatically. Please open using the link below"
);
setError(true);
}, 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>
); );