fix: improve proxy fallback and kill switch logic

This commit is contained in:
johnysigma 2026-03-30 15:13:46 +03:00
parent 75bd68aa15
commit bb34b67680
2 changed files with 35 additions and 19 deletions

View File

@ -307,6 +307,7 @@ async function handleNextProxy() {
status: 'connected',
proxy: result.proxy
});
} else {
}
return result;
@ -556,6 +557,7 @@ function broadcastMessage(message) {
});
}
/**
* Handle proxy errors
*/
@ -573,14 +575,27 @@ chrome.proxy.onProxyError.addListener(async (details) => {
Utils.log('error', 'Proxy error', details);
// Check if proxy is enabled - don't reconnect if user disconnected
const proxyEnabled = await Storage.isProxyEnabled();
if (!proxyEnabled) {
Utils.log('debug', 'Proxy disabled, ignoring error');
return;
}
// Check current mode - only attempt fallback for public proxies
const mode = await Storage.getMode();
const killSwitch = await Storage.getValue(CONFIG.STORAGE_KEYS.KILL_SWITCH, false);
// For non-fatal errors in public mode, attempt fallback
if (!details.fatal && mode === CONFIG.MODES.PUBLIC) {
// For public mode, always attempt fallback (regardless of fatal flag)
if (mode === CONFIG.MODES.PUBLIC) {
const result = await ProxyManager.fallbackToNext();
if (result.success) {
// Deactivate kill switch if it was active
const killSwitchActive = await Storage.getValue('killSwitchActive', false);
if (killSwitchActive) {
await applyKillSwitch(false);
Utils.log('info', 'Kill switch deactivated after successful fallback');
}
broadcastMessage({
action: 'statusUpdate',
status: 'connected',
@ -653,23 +668,17 @@ async function applyKillSwitch(activate) {
};
await chrome.proxy.settings.set({ value: config, scope: 'regular' });
await Storage.setValue('killSwitchActive', true);
Utils.log('info', 'Kill switch ACTIVATED - all traffic blocked');
// Update icon to show blocked state
try {
// Show red badge with exclamation mark
await chrome.action.setBadgeText({ text: '!' });
await chrome.action.setBadgeBackgroundColor({ color: '#e74c3c' });
} catch (e) {}
await chrome.action.setBadgeBackgroundColor({ color: '#E74C3C' });
Utils.log('info', 'Kill switch ACTIVATED - all traffic blocked');
} else {
// Deactivate kill switch - clear proxy settings
await chrome.proxy.settings.clear({ scope: 'regular' });
await Storage.setValue('killSwitchActive', false);
Utils.log('info', 'Kill switch DEACTIVATED - traffic restored');
// Clear badge
try {
// Remove badge
await chrome.action.setBadgeText({ text: '' });
} catch (e) {}
Utils.log('info', 'Kill switch DEACTIVATED - traffic restored');
}
return true;
} catch (error) {

View File

@ -375,9 +375,10 @@ const ProxyManager = {
/**
* Fallback to next proxy
* @param {number} attemptCount - Number of attempts made
* @returns {Promise<{success: boolean, proxy: object|null, error: string|null}>}
*/
async fallbackToNext() {
async fallbackToNext(attemptCount = 0) {
if (this._proxyList.length === 0) {
return { success: false, proxy: null, error: 'No proxies available. Please refresh the proxy list.' };
}
@ -386,17 +387,23 @@ const ProxyManager = {
return { success: false, proxy: null, error: 'Only one proxy available' };
}
// Prevent infinite loop - stop after trying all proxies
if (attemptCount >= this._proxyList.length) {
Utils.log('error', 'All proxies failed, no working proxy found');
return { success: false, proxy: null, error: 'All proxies are unavailable' };
}
this._currentIndex = (this._currentIndex + 1) % this._proxyList.length;
const proxy = this._proxyList[this._currentIndex];
Utils.log('info', `Switching to proxy ${this._currentIndex + 1}/${this._proxyList.length}: ${proxy.host}:${proxy.port}`);
Utils.log('info', `Switching to proxy ${this._currentIndex + 1}/${this._proxyList.length}: ${proxy.host}:${proxy.port} (attempt ${attemptCount + 1}/${this._proxyList.length})`);
// Test the proxy first to get latency
const testResult = await this.testProxy(proxy);
if (!testResult.working) {
// Try next proxy if this one doesn't work
Utils.log('warn', `Proxy ${proxy.host}:${proxy.port} not working, trying next...`);
return this.fallbackToNext();
return this.fallbackToNext(attemptCount + 1);
}
// Merge test results (latency) with proxy info