From 8eb268753d21bca151b64b126e2bc298ac0d134d Mon Sep 17 00:00:00 2001 From: DeBros Date: Wed, 12 Feb 2025 12:08:08 +0200 Subject: [PATCH] Add files via upload --- js/background.js | 15 +++++++++++---- js/options.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/js/background.js b/js/background.js index 4076544..b55a422 100644 --- a/js/background.js +++ b/js/background.js @@ -74,12 +74,15 @@ function checkProxyFunctionality(proxy, callback) { return; } - // Use Mullvad's DNS server for privacy check - fetch('https://doh.mullvad.net/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB', { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout + + fetch('https://check.en.anyone.tech', { mode: 'no-cors', - headers: { 'accept': 'application/dns-message' } + signal: controller.signal }) .then(response => { + clearTimeout(timeoutId); if (response.ok) { console.log("Proxy check successful"); callback(true); // Connection successful @@ -89,7 +92,11 @@ function checkProxyFunctionality(proxy, callback) { } }) .catch(error => { - console.error("Error in proxy check:", error); + if (error.name === 'AbortError') { + console.log("Request timed out"); + } else { + console.error("Error in proxy check:", error); + } callback(false); }); }); diff --git a/js/options.js b/js/options.js index f29be5b..12e654c 100644 --- a/js/options.js +++ b/js/options.js @@ -29,9 +29,12 @@ chrome.storage.local.get(["proxyIP", "proxyPort", "proxyType", "noProxyFor"], (s noProxyFor.value = settings.noProxyFor || ""; }); -// Function to check internet connectivity +// Function to check internet connectivity with a timeout function checkInternetConnection(host, port) { return new Promise((resolve, reject) => { + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 5000); // 5 second timeout + const proxyConfig = { mode: "fixed_servers", rules: { @@ -46,16 +49,18 @@ function checkInternetConnection(host, port) { chrome.proxy.settings.set({ value: proxyConfig, scope: "regular" }, () => { if (chrome.runtime.lastError) { + clearTimeout(timeoutId); reject(chrome.runtime.lastError.message); return; } - fetch('https://doh.mullvad.net/dns-query?dns=q80BAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB', { + fetch('https://check.en.anyone.tech', { method: 'GET', mode: 'no-cors', - headers: { 'accept': 'application/dns-message' } + signal: controller.signal }) .then(response => { + clearTimeout(timeoutId); if (response.ok) { resolve(true); // Connection successful } else { @@ -63,7 +68,12 @@ function checkInternetConnection(host, port) { } }) .catch(error => { - reject(error.message || "Network error encountered"); + clearTimeout(timeoutId); + if (error.name === 'AbortError') { + reject("Connection timed out"); + } else { + reject(error.message || "Network error encountered"); + } }); }); }); @@ -109,8 +119,12 @@ function clearStatusMessage() { }, 5000); // Clear message after 5 seconds (5000 milliseconds) } +let isCheckingProxy = false; + // Save settings when clicking "Save Settings" saveSettings.addEventListener("click", () => { + if (isCheckingProxy) return; // If a check is already in progress, do nothing + if (!isValidIP(proxyIP.value)) { statusMessage.textContent = "Invalid IP address."; statusMessage.style.color = "red"; @@ -134,6 +148,10 @@ saveSettings.addEventListener("click", () => { statusMessage.style.fontFamily = "Arial"; statusMessage.style.fontWeight = "bold"; + // Disable the saveSettings button + saveSettings.disabled = true; + isCheckingProxy = true; + const noProxyExceptions = noProxyFor.value.split(',').map(ex => ex.trim()); const filteredExceptions = noProxyExceptions.filter(ex => ex !== ''); @@ -171,6 +189,11 @@ saveSettings.addEventListener("click", () => { statusMessage.style.fontFamily = "Arial"; statusMessage.style.fontWeight = "bold"; chrome.proxy.settings.clear({}); + }) + .finally(() => { + // Re-enable the saveSettings button after the check is complete + saveSettings.disabled = false; + isCheckingProxy = false; }); });