From 22f73e67e88ea33b2482cf0ed3bc9645fbe168a8 Mon Sep 17 00:00:00 2001 From: go0p Date: Mon, 17 Feb 2025 13:35:32 +0800 Subject: [PATCH] yakit mitm config --- public/db/proxy-store.js | 38 +++++++++++++++++++++++++++++ public/manifest.json | 4 +++- public/proxy.js | 52 +++++++++++++++++++++++++++++++++++++++- public/types/action.js | 3 ++- 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/public/db/proxy-store.js b/public/db/proxy-store.js index 0cf28f0..bbd6add 100644 --- a/public/db/proxy-store.js +++ b/public/db/proxy-store.js @@ -132,6 +132,44 @@ class ProxyStore { // 忽略接收者不存在的错误 }); } + + async addAndEnableProxy(config) { + try { + // 先禁用所有其他代理 + const existingConfigs = await this.getProxyConfigs(); + for (const existingConfig of existingConfigs) { + if (existingConfig.enabled) { + await this.saveProxyConfigs([{ + ...existingConfig, + enabled: false + }]); + } + } + + // 添加并启用新代理 + await this.saveProxyConfigs([config]); + + // 应用新代理 + await chrome.proxy.settings.set({ + value: { + mode: config.proxyType, + rules: { + singleProxy: { + scheme: config.scheme, + host: config.host, + port: config.port + } + } + }, + scope: 'regular' + }); + + return { success: true }; + } catch (error) { + console.error('Error in addAndEnableProxy:', error); + return { success: false, error }; + } + } } export const proxyStore = new ProxyStore(); \ No newline at end of file diff --git a/public/manifest.json b/public/manifest.json index 4e1bb1e..c4f7b03 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -28,10 +28,12 @@ "sidePanel", "webRequest", "declarativeNetRequest", + "webNavigation", "tabs" ], "host_permissions": [ - "" + "", + "*://mitm/*" ], "web_accessible_resources": [ diff --git a/public/proxy.js b/public/proxy.js index a6508a5..0107223 100644 --- a/public/proxy.js +++ b/public/proxy.js @@ -292,7 +292,54 @@ async function queueProxyLog(details, error = null) { } } -// 导出代理处理器设置函数 +// 添加 URL 拦截处理 +function setupUrlInterceptor() { + chrome.webNavigation.onBeforeNavigate.addListener(async (details) => { + try { + const url = new URL(details.url); + + if (url.hostname === 'mitm') { + // 解析参数 + const host = url.searchParams.get('host'); + const port = parseInt(url.searchParams.get('port') || '0'); + const scheme = url.searchParams.get('scheme'); + + if (host && port && scheme) { + // 创建新的代理配置 + const newConfig = { + id: Date.now().toString(), + name: "Yakit MITM", + proxyType: 'fixed_servers', + scheme: scheme, + host: host, + port: port, + enabled: true + }; + + // 直接使用 proxyStore 实例的方法 + const result = await proxyStore.addAndEnableProxy(newConfig); + + if (result.success) { + // 重定向回 mitm 页面 + chrome.tabs.update(details.tabId, { + url: "http://mitm/" + }); + } else { + console.error('Failed to add and enable proxy:', result.error); + } + } + } + } catch (error) { + console.error('Error handling proxy URL:', error); + } + }, { + url: [{ + hostEquals: 'mitm' + }] + }); +} + +// 修改 setupProxyHandlers 函数 export function setupProxyHandlers() { // 设置代理错误处理和认证 ProxyAuth.setupErrorHandler(); @@ -300,6 +347,9 @@ export function setupProxyHandlers() { // 设置代理请求监听器 setupProxyRequestListener(); + + // 设置 URL 拦截器 + setupUrlInterceptor(); // 消息监听器 chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { diff --git a/public/types/action.js b/public/types/action.js index b9adfa0..823e750 100644 --- a/public/types/action.js +++ b/public/types/action.js @@ -8,5 +8,6 @@ export const ProxyActionType = { CLEAR_PROXY_LOGS: "CLEAR_PROXY_LOGS", GET_PROXY_CONFIGS: "GET_PROXY_CONFIGS", ADD_PROXY_CONFIG: "ADD_PROXY_CONFIG", - UPDATE_PROXY_CONFIG: "UPDATE_PROXY_CONFIG" + UPDATE_PROXY_CONFIG: "UPDATE_PROXY_CONFIG", + ADD_AND_ENABLE_PROXY: 'ADD_AND_ENABLE_PROXY' }; \ No newline at end of file