refactor: refactor checkFakeIP to return a promise and update updateDiagnostics to use async/await

This commit is contained in:
Ivan K
2025-02-21 11:22:37 +03:00
parent 4608bc31cd
commit 89737efcbc

View File

@@ -867,10 +867,7 @@ return view.extend({
o.cfgvalue = () => E('div', { id: 'diagnostics-status' }, _('Loading diagnostics...')); o.cfgvalue = () => E('div', { id: 'diagnostics-status' }, _('Loading diagnostics...'));
function checkFakeIP() { function checkFakeIP() {
let lastStatus = null; return new Promise((resolve) => {
let statusElement = document.getElementById('fakeip-status');
function updateFakeIPStatus() {
const controller = new AbortController(); const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000); const timeoutId = setTimeout(() => controller.abort(), 10000);
@@ -878,75 +875,93 @@ return view.extend({
.then(response => response.text()) .then(response => response.text())
.then(text => { .then(text => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
if (!statusElement) { let status = {
statusElement = document.getElementById('fakeip-status'); state: 'unknown',
if (!statusElement) return; message: '',
} color: '#ff9800'
};
let currentStatus, statusHTML;
if (text.includes('Cannot GET /ip')) { if (text.includes('Cannot GET /ip')) {
currentStatus = 'working'; status.state = 'working';
statusHTML = E('span', { 'style': 'color: #4caf50' }, ['✔ ', _('working')]).outerHTML; status.message = _('working');
status.color = '#4caf50';
} else if (text.includes('"origin":')) { } else if (text.includes('"origin":')) {
currentStatus = 'not_working'; status.state = 'not_working';
statusHTML = E('span', { 'style': 'color: #f44336' }, ['✘ ', _('not working')]).outerHTML; status.message = _('not working');
status.color = '#f44336';
} else { } else {
currentStatus = 'error'; status.state = 'error';
statusHTML = E('span', { 'style': 'color: #ff9800' }, ['! ', _('check error')]).outerHTML; status.message = _('check error');
}
if (currentStatus !== lastStatus) {
lastStatus = currentStatus;
statusElement.innerHTML = statusHTML;
} }
resolve(status);
}) })
.catch(error => { .catch(error => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
const errorStatus = 'error'; resolve({
if (errorStatus !== lastStatus) { state: 'error',
lastStatus = errorStatus; message: error.name === 'AbortError' ? _('timeout') : _('check error'),
if (statusElement) { color: '#ff9800'
statusElement.innerHTML = E('span', { 'style': 'color: #ff9800' }, [ });
'! ', });
error.name === 'AbortError' ? _('timeout') : _('check error')
]).outerHTML;
}
}
}); });
} }
updateFakeIPStatus(); async function updateDiagnostics() {
const intervalId = setInterval(updateFakeIPStatus, 10000); try {
window.addEventListener('unload', () => clearInterval(intervalId)); const [
} podkopStatus,
singboxStatus,
function updateDiagnostics() { podkop,
Promise.all([ luci,
singbox,
system,
fakeipStatus
] = await Promise.all([
fs.exec('/etc/init.d/podkop', ['get_status']), fs.exec('/etc/init.d/podkop', ['get_status']),
fs.exec('/etc/init.d/podkop', ['get_sing_box_status']), fs.exec('/etc/init.d/podkop', ['get_sing_box_status']),
fs.exec('/etc/init.d/podkop', ['show_version']), fs.exec('/etc/init.d/podkop', ['show_version']),
fs.exec('/etc/init.d/podkop', ['show_luci_version']), fs.exec('/etc/init.d/podkop', ['show_luci_version']),
fs.exec('/etc/init.d/podkop', ['show_sing_box_version']), fs.exec('/etc/init.d/podkop', ['show_sing_box_version']),
fs.exec('/etc/init.d/podkop', ['show_system_info']) fs.exec('/etc/init.d/podkop', ['show_system_info']),
]).then(([podkopStatus, singboxStatus, podkop, luci, singbox, system]) => { checkFakeIP()
try { ]);
const parsedPodkopStatus = JSON.parse(podkopStatus.stdout || '{"running":0,"enabled":0,"status":"unknown"}'); const parsedPodkopStatus = JSON.parse(podkopStatus.stdout || '{"running":0,"enabled":0,"status":"unknown"}');
const parsedSingboxStatus = JSON.parse(singboxStatus.stdout || '{"running":0,"enabled":0,"status":"unknown"}'); const parsedSingboxStatus = JSON.parse(singboxStatus.stdout || '{"running":0,"enabled":0,"status":"unknown"}');
const newContent = createStatusSection(parsedPodkopStatus, parsedSingboxStatus, podkop, luci, singbox, system);
const container = document.getElementById('diagnostics-status'); const container = document.getElementById('diagnostics-status');
if (container) { if (!container) return;
const statusSection = createStatusSection(parsedPodkopStatus, parsedSingboxStatus, podkop, luci, singbox, system);
container.innerHTML = ''; container.innerHTML = '';
container.appendChild(newContent); container.appendChild(statusSection);
checkFakeIP();
// Update FakeIP status
const fakeipElement = document.getElementById('fakeip-status');
if (fakeipElement) {
fakeipElement.innerHTML = E('span', { 'style': `color: ${fakeipStatus.color}` }, [
fakeipStatus.state === 'working' ? '✔ ' : fakeipStatus.state === 'not_working' ? '✘ ' : '! ',
fakeipStatus.message
]).outerHTML;
} }
} catch (e) { } catch (e) {
console.error('Error parsing diagnostics status:', e); console.error('Error updating diagnostics:', e);
const container = document.getElementById('diagnostics-status'); const container = document.getElementById('diagnostics-status');
if (container) { if (container) {
container.innerHTML = '<div class="alert-message warning"><strong>' + _('Error loading diagnostics') + '</strong><br><pre>' + e.toString() + '</pre></div>'; container.innerHTML = E('div', { 'class': 'alert-message warning' }, [
E('strong', {}, _('Error loading diagnostics')),
E('br'),
E('pre', {}, e.toString())
]).outerHTML;
} }
} }
}); }
// Start periodic updates
function startPeriodicUpdates() {
updateDiagnostics();
const intervalId = setInterval(updateDiagnostics, 10000);
window.addEventListener('unload', () => clearInterval(intervalId));
} }
// Extra Section // Extra Section
@@ -960,7 +975,7 @@ return view.extend({
const map_promise = m.render(); const map_promise = m.render();
map_promise.then(node => { map_promise.then(node => {
node.classList.add('fade-in'); node.classList.add('fade-in');
updateDiagnostics(); startPeriodicUpdates();
return node; return node;
}); });