proxy01 / extension /background.js
phonglanvq002's picture
Upload folder using huggingface_hub
4a58a99 verified
/**
* HF Proxy Router - Background Service Worker
* Routes ALL browser requests through configured HTTP proxy
*/
// Default proxy settings
const DEFAULT_CONFIG = {
enabled: false,
host: 'localhost',
port: 8080,
scheme: 'http',
routeAll: true, // Route ALL traffic including localhost
blockWebRTC: true // Block WebRTC to prevent IP leaks
};
// Load config from storage
async function loadConfig() {
const result = await chrome.storage.local.get('proxyConfig');
return { ...DEFAULT_CONFIG, ...result.proxyConfig };
}
// Save config to storage
async function saveConfig(config) {
await chrome.storage.local.set({ proxyConfig: config });
}
// Apply proxy settings
async function applyProxySettings(config) {
if (config.enabled) {
const proxyConfig = {
mode: 'fixed_servers',
rules: {
singleProxy: {
scheme: config.scheme,
host: config.host,
port: parseInt(config.port)
},
// Only bypass if routeAll is false
bypassList: config.routeAll ? [] : ['localhost', '127.0.0.1', '<local>']
}
};
await chrome.proxy.settings.set({
value: proxyConfig,
scope: 'regular'
});
// Block WebRTC if enabled to prevent IP leaks
if (config.blockWebRTC) {
await blockWebRTC();
} else {
await unblockWebRTC();
}
console.log('🟒 Proxy enabled:', `${config.host}:${config.port}`);
console.log(' Route all:', config.routeAll);
console.log(' Block WebRTC:', config.blockWebRTC);
} else {
await chrome.proxy.settings.clear({ scope: 'regular' });
await unblockWebRTC();
console.log('πŸ”΄ Proxy disabled');
}
}
// Block WebRTC to prevent IP leaks
async function blockWebRTC() {
try {
// Try to set WebRTC policy if available
if (chrome.privacy && chrome.privacy.network && chrome.privacy.network.webRTCIPHandlingPolicy) {
await chrome.privacy.network.webRTCIPHandlingPolicy.set({
value: 'disable_non_proxied_udp'
});
console.log('πŸ›‘οΈ WebRTC protection enabled');
}
} catch (e) {
console.warn('Could not set WebRTC policy:', e);
}
}
// Unblock WebRTC
async function unblockWebRTC() {
try {
if (chrome.privacy && chrome.privacy.network && chrome.privacy.network.webRTCIPHandlingPolicy) {
await chrome.privacy.network.webRTCIPHandlingPolicy.clear({});
}
} catch (e) {
console.warn('Could not clear WebRTC policy:', e);
}
}
// Initialize on install/startup
chrome.runtime.onInstalled.addListener(async () => {
const config = await loadConfig();
await applyProxySettings(config);
});
chrome.runtime.onStartup.addListener(async () => {
const config = await loadConfig();
await applyProxySettings(config);
});
// Handle messages from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
(async () => {
switch (message.action) {
case 'getConfig':
const config = await loadConfig();
sendResponse(config);
break;
case 'setConfig':
await saveConfig(message.config);
await applyProxySettings(message.config);
sendResponse({ success: true });
break;
case 'toggle':
const currentConfig = await loadConfig();
currentConfig.enabled = !currentConfig.enabled;
await saveConfig(currentConfig);
await applyProxySettings(currentConfig);
sendResponse(currentConfig);
break;
}
})();
return true; // Keep message channel open for async response
});
// Handle proxy errors
chrome.proxy.onProxyError.addListener((details) => {
console.error('Proxy error:', details);
});
// Log all requests going through proxy (for debugging)
chrome.webRequest?.onBeforeRequest?.addListener?.(
(details) => {
// Uncomment below line to see all requests in console
// console.log('πŸ“‘ Request:', details.url);
return {};
},
{ urls: ['<all_urls>'] }
);