-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbackground.js
More file actions
664 lines (587 loc) · 19.4 KB
/
Copy pathbackground.js
File metadata and controls
664 lines (587 loc) · 19.4 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
'use strict';
let safeLogResponse = null;
let sanitizeRequestHeaders = null;
let serializeRequestBody = null;
try {
/* global importScripts */
importScripts('src/helpers/logger.js');
importScripts('src/helpers/request-headers.js');
importScripts('src/helpers/request-body.js');
if (self.CrossRequestHelpers && self.CrossRequestHelpers.safeLogResponse) {
safeLogResponse = self.CrossRequestHelpers.safeLogResponse;
}
if (self.CrossRequestHelpers && self.CrossRequestHelpers.sanitizeRequestHeaders) {
sanitizeRequestHeaders = self.CrossRequestHelpers.sanitizeRequestHeaders;
}
if (self.CrossRequestHelpers && self.CrossRequestHelpers.serializeRequestBody) {
serializeRequestBody = self.CrossRequestHelpers.serializeRequestBody;
}
} catch (helperError) {
console.warn('[Background] safeLogResponse helper 加载失败:', helperError);
}
if (!safeLogResponse) {
safeLogResponse = function (originalBody, options) {
const opts = options || {};
const maxBytes = typeof opts.maxBytes === 'number' ? opts.maxBytes : 10 * 1024;
const headChars = typeof opts.headChars === 'number' ? opts.headChars : 512;
const tailChars = typeof opts.tailChars === 'number' ? opts.tailChars : 512;
function toText(value) {
if (value == null) {
return '';
}
if (typeof value === 'string') {
return value;
}
try {
return JSON.stringify(value);
} catch (e) {
return String(value);
}
}
const text = toText(originalBody);
let byteLength;
if (typeof TextEncoder !== 'undefined') {
byteLength = new TextEncoder().encode(text).length;
} else {
byteLength = text.length * 2;
}
if (byteLength <= maxBytes) {
return originalBody;
}
return {
truncated: true,
size: byteLength + ' bytes',
head: text.slice(0, headChars),
tail: tailChars > 0 ? text.slice(-tailChars) : '',
hint: '响应体过大,已截断显示'
};
};
}
if (!sanitizeRequestHeaders) {
sanitizeRequestHeaders = function (headers = {}) {
const forbidden = new Set([
'accept-encoding',
'connection',
'content-length',
'cookie',
'host',
'origin',
'referer',
'user-agent'
]);
const sanitizedHeaders = {};
const droppedHeaders = [];
Object.entries(headers || {}).forEach(([rawKey, value]) => {
const key = String(rawKey || '').trim();
if (!key) return;
const lowerKey = key.toLowerCase();
if (forbidden.has(lowerKey)) {
if (lowerKey === 'origin' && typeof value === 'string') {
const origin = value.trim();
if (
origin.startsWith('chrome-extension://') ||
origin.startsWith('moz-extension://') ||
origin.startsWith('safari-extension://')
) {
droppedHeaders.push(`${key}=${origin}`);
return;
}
}
droppedHeaders.push(key);
return;
}
sanitizedHeaders[key] = value;
});
return { sanitizedHeaders, droppedHeaders };
};
}
if (!serializeRequestBody) {
serializeRequestBody = function (body, contentType = '') {
const normalizedContentType = String(contentType || '');
const comparableContentType = normalizedContentType.toLowerCase();
if (Array.isArray(body)) {
return {
body: JSON.stringify(body),
contentType: comparableContentType.includes('application/json')
? normalizedContentType
: 'application/json'
};
}
if (Object.prototype.toString.call(body) === '[object Object]') {
if (comparableContentType.includes('application/x-www-form-urlencoded')) {
return {
body: new URLSearchParams(body).toString(),
contentType: normalizedContentType
};
}
return {
body: JSON.stringify(body),
contentType: normalizedContentType || 'application/json'
};
}
return { body, contentType: normalizedContentType };
};
}
// 域名白名单管理
let allowedDomains = new Set(['*']); // 默认允许所有域名,后续可以限制
// 初始化白名单 - 强制使用允许所有域名
chrome.storage.local.get(['allowedDomains'], (_result) => {
// 忽略存储的值,始终使用 ['*']
allowedDomains = new Set(['*']);
});
// 监听白名单更新 - 强制使用允许所有域名
chrome.storage.onChanged.addListener((changes, namespace) => {
if (namespace === 'local' && changes.allowedDomains) {
// 忽略更新,始终使用 ['*']
allowedDomains = new Set(['*']);
}
});
// 检查域名是否在白名单中
function isDomainAllowed(_url) {
// 强制允许所有域名
return true;
/* 以下是原有逻辑(已禁用)
if (allowedDomains.has('*')) {
return true;
}
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
// 检查完全匹配或通配符匹配
for (const domain of allowedDomains) {
if (domain === hostname) {
return true;
}
// 支持通配符子域名,如 *.example.com
if (domain.startsWith('*.')) {
const baseDomain = domain.substring(2);
if (hostname === baseDomain || hostname.endsWith('.' + baseDomain)) {
return true;
}
}
}
} catch (e) {
console.error('Invalid URL:', url);
return false;
}
return false;
*/
}
// 获取标准的 HTTP 状态文本
function getStatusText(status) {
const statusTexts = {
200: 'OK',
201: 'Created',
202: 'Accepted',
204: 'No Content',
301: 'Moved Permanently',
302: 'Found',
304: 'Not Modified',
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
422: 'Unprocessable Entity',
429: 'Too Many Requests',
500: 'Internal Server Error',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout'
};
return statusTexts[status] || 'Unknown Status';
}
// 处理跨域请求
async function handleCrossOriginRequest(request) {
const { url, method = 'GET', headers = {}, body, timeout = 30000 } = request;
// 支持 FormData/File/Blob 的序列化传输(Issue #14)
const base64ToUint8Array = (base64) => {
const binary = atob(base64 || '');
const len = binary.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
};
const deserializeFileLike = (serialized) => {
const bytes = base64ToUint8Array(serialized.data);
const blob = new Blob([bytes], {
type: serialized.type || 'application/octet-stream'
});
return { blob, name: serialized.name || 'blob' };
};
const deserializeFormData = (serialized) => {
const formData = new FormData();
const entries = Array.isArray(serialized.entries) ? serialized.entries : [];
entries.forEach((entry) => {
if (!entry) return;
const key = entry.key;
const value = entry.value;
if (value && typeof value === 'object' && value.__isFile) {
const file = deserializeFileLike(value);
formData.append(key, file.blob, file.name);
} else {
formData.append(key, value);
}
});
return formData;
};
let requestBody = body;
let isMultipartBody = false;
if (body && typeof body === 'object') {
if (body.__isFormData) {
requestBody = deserializeFormData(body);
isMultipartBody = true;
} else if (body.__isFile) {
requestBody = deserializeFileLike(body).blob;
isMultipartBody = true;
}
}
// 检查域名白名单
if (!isDomainAllowed(url)) {
throw new Error('Domain not allowed: ' + url);
}
// 构建请求选项
const { sanitizedHeaders, droppedHeaders } = sanitizeRequestHeaders(headers);
if (droppedHeaders && droppedHeaders.length) {
console.log('[Background] 已移除受限请求头(fetch 不支持设置):', droppedHeaders);
}
const fetchOptions = {
method,
headers: new Headers(sanitizedHeaders),
mode: 'cors',
credentials: 'include'
};
// multipart/form-data 让浏览器自动设置 boundary,移除手动 Content-Type
if (isMultipartBody) {
const contentTypeHeader =
fetchOptions.headers.get('Content-Type') || fetchOptions.headers.get('content-type') || '';
if (contentTypeHeader.includes('multipart/form-data')) {
fetchOptions.headers.delete('Content-Type');
fetchOptions.headers.delete('content-type');
}
}
// 添加请求体(如果有)
if (requestBody && method.toLowerCase() !== 'get' && method.toLowerCase() !== 'head') {
if (typeof requestBody === 'object' && !(requestBody instanceof FormData)) {
const serialized = serializeRequestBody(
requestBody,
fetchOptions.headers.get('Content-Type') || ''
);
fetchOptions.body = serialized.body;
if (serialized.contentType) {
fetchOptions.headers.set('Content-Type', serialized.contentType);
}
} else {
fetchOptions.body = requestBody;
}
}
// 使用 AbortController 实现超时
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
fetchOptions.signal = controller.signal;
try {
console.log('[Background] 发送请求:', {
url,
method,
headers: Object.fromEntries(fetchOptions.headers.entries()),
hasBody: !!fetchOptions.body
});
// 将日志也发送到所有标签页,方便在网页控制台查看
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.url && (tab.url.startsWith('http://') || tab.url.startsWith('https://'))) {
chrome.tabs
.sendMessage(tab.id, {
type: 'debug_log',
source: 'Background',
message: '发送请求',
data: { url, method, hasBody: !!fetchOptions.body }
})
.catch(() => {}); // 忽略错误
}
});
});
let response;
try {
response = await fetch(url, fetchOptions);
} catch (fetchError) {
// 捕获 fetch 的网络错误
console.error('[Background] Fetch 失败:', fetchError);
// 判断错误类型
if (fetchError.message.includes('net::ERR_CONNECTION_REFUSED')) {
throw new Error(
`无法连接到服务器 ${url}\n请确认:\n1. 服务器是否已启动\n2. 端口是否正确\n3. 防火墙设置`
);
} else if (fetchError.message.includes('net::ERR_NAME_NOT_RESOLVED')) {
throw new Error(`无法解析域名 ${url}\n请检查域名是否正确`);
} else if (fetchError.message.includes('net::ERR_INTERNET_DISCONNECTED')) {
throw new Error('网络连接已断开,请检查网络设置');
} else if (fetchError.message.includes('net::ERR_TIMED_OUT')) {
throw new Error(`连接超时 ${url}\n服务器响应太慢或网络不稳定`);
}
// 重新抛出原始错误
throw fetchError;
}
clearTimeout(timeoutId);
// 获取响应头
const responseHeaders = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = value;
});
// 获取响应体
const responseBody = await response.text();
// 添加调试日志
const safePreview = safeLogResponse(responseBody);
console.log('[Background] 响应详情:', {
url,
status: response.status,
statusText: response.statusText,
contentType: responseHeaders['content-type'] || 'unknown',
bodyLength: responseBody.length,
bodyPreview: safePreview
});
// 同样发送到网页控制台
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.url && (tab.url.startsWith('http://') || tab.url.startsWith('https://'))) {
chrome.tabs
.sendMessage(tab.id, {
type: 'debug_log',
source: 'Background',
message: '响应详情',
data: {
url,
status: response.status,
contentType: responseHeaders['content-type'] || 'unknown',
bodyLength: responseBody.length,
bodyPreview: safePreview
}
})
.catch(() => {});
}
});
});
const contentType = responseHeaders['content-type'] || '';
const looksLikeJsonString = (val) => {
if (typeof val !== 'string') return false;
const trimmed = val.trim();
return trimmed.startsWith('{') || trimmed.startsWith('[');
};
let parsedBody;
let hasParsedBody = false;
if (contentType.includes('application/json') || looksLikeJsonString(responseBody)) {
try {
parsedBody = JSON.parse(responseBody);
hasParsedBody = true;
console.log('[Background] JSON 解析成功:', {
dataType: typeof parsedBody,
isArray: Array.isArray(parsedBody),
keys:
parsedBody && typeof parsedBody === 'object' ? Object.keys(parsedBody).slice(0, 10) : []
});
} catch (e) {
console.error('[Background] JSON 解析失败:', e.message);
console.log('[Background] 原始响应体:', responseBody);
}
}
const result = {
status: response.status,
statusText: response.statusText || getStatusText(response.status),
headers: responseHeaders,
body: responseBody,
ok: response.ok
};
if (hasParsedBody) {
result.bodyParsed = parsedBody;
}
return result;
} catch (error) {
clearTimeout(timeoutId);
console.error('[Background] 请求失败:', {
url,
error: error.message,
errorType: error.name,
errorStack: error.stack
});
// 为网页控制台也记录错误
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
if (tab.url && (tab.url.startsWith('http://') || tab.url.startsWith('https://'))) {
chrome.tabs
.sendMessage(tab.id, {
type: 'debug_log',
source: 'Background',
message: '请求失败',
data: {
url,
error: error.message,
errorType: error.name
}
})
.catch(() => {});
}
});
});
if (error.name === 'AbortError') {
throw new Error('请求超时 (' + timeout + 'ms)');
}
// 检测常见的网络错误
if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {
// 这通常意味着网络错误、CORS错误或服务器不可达
const detailedError = `无法连接到服务器 ${url}
请检查:
1. 目标服务器是否已启动
2. 网络是否正常
3. 防火墙设置
4. CORS 配置是否正确`;
console.error('[Background] Failed to fetch 详细错误:', detailedError);
throw new Error(detailedError);
}
if (error.name === 'NetworkError') {
throw new Error('网络错误:' + error.message);
}
// 对于其他错误,提供更详细的信息
throw new Error('请求失败:' + (error.message || '未知错误'));
}
}
// 监听来自内容脚本的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('[Background] 收到消息:', {
action: request.action,
fromTab: !!sender?.tab,
fromPopup: !sender?.tab && !!sender,
sender: sender
});
// 对于跨域请求,需要检查发送者是否来自标签页
if (request.action === 'crossOriginRequest' && (!sender || !sender.tab)) {
console.warn('Cross-origin request from invalid sender');
return false;
}
if (request.action === 'crossOriginRequest') {
handleCrossOriginRequest(request.data)
.then((response) => {
// 检查是否仍然可以发送响应
try {
sendResponse({ success: true, data: response });
} catch (e) {
console.warn('Failed to send response, port might be closed:', e);
}
})
.catch((error) => {
console.error('[Background] 准备发送错误响应:', {
errorMessage: error.message,
errorType: error.name
});
try {
sendResponse({
success: false,
error: error.message,
errorDetails: {
name: error.name,
message: error.message,
url: request.data?.url
}
});
} catch (e) {
console.warn('Failed to send error response, port might be closed:', e);
}
});
// 返回 true 表示异步响应
return true;
}
// 处理白名单管理
if (request.action === 'getAllowedDomains') {
console.log('[Background] 处理 getAllowedDomains 请求');
const domainsArray = Array.from(allowedDomains);
console.log('[Background] 当前域名列表:', domainsArray);
try {
sendResponse({ domains: domainsArray });
console.log('[Background] 域名列表已发送');
} catch (e) {
console.warn('[Background] 发送域名列表失败:', e);
}
return false; // 同步响应
}
if (request.action === 'setAllowedDomains') {
console.log('[Background] 处理 setAllowedDomains 请求:', request.domains);
chrome.storage.local.set({ allowedDomains: request.domains }, () => {
if (chrome.runtime.lastError) {
console.error('[Background] 保存域名失败:', chrome.runtime.lastError);
try {
sendResponse({ success: false, error: chrome.runtime.lastError.message });
} catch (e) {
console.warn('[Background] 发送错误响应失败:', e);
}
return;
}
console.log('[Background] 域名保存成功');
try {
sendResponse({ success: true });
} catch (e) {
console.warn('[Background] 发送成功响应失败:', e);
}
});
return true;
}
// 处理重新加载域名列表的请求
if (request.action === 'reloadAllowedDomains') {
console.log('[Background] 重新加载域名列表');
chrome.storage.local.get(['allowedDomains'], (result) => {
if (result.allowedDomains) {
allowedDomains = new Set(result.allowedDomains);
console.log('[Background] 域名列表已更新:', Array.from(allowedDomains));
}
});
return false;
}
return false;
});
// 处理扩展安装或更新
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
// 首次安装时设置默认配置
chrome.storage.local.set({
allowedDomains: ['*'],
settings: {
enableLogging: false,
maxTimeout: 60000
}
});
console.log('[Background] 扩展已安装');
} else if (details.reason === 'update') {
// 处理版本更新
console.log(
'Extension updated from',
details.previousVersion,
'to',
chrome.runtime.getManifest().version
);
// 版本更新:无需额外处理
}
});
// Service Worker 保活(Manifest V3 需要)
const keepAlive = () => {
const interval = setInterval(() => {
chrome.runtime.getPlatformInfo(() => {
if (chrome.runtime.lastError) {
clearInterval(interval);
}
});
}, 20e3);
return interval;
};
chrome.runtime.onStartup.addListener(keepAlive);
chrome.runtime.onInstalled.addListener(keepAlive);
// 立即启动保活
keepAlive();
// 添加更强的保活机制
chrome.runtime.onMessage.addListener(() => {
// 每次收到消息都重新保活
keepAlive();
});
console.log('[Background] Service Worker 已启动');