use indexedDB manager proxySwitch

This commit is contained in:
go0p
2025-03-04 16:07:14 +08:00
parent 1e25561fae
commit 6f82842c92
23 changed files with 1542 additions and 753 deletions
+71 -53
View File
@@ -1,72 +1,91 @@
// 代理认证管理
import { proxyStore } from '../db/proxy-store.js';
export class ProxyAuth {
static async setupAuthListener() {
try {
// 保存认证信息到 storage
const saveAuth = async (config) => {
await chrome.storage.local.set({
proxyAuth: {
username: config.username,
password: config.password,
timestamp: Date.now()
// 使用 chrome.webRequest.onAuthRequired 的非阻塞版本
chrome.webRequest.onAuthRequired.addListener(
async (details) => {
try {
// 获取认证处理器
const handlers = await proxyStore.getAuthHandlers();
const handler = handlers.find(h =>
details.challenger?.host === h.host
);
if (handler) {
// 使用 declarativeNetRequest 规则来处理认证
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [handler.id],
addRules: [{
id: parseInt(handler.id),
priority: 1,
action: {
type: 'modifyHeaders',
requestHeaders: [
{
header: 'Proxy-Authorization',
operation: 'set',
value: 'Basic ' + btoa(`${handler.username}:${handler.password}`)
}
]
},
condition: {
domains: [handler.host],
resourceTypes: ['main_frame', 'sub_frame', 'stylesheet', 'script', 'image', 'font', 'object', 'xmlhttprequest', 'ping', 'csp_report', 'media', 'websocket', 'other']
}
}]
});
}
});
};
} catch (error) {
console.error('Auth error:', error);
}
},
{ urls: ["<all_urls>"] }
);
}
// 获取认证信息
const getAuth = async () => {
const result = await chrome.storage.local.get('proxyAuth');
return result.proxyAuth;
};
static async saveAuthHandler(host, username, password) {
const handler = {
id: Date.now().toString(),
host,
username,
password
};
await proxyStore.saveAuthHandler(handler);
await this.setupAuthListener(); // 重新设置认证规则
}
// 清除认证信息
const clearAuth = async () => {
await chrome.storage.local.remove('proxyAuth');
};
return {
saveAuth,
getAuth,
clearAuth
};
} catch (error) {
console.error('Error setting up auth listener:', error);
return null;
static async removeAuthHandler(host) {
const handlers = await proxyStore.getAuthHandlers();
const handler = handlers.find(h => h.host === host);
if (handler) {
await proxyStore.deleteAuthHandler(handler.id);
// 移除对应的认证规则
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [parseInt(handler.id)]
});
}
}
static setupErrorHandler() {
// 在 Manifest V3 中,我们不能使用 chrome.proxy.onProxyError
// 所以我们只记录错误到 storage
try {
const logError = async (error) => {
const errors = await chrome.storage.local.get('proxyErrors') || [];
// 使用 storage 记录错误
return {
logError: async (error) => {
const errors = await proxyStore.getErrors() || [];
errors.push({
timestamp: Date.now(),
error: error.message || error
});
await chrome.storage.local.set({
proxyErrors: errors.slice(-100) // 只保留最近100条错误记录
});
};
return { logError };
} catch (error) {
console.error('Error setting up error handler:', error);
return null;
}
await proxyStore.saveErrors(errors.slice(-100)); // 只保留最近100条错误记录
}
};
}
// 设置代理认证信息
static async setProxyAuth(username, password) {
try {
await chrome.storage.local.set({
proxyAuth: {
username,
password,
timestamp: Date.now()
}
});
await proxyStore.saveAuth({ username, password, timestamp: Date.now() });
return true;
} catch (error) {
console.error('Error setting proxy auth:', error);
@@ -77,8 +96,7 @@ export class ProxyAuth {
// 获取代理认证信息
static async getProxyAuth() {
try {
const result = await chrome.storage.local.get('proxyAuth');
return result.proxyAuth || null;
return await proxyStore.getAuth();
} catch (error) {
console.error('Error getting proxy auth:', error);
return null;
@@ -88,7 +106,7 @@ export class ProxyAuth {
// 清除代理认证信息
static async clearProxyAuth() {
try {
await chrome.storage.local.remove('proxyAuth');
await proxyStore.clearAuth();
return true;
} catch (error) {
console.error('Error clearing proxy auth:', error);