import type { ProxySettings } from './proxy'; import { resolveProxySettings } from './proxy'; const WHATSAPP_WEB_HOST = 'web.whatsapp.com'; function splitBypassRules(rules: string): string[] { return rules .split(/[,\n;]/) .map((rule) => rule.trim()) .filter(Boolean); } function matchesBypassRule(host: string, rule: string): boolean { const normalizedHost = host.toLowerCase(); const normalizedRule = rule.toLowerCase(); if (normalizedRule === '*') return true; if (normalizedRule === '') { return normalizedHost === 'localhost' || normalizedHost === '127.0.0.1' || normalizedHost === '::1' || !normalizedHost.includes('.'); } if (normalizedRule.startsWith('*.')) { const suffix = normalizedRule.slice(1); return normalizedHost.endsWith(suffix); } if (normalizedRule.startsWith('.')) { return normalizedHost.endsWith(normalizedRule); } return normalizedHost === normalizedRule; } export function shouldBypassWhatsAppProxy(bypassRules: string): boolean { return splitBypassRules(bypassRules).some((rule) => matchesBypassRule(WHATSAPP_WEB_HOST, rule)); } export function resolveWhatsAppProxyUrl(settings: ProxySettings): string { if (!settings.proxyEnabled) return ''; const resolved = resolveProxySettings(settings); if (shouldBypassWhatsAppProxy(resolved.bypassRules)) return ''; return resolved.allProxy || resolved.httpsProxy || resolved.httpProxy; } export function isSocksProxyUrl(proxyUrl: string): boolean { return /^socks[45]h?:\/\//i.test(proxyUrl.trim()); } export function redactProxyUrlForLog(proxyUrl: string): string { try { const parsed = new URL(proxyUrl); if (parsed.username) parsed.username = 'redacted'; if (parsed.password) parsed.password = 'redacted'; return parsed.toString(); } catch { return proxyUrl.replace(/\/\/([^/@:]+):([^/@]+)@/, '//redacted:redacted@'); } }