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