-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
282 lines (248 loc) · 8.25 KB
/
background.js
File metadata and controls
282 lines (248 loc) · 8.25 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Network Intercept Background Service Worker
let interceptRules = [];
let attachedTabs = new Set();
let isEnabled = false;
// Load rules from storage on startup
chrome.storage.local.get(['interceptRules', 'isEnabled'], (result) => {
interceptRules = result.interceptRules || [];
isEnabled = result.isEnabled || false;
});
// Listen for messages from popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
case 'getStatus':
sendResponse({ isEnabled, attachedTabs: Array.from(attachedTabs) });
break;
case 'toggleIntercept':
isEnabled = message.enabled;
chrome.storage.local.set({ isEnabled });
if (isEnabled) {
attachDebuggerToActiveTab();
} else {
detachAllDebuggers();
}
sendResponse({ success: true });
break;
case 'getRules':
sendResponse({ rules: interceptRules });
break;
case 'addRule':
interceptRules.push(message.rule);
chrome.storage.local.set({ interceptRules });
sendResponse({ success: true });
break;
case 'updateRule':
if (message.index >= 0 && message.index < interceptRules.length) {
interceptRules[message.index] = message.rule;
chrome.storage.local.set({ interceptRules });
sendResponse({ success: true });
} else {
sendResponse({ success: false, error: 'Invalid index' });
}
break;
case 'deleteRule':
if (message.index >= 0 && message.index < interceptRules.length) {
interceptRules.splice(message.index, 1);
chrome.storage.local.set({ interceptRules });
sendResponse({ success: true });
} else {
sendResponse({ success: false, error: 'Invalid index' });
}
break;
case 'toggleRule':
if (message.index >= 0 && message.index < interceptRules.length) {
interceptRules[message.index].enabled = !interceptRules[message.index].enabled;
chrome.storage.local.set({ interceptRules });
sendResponse({ success: true });
} else {
sendResponse({ success: false, error: 'Invalid index' });
}
break;
case 'clearAllRules':
interceptRules = [];
chrome.storage.local.set({ interceptRules });
sendResponse({ success: true });
break;
}
return true; // Keep message channel open for async response
});
// Attach debugger to active tab
async function attachDebuggerToActiveTab() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab && tab.id) {
await attachDebugger(tab.id);
}
} catch (error) {
console.error('Error attaching debugger:', error);
}
}
// Attach debugger to specific tab
async function attachDebugger(tabId) {
if (attachedTabs.has(tabId)) {
return; // Already attached
}
try {
await chrome.debugger.attach({ tabId }, '1.3');
attachedTabs.add(tabId);
// Enable network tracking
await chrome.debugger.sendCommand({ tabId }, 'Network.enable');
await chrome.debugger.sendCommand({ tabId }, 'Fetch.enable', {
patterns: [{ urlPattern: '*' }]
});
console.log(`Debugger attached to tab ${tabId}`);
} catch (error) {
console.error(`Failed to attach debugger to tab ${tabId}:`, error);
attachedTabs.delete(tabId);
}
}
// Detach debugger from all tabs
async function detachAllDebuggers() {
for (const tabId of attachedTabs) {
try {
await chrome.debugger.detach({ tabId });
console.log(`Debugger detached from tab ${tabId}`);
} catch (error) {
console.error(`Failed to detach debugger from tab ${tabId}:`, error);
}
}
attachedTabs.clear();
}
// Handle debugger events
chrome.debugger.onEvent.addListener((source, method, params) => {
if (!isEnabled) return;
if (method === 'Fetch.requestPaused') {
handleRequestPaused(source.tabId, params);
}
});
// Handle debugger detach
chrome.debugger.onDetach.addListener((source, reason) => {
attachedTabs.delete(source.tabId);
console.log(`Debugger detached from tab ${source.tabId}. Reason: ${reason}`);
});
// Handle request interception
async function handleRequestPaused(tabId, params) {
const { requestId, request } = params;
const url = request.url;
// Skip preflight OPTIONS requests - let them pass through
if (request.method === 'OPTIONS') {
try {
await chrome.debugger.sendCommand(
{ tabId },
'Fetch.continueRequest',
{ requestId }
);
} catch (error) {
console.error('[DevProxy] Error continuing preflight:', error);
}
return;
}
// Find matching rule
let testCount = 0;
const matchingRule = interceptRules.find(rule => {
if (!rule.enabled) return false;
testCount++;
try {
const urlPattern = new RegExp(rule.urlPattern);
return urlPattern.test(url);
} catch (error) {
console.error('[DevProxy] Invalid pattern:', rule.urlPattern, error);
return false;
}
});
if (matchingRule) {
await applyRule(tabId, requestId, params, matchingRule);
} else {
// Continue without modification
try {
await chrome.debugger.sendCommand(
{ tabId },
'Fetch.continueRequest',
{ requestId }
);
} catch (error) {
console.error('[DevProxy] Error continuing request:', error);
}
}
}
// Apply interception rule
async function applyRule(tabId, requestId, params, rule) {
try {
// Apply delay if specified
if (rule.delay && rule.delay > 0) {
await new Promise(resolve => setTimeout(resolve, rule.delay));
}
// Get origin from request headers for proper CORS handling
let origin = '*';
if (params.request.headers) {
// Headers can be an object or array depending on the event
if (Array.isArray(params.request.headers)) {
const originHeader = params.request.headers.find(h => h.name.toLowerCase() === 'origin');
origin = originHeader ? originHeader.value : '*';
} else {
// Headers as object
origin = params.request.headers['origin'] || params.request.headers['Origin'] || '*';
}
}
// Prepare response override
const responseHeaders = [
{ name: 'Access-Control-Allow-Origin', value: origin },
{ name: 'Access-Control-Allow-Credentials', value: 'true' },
{ name: 'Access-Control-Allow-Methods', value: 'GET, POST, PUT, DELETE, PATCH, OPTIONS' },
{ name: 'Access-Control-Allow-Headers', value: 'Content-Type, Authorization, X-Requested-With' }
];
// Add custom headers from rule
if (rule.responseHeaders && typeof rule.responseHeaders === 'object') {
for (const [name, value] of Object.entries(rule.responseHeaders)) {
responseHeaders.push({ name, value: String(value) });
}
}
let responseCode = rule.statusCode || 200;
let body = rule.responseBody || '';
// Convert body to base64 if needed
let base64Body = '';
if (body) {
if (typeof body === 'object') {
body = JSON.stringify(body);
// Only add Content-Type if not already set by custom headers
if (!rule.responseHeaders || !rule.responseHeaders['Content-Type']) {
responseHeaders.push({ name: 'Content-Type', value: 'application/json' });
}
}
base64Body = btoa(unescape(encodeURIComponent(body)));
}
// Fulfill request with modified response
await chrome.debugger.sendCommand(
{ tabId },
'Fetch.fulfillRequest',
{
requestId,
responseCode,
responseHeaders,
body: base64Body
}
);
} catch (error) {
console.error('[DevProxy] Error applying rule:', error);
// Try to continue the request if modification fails
try {
await chrome.debugger.sendCommand(
{ tabId },
'Fetch.continueRequest',
{ requestId }
);
} catch (continueError) {
console.error('[DevProxy] Error continuing request after failure:', continueError);
}
}
}
// Clean up when tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
attachedTabs.delete(tabId);
// Auto-disable extension when all tabs are closed
if (attachedTabs.size === 0 && isEnabled) {
isEnabled = false;
chrome.storage.local.set({ isEnabled });
console.log('Extension auto-disabled: all tabs closed');
}
});