feat: enhance FakeIP status check with periodic updates

This commit is contained in:
Ivan K
2025-02-20 20:28:51 +03:00
parent ec3a281cef
commit a25c6b8013

View File

@@ -1230,40 +1230,80 @@ return view.extend({
}; };
function checkFakeIP() { function checkFakeIP() {
fetch('http://httpbin.org/ip') let lastStatus = null;
let statusElement = document.getElementById('fakeip-status');
function updateFakeIPStatus() {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
fetch('http://httpbin.org/ip', {
signal: controller.signal
})
.then(response => response.text()) .then(response => response.text())
.then(text => { .then(text => {
const statusElement = document.getElementById('fakeip-status'); clearTimeout(timeoutId);
if (!statusElement) {
statusElement = document.getElementById('fakeip-status');
if (!statusElement) return; if (!statusElement) return;
}
console.log('FakeIP check response:', text); console.log('FakeIP check response:', text);
let currentStatus;
let statusHTML;
if (text.includes('Cannot GET /ip')) { if (text.includes('Cannot GET /ip')) {
console.log('FakeIP status: working (Cannot GET /ip)'); console.log('FakeIP status: working (Cannot GET /ip)');
statusElement.innerHTML = E('span', { 'style': 'color: #4caf50' }, [ currentStatus = 'working';
statusHTML = E('span', { 'style': 'color: #4caf50' }, [
'✔ ', '✔ ',
_('working') _('working')
]).outerHTML; ]).outerHTML;
} else if (text.includes('"origin":')) { } else if (text.includes('"origin":')) {
console.log('FakeIP status: not working (got IP response)'); console.log('FakeIP status: not working (got IP response)');
statusElement.innerHTML = E('span', { 'style': 'color: #f44336' }, [ currentStatus = 'not_working';
statusHTML = E('span', { 'style': 'color: #f44336' }, [
'✘ ', '✘ ',
_('not working') _('not working')
]).outerHTML; ]).outerHTML;
} else { } else {
console.log('FakeIP status: check error (unexpected response)'); console.log('FakeIP status: check error (unexpected response)');
statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, [ currentStatus = 'error';
statusHTML = E('span', { 'style': 'color: #ff9800' }, [
'! ', '! ',
_('check error') _('check error')
]).outerHTML; ]).outerHTML;
} }
if (currentStatus !== lastStatus) {
lastStatus = currentStatus;
statusElement.innerHTML = statusHTML;
}
}) })
.catch(error => { .catch(error => {
clearTimeout(timeoutId);
console.log('FakeIP check error:', error.message); console.log('FakeIP check error:', error.message);
const statusElement = document.getElementById('fakeip-status'); const errorStatus = 'error';
if (errorStatus !== lastStatus) {
lastStatus = errorStatus;
if (statusElement) { if (statusElement) {
statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, 'check error').outerHTML; statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, [
'! ',
error.name === 'AbortError' ? _('timeout') : _('check error')
]).outerHTML;
} }
}
});
}
updateFakeIPStatus();
// Set up periodic checks every 10 seconds
const intervalId = setInterval(updateFakeIPStatus, 10000);
window.addEventListener('unload', () => {
clearInterval(intervalId);
}); });
} }