-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
1114 lines (957 loc) · 37.8 KB
/
Copy pathbackground.js
File metadata and controls
1114 lines (957 loc) · 37.8 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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ProxyMaster Background Service Worker
// 动态加载性能监控脚本
try {
self.importScripts('performance-monitor.js');
console.log('✅ Performance monitor script loaded successfully');
} catch (error) {
console.error('❌ Failed to load performance monitor script:', error);
}
class ProxyManager {
constructor() {
this.profiles = new Map();
this.currentProfile = null;
this.autoSwitchRules = [];
this.performanceStats = new Map();
this.contextMenuListenerAdded = false;
this.tabStates = new Map(); // 记录每个标签页的代理状态
this.proxyConfigCache = new Map(); // 缓存代理配置
this._switching = false; // 防止并发切换
this._switchPromise = null;
this.performanceMonitor = null; // 性能监控器
this.init();
}
async init() {
// 加载保存的配置
await this.loadProfiles();
await this.loadAutoSwitchRules();
// 初始化性能监控器
await this.initPerformanceMonitor();
// 设置事件监听器
this.setupEventListeners();
// 初始化上下文菜单
this.setupContextMenus();
// 预热代理连接(异步,不阻塞初始化)
this.warmupProxyConnections();
console.log('ProxyMaster initialized');
}
async loadProfiles() {
const result = await chrome.storage.sync.get(['profiles', 'currentProfile']);
if (result.profiles) {
this.profiles = new Map(Object.entries(result.profiles));
}
// 确保直连配置存在
if (!this.profiles.has('direct')) {
this.profiles.set('direct', {
name: 'direct',
displayName: chrome.i18n.getMessage('directConnection'),
type: 'direct'
});
}
this.currentProfile = result.currentProfile || 'direct';
console.log('Loaded profiles:', Array.from(this.profiles.keys()));
console.log('Current profile:', this.currentProfile);
// 预热:预构建所有代理配置缓存
this.preloadProxyConfigs();
}
preloadProxyConfigs() {
console.log('🔥 Preloading proxy configurations...');
let cachedCount = 0;
for (const [profileName, profile] of this.profiles) {
if (profileName !== 'direct' && profile.host && profile.port) {
const config = {
mode: 'fixed_servers',
rules: {
singleProxy: {
scheme: profile.protocol || 'http',
host: profile.host,
port: profile.port
}
}
};
this.proxyConfigCache.set(profileName, config);
cachedCount++;
}
}
console.log(`📦 Preloaded ${cachedCount} proxy configurations`);
}
async warmupProxyConnections() {
// 预热连接被移除以符合Chrome Web Store审核要求
// 用户首次使用代理时可能会有轻微延迟
console.log('🔥 Proxy warmup skipped for store compliance');
}
async waitForProxySwitch(targetProfile, maxWaitTime = 3000) {
console.log(`🔍 Confirming proxy switch to: ${targetProfile}`);
const startTime = Date.now();
return new Promise((resolve) => {
const checkInterval = 50; // 每50ms检查一次,更快响应
let attempts = 0;
const maxAttempts = Math.floor(maxWaitTime / checkInterval);
const checkProxy = async () => {
attempts++;
try {
// 获取当前代理设置
const proxySettings = await chrome.proxy.settings.get({ incognito: false });
const currentSettings = proxySettings.value;
let isCorrectProxy = false;
if (targetProfile === 'direct') {
// 检查是否为直连
isCorrectProxy = currentSettings.mode === 'direct';
} else {
// 检查是否为指定代理
const profile = this.profiles.get(targetProfile);
if (profile && currentSettings.mode === 'fixed_servers' && currentSettings.rules) {
const singleProxy = currentSettings.rules.singleProxy;
isCorrectProxy = singleProxy &&
singleProxy.host === profile.host &&
singleProxy.port === profile.port &&
singleProxy.scheme === profile.protocol;
}
}
if (isCorrectProxy) {
const waitTime = Date.now() - startTime;
console.log(`✅ Proxy switch confirmed in ${waitTime}ms (${attempts} attempts)`);
resolve(true);
return;
}
// 如果还没有达到最大尝试次数,继续检查
if (attempts < maxAttempts) {
setTimeout(checkProxy, checkInterval);
} else {
const waitTime = Date.now() - startTime;
console.warn(`⚠️ Proxy switch confirmation timeout after ${waitTime}ms (${attempts} attempts)`);
// 即使超时也返回true,避免阻塞用户操作
resolve(true);
}
} catch (error) {
console.error('Error checking proxy settings:', error);
if (attempts < maxAttempts) {
setTimeout(checkProxy, checkInterval);
} else {
console.warn(`⚠️ Proxy switch confirmation failed after ${attempts} attempts`);
// 即使失败也返回true,避免阻塞用户操作
resolve(true);
}
}
};
// 开始检查
checkProxy();
});
}
async saveProfiles() {
const profilesObj = Object.fromEntries(this.profiles);
await chrome.storage.sync.set({
profiles: profilesObj,
currentProfile: this.currentProfile
});
// 只在配置真正改变时才清理缓存,而不是每次保存都清理
// 这样可以保持性能优化
console.log(`💾 Profiles saved to storage`);
}
async loadAutoSwitchRules() {
const result = await chrome.storage.sync.get(['autoSwitchRules']);
this.autoSwitchRules = result.autoSwitchRules || [];
console.log('Loaded auto switch rules:', this.autoSwitchRules.length);
this.autoSwitchRules.forEach((rule, index) => {
console.log(`Rule ${index}: ${rule.name} (${rule.type}: ${rule.pattern}) -> ${rule.profile} [${rule.enabled ? 'enabled' : 'disabled'}]`);
});
}
// 提取URL的域名,统一处理www前缀
extractDomain(url) {
try {
if (!url) return null;
const urlObj = new URL(url);
let hostname = urlObj.hostname;
// 统一处理www前缀:www.example.com 和 example.com 视为同一域名
if (hostname.startsWith('www.')) {
hostname = hostname.substring(4);
}
return hostname;
} catch (error) {
return null;
}
}
// 判断是否应该拦截这个URL
shouldInterceptUrl(url) {
// 排除内部页面和扩展页面
if (url.startsWith('chrome://') ||
url.startsWith('chrome-extension://') ||
url.startsWith('devtools://') ||
url.startsWith('moz-extension://') ||
url.startsWith('edge://') ||
url === 'about:blank' ||
url === '') {
return false;
}
return true;
}
setupEventListeners() {
// 备用监听:标签页更新(仅用于状态同步)
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
// 只处理页面加载完成的状态同步,主要逻辑已在webRequest中处理
if (changeInfo.status === 'complete' && tab.url && !tab.url.startsWith('chrome://')) {
// 确保标签页状态记录存在
const tabState = this.tabStates.get(tabId);
if (!tabState) {
console.log(`📋 Syncing state for tab ${tabId}: ${tab.url}`);
// 根据URL规则确定正确的代理,而不是使用当前全局代理
const matchedRule = this.findMatchingRule(tab.url);
const correctProxy = matchedRule ? matchedRule.profile : 'direct';
this.tabStates.set(tabId, {
proxy: correctProxy,
setBy: 'auto',
timestamp: Date.now(),
lastProcessedUrl: tab.url,
currentDomain: this.extractDomain(tab.url)
});
console.log(`📋 Tab ${tabId} synced with correct proxy: ${correctProxy}`);
}
}
});
// 监听标签页创建 - 新标签页立即处理
chrome.tabs.onCreated.addListener((tab) => {
if (tab.url && !tab.url.startsWith('chrome://') && tab.url !== 'chrome://newtab/') {
console.log(`🆕 New tab created: ${tab.url}`);
this.handleNavigationIntercept(tab.id, tab.url);
}
});
// 监听标签页切换 - 智能代理切换
chrome.tabs.onActivated.addListener(async (activeInfo) => {
await this.handleTabActivated(activeInfo.tabId);
});
// 监听标签页关闭,清理状态
chrome.tabs.onRemoved.addListener((tabId) => {
this.tabStates.delete(tabId);
console.log(`Tab ${tabId} closed, state cleaned up`);
});
// 监听扩展消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
this.handleMessage(message, sender, sendResponse);
return true; // 保持消息通道开放
});
}
setupContextMenus() {
// 先清除所有现有的右键菜单项,避免重复创建
chrome.contextMenus.removeAll(() => {
try {
chrome.contextMenus.create({
id: 'proxymaster-quick-switch',
title: '快速切换代理',
contexts: ['page']
});
chrome.contextMenus.create({
id: 'proxymaster-add-rule',
title: '为此网站添加规则',
contexts: ['page']
});
} catch (error) {
console.error('Error creating context menus:', error);
}
});
// 确保只添加一次点击监听器
if (!this.contextMenuListenerAdded) {
chrome.contextMenus.onClicked.addListener((info, tab) => {
this.handleContextMenuClick(info, tab);
});
this.contextMenuListenerAdded = true;
}
}
async handleNavigationIntercept(tabId, url) {
try {
console.log(`🚦 Intercepting navigation: Tab ${tabId} → ${url}`);
// 快速检查:如果自动切换被禁用,直接返回
const autoSwitchEnabled = await this.isAutoSwitchEnabled();
if (!autoSwitchEnabled) {
console.log('⏭️ Auto switch disabled, allowing navigation');
return;
}
// 获取当前标签页状态
const currentTabState = this.tabStates.get(tabId);
console.log(`📋 Current tab state:`, currentTabState);
// 防止重复处理:如果已经处理过相同URL,跳过(但延迟决策的除外)
if (currentTabState && currentTabState.lastProcessedUrl === url && !currentTabState.deferred) {
console.log(`✅ URL already processed: ${url}`);
return;
}
// 检查是否是标签页切换导致的代理变更后的重新导航
// 如果当前代理已经正确,且URL已经匹配,则跳过处理
const matchedRule = this.findMatchingRule(url);
const expectedProfile = matchedRule ? matchedRule.profile : 'direct';
if (this.currentProfile === expectedProfile) {
console.log(`✅ Proxy already correct for ${url}: ${expectedProfile}, skipping intercept`);
// 更新标签页状态但不重定向
this.tabStates.set(tabId, {
proxy: expectedProfile,
setBy: 'auto',
timestamp: Date.now(),
lastProcessedUrl: url,
currentDomain: this.extractDomain(url)
});
return;
}
// 1. 使用已经找到的规则确定需要的代理
let targetProfile = expectedProfile; // 使用之前计算的代理
if (matchedRule) {
console.log(`🎯 Rule matched: ${matchedRule.name} → ${targetProfile}`);
} else {
console.log(`🔍 No rule matched for ${url}, using direct connection`);
}
// 使用统一的代理切换函数,传入新的目标URL
await this.unifiedProxySwitch(tabId, targetProfile, 'auto', url);
} catch (error) {
console.error('Error in handleNavigationIntercept:', error);
}
}
async handleTabActivated(tabId) {
try {
console.log(`🔄 Tab ${tabId} activated`);
// 获取标签页信息
const tab = await chrome.tabs.get(tabId);
// 跳过扩展页面和内部页面
if (tab.url.startsWith('chrome://') || tab.url.startsWith('chrome-extension://')) {
console.log(`⏭️ Skipping internal page: ${tab.url}`);
return;
}
// 获取该标签页的代理状态
const tabState = this.tabStates.get(tabId);
if (tabState) {
// 标签页有记录的代理状态
const targetProxy = tabState.proxy;
console.log(`📋 Tab ${tabId} has recorded proxy: ${targetProxy}`);
// 检查是否需要切换全局代理
if (this.currentProfile !== targetProxy) {
console.log(`🔄 Switching global proxy from ${this.currentProfile} to ${targetProxy} for tab ${tabId}`);
await this.switchToProfile(targetProxy, false, null, false); // 不刷新,不更新标签页状态
} else {
console.log(`✅ Global proxy already matches tab ${tabId} proxy: ${targetProxy}`);
}
} else {
// 新标签页,延迟代理切换,等待用户输入URL后再决定
console.log(`🆕 New tab ${tabId}, deferring proxy decision until navigation`);
this.tabStates.set(tabId, {
proxy: this.currentProfile, // 暂时使用当前代理,避免不必要的切换
setBy: 'auto',
timestamp: Date.now(),
currentDomain: this.extractDomain(tab.url),
deferred: true // 标记为延迟决策
});
// 不立即切换代理,等待导航时再决定
console.log(`⏳ Proxy decision deferred for new tab ${tabId}`);
}
} catch (error) {
console.error('Error in handleTabActivated:', error);
}
}
findMatchingRule(url) {
console.log(`Finding matching rule for URL: ${url}`);
console.log(`Available rules: ${this.autoSwitchRules.length}`);
// 只处理启用的规则,按优先级排序
const enabledRules = this.autoSwitchRules
.filter(rule => rule.enabled)
.sort((a, b) => (b.priority || 100) - (a.priority || 100));
console.log(`Enabled rules: ${enabledRules.length}`);
for (const rule of enabledRules) {
console.log(`Checking rule: ${rule.name} (${rule.type}: ${rule.pattern})`);
if (this.matchesRule(url, rule)) {
console.log(`Rule matched: ${rule.name} for ${url}`);
return rule;
}
}
console.log(`No matching rule found for ${url}`);
return null;
}
matchesRule(url, rule) {
try {
const urlObj = new URL(url);
let result = false;
switch (rule.type) {
case 'domain':
result = this.matchDomain(urlObj.hostname, rule.pattern);
console.log(`Domain match: ${urlObj.hostname} vs ${rule.pattern} = ${result}`);
break;
case 'url':
result = this.wildcardMatch(url, rule.pattern);
console.log(`URL match: ${url} vs ${rule.pattern} = ${result}`);
break;
case 'wildcard':
result = this.wildcardMatch(url, rule.pattern);
console.log(`Wildcard match: ${url} vs ${rule.pattern} = ${result}`);
break;
case 'regex':
result = new RegExp(rule.pattern).test(url);
console.log(`Regex match: ${url} vs ${rule.pattern} = ${result}`);
break;
default:
console.log(`Unknown rule type: ${rule.type}`);
return false;
}
return result;
} catch (e) {
console.error('Rule matching error:', e, rule);
return false;
}
}
matchDomain(hostname, pattern) {
// 处理域名匹配,支持通配符
if (pattern.startsWith('*.')) {
// 通配符域名匹配,如 *.google.com
const domain = pattern.substring(2);
return hostname === domain || hostname.endsWith('.' + domain);
} else {
// 精确域名匹配或包含匹配
return hostname === pattern || hostname.endsWith('.' + pattern);
}
}
wildcardMatch(str, pattern) {
const regexPattern = pattern
.replace(/\*/g, '.*')
.replace(/\?/g, '.');
return new RegExp(`^${regexPattern}$`).test(str);
}
// 缓存设置以提高性能
async isAutoSwitchEnabled() {
if (!this._autoSwitchCache || Date.now() - this._autoSwitchCache.timestamp > 5000) {
const settings = await chrome.storage.sync.get(['enableAutoSwitch']);
this._autoSwitchCache = {
enabled: settings.enableAutoSwitch !== false,
timestamp: Date.now()
};
}
return this._autoSwitchCache.enabled;
}
async isAutoFallbackEnabled() {
if (!this._autoFallbackCache || Date.now() - this._autoFallbackCache.timestamp > 5000) {
const settings = await chrome.storage.sync.get(['enableAutoFallback']);
this._autoFallbackCache = {
enabled: settings.enableAutoFallback !== false,
timestamp: Date.now()
};
}
return this._autoFallbackCache.enabled;
}
// 自动添加规则
async autoAddRule(domain, profile) {
// 检查是否已存在相同域名的规则
const existingRule = this.autoSwitchRules.find(rule =>
rule.type === 'domain' && rule.pattern === domain
);
if (!existingRule) {
// 创建新规则
const newRule = {
name: `${chrome.i18n.getMessage('autoRule') || 'Auto'}-${domain}`,
type: 'domain',
pattern: domain,
profile: profile,
enabled: true,
priority: 100
};
this.autoSwitchRules.push(newRule);
// 保存规则
await chrome.storage.sync.set({ autoSwitchRules: this.autoSwitchRules });
console.log(`✅ Auto-created rule: ${domain} → ${profile}`);
}
}
// 统一的代理切换处理函数
async unifiedProxySwitch(tabId, targetProfile, switchType, providedUrl = null) {
try {
console.log(`🔄 Unified proxy switch: Tab ${tabId} → ${targetProfile} (${switchType})`);
// 1. 优先级检查:只在同一域名内保护手工设置
const currentTabState = this.tabStates.get(tabId);
if (switchType === 'auto' && currentTabState && currentTabState.setBy === 'manual') {
// 检查是否是同一域名
const currentDomain = currentTabState.currentDomain || this.extractDomain(currentTabState.lastProcessedUrl || '');
const newDomain = this.extractDomain(providedUrl || targetUrl);
if (currentDomain && newDomain && currentDomain === newDomain) {
console.log(`🛡️ Same domain (${currentDomain}), keeping manual setting`);
return false;
} else {
console.log(`🔄 Different domain: ${currentDomain} → ${newDomain}, allowing auto switch`);
}
}
// 2. 获取目标URL:优先级顺序
let targetUrl = providedUrl;
if (!targetUrl) {
// 对于手工切换,优先使用标签页状态中记录的最后尝试访问的URL
if (switchType === 'manual' && currentTabState && currentTabState.lastProcessedUrl) {
targetUrl = currentTabState.lastProcessedUrl;
console.log(`📋 Using last processed URL from tab state: ${targetUrl}`);
} else {
// 否则获取当前标签页URL
try {
const tab = await chrome.tabs.get(tabId);
targetUrl = tab.url;
console.log(`📄 Using current tab URL: ${targetUrl}`);
} catch (error) {
console.error(`❌ Failed to get tab ${tabId}:`, error);
return false;
}
}
}
console.log(`🎯 Target URL for redirect: ${targetUrl}`);
// 3. 防止重复处理同一个URL和代理组合
if (currentTabState &&
currentTabState.lastProcessedUrl === targetUrl &&
currentTabState.proxy === targetProfile) {
console.log(`✅ URL already processed with correct proxy: ${targetUrl}`);
return true;
}
// 4. 检查是否需要切换代理
const needsSwitch = this.currentProfile !== targetProfile;
if (needsSwitch) {
// 切换代理
const success = await this.switchToProfile(targetProfile, switchType === 'manual', null, false);
if (!success) {
console.error(`❌ Failed to switch proxy to ${targetProfile}`);
return false;
}
// 对于自动切换,跳过等待确认以提升速度
if (switchType === 'manual') {
// 手工切换需要确认,确保用户操作的准确性
console.log(`⏳ Waiting for manual proxy switch confirmation...`);
const confirmed = await this.waitForProxySwitch(targetProfile);
if (!confirmed) {
console.error(`❌ Manual proxy switch confirmation failed for ${targetProfile}`);
}
} else {
// 自动切换信任API返回,不等待确认,提升速度
console.log(`⚡ Auto proxy switch completed, trusting API response`);
}
console.log(`✅ Proxy switched to ${targetProfile}`);
} else {
console.log(`✅ Proxy already correct: ${targetProfile}`);
}
// 5. 更新标签页状态
this.tabStates.set(tabId, {
proxy: targetProfile,
setBy: switchType,
timestamp: Date.now(),
lastProcessedUrl: targetUrl,
currentDomain: this.extractDomain(targetUrl)
// 清除deferred标记
});
// 6. 只在需要切换代理且不是重定向导航时才重定向
if (needsSwitch && switchType !== 'redirect') {
try {
// 检查是否可以重定向
if (!this.shouldInterceptUrl(targetUrl)) {
console.log(`⏭️ Skipping redirect for internal URL: ${targetUrl}`);
return true;
}
// 检查当前标签页URL是否已经是目标URL
try {
const currentTab = await chrome.tabs.get(tabId);
if (currentTab.url === targetUrl) {
console.log(`⏭️ Tab already at target URL, no redirect needed: ${targetUrl}`);
return true;
}
} catch (error) {
console.warn('Could not get current tab URL:', error);
}
console.log(`🧭 Redirecting to: ${targetUrl}`);
await chrome.tabs.update(tabId, { url: targetUrl });
console.log(`✅ Redirected successfully`);
} catch (error) {
console.error('Error during redirect:', error);
}
} else if (needsSwitch) {
console.log(`⏭️ Proxy switched but no redirect needed (redirect navigation)`);
} else {
console.log(`⏭️ No redirect needed, proxy already correct`);
}
// 8. 手工切换成功后,自动添加规则
if (switchType === 'manual') {
const domain = this.extractDomain(targetUrl);
if (domain) {
this.autoAddRule(domain, targetProfile);
}
}
return true;
} catch (error) {
console.error('Error in unifiedProxySwitch:', error);
return false;
}
}
// 保留原有函数作为兼容性接口,内部调用统一函数
async setTabProxy(tabId, profileName, setBy = 'manual') {
return await this.unifiedProxySwitch(tabId, profileName, setBy);
}
async switchToProfile(profileName, isManual = true, targetTabId = null, updateTabState = true) {
const startTime = Date.now();
console.log(`⚡ Fast switching to profile: ${profileName} (${isManual ? 'manual' : 'auto'})`);
// 快速检查:如果已经是目标代理,直接返回
if (this.currentProfile === profileName) {
console.log(`✅ Already using ${profileName}, skipping switch`);
return true;
}
// 防止并发切换
if (this._switching) {
console.log(`⏳ Switch already in progress, waiting...`);
await this._switchPromise;
return this.currentProfile === profileName;
}
// 标记正在切换
this._switching = true;
this._switchPromise = this._performSwitch(profileName, isManual, startTime);
try {
const result = await this._switchPromise;
return result;
} finally {
this._switching = false;
this._switchPromise = null;
}
}
async _performSwitch(profileName, isManual, startTime) {
// 保存之前的配置,用于错误恢复
const previousProfile = this.currentProfile;
try {
// 对于直连模式,不需要检查profile是否存在
if (profileName !== 'direct') {
const profile = this.profiles.get(profileName);
if (!profile) {
console.error(`❌ Profile ${profileName} not found`);
return false;
}
}
// 立即更新当前配置,避免重复切换
this.currentProfile = profileName;
console.log(`🔄 Setting proxy configuration...`);
if (profileName === 'direct') {
// 直连模式 - 使用最快的方式,只设置一次
await chrome.proxy.settings.set({
value: { mode: 'direct' },
scope: 'regular'
});
console.log('✅ Direct connection activated');
} else {
// 代理模式 - 使用缓存的配置,减少处理时间
const profile = this.profiles.get(profileName);
// 从缓存获取或构建配置
let config = this.proxyConfigCache.get(profileName);
if (!config) {
config = {
mode: 'fixed_servers',
rules: {
singleProxy: {
scheme: profile.protocol,
host: profile.host,
port: profile.port
}
}
};
this.proxyConfigCache.set(profileName, config);
console.log(`📦 Cached config for ${profileName}`);
}
// 如果有认证,先设置认证(这个比较快)
if (profile.auth) {
this.setupProxyAuth(profile.auth);
}
// 设置代理配置(这是最耗时的操作)
await chrome.proxy.settings.set({
value: config,
scope: 'regular'
});
console.log(`✅ Proxy activated: ${profile.host}:${profile.port}`);
}
const switchTime = Date.now() - startTime;
console.log(`⏱️ Proxy switch API completed in ${switchTime}ms`);
// 记录代理切换性能数据
this.recordProxySwitch(
previousProfile,
profileName,
isManual ? 'manual' : 'auto',
null, // url
null // tabId
);
// 立即更新徽章(这个很快)
this.updateBadge(profileName);
// 立即发送状态更新消息给popup
chrome.runtime.sendMessage({
action: 'profileSwitched',
profileName: profileName
}).catch(() => {
// 忽略错误,popup可能没有打开
});
// 异步保存配置,不阻塞
setTimeout(() => {
this.saveProfiles().catch(error => {
console.warn('Failed to save profiles:', error);
});
}, 0);
return true;
} catch (error) {
console.error('❌ Error switching profile:', error);
// 恢复之前的状态
this.currentProfile = previousProfile;
return false;
}
}
setupProxyAuth(auth) {
// 避免重复添加认证监听器
if (this._authListener) {
chrome.webRequest.onAuthRequired.removeListener(this._authListener);
}
this._authListener = (details) => {
return {
authCredentials: {
username: auth.username,
password: auth.password
}
};
};
chrome.webRequest.onAuthRequired.addListener(
this._authListener,
{ urls: ['<all_urls>'] },
['blocking']
);
console.log(`🔐 Proxy auth configured for user: ${auth.username}`);
}
updateBadge(profileName) {
const badgeText = profileName === 'direct' ? '' : profileName.substring(0, 2).toUpperCase();
chrome.action.setBadgeText({ text: badgeText });
chrome.action.setBadgeBackgroundColor({ color: '#4CAF50' });
console.log(`Badge updated: ${profileName} -> ${badgeText}`);
}
async handleMessage(message, sender, sendResponse) {
try {
switch (message.action) {
case 'getProfiles':
console.log('getProfiles called, currentProfile:', this.currentProfile);
sendResponse({
profiles: Object.fromEntries(this.profiles),
currentProfile: this.currentProfile
});
break;
case 'switchProfile':
// 获取当前活跃标签页
let currentTabId = message.tabId;
if (!currentTabId) {
try {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs.length > 0 && !tabs[0].url.startsWith('chrome-extension://')) {
currentTabId = tabs[0].id;
} else {
// 如果当前是扩展页面,查找最近的网页标签页
const allTabs = await chrome.tabs.query({});
const webTabs = allTabs.filter(tab =>
!tab.url.startsWith('chrome://') &&
!tab.url.startsWith('chrome-extension://') &&
tab.url !== ''
).sort((a, b) => (b.lastAccessed || 0) - (a.lastAccessed || 0));
if (webTabs.length > 0) {
currentTabId = webTabs[0].id;
}
}
} catch (error) {
console.warn('Could not get current tab:', error);
}
}
let success;
const isManual = message.isManual !== false; // 默认为手动
if (currentTabId) {
success = await this.setTabProxy(currentTabId, message.profileName, isManual ? 'manual' : 'auto');
} else {
success = await this.switchToProfile(message.profileName, isManual);
}
sendResponse({
success: success,
currentProfile: this.currentProfile
});
break;
case 'addProfile':
if (!message.profile || !message.profile.name) {
sendResponse({ success: false, error: 'Invalid profile data' });
return;
}
this.profiles.set(message.profile.name, message.profile);
// 清理缓存,因为添加了新的配置文件
this.proxyConfigCache.clear();
console.log(`🧹 Proxy config cache cleared due to profile addition`);
await this.saveProfiles();
console.log('Profile added:', message.profile.name);
sendResponse({ success: true });
break;
case 'deleteProfile':
if (!message.profileName) {
sendResponse({ success: false, error: 'Profile name required' });
return;
}
this.profiles.delete(message.profileName);
// 清理缓存,因为删除了配置文件
this.proxyConfigCache.delete(message.profileName);
console.log(`🧹 Removed ${message.profileName} from proxy config cache`);
await this.saveProfiles();
console.log('Profile deleted:', message.profileName);
sendResponse({ success: true });
break;
case 'addAutoSwitchRule':
this.autoSwitchRules.push(message.rule);
await chrome.storage.sync.set({ autoSwitchRules: this.autoSwitchRules });
sendResponse({ success: true });
break;
case 'getPerformanceStats':
sendResponse(Object.fromEntries(this.performanceStats));
break;
case 'reportPerformance':
// 处理性能报告
console.log('Performance data received:', message.data);
sendResponse({ success: true });
break;
case 'updateRules':
// 更新自动切换规则
this.autoSwitchRules = message.rules || [];
console.log('Rules updated:', this.autoSwitchRules.length);
sendResponse({ success: true });
break;
case 'reloadProfiles':
// 重新加载配置
// 清理缓存,因为要重新加载配置
this.proxyConfigCache.clear();
console.log(`🧹 Proxy config cache cleared due to profile reload`);
await this.loadProfiles();
await this.loadAutoSwitchRules();
console.log('Profiles reloaded');
sendResponse({ success: true });
break;
case 'testAutoSwitch':
// 测试自动切换功能
if (message.url) {
const matchedRule = this.findMatchingRule(message.url);
sendResponse({
success: true,
matchedRule: matchedRule,
currentProfile: this.currentProfile,
rulesCount: this.autoSwitchRules.length
});
} else {
sendResponse({ success: false, error: 'URL required' });
}
break;
case 'getTabProxyStates':
// 返回所有标签页的代理状态
const tabStates = Object.fromEntries(this.tabStates);
sendResponse({
success: true,
tabStates: tabStates,
currentProfile: this.currentProfile
});
break;
case 'testProxy':
// 测试代理连接
this.testProxyConnection(message.profileName).then(result => {
sendResponse(result);
}).catch(error => {
sendResponse({ success: false, error: error.message });
});
break;
default:
console.warn('Unknown action:', message.action);
sendResponse({ error: 'Unknown action' });
}
} catch (error) {
console.error('Error handling message:', error);
sendResponse({ success: false, error: error.message });
}
}
handleContextMenuClick(info, tab) {
if (info.menuItemId === 'proxymaster-quick-switch') {
// 打开快速切换面板
chrome.action.openPopup();
} else if (info.menuItemId === 'proxymaster-add-rule') {
// 为当前网站添加规则
const hostname = new URL(tab.url).hostname;
chrome.tabs.create({
url: `options.html#add-rule?domain=${hostname}`
});
}
}