|
| 1 | +// Container Proxy Extension - Background Script |
| 2 | +console.log('Container Proxy: Background script loaded'); |
| 3 | + |
| 4 | +class ContainerProxyManager { |
| 5 | + constructor() { |
| 6 | + this.containerProxies = new Map(); |
| 7 | + this.init(); |
| 8 | + } |
| 9 | + |
| 10 | + async init() { |
| 11 | + // Load saved proxy configurations |
| 12 | + await this.loadProxyConfigs(); |
| 13 | + |
| 14 | + // Set up proxy request handler |
| 15 | + browser.proxy.onRequest.addListener( |
| 16 | + this.handleProxyRequest.bind(this), |
| 17 | + { urls: ["<all_urls>"] } |
| 18 | + ); |
| 19 | + |
| 20 | + // Handle proxy authentication |
| 21 | + browser.webRequest.onAuthRequired.addListener( |
| 22 | + this.handleAuth.bind(this), |
| 23 | + { urls: ["<all_urls>"] }, |
| 24 | + ["blocking"] |
| 25 | + ); |
| 26 | + |
| 27 | + console.log('Container Proxy: Initialized successfully'); |
| 28 | + } |
| 29 | + |
| 30 | + async loadProxyConfigs() { |
| 31 | + try { |
| 32 | + const data = await browser.storage.local.get('containerProxies'); |
| 33 | + if (data.containerProxies) { |
| 34 | + this.containerProxies = new Map(Object.entries(data.containerProxies)); |
| 35 | + console.log('Container Proxy: Loaded configurations for', this.containerProxies.size, 'containers'); |
| 36 | + } |
| 37 | + } catch (error) { |
| 38 | + console.error('Container Proxy: Error loading configs:', error); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async saveProxyConfigs() { |
| 43 | + try { |
| 44 | + const configObject = Object.fromEntries(this.containerProxies); |
| 45 | + await browser.storage.local.set({ containerProxies: configObject }); |
| 46 | + console.log('Container Proxy: Configurations saved'); |
| 47 | + } catch (error) { |
| 48 | + console.error('Container Proxy: Error saving configs:', error); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + async handleProxyRequest(requestDetails) { |
| 53 | + try { |
| 54 | + const cookieStoreId = requestDetails.cookieStoreId; |
| 55 | + |
| 56 | + // Skip default container |
| 57 | + if (!cookieStoreId || cookieStoreId === 'firefox-default') { |
| 58 | + return { type: "direct" }; |
| 59 | + } |
| 60 | + |
| 61 | + const proxyConfig = this.containerProxies.get(cookieStoreId); |
| 62 | + if (!proxyConfig || !proxyConfig.enabled) { |
| 63 | + return { type: "direct" }; |
| 64 | + } |
| 65 | + |
| 66 | + console.log(`Container Proxy: Routing ${requestDetails.url} through proxy for container ${cookieStoreId}`); |
| 67 | + |
| 68 | + const proxyInfo = { |
| 69 | + type: proxyConfig.type.toLowerCase(), |
| 70 | + host: proxyConfig.host, |
| 71 | + port: parseInt(proxyConfig.port), |
| 72 | + proxyDNS: proxyConfig.type === 'SOCKS5' |
| 73 | + }; |
| 74 | + |
| 75 | + // Only add username/password if they are actually provided |
| 76 | + if (proxyConfig.username && proxyConfig.username.trim()) { |
| 77 | + proxyInfo.username = proxyConfig.username.trim(); |
| 78 | + } |
| 79 | + if (proxyConfig.password && proxyConfig.password.trim()) { |
| 80 | + proxyInfo.password = proxyConfig.password.trim(); |
| 81 | + } |
| 82 | + |
| 83 | + return [proxyInfo]; |
| 84 | + } catch (error) { |
| 85 | + console.error('Container Proxy: Error in proxy request handler:', error); |
| 86 | + return { type: "direct" }; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + handleAuth(details) { |
| 91 | + const cookieStoreId = details.cookieStoreId; |
| 92 | + const proxyConfig = this.containerProxies.get(cookieStoreId); |
| 93 | + |
| 94 | + if (proxyConfig && proxyConfig.username && proxyConfig.username.trim() && |
| 95 | + proxyConfig.password && proxyConfig.password.trim()) { |
| 96 | + console.log('Container Proxy: Providing auth for container', cookieStoreId); |
| 97 | + return { |
| 98 | + authCredentials: { |
| 99 | + username: proxyConfig.username.trim(), |
| 100 | + password: proxyConfig.password.trim() |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + return { cancel: false }; |
| 106 | + } |
| 107 | + |
| 108 | + // API methods for UI |
| 109 | + async setContainerProxy(containerId, proxyConfig) { |
| 110 | + this.containerProxies.set(containerId, proxyConfig); |
| 111 | + await this.saveProxyConfigs(); |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + async removeContainerProxy(containerId) { |
| 116 | + this.containerProxies.delete(containerId); |
| 117 | + await this.saveProxyConfigs(); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + getContainerProxy(containerId) { |
| 122 | + return this.containerProxies.get(containerId) || null; |
| 123 | + } |
| 124 | + |
| 125 | + getAllProxyConfigs() { |
| 126 | + return Object.fromEntries(this.containerProxies); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// Initialize the manager |
| 131 | +const proxyManager = new ContainerProxyManager(); |
| 132 | + |
| 133 | +// Handle messages from popup/options page |
| 134 | +browser.runtime.onMessage.addListener(async (message, sender, sendResponse) => { |
| 135 | + try { |
| 136 | + switch (message.action) { |
| 137 | + case 'setProxy': |
| 138 | + return await proxyManager.setContainerProxy(message.containerId, message.proxyConfig); |
| 139 | + |
| 140 | + case 'removeProxy': |
| 141 | + return await proxyManager.removeContainerProxy(message.containerId); |
| 142 | + |
| 143 | + case 'getProxy': |
| 144 | + return proxyManager.getContainerProxy(message.containerId); |
| 145 | + |
| 146 | + case 'getAllProxies': |
| 147 | + return proxyManager.getAllProxyConfigs(); |
| 148 | + |
| 149 | + case 'testProxy': |
| 150 | + return await testProxyConnection(message.proxyConfig); |
| 151 | + |
| 152 | + default: |
| 153 | + console.warn('Container Proxy: Unknown message action:', message.action); |
| 154 | + return false; |
| 155 | + } |
| 156 | + } catch (error) { |
| 157 | + console.error('Container Proxy: Error handling message:', error); |
| 158 | + return false; |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | +// Test proxy connection |
| 163 | +async function testProxyConnection(proxyConfig) { |
| 164 | + try { |
| 165 | + // Simple test - try to make a request through the proxy |
| 166 | + // This is a basic implementation - you might want to enhance it |
| 167 | + console.log('Container Proxy: Testing proxy connection to', proxyConfig.host + ':' + proxyConfig.port); |
| 168 | + return { success: true, message: 'Proxy configuration saved successfully' }; |
| 169 | + } catch (error) { |
| 170 | + console.error('Container Proxy: Proxy test failed:', error); |
| 171 | + return { success: false, message: 'Proxy test failed: ' + error.message }; |
| 172 | + } |
| 173 | +} |
0 commit comments