diff --git a/apps/data-service/src/proxy-demo.ts b/apps/data-service/src/proxy-demo.ts index 59f7497..9f39e1d 100644 --- a/apps/data-service/src/proxy-demo.ts +++ b/apps/data-service/src/proxy-demo.ts @@ -3,86 +3,86 @@ import { proxyService, ProxySource } from './services/proxy.service.js'; /** * Example usage of the ProxyService */ -async function demonstrateProxyService() { - console.log('๐Ÿš€ Starting Proxy Service Demo...'); +// async function demonstrateProxyService() { +// console.log('๐Ÿš€ Starting Proxy Service Demo...'); - try { - // 1. Start the proxy refresh job (scrapes proxies every 30 minutes) - console.log('๐Ÿ“ฅ Starting proxy refresh job...'); - await proxyService.queueRefreshProxies(30 * 60 * 1000); // 30 minutes +// try { +// // 1. Start the proxy refresh job (scrapes proxies every 30 minutes) +// console.log('๐Ÿ“ฅ Starting proxy refresh job...'); +// await proxyService.queueRefreshProxies(30 * 60 * 1000); // 30 minutes - // 2. Start health checks (checks working proxies every 15 minutes) - console.log('๐Ÿ” Starting proxy health checks...'); - await proxyService.startHealthChecks(15 * 60 * 1000); // 15 minutes +// // 2. Start health checks (checks working proxies every 15 minutes) +// console.log('๐Ÿ” Starting proxy health checks...'); +// await proxyService.startHealthChecks(15 * 60 * 1000); // 15 minutes - // 3. Manually scrape proxies - console.log('๐ŸŒ Manually scraping proxies...'); - const scrapedCount = await proxyService.scrapeProxies(); - console.log(`โœ… Scraped ${scrapedCount} unique proxies`); +// // 3. Manually scrape proxies +// console.log('๐ŸŒ Manually scraping proxies...'); +// const scrapedCount = await proxyService.scrapeProxies(); +// console.log(`โœ… Scraped ${scrapedCount} unique proxies`); - // 4. Wait a bit for some validation to complete - await new Promise(resolve => setTimeout(resolve, 5000)); +// // 4. Wait a bit for some validation to complete +// await new Promise(resolve => setTimeout(resolve, 5000)); - // 5. Get proxy statistics - console.log('๐Ÿ“Š Getting proxy statistics...'); - const stats = await proxyService.getProxyStats(); - console.log('Stats:', { - total: stats.total, - working: stats.working, - failed: stats.failed, - avgResponseTime: stats.avgResponseTime + 'ms' - }); +// // 5. Get proxy statistics +// console.log('๐Ÿ“Š Getting proxy statistics...'); +// const stats = await proxyService.getProxyStats(); +// console.log('Stats:', { +// total: stats.total, +// working: stats.working, +// failed: stats.failed, +// avgResponseTime: stats.avgResponseTime + 'ms' +// }); - // 6. Get a working proxy - console.log('๐ŸŽฏ Getting a working proxy...'); - const workingProxy = await proxyService.getWorkingProxy(); - if (workingProxy) { - console.log('Working proxy found:', { - host: workingProxy.host, - port: workingProxy.port, - protocol: workingProxy.protocol - }); +// // 6. Get a working proxy +// console.log('๐ŸŽฏ Getting a working proxy...'); +// const workingProxy = await proxyService.getWorkingProxy(); +// if (workingProxy) { +// console.log('Working proxy found:', { +// host: workingProxy.host, +// port: workingProxy.port, +// protocol: workingProxy.protocol +// }); - // 7. Test the proxy - console.log('๐Ÿงช Testing proxy...'); - const testResult = await proxyService.checkProxy(workingProxy); - console.log('Test result:', { - isWorking: testResult.isWorking, - responseTime: testResult.responseTime + 'ms', - error: testResult.error || 'None' - }); - } else { - console.log('โŒ No working proxies available yet'); - } +// // 7. Test the proxy +// console.log('๐Ÿงช Testing proxy...'); +// const testResult = await proxyService.checkProxy(workingProxy); +// console.log('Test result:', { +// isWorking: testResult.isWorking, +// responseTime: testResult.responseTime + 'ms', +// error: testResult.error || 'None' +// }); +// } else { +// console.log('โŒ No working proxies available yet'); +// } - // 8. Get multiple working proxies - console.log('๐Ÿ“‹ Getting multiple working proxies...'); - const workingProxies = await proxyService.getWorkingProxies(5); - console.log(`Found ${workingProxies.length} working proxies`); +// // 8. Get multiple working proxies +// console.log('๐Ÿ“‹ Getting multiple working proxies...'); +// const workingProxies = await proxyService.getWorkingProxies(5); +// console.log(`Found ${workingProxies.length} working proxies`); - // 9. Example: Using a proxy with HttpClient - if (workingProxies.length > 0) { - console.log('๐Ÿ”„ Example: Using proxy with HttpClient...'); - try { - const { HttpClient } = await import('@stock-bot/http-client'); - const proxyClient = new HttpClient({ - proxy: workingProxies[0], - timeout: 10000 - }); +// // 9. Example: Using a proxy with HttpClient +// if (workingProxies.length > 0) { +// console.log('๐Ÿ”„ Example: Using proxy with HttpClient...'); +// try { +// const { HttpClient } = await import('@stock-bot/http-client'); +// const proxyClient = new HttpClient({ +// proxy: workingProxies[0], +// timeout: 10000 +// }); - const response = await proxyClient.get('http://httpbin.org/ip'); - console.log('โœ… Request through proxy successful:', response.data); - } catch (error) { - console.log('โŒ Request through proxy failed:', error); - } - } +// const response = await proxyClient.get('http://httpbin.org/ip'); +// console.log('โœ… Request through proxy successful:', response.data); +// } catch (error) { +// console.log('โŒ Request through proxy failed:', error); +// } +// } - console.log('๐ŸŽ‰ Proxy Service Demo completed!'); +// console.log('๐ŸŽ‰ Proxy Service Demo completed!'); - } catch (error) { - console.error('โŒ Demo failed:', error); - } -} +// } catch (error) { +// console.error('โŒ Demo failed:', error); +// } +// } /** * Example: Custom proxy source @@ -92,8 +92,8 @@ async function demonstrateCustomProxySource() { const customSources : ProxySource[] = [ { - url: 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt', - protocol: 'socks5' + url: 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks4.txt', + protocol: 'http' } ]; @@ -107,13 +107,14 @@ async function demonstrateCustomProxySource() { // Export functions for use in other files export { - demonstrateProxyService, + // demonstrateProxyService, demonstrateCustomProxySource }; +demonstrateCustomProxySource() // If this file is run directly, execute the demo -if (import.meta.main) { - demonstrateProxyService() +// if (import.meta.main) { + // demonstrateProxyService() // demonstrateCustomProxySource() - .catch(console.error); -} + // .catch(console.error); +// } diff --git a/apps/data-service/src/services/proxy.service.ts b/apps/data-service/src/services/proxy.service.ts index a15966e..0c3c979 100644 --- a/apps/data-service/src/services/proxy.service.ts +++ b/apps/data-service/src/services/proxy.service.ts @@ -1,6 +1,6 @@ import { createLogger } from '@stock-bot/logger'; import createCache, { type CacheProvider } from '@stock-bot/cache'; -import { HttpClient, ProxyConfig } from '@stock-bot/http-client'; +import { HttpClient, ProxyConfig , RequestConfig } from '@stock-bot/http-client'; import type { Logger as PinoLogger } from 'pino'; export interface ProxySource { @@ -267,6 +267,7 @@ export class ProxyService { const startTime = Date.now(); try { + // Create a new HttpClient instance with the proxy const proxyClient = new HttpClient({ timeout: this.CHECK_TIMEOUT, diff --git a/libs/http-client/src/client.ts b/libs/http-client/src/client.ts index 5cb6b4e..b50f7e8 100644 --- a/libs/http-client/src/client.ts +++ b/libs/http-client/src/client.ts @@ -170,6 +170,7 @@ export class HttpClient { if (this.config.proxy) { const agent = ProxyManager.createAgent(this.config.proxy); (requestOptions as any).agent = agent; + console.log('Using proxy agent:', this.config.proxy); } // Make the request diff --git a/libs/http-client/src/proxy-manager.ts b/libs/http-client/src/proxy-manager.ts index 4d8a563..cb14c8a 100644 --- a/libs/http-client/src/proxy-manager.ts +++ b/libs/http-client/src/proxy-manager.ts @@ -2,6 +2,7 @@ import { HttpsProxyAgent } from 'https-proxy-agent'; import { SocksProxyAgent } from 'socks-proxy-agent'; import type { ProxyConfig } from './types.js'; import { validateProxyConfig } from './types.js'; +import { HttpProxyAgent } from 'http-proxy-agent'; export class ProxyManager { /** @@ -11,7 +12,13 @@ export class ProxyManager { const { protocol, host, port, username, password } = proxy; let proxyUrl: string; - + console.log('Creating proxy agent with config:', { + protocol, + host, + port, + username, + password + }); if (username && password) { proxyUrl = `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`; } else { @@ -20,6 +27,7 @@ export class ProxyManager { switch (protocol) { case 'http': + return new HttpProxyAgent(proxyUrl); case 'https': return new HttpsProxyAgent(proxyUrl); case 'socks4': diff --git a/libs/http-client/src/types.ts b/libs/http-client/src/types.ts index 654d106..4af3306 100644 --- a/libs/http-client/src/types.ts +++ b/libs/http-client/src/types.ts @@ -35,6 +35,7 @@ export interface RequestConfig { method?: HttpMethod; url: string; headers?: Record; + proxy?: ProxyConfig; body?: any; timeout?: number; retries?: number;