working on proxy-service
This commit is contained in:
parent
b23ca42f4d
commit
953b361d30
14 changed files with 1020 additions and 56 deletions
133
apps/data-service/src/proxy-demo.ts
Normal file
133
apps/data-service/src/proxy-demo.ts
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
import { proxyService } from './services/proxy.service.js';
|
||||
|
||||
/**
|
||||
* Example usage of the ProxyService
|
||||
*/
|
||||
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
|
||||
|
||||
// 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`);
|
||||
|
||||
// 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'
|
||||
});
|
||||
|
||||
// 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,
|
||||
type: workingProxy.type
|
||||
});
|
||||
|
||||
// 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`);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('🎉 Proxy Service Demo completed!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Demo failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Custom proxy source
|
||||
*/
|
||||
async function demonstrateCustomProxySource() {
|
||||
console.log('🔧 Demonstrating custom proxy source...');
|
||||
|
||||
const customSources = [
|
||||
{
|
||||
url: 'https://raw.githubusercontent.com/TheSpeedX/PROXY-List/master/socks4.txt',
|
||||
type: 'free' as const,
|
||||
format: 'text' as const,
|
||||
parser: (content: string) => {
|
||||
return content.split('\n')
|
||||
.filter(line => line.trim() && !line.startsWith('#'))
|
||||
.map(line => {
|
||||
const [host, port] = line.trim().split(':');
|
||||
return {
|
||||
type: 'socks4' as const,
|
||||
host: host.trim(),
|
||||
port: parseInt(port.trim())
|
||||
};
|
||||
})
|
||||
.filter(proxy => proxy.host && !isNaN(proxy.port));
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
try {
|
||||
const count = await proxyService.scrapeProxies(customSources);
|
||||
console.log(`✅ Scraped ${count} SOCKS4 proxies from custom source`);
|
||||
} catch (error) {
|
||||
console.error('❌ Custom source scraping failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Export functions for use in other files
|
||||
export {
|
||||
demonstrateProxyService,
|
||||
demonstrateCustomProxySource
|
||||
};
|
||||
|
||||
// If this file is run directly, execute the demo
|
||||
if (import.meta.main) {
|
||||
demonstrateProxyService()
|
||||
.then(() => demonstrateCustomProxySource())
|
||||
.catch(console.error);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue