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

@@ -672,7 +672,7 @@ return view.extend({
o.rmempty = false; o.rmempty = false;
o.ucisection = 'main'; o.ucisection = 'main';
o.validate = function(section_id, value) { o.validate = function (section_id, value) {
if (!value) { if (!value) {
return _('DNS server address cannot be empty'); return _('DNS server address cannot be empty');
} }
@@ -1230,41 +1230,81 @@ return view.extend({
}; };
function checkFakeIP() { function checkFakeIP() {
fetch('http://httpbin.org/ip') let lastStatus = null;
.then(response => response.text()) let statusElement = document.getElementById('fakeip-status');
.then(text => {
const statusElement = document.getElementById('fakeip-status');
if (!statusElement) return;
console.log('FakeIP check response:', text); function updateFakeIPStatus() {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
if (text.includes('Cannot GET /ip')) { fetch('http://httpbin.org/ip', {
console.log('FakeIP status: working (Cannot GET /ip)'); signal: controller.signal
statusElement.innerHTML = E('span', { 'style': 'color: #4caf50' }, [
'✔ ',
_('working')
]).outerHTML;
} else if (text.includes('"origin":')) {
console.log('FakeIP status: not working (got IP response)');
statusElement.innerHTML = E('span', { 'style': 'color: #f44336' }, [
'✘ ',
_('not working')
]).outerHTML;
} else {
console.log('FakeIP status: check error (unexpected response)');
statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, [
'! ',
_('check error')
]).outerHTML;
}
}) })
.catch(error => { .then(response => response.text())
console.log('FakeIP check error:', error.message); .then(text => {
const statusElement = document.getElementById('fakeip-status'); clearTimeout(timeoutId);
if (statusElement) { if (!statusElement) {
statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, 'check error').outerHTML; statusElement = document.getElementById('fakeip-status');
} if (!statusElement) return;
}); }
console.log('FakeIP check response:', text);
let currentStatus;
let statusHTML;
if (text.includes('Cannot GET /ip')) {
console.log('FakeIP status: working (Cannot GET /ip)');
currentStatus = 'working';
statusHTML = E('span', { 'style': 'color: #4caf50' }, [
'✔ ',
_('working')
]).outerHTML;
} else if (text.includes('"origin":')) {
console.log('FakeIP status: not working (got IP response)');
currentStatus = 'not_working';
statusHTML = E('span', { 'style': 'color: #f44336' }, [
'✘ ',
_('not working')
]).outerHTML;
} else {
console.log('FakeIP status: check error (unexpected response)');
currentStatus = 'error';
statusHTML = E('span', { 'style': 'color: #ff9800' }, [
'! ',
_('check error')
]).outerHTML;
}
if (currentStatus !== lastStatus) {
lastStatus = currentStatus;
statusElement.innerHTML = statusHTML;
}
})
.catch(error => {
clearTimeout(timeoutId);
console.log('FakeIP check error:', error.message);
const errorStatus = 'error';
if (errorStatus !== lastStatus) {
lastStatus = errorStatus;
if (statusElement) {
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);
});
} }
function updateDiagnostics() { function updateDiagnostics() {