working on proxy service. getting closer

This commit is contained in:
Bojan Kucera 2025-06-08 07:36:07 -04:00
parent 6380165a94
commit afc1843fdb
5 changed files with 91 additions and 892 deletions

View file

@ -7,7 +7,7 @@ import type {
import { HttpError } from './types.js';
import { ProxyManager } from './proxy-manager.js';
import axios, { type AxiosResponse, AxiosError } from 'axios';
import { clear } from 'node:console';
import { loggingConfig } from '@stock-bot/config';
export class HttpClient {
private readonly config: HttpClientConfig;
@ -15,8 +15,9 @@ export class HttpClient {
constructor(config: HttpClientConfig = {}, logger?: Logger) {
this.config = config;
this.logger = logger?.child({
this.logger = logger?.child({ //TODO fix pino levels
component: 'http',
// level: loggingConfig?.LOG_LEVEL || 'info',
});
}
@ -68,11 +69,11 @@ export class HttpClient {
return response;
} catch (error) {
this.logger?.warn('HTTP request failed', {
method: finalConfig.method,
url: finalConfig.url,
error: (error as Error).message,
});
// this.logger?.warn('HTTP request failed', {
// method: finalConfig.method,
// url: finalConfig.url,
// error: (error as Error).message,
// });
throw error;
}
}
@ -124,10 +125,10 @@ export class HttpClient {
try {
const options = this.buildFetchOptions(config, signal);
this.logger?.debug('Making request with fetch: ', { url: config.url, options })
this.logger?.debug('Making request with fetch: ', { url: config.url, options })
// console.log(options)
const response = await fetch(config.url, options);
// console.log('Fetch response:', response.status);
return this.parseFetchResponse<T>(response);
} catch (error) {
throw signal.aborted
@ -201,7 +202,6 @@ export class HttpClient {
if (config.proxy && ProxyManager.shouldUseBunFetch(config.proxy)) {
(options as any).proxy = ProxyManager.createProxyUrl(config.proxy);
}
return options;
} /**
* Build Axios options (for reference, though we're creating instance in ProxyManager)

View file

@ -16,9 +16,6 @@ export class ProxyManager {
*/
static createProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if(protocol.includes('socks')) {
return `${protocol}://${host}:${port}`;
}
if (username && password) {
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}
@ -32,11 +29,10 @@ export class ProxyManager {
this.validateConfig(proxy);
const proxyUrl = this.createProxyUrl(proxy);
console.log(`Using proxy url: ${proxyUrl}`);
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
console.log(`Using SOCKS proxy: ${proxyUrl}`);
// console.log(`Using SOCKS proxy: ${proxyUrl}`);
return new SocksProxyAgent(proxyUrl);
case 'http':
return new HttpProxyAgent(proxyUrl);
@ -54,12 +50,6 @@ export class ProxyManager {
return {
httpAgent: agent,
httpsAgent: agent,
// timeout: 30000,
// validateStatus: () => true, // Don't throw errors on HTTP status codes
// maxRedirects: 5,
// headers: {
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
// }
};
}
/**