diff --git a/package.json b/package.json index 8b21428..bcf3bb1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "chrome-power", "description": "The first open source fingerprint browser.", - "version": "1.1.0", + "version": "1.1.1", "private": true, "author": { "email": "zmzimpl@gmail.com", diff --git a/packages/main/src/fingerprint/prepare.ts b/packages/main/src/fingerprint/prepare.ts index 50e3272..69970fe 100644 --- a/packages/main/src/fingerprint/prepare.ts +++ b/packages/main/src/fingerprint/prepare.ts @@ -17,13 +17,22 @@ import type {AxiosProxyConfig} from 'axios'; const logger = createLogger(API_LOGGER_LABEL); const getRealIP = async (proxy: DB.Proxy) => { - const requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!); + let agent: SocksProxyAgent | HttpProxyAgent<`http://${string}:${string}`> | HttpsProxyAgent<`http://${string}:${string}`> | undefined = undefined; + let requestProxy: AxiosProxyConfig | undefined = undefined; + if (proxy.proxy_type?.toLowerCase() === 'socks5') { + const agentInfo = getAgent(proxy); + agent = agentInfo.agent; + } else { + requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!); + } const makeRequest = async (url: string, proxy: AxiosProxyConfig | undefined) => { try { const {data} = await axios.get(url, { - proxy: proxy, + proxy: agent ? false : proxy, timeout: 5_000, + httpAgent: agent, + httpsAgent: agent, }); return url.includes('ip-api.com') ? data.query : data.ip; } catch (error) { @@ -114,7 +123,14 @@ export async function testProxy(proxy: DB.Proxy) { connectivity: {name: string; elapsedTime: number; status: string; reason?: string}[]; } = {connectivity: []}; - const requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!); + let agent: SocksProxyAgent | HttpProxyAgent<`http://${string}:${string}`> | HttpsProxyAgent<`http://${string}:${string}`> | undefined = undefined; + let requestProxy: AxiosProxyConfig | undefined = undefined; + if (proxy.proxy_type?.toLowerCase() === 'socks5') { + const agentInfo = getAgent(proxy); + agent = agentInfo.agent; + } else { + requestProxy = getRequestProxy(proxy.proxy!, proxy.proxy_type!); + } try { const ipInfo = await getProxyInfo(proxy); result.ipInfo = ipInfo || {}; @@ -125,8 +141,10 @@ export async function testProxy(proxy: DB.Proxy) { const startTime = Date.now(); try { const response = await axios.get(pin.url, { - proxy: proxy.proxy ? requestProxy : undefined, + proxy: proxy.proxy && proxy.proxy_type?.toLocaleLowerCase() !== 'socks5' ? requestProxy : undefined, timeout: 5_000, + httpAgent: agent, + httpsAgent: agent, }); const endTime = Date.now(); const elapsedTime = endTime - startTime; // Calculate the time taken for the request diff --git a/packages/renderer/src/pages/proxy/import/index.tsx b/packages/renderer/src/pages/proxy/import/index.tsx index 50a46c2..c4bd4e2 100644 --- a/packages/renderer/src/pages/proxy/import/index.tsx +++ b/packages/renderer/src/pages/proxy/import/index.tsx @@ -162,49 +162,44 @@ const ProxyImport = () => { }, []); const parseProxy = (proxy: string, index: number) => { - // Default type is HTTP - let type = 'HTTP'; + let type = 'HTTP'; // 默认类型 let host = '', port = 0, username = '', password = '', remark = ''; - // Convert the start of the proxy string to lowercase for comparison const proxyLower = proxy.toLowerCase(); - // Check if the type is specified and it's SOCKS5 or HTTP if (proxyLower.startsWith('socks5://')) { type = 'SOCKS5'; - proxy = proxy.substring(9); // Remove the 'socks5://' (9 characters) + proxy = proxy.substring(9); } else if (proxyLower.startsWith('http://')) { - type = 'HTTP'; - proxy = proxy.substring(7); // Remove the 'http://' (7 characters) + proxy = proxy.substring(7); } - // Extract the remark if present + // 如果存在,提取备注 const remarkIndex = proxy.indexOf('{'); if (remarkIndex !== -1) { remark = proxy.substring(remarkIndex + 1, proxy.length - 1); - proxy = proxy.substring(0, remarkIndex); + proxy = proxy.substring(0, remarkIndex).trim(); } - // Validate the complete proxy format - const proxyRegex = /^([a-zA-Z0-9.-]+):(\d{1,5}):([a-zA-Z0-9-]+):([a-zA-Z0-9]+)$/; + // 调整正则表达式以使用户名和密码可选 + const proxyRegex = /^([a-zA-Z0-9.-]+):(\d{1,5})(?::([a-zA-Z0-9-]*):([a-zA-Z0-9]*))?$/; + if (!proxyRegex.test(proxy)) { - throw new Error('Invalid proxy format'); + throw new Error('无效的代理格式'); } - // Split the remaining proxy string by ':' - const parts = proxy.split(':'); + const parts = proxy.match(proxyRegex); + + host = parts?.[1] || ''; + port = Number(parts?.[2] || ''); - // Assign values based on the number of parts we got - host = parts[0]; - port = Number(parts[1]); - if (parts.length >= 4) { - // Only assign username and password if they exist in the split parts - username = parts[2]; - password = parts[3]; + if (parts?.[3] && parts[4]) { + username = parts[3]; + password = parts[4]; } return { diff --git a/packages/shared/utils/proxy.ts b/packages/shared/utils/proxy.ts index 70f817b..4aa2683 100644 --- a/packages/shared/utils/proxy.ts +++ b/packages/shared/utils/proxy.ts @@ -6,14 +6,13 @@ export const getRequestProxy = ( ): AxiosProxyConfig | undefined => { if (!proxy) return; const [host, port, username, password] = proxy.split(':'); - return { protocol: proxy_type.toLocaleLowerCase(), host, port: +port, - auth: { + auth: username ? { username, password, - }, + } : undefined, }; };