feat: implement humanization enforcement and improve Axios request error handling

This commit is contained in:
2025-12-06 14:24:10 +01:00
parent 61b8b1a6af
commit 6bee98289e
5 changed files with 60 additions and 25 deletions

View File

@@ -99,9 +99,8 @@ class AxiosClient {
// Handle HTTP 407 Proxy Authentication Required
if (this.isProxyAuthError(err)) {
// Retry without proxy on auth failure
const bypassInstance = axios.create()
return bypassInstance.request(config)
const msg = err instanceof Error ? err.message : String(err)
throw new Error(`Proxy authentication failed. Direct fallback disabled to avoid IP leakage. Details: ${msg}`)
}
// Handle retryable network errors
@@ -112,9 +111,6 @@ class AxiosClient {
await this.sleep(delayMs)
continue
}
// Last attempt: try without proxy
const bypassInstance = axios.create()
return bypassInstance.request(config)
}
// Non-retryable error

View File

@@ -1,6 +1,8 @@
import axios from 'axios'
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
import { Util } from '../core/Utils'
type HttpClient = Pick<typeof axios, 'request'> | { request: (config: AxiosRequestConfig) => Promise<AxiosResponse> }
export interface QueryDiversityConfig {
sources: Array<'google-trends' | 'reddit' | 'news' | 'wikipedia' | 'local-fallback'>
deduplicate: boolean
@@ -18,8 +20,9 @@ export class QueryDiversityEngine {
private cache: Map<string, { queries: string[]; expires: number }> = new Map()
private util: Util = new Util()
private logger?: (source: string, message: string, level?: 'info' | 'warn' | 'error') => void
private httpClient: HttpClient
constructor(config?: Partial<QueryDiversityConfig>, logger?: (source: string, message: string, level?: 'info' | 'warn' | 'error') => void) {
constructor(config?: Partial<QueryDiversityConfig>, logger?: (source: string, message: string, level?: 'info' | 'warn' | 'error'), httpClient?: HttpClient) {
const maxQueriesPerSource = Math.max(1, Math.min(config?.maxQueriesPerSource || 10, 50))
const cacheMinutes = Math.max(1, Math.min(config?.cacheMinutes || 30, 1440))
@@ -33,6 +36,7 @@ export class QueryDiversityEngine {
cacheMinutes
}
this.logger = logger
this.httpClient = httpClient || axios
}
private log(source: string, message: string, level: 'info' | 'warn' | 'error' = 'info'): void {
@@ -51,7 +55,7 @@ export class QueryDiversityEngine {
timeout?: number
}): Promise<string> {
try {
const response = await axios({
const response = await this.httpClient.request({
url,
method: config?.method || 'GET',
headers: config?.headers || { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' },

View File

@@ -226,10 +226,19 @@ export function log(isMobile: boolean | 'main', title: string, message: string,
type LoggingCfg = { excludeFunc?: string[]; webhookExcludeFunc?: string[]; redactEmails?: boolean }
const loggingCfg: LoggingCfg = logging || {}
const shouldRedact = !!loggingCfg.redactEmails
const redact = (s: string) => shouldRedact ? s.replace(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/ig, (m) => {
const [u, d] = m.split('@'); return `${(u || '').slice(0, 2)}***@${d || ''}`
}) : s
const cleanStr = redact(`[${currentTime}] [PID: ${process.pid}] [${type.toUpperCase()}] ${platformText} [${title}] ${message}`)
const redactSensitive = (s: string) => {
const scrubbed = s
.replace(/:\/\/[A-Z0-9._%+-]+:[^@\s]+@/ig, '://***:***@')
.replace(/(token=|apikey=|auth=)[^\s&]+/ig, '$1***')
if (!shouldRedact) return scrubbed
return scrubbed.replace(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/ig, (m) => {
const [u, d] = m.split('@'); return `${(u || '').slice(0, 2)}***@${d || ''}`
})
}
const cleanStr = redactSensitive(`[${currentTime}] [PID: ${process.pid}] [${type.toUpperCase()}] ${platformText} [${title}] ${message}`)
// Define conditions for sending to NTFY
const ntfyConditions = {
@@ -293,7 +302,7 @@ export function log(isMobile: boolean | 'main', title: string, message: string,
typeColor(`${typeIndicator}`),
platformColor(`[${platformText}]`),
chalk.bold(`[${title}]`),
iconPart + redact(message)
iconPart + redactSensitive(message)
].join(' ')
const applyChalk = color && typeof chalk[color] === 'function' ? chalk[color] as (msg: string) => string : null