fix: route EMEX HTTP fetches through DataImpulse proxy
Some checks failed
CI / Lint, Typecheck, Test & Build (push) Has been cancelled

EMEX server blocks direct server IP requests (geo/bot protection),
causing all HTTP-based VIN decodes to return empty results. Add proxy
support to fetchEmexHtml() using undici ProxyAgent, reusing the same
EMEX_USE_PROXY / EMEX_PROXY_* env vars as the browser service.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 11:29:00 +00:00
parent 7a322c5574
commit 016860c68a

View File

@@ -18,6 +18,7 @@ import {
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as path from 'path';
import { ProxyAgent } from 'undici';
import {
EmexScraperResponse,
@@ -99,6 +100,7 @@ export class EmexService {
private readonly scraperPath: string;
private readonly timeout: number;
private readonly debug: boolean;
private readonly proxyUrl: string | null;
constructor(
private configService: ConfigService,
@@ -117,6 +119,19 @@ export class EmexService {
this.timeout = this.configService.get<number>('EMEX_TIMEOUT', 60000);
this.debug = this.configService.get<boolean>('EMEX_DEBUG', false);
// Proxy config — same env vars as emex.browser.ts
const useProxy = this.configService.get<string>('EMEX_USE_PROXY', 'false') === 'true';
if (useProxy) {
const host = this.configService.get<string>('EMEX_PROXY_HOST', '74.81.81.81');
const port = this.configService.get<number>('EMEX_PROXY_PORT_START', 10000);
const user = this.configService.get<string>('EMEX_PROXY_USER', '1726bbe361918676d44e');
const pass = this.configService.get<string>('EMEX_PROXY_PASS', 'f11c7b6128cc86c6');
this.proxyUrl = `http://${user}:${pass}@${host}:${port}`;
this.logger.log(`EMEX HTTP proxy enabled: ${host}:${port}`);
} else {
this.proxyUrl = null;
}
this.logger.log(`EMEX Service initialized with scraper path: ${this.scraperPath}`);
}
@@ -227,10 +242,15 @@ export class EmexService {
* without requiring authentication cookies.
*/
private async fetchEmexHtml(url: string): Promise<string> {
const res = await fetch(url, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const options: any = {
headers: { 'User-Agent': EMEX_UA, 'Accept': 'text/html,application/xhtml+xml' },
signal: AbortSignal.timeout(this.timeout),
});
};
if (this.proxyUrl) {
options.dispatcher = new ProxyAgent(this.proxyUrl);
}
const res = await fetch(url, options);
if (!res.ok) {
throw new Error(`EMEX HTTP ${res.status} for ${url}`);
}