import { useState, useEffect } from "preact/hooks"; import { set } from "../../util/IDB"; import { uninstallServiceWorkers } from "../../util/SWHelper"; import { useTranslation } from "react-i18next"; import { ToastContainer, toast } from "react-toastify"; interface BareInputProps { placeholder: string; storageKey: string; } function ProxyInput(props: BareInputProps) { const { t } = useTranslation(); const bareServer = localStorage.getItem("bare") || "/bare/"; const HTTPProxy = localStorage.getItem("HTTPProxy") || ""; const [inputValue, setInputValue] = useState(HTTPProxy); function resetProxy() { set("HTTPProxy", ""); localStorage.setItem("HTTPProxy", ""); uninstallServiceWorkers(); window.location.reload(); } function validateUrl(url: string) { let finalUrl = url; if (url === null || url === undefined || url === "") { finalUrl = ""; return finalUrl; } return finalUrl; } function handleChange() { const proxyUrl = validateUrl( (document.getElementById("pinput") as HTMLInputElement).value ); if (!(proxyUrl === "")) { const [proxyIP, proxyPort] = proxyUrl.split(":"); fetch(bareServer) .then((response) => response.json()) .then((jsonResponse) => { if (jsonResponse.hasOwnProperty("HTTPProxy")) { const headers = new Headers({ "x-bare-url": "https://www.google.com", "X-Bare-Headers": JSON.stringify({ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" }), "x-bare-proxy-ip": proxyIP, "x-bare-proxy-port": proxyPort }); return fetch(bareServer + "v3/", { method: "GET", headers: headers }) .then((response) => { if ( response.headers.get("x-bare-status") === "200" || response.headers.get("x-bare-status") === "302" ) { // Success! set("HTTPProxy", proxyUrl); localStorage.setItem("HTTPProxy", proxyUrl); uninstallServiceWorkers(); window.location.reload(); return true; } else { ( document.getElementById("pinput") as HTMLInputElement ).value = localStorage.getItem("HTTPProxy") || ""; toast(t("settings.httpProxy.badProxy"), { type: "error" }); } }) .catch((error) => { (document.getElementById("pinput") as HTMLInputElement).value = localStorage.getItem("HTTPProxy") || ""; toast(t("settings.httpProxy.badProxy"), { type: "error" }); }); } else { (document.getElementById("pinput") as HTMLInputElement).value = localStorage.getItem("HTTPProxy") || ""; toast(t("settings.httpProxy.badBare"), { type: "error" }); } }) .catch((error) => console.error("Error:", error)); } else { // reset UV config to have no proxy set("HTTPProxy", ""); localStorage.setItem("HTTPProxy", ""); uninstallServiceWorkers(); window.location.reload(); } } return (